Vidalia  0.2.21
BandwidthGraph.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file BandwidthGraph.cpp
13 ** \brief Displays a graph of Tor's bandwidth usage
14 */
15 
16 #include "BandwidthGraph.h"
17 #include "Vidalia.h"
18 
19 #define BWGRAPH_LINE_SEND (1u<<0)
20 #define BWGRAPH_LINE_RECV (1u<<1)
21 #define SETTING_FILTER "LineFilter"
22 #define SETTING_OPACITY "Opacity"
23 #define SETTING_ALWAYS_ON_TOP "AlwaysOnTop"
24 #define SETTING_STYLE "GraphStyle"
25 #define DEFAULT_FILTER (BWGRAPH_LINE_SEND|BWGRAPH_LINE_RECV)
26 #define DEFAULT_ALWAYS_ON_TOP false
27 #define DEFAULT_OPACITY 100
28 #define DEFAULT_STYLE GraphFrame::AreaGraph
29 
30 #define ADD_TO_FILTER(f,v,b) (f = ((b) ? ((f) | (v)) : ((f) & ~(v))))
31 
32 /* Define the format used for displaying the date and time */
33 #define DATETIME_FMT "MMM dd hh:mm:ss"
34 
35 /* Images used in the graph style drop-down */
36 #define IMG_AREA_GRAPH ":/images/16x16/graph-area.png"
37 #define IMG_LINE_GRAPH ":/images/16x16/graph-line.png"
38 
39 
40 /** Default constructor */
41 BandwidthGraph::BandwidthGraph(QWidget *parent, Qt::WFlags flags)
42  : VidaliaWindow("BandwidthGraph", parent, flags)
43 {
44  /* Invoke Qt Designer generated QObject setup routine */
45  ui.setupUi(this);
46 
47  /* Ask Tor to notify us about bandwidth updates */
49  connect(Vidalia::torControl(), SIGNAL(bandwidthUpdate(quint64,quint64)),
50  this, SLOT(updateGraph(quint64,quint64)));
51 
52  /* Pressing 'Esc' or 'Ctrl+W' will close the window */
53  setShortcut("Esc", SLOT(close()));
54  setShortcut("Ctrl+W", SLOT(close()));
55 
56  /* Bind events to actions */
57  createActions();
58 
59  /* Initialize Sent/Receive data counters */
60  reset();
61  /* Hide Bandwidth Graph Settings frame */
62  showSettingsFrame(false);
63  /* Load the previously saved settings */
64  loadSettings();
65 
66  /* Turn off opacity group on unsupported platforms */
67 #if defined(Q_WS_WIN)
68  if(!(QSysInfo::WindowsVersion & QSysInfo::WV_NT_based)
69  || QSysInfo::WindowsVersion < QSysInfo::WV_2000) {
70  ui.frmOpacity->setVisible(false);
71  }
72 #endif
73 
74 #if defined(Q_WS_X11)
75  ui.frmOpacity->setVisible(false);
76 #endif
77 }
78 
79 /** Called when the user changes the UI translation. */
80 void
82 {
83  ui.retranslateUi(this);
84 }
85 
86 /** Binds events to actions. */
87 void
89 {
90  connect(ui.btnToggleSettings, SIGNAL(toggled(bool)),
91  this, SLOT(showSettingsFrame(bool)));
92 
93  connect(ui.btnReset, SIGNAL(clicked()),
94  this, SLOT(reset()));
95 
96  connect(ui.btnSaveSettings, SIGNAL(clicked()),
97  this, SLOT(saveChanges()));
98 
99  connect(ui.btnCancelSettings, SIGNAL(clicked()),
100  this, SLOT(cancelChanges()));
101 
102  connect(ui.sldrOpacity, SIGNAL(valueChanged(int)),
103  this, SLOT(setOpacity(int)));
104 }
105 
106 /** Adds new data to the graph. */
107 void
108 BandwidthGraph::updateGraph(quint64 bytesRead, quint64 bytesWritten)
109 {
110  /* Graph only cares about kilobytes */
111  ui.frmGraph->addPoints(bytesRead/1024.0, bytesWritten/1024.0);
112 }
113 
114 /** Loads the saved Bandwidth Graph settings. */
115 void
117 {
118  /* Set window opacity slider widget */
119  ui.sldrOpacity->setValue(getSetting(SETTING_OPACITY, DEFAULT_OPACITY).toInt());
120  setOpacity(ui.sldrOpacity->value());
121 
122  /* Set whether the window appears on top. */
123  ui.chkAlwaysOnTop->setChecked(getSetting(SETTING_ALWAYS_ON_TOP,
124  DEFAULT_ALWAYS_ON_TOP).toBool());
125  if (ui.chkAlwaysOnTop->isChecked()) {
126  setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
127  } else {
128  setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
129  }
130 
131  /* Set the line filter checkboxes accordingly */
132  uint filter = getSetting(SETTING_FILTER, DEFAULT_FILTER).toUInt();
133  ui.chkReceiveRate->setChecked(filter & BWGRAPH_LINE_RECV);
134  ui.chkSendRate->setChecked(filter & BWGRAPH_LINE_SEND);
135 
136  /* Set whether we are plotting bandwidth as area graphs or not */
137  int graphStyle = getSetting(SETTING_STYLE, DEFAULT_STYLE).toInt();
138  if (graphStyle < 0 || graphStyle >= ui.cmbGraphStyle->count()) {
139  graphStyle = DEFAULT_STYLE;
140  }
141  ui.cmbGraphStyle->setCurrentIndex(graphStyle);
142  ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)graphStyle);
143 
144  /* Set graph frame settings */
145  ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
146  ui.chkSendRate->isChecked());
147 }
148 
149 /** Resets the log start time. */
150 void
152 {
153  /* Set to current time */
154  ui.statusbar->showMessage(tr("Since:") + " " +
155  QDateTime::currentDateTime()
156  .toString(DATETIME_FMT));
157  /* Reset the graph */
158  ui.frmGraph->resetGraph();
159 }
160 
161 /** Saves the Bandwidth Graph settings and adjusts the graph if necessary. */
162 void
164 {
165  /* Hide the settings frame and reset toggle button */
166  showSettingsFrame(false);
167 
168  /* Save the opacity and graph style */
169  saveSetting(SETTING_OPACITY, ui.sldrOpacity->value());
170  saveSetting(SETTING_STYLE, ui.cmbGraphStyle->currentIndex());
171 
172  /* Save the Always On Top setting */
173  saveSetting(SETTING_ALWAYS_ON_TOP, ui.chkAlwaysOnTop->isChecked());
174  if (ui.chkAlwaysOnTop->isChecked()) {
175  setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
176  } else {
177  setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
178  }
179  setOpacity(ui.sldrOpacity->value());
180 
181  /* Save the line filter values */
182  uint filter = 0;
183  ADD_TO_FILTER(filter, BWGRAPH_LINE_RECV, ui.chkReceiveRate->isChecked());
184  ADD_TO_FILTER(filter, BWGRAPH_LINE_SEND, ui.chkSendRate->isChecked());
185  saveSetting(SETTING_FILTER, filter);
186 
187 
188  /* Update the graph frame settings */
189  ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
190  ui.chkSendRate->isChecked());
191  ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)ui.cmbGraphStyle->currentIndex());
192 
193  /* A change in window flags causes the window to disappear, so make sure
194  * it's still visible. */
195  showNormal();
196 }
197 
198 /** Simply restores the previously saved settings. */
199 void
201 {
202  /* Hide the settings frame and reset toggle button */
203  showSettingsFrame(false);
204 
205  /* Reload the settings */
206  loadSettings();
207 }
208 
209 /** Toggles the Settings pane on and off, changes toggle button text. */
210 void
212 {
213  static QSize minSize = minimumSize();
214 
215  QSize newSize = size();
216  if (show) {
217  /* Extend the bottom of the bandwidth graph and show the settings */
218  ui.frmSettings->setVisible(true);
219  ui.btnToggleSettings->setChecked(true);
220  ui.btnToggleSettings->setText(tr("Hide Settings"));
221 
222  /* 6 = vertical spacing between the settings frame and graph frame */
223  newSize.setHeight(newSize.height() + ui.frmSettings->height() + 6);
224  } else {
225  /* Shrink the height of the bandwidth graph and hide the settings */
226  ui.frmSettings->setVisible(false);
227  ui.btnToggleSettings->setChecked(false);
228  ui.btnToggleSettings->setText(tr("Show Settings"));
229 
230  /* 6 = vertical spacing between the settings frame and graph frame */
231  newSize.setHeight(newSize.height() - ui.frmSettings->height() - 6);
232  setMinimumSize(minSize);
233  }
234  resize(newSize);
235 }
236 
237 /** Sets the opacity of the Bandwidth Graph window. */
238 void
240 {
241  qreal newValue = value / 100.0;
242 
243  /* Opacity only supported by Mac and Win32 */
244 #if defined(Q_WS_MAC)
245  this->setWindowOpacity(newValue);
246  ui.lblPercentOpacity->setText(QString::number(value));
247 #elif defined(Q_WS_WIN)
248  if (QSysInfo::WindowsVersion & QSysInfo::WV_NT_based
249  && QSysInfo::WindowsVersion >= QSysInfo::WV_2000) {
250  this->setWindowOpacity(newValue);
251  ui.lblPercentOpacity->setText(QString::number(value));
252  }
253 #else
254  Q_UNUSED(newValue);
255 #endif
256 }
257 
258 /** Overloads the default show() slot so we can set opacity. */
259 void
261 {
262  /* Load saved settings */
263  loadSettings();
264  /* Show the window */
266 }
267