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

Generated on Sat Aug 16 17:31:48 2008 for Vidalia by  doxygen 1.5.6