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 2983 2008-08-17 05:59:43Z 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 
00049   /* Pressing 'Esc' or 'Ctrl+W' will close the window */
00050   setShortcut("Esc", SLOT(close()));
00051   setShortcut("Ctrl+W", SLOT(close()));
00052 
00053   /* Bind events to actions */
00054   createActions();
00055 
00056   /* Ask Tor to notify us about bandwidth updates */
00057   _torControl = Vidalia::torControl();
00058   _torControl->setEvent(TorEvents::Bandwidth, this, true);
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 /** Custom event handler. Checks if the event is a bandwidth update event. If it
00081  * is, it will add the data point to the history and updates the graph. */
00082 void
00083 BandwidthGraph::customEvent(QEvent *event)
00084 {
00085   if (event->type() == CustomEventType::BandwidthEvent) {
00086     BandwidthEvent *bw = (BandwidthEvent *)event;
00087     updateGraph(bw->bytesRead(), bw->bytesWritten());
00088   }
00089 }
00090 
00091 /** Binds events to actions. */
00092 void
00093 BandwidthGraph::createActions()
00094 {
00095   connect(ui.btnToggleSettings, SIGNAL(toggled(bool)),
00096       this, SLOT(showSettingsFrame(bool)));
00097 
00098   connect(ui.btnReset, SIGNAL(clicked()),
00099       this, SLOT(reset()));
00100 
00101   connect(ui.btnSaveSettings, SIGNAL(clicked()),
00102       this, SLOT(saveChanges()));
00103 
00104   connect(ui.btnCancelSettings, SIGNAL(clicked()),
00105       this, SLOT(cancelChanges()));
00106   
00107   connect(ui.sldrOpacity, SIGNAL(valueChanged(int)),
00108       this, SLOT(setOpacity(int)));
00109 }
00110 
00111 /** Adds new data to the graph. */
00112 void
00113 BandwidthGraph::updateGraph(quint64 bytesRead, quint64 bytesWritten)
00114 {
00115   /* Graph only cares about kilobytes */
00116   ui.frmGraph->addPoints(bytesRead/1024.0, bytesWritten/1024.0);
00117 }
00118 
00119 /** Loads the saved Bandwidth Graph settings. */
00120 void
00121 BandwidthGraph::loadSettings()
00122 {
00123   /* Set window opacity slider widget */
00124   ui.sldrOpacity->setValue(getSetting(SETTING_OPACITY, DEFAULT_OPACITY).toInt());
00125   setOpacity(ui.sldrOpacity->value());
00126 
00127   /* Set whether the window appears on top. */
00128   ui.chkAlwaysOnTop->setChecked(getSetting(SETTING_ALWAYS_ON_TOP,
00129                                            DEFAULT_ALWAYS_ON_TOP).toBool());
00130   if (ui.chkAlwaysOnTop->isChecked()) {
00131     setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00132   } else {
00133     setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00134   }
00135 
00136   /* Set the line filter checkboxes accordingly */
00137   uint filter = getSetting(SETTING_FILTER, DEFAULT_FILTER).toUInt();
00138   ui.chkReceiveRate->setChecked(filter & BWGRAPH_LINE_RECV);
00139   ui.chkSendRate->setChecked(filter & BWGRAPH_LINE_SEND);
00140 
00141   /* Set whether we are plotting bandwidth as area graphs or not */
00142   int graphStyle = getSetting(SETTING_STYLE, DEFAULT_STYLE).toInt();
00143   if (graphStyle < 0 || graphStyle >= ui.cmbGraphStyle->count()) {
00144     graphStyle = DEFAULT_STYLE;
00145   }
00146   ui.cmbGraphStyle->setCurrentIndex(graphStyle);
00147   ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)graphStyle);
00148 
00149   /* Set graph frame settings */
00150   ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
00151                                ui.chkSendRate->isChecked());
00152 }
00153 
00154 /** Resets the log start time. */
00155 void
00156 BandwidthGraph::reset()
00157 {
00158   /* Set to current time */
00159   ui.statusbar->showMessage(tr("Since:") + " " + 
00160                             QDateTime::currentDateTime()
00161                             .toString(DATETIME_FMT));
00162   /* Reset the graph */
00163   ui.frmGraph->resetGraph();
00164 }
00165 
00166 /** Saves the Bandwidth Graph settings and adjusts the graph if necessary. */
00167 void
00168 BandwidthGraph::saveChanges()
00169 {
00170   /* Hide the settings frame and reset toggle button */
00171   showSettingsFrame(false);
00172   
00173   /* Save the opacity and graph style */
00174   saveSetting(SETTING_OPACITY, ui.sldrOpacity->value());
00175   saveSetting(SETTING_STYLE, ui.cmbGraphStyle->currentIndex());
00176 
00177   /* Save the Always On Top setting */
00178   saveSetting(SETTING_ALWAYS_ON_TOP, ui.chkAlwaysOnTop->isChecked());
00179   if (ui.chkAlwaysOnTop->isChecked()) {
00180     setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00181   } else {
00182     setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00183   }
00184   setOpacity(ui.sldrOpacity->value());
00185 
00186   /* Save the line filter values */
00187   uint filter = 0;
00188   ADD_TO_FILTER(filter, BWGRAPH_LINE_RECV, ui.chkReceiveRate->isChecked());
00189   ADD_TO_FILTER(filter, BWGRAPH_LINE_SEND, ui.chkSendRate->isChecked());
00190   saveSetting(SETTING_FILTER, filter);
00191 
00192 
00193   /* Update the graph frame settings */
00194   ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
00195                                ui.chkSendRate->isChecked());
00196   ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)ui.cmbGraphStyle->currentIndex());
00197   
00198   /* A change in window flags causes the window to disappear, so make sure
00199    * it's still visible. */
00200   showNormal();
00201 }
00202 
00203 /** Simply restores the previously saved settings. */
00204 void 
00205 BandwidthGraph::cancelChanges()
00206 {
00207   /* Hide the settings frame and reset toggle button */
00208   showSettingsFrame(false);
00209 
00210   /* Reload the settings */
00211   loadSettings();
00212 }
00213 
00214 /** Toggles the Settings pane on and off, changes toggle button text. */
00215 void
00216 BandwidthGraph::showSettingsFrame(bool show)
00217 {
00218   static QSize minSize = minimumSize();
00219   
00220   QSize newSize = size();
00221   if (show) {
00222     /* Extend the bottom of the bandwidth graph and show the settings */
00223     ui.frmSettings->setVisible(true);
00224     ui.btnToggleSettings->setChecked(true);
00225     ui.btnToggleSettings->setText(tr("Hide Settings"));
00226 
00227     /* 6 = vertical spacing between the settings frame and graph frame */
00228     newSize.setHeight(newSize.height() + ui.frmSettings->height() + 6);
00229   } else {
00230     /* Shrink the height of the bandwidth graph and hide the settings */
00231     ui.frmSettings->setVisible(false);
00232     ui.btnToggleSettings->setChecked(false);
00233     ui.btnToggleSettings->setText(tr("Show Settings"));
00234     
00235     /* 6 = vertical spacing between the settings frame and graph frame */
00236     newSize.setHeight(newSize.height() - ui.frmSettings->height() - 6);
00237     setMinimumSize(minSize);
00238   }
00239   resize(newSize);
00240 }
00241 
00242 /** Sets the opacity of the Bandwidth Graph window. */
00243 void
00244 BandwidthGraph::setOpacity(int value)
00245 {
00246   qreal newValue = value / 100.0;
00247   
00248   /* Opacity only supported by Mac and Win32 */
00249 #if defined(Q_WS_MAC)
00250   this->setWindowOpacity(newValue);
00251   ui.lblPercentOpacity->setText(QString::number(value));
00252 #elif defined(Q_WS_WIN)
00253   if (QSysInfo::WindowsVersion & QSysInfo::WV_NT_based
00254         && QSysInfo::WindowsVersion >= QSysInfo::WV_2000) {
00255     this->setWindowOpacity(newValue);
00256     ui.lblPercentOpacity->setText(QString::number(value));
00257   }
00258 #else
00259   Q_UNUSED(newValue);
00260 #endif
00261 }
00262 
00263 /** Overloads the default show() slot so we can set opacity. */
00264 void
00265 BandwidthGraph::showWindow()
00266 {
00267   /* Load saved settings */
00268   loadSettings();
00269   /* Show the window */
00270   VidaliaWindow::showWindow();
00271 }
00272 

Generated on 22 Feb 2010 for Vidalia by  doxygen 1.6.1