bwgraph.cpp

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

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3