BandwidthGraph.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file BandwidthGraph.cpp
00013 ** \version $Id: BandwidthGraph.cpp 4054 2009-08-17 02:25:08Z edmanm $
00014 ** \brief Displays a graph of Tor's bandwidth usage
00015 */
00016 
00017 #include "BandwidthGraph.h"
00018 #include "Vidalia.h"
00019 
00020 #define BWGRAPH_LINE_SEND       (1u<<0)
00021 #define BWGRAPH_LINE_RECV       (1u<<1)
00022 #define SETTING_FILTER          "LineFilter"
00023 #define SETTING_OPACITY         "Opacity"
00024 #define SETTING_ALWAYS_ON_TOP   "AlwaysOnTop"
00025 #define SETTING_STYLE           "GraphStyle"
00026 #define DEFAULT_FILTER          (BWGRAPH_LINE_SEND|BWGRAPH_LINE_RECV)
00027 #define DEFAULT_ALWAYS_ON_TOP   false
00028 #define DEFAULT_OPACITY         100
00029 #define DEFAULT_STYLE           GraphFrame::AreaGraph
00030 
00031 #define ADD_TO_FILTER(f,v,b)  (f = ((b) ? ((f) | (v)) : ((f) & ~(v))))
00032 
00033 /* Define the format used for displaying the date and time */
00034 #define DATETIME_FMT  "MMM dd hh:mm:ss"
00035 
00036 /* Images used in the graph style drop-down */
00037 #define IMG_AREA_GRAPH    ":/images/16x16/graph-area.png"
00038 #define IMG_LINE_GRAPH    ":/images/16x16/graph-line.png"
00039 
00040 
00041 /** Default constructor */
00042 BandwidthGraph::BandwidthGraph(QWidget *parent, Qt::WFlags flags)
00043   : VidaliaWindow("BandwidthGraph", parent, flags)
00044 {
00045   /* Invoke Qt Designer generated QObject setup routine */
00046   ui.setupUi(this);
00047 
00048   /* Ask Tor to notify us about bandwidth updates */
00049   Vidalia::torControl()->setEvent(TorEvents::Bandwidth);
00050   connect(Vidalia::torControl(), SIGNAL(bandwidthUpdate(quint64,quint64)),
00051           this, SLOT(updateGraph(quint64,quint64)));
00052 
00053   /* Pressing 'Esc' or 'Ctrl+W' will close the window */
00054   setShortcut("Esc", SLOT(close()));
00055   setShortcut("Ctrl+W", SLOT(close()));
00056 
00057   /* Bind events to actions */
00058   createActions();
00059 
00060   /* Initialize Sent/Receive data counters */
00061   reset();
00062   /* Hide Bandwidth Graph Settings frame */
00063   showSettingsFrame(false);
00064   /* Load the previously saved settings */
00065   loadSettings();
00066 
00067   /* Turn off opacity group on unsupported platforms */
00068 #if defined(Q_WS_WIN)
00069   if(!(QSysInfo::WindowsVersion & QSysInfo::WV_NT_based)
00070        || QSysInfo::WindowsVersion < QSysInfo::WV_2000) {
00071     ui.frmOpacity->setVisible(false);
00072   }
00073 #endif
00074   
00075 #if defined(Q_WS_X11)
00076   ui.frmOpacity->setVisible(false);
00077 #endif
00078 }
00079 
00080 /** Called when the user changes the UI translation. */
00081 void
00082 BandwidthGraph::retranslateUi()
00083 {
00084   ui.retranslateUi(this);
00085 }
00086 
00087 /** Binds events to actions. */
00088 void
00089 BandwidthGraph::createActions()
00090 {
00091   connect(ui.btnToggleSettings, SIGNAL(toggled(bool)),
00092           this, SLOT(showSettingsFrame(bool)));
00093 
00094   connect(ui.btnReset, SIGNAL(clicked()),
00095           this, SLOT(reset()));
00096 
00097   connect(ui.btnSaveSettings, SIGNAL(clicked()),
00098           this, SLOT(saveChanges()));
00099 
00100   connect(ui.btnCancelSettings, SIGNAL(clicked()),
00101           this, SLOT(cancelChanges()));
00102 
00103   connect(ui.sldrOpacity, SIGNAL(valueChanged(int)),
00104           this, SLOT(setOpacity(int)));
00105 }
00106 
00107 /** Adds new data to the graph. */
00108 void
00109 BandwidthGraph::updateGraph(quint64 bytesRead, quint64 bytesWritten)
00110 {
00111   /* Graph only cares about kilobytes */
00112   ui.frmGraph->addPoints(bytesRead/1024.0, bytesWritten/1024.0);
00113 }
00114 
00115 /** Loads the saved Bandwidth Graph settings. */
00116 void
00117 BandwidthGraph::loadSettings()
00118 {
00119   /* Set window opacity slider widget */
00120   ui.sldrOpacity->setValue(getSetting(SETTING_OPACITY, DEFAULT_OPACITY).toInt());
00121   setOpacity(ui.sldrOpacity->value());
00122 
00123   /* Set whether the window appears on top. */
00124   ui.chkAlwaysOnTop->setChecked(getSetting(SETTING_ALWAYS_ON_TOP,
00125                                            DEFAULT_ALWAYS_ON_TOP).toBool());
00126   if (ui.chkAlwaysOnTop->isChecked()) {
00127     setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00128   } else {
00129     setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00130   }
00131 
00132   /* Set the line filter checkboxes accordingly */
00133   uint filter = getSetting(SETTING_FILTER, DEFAULT_FILTER).toUInt();
00134   ui.chkReceiveRate->setChecked(filter & BWGRAPH_LINE_RECV);
00135   ui.chkSendRate->setChecked(filter & BWGRAPH_LINE_SEND);
00136 
00137   /* Set whether we are plotting bandwidth as area graphs or not */
00138   int graphStyle = getSetting(SETTING_STYLE, DEFAULT_STYLE).toInt();
00139   if (graphStyle < 0 || graphStyle >= ui.cmbGraphStyle->count()) {
00140     graphStyle = DEFAULT_STYLE;
00141   }
00142   ui.cmbGraphStyle->setCurrentIndex(graphStyle);
00143   ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)graphStyle);
00144 
00145   /* Set graph frame settings */
00146   ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
00147                                ui.chkSendRate->isChecked());
00148 }
00149 
00150 /** Resets the log start time. */
00151 void
00152 BandwidthGraph::reset()
00153 {
00154   /* Set to current time */
00155   ui.statusbar->showMessage(tr("Since:") + " " + 
00156           QDateTime::currentDateTime()
00157           .toString(DATETIME_FMT));
00158   /* Reset the graph */
00159   ui.frmGraph->resetGraph();
00160 }
00161 
00162 /** Saves the Bandwidth Graph settings and adjusts the graph if necessary. */
00163 void
00164 BandwidthGraph::saveChanges()
00165 {
00166   /* Hide the settings frame and reset toggle button */
00167   showSettingsFrame(false);
00168   
00169   /* Save the opacity and graph style */
00170   saveSetting(SETTING_OPACITY, ui.sldrOpacity->value());
00171   saveSetting(SETTING_STYLE, ui.cmbGraphStyle->currentIndex());
00172 
00173   /* Save the Always On Top setting */
00174   saveSetting(SETTING_ALWAYS_ON_TOP, ui.chkAlwaysOnTop->isChecked());
00175   if (ui.chkAlwaysOnTop->isChecked()) {
00176     setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00177   } else {
00178     setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00179   }
00180   setOpacity(ui.sldrOpacity->value());
00181 
00182   /* Save the line filter values */
00183   uint filter = 0;
00184   ADD_TO_FILTER(filter, BWGRAPH_LINE_RECV, ui.chkReceiveRate->isChecked());
00185   ADD_TO_FILTER(filter, BWGRAPH_LINE_SEND, ui.chkSendRate->isChecked());
00186   saveSetting(SETTING_FILTER, filter);
00187 
00188 
00189   /* Update the graph frame settings */
00190   ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
00191                                ui.chkSendRate->isChecked());
00192   ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)ui.cmbGraphStyle->currentIndex());
00193   
00194   /* A change in window flags causes the window to disappear, so make sure
00195    * it's still visible. */
00196   showNormal();
00197 }
00198 
00199 /** Simply restores the previously saved settings. */
00200 void 
00201 BandwidthGraph::cancelChanges()
00202 {
00203   /* Hide the settings frame and reset toggle button */
00204   showSettingsFrame(false);
00205 
00206   /* Reload the settings */
00207   loadSettings();
00208 }
00209 
00210 /** Toggles the Settings pane on and off, changes toggle button text. */
00211 void
00212 BandwidthGraph::showSettingsFrame(bool show)
00213 {
00214   static QSize minSize = minimumSize();
00215   
00216   QSize newSize = size();
00217   if (show) {
00218     /* Extend the bottom of the bandwidth graph and show the settings */
00219     ui.frmSettings->setVisible(true);
00220     ui.btnToggleSettings->setChecked(true);
00221     ui.btnToggleSettings->setText(tr("Hide Settings"));
00222 
00223     /* 6 = vertical spacing between the settings frame and graph frame */
00224     newSize.setHeight(newSize.height() + ui.frmSettings->height() + 6);
00225   } else {
00226     /* Shrink the height of the bandwidth graph and hide the settings */
00227     ui.frmSettings->setVisible(false);
00228     ui.btnToggleSettings->setChecked(false);
00229     ui.btnToggleSettings->setText(tr("Show Settings"));
00230     
00231     /* 6 = vertical spacing between the settings frame and graph frame */
00232     newSize.setHeight(newSize.height() - ui.frmSettings->height() - 6);
00233     setMinimumSize(minSize);
00234   }
00235   resize(newSize);
00236 }
00237 
00238 /** Sets the opacity of the Bandwidth Graph window. */
00239 void
00240 BandwidthGraph::setOpacity(int value)
00241 {
00242   qreal newValue = value / 100.0;
00243   
00244   /* Opacity only supported by Mac and Win32 */
00245 #if defined(Q_WS_MAC)
00246   this->setWindowOpacity(newValue);
00247   ui.lblPercentOpacity->setText(QString::number(value));
00248 #elif defined(Q_WS_WIN)
00249   if (QSysInfo::WindowsVersion & QSysInfo::WV_NT_based
00250         && QSysInfo::WindowsVersion >= QSysInfo::WV_2000) {
00251     this->setWindowOpacity(newValue);
00252     ui.lblPercentOpacity->setText(QString::number(value));
00253   }
00254 #else
00255   Q_UNUSED(newValue);
00256 #endif
00257 }
00258 
00259 /** Overloads the default show() slot so we can set opacity. */
00260 void
00261 BandwidthGraph::showWindow()
00262 {
00263   /* Load saved settings */
00264   loadSettings();
00265   /* Show the window */
00266   VidaliaWindow::showWindow();
00267 }
00268 

Generated on Mon Aug 30 19:14:02 2010 for Vidalia by  doxygen 1.5.9