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