Vidalia 0.3.1
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 */
41BandwidthGraph::BandwidthGraph(QStatusBar *st, QWidget *parent)
42 : VidaliaTab(tr("Bandwidth Graph"), "BandwidthGraph", parent),
43 _statusBar(st)
44{
45 /* Invoke Qt Designer generated QObject setup routine */
46 ui.setupUi(this);
47
48 /* Ask Tor to notify us about bandwidth updates */
50 connect(Vidalia::torControl(), SIGNAL(bandwidthUpdate(quint64,quint64)),
51 this, SLOT(updateGraph(quint64,quint64)));
52
53 /* Pressing 'Esc' or 'Ctrl+W' will close the window */
54// setShortcut("Esc", SLOT(close()));
55// setShortcut("Ctrl+W", SLOT(close()));
56
57 /* Bind events to actions */
59
60 /* Initialize Sent/Receive data counters */
61 reset();
62 /* Hide Bandwidth Graph Settings frame */
63 showSettingsFrame(false);
64 /* Load the previously saved settings */
66
67 /* Turn off opacity group on unsupported platforms */
68#if defined(Q_WS_WIN)
69 if(!(QSysInfo::WindowsVersion & QSysInfo::WV_NT_based)
70 || QSysInfo::WindowsVersion < QSysInfo::WV_2000) {
71 ui.frmOpacity->setVisible(false);
72 }
73#endif
74
75#if defined(Q_WS_X11)
76 ui.frmOpacity->setVisible(false);
77#endif
78}
79
80/** Called when the user changes the UI translation. */
81void
83{
84 ui.retranslateUi(this);
85 setTitle(tr("Bandwidth Graph"));
86}
87
88/** Binds events to actions. */
89void
91{
92 connect(ui.btnToggleSettings, SIGNAL(toggled(bool)),
93 this, SLOT(showSettingsFrame(bool)));
94
95 connect(ui.btnReset, SIGNAL(clicked()),
96 this, SLOT(reset()));
97
98 connect(ui.btnSaveSettings, SIGNAL(clicked()),
99 this, SLOT(saveChanges()));
100
101 connect(ui.btnCancelSettings, SIGNAL(clicked()),
102 this, SLOT(cancelChanges()));
103
104 connect(ui.sldrOpacity, SIGNAL(valueChanged(int)),
105 this, SLOT(setOpacity(int)));
106}
107
108/** Adds new data to the graph. */
109void
110BandwidthGraph::updateGraph(quint64 bytesRead, quint64 bytesWritten)
111{
112 /* Graph only cares about kilobytes */
113 ui.frmGraph->addPoints(bytesRead/1024.0, bytesWritten/1024.0);
114}
115
116/** Loads the saved Bandwidth Graph settings. */
117void
119{
120 /* Set window opacity slider widget */
121 ui.sldrOpacity->setValue(getSetting(SETTING_OPACITY, DEFAULT_OPACITY).toInt());
122 setOpacity(ui.sldrOpacity->value());
123
124 /* Set whether the window appears on top. */
125 ui.chkAlwaysOnTop->setChecked(getSetting(SETTING_ALWAYS_ON_TOP,
126 DEFAULT_ALWAYS_ON_TOP).toBool());
127 if (ui.chkAlwaysOnTop->isChecked()) {
128 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
129 } else {
130 setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
131 }
132
133 /* Set the line filter checkboxes accordingly */
134 uint filter = getSetting(SETTING_FILTER, DEFAULT_FILTER).toUInt();
135 ui.chkReceiveRate->setChecked(filter & BWGRAPH_LINE_RECV);
136 ui.chkSendRate->setChecked(filter & BWGRAPH_LINE_SEND);
137
138 /* Set whether we are plotting bandwidth as area graphs or not */
139 int graphStyle = getSetting(SETTING_STYLE, DEFAULT_STYLE).toInt();
140 if (graphStyle < 0 || graphStyle >= ui.cmbGraphStyle->count()) {
141 graphStyle = DEFAULT_STYLE;
142 }
143 ui.cmbGraphStyle->setCurrentIndex(graphStyle);
144 ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)graphStyle);
145
146 /* Set graph frame settings */
147 ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
148 ui.chkSendRate->isChecked());
149}
150
151/** Resets the log start time. */
152void
154{
155 /* Set to current time */
156 if(_statusBar && _onTop)
157 _statusBar->showMessage(tr("Since:") + " " +
158 QDateTime::currentDateTime()
159 .toString(DATETIME_FMT));
160 /* Reset the graph */
161 ui.frmGraph->resetGraph();
162}
163
164/** Saves the Bandwidth Graph settings and adjusts the graph if necessary. */
165void
167{
168 /* Hide the settings frame and reset toggle button */
169 showSettingsFrame(false);
170
171 /* Save the opacity and graph style */
172 saveSetting(SETTING_OPACITY, ui.sldrOpacity->value());
173 saveSetting(SETTING_STYLE, ui.cmbGraphStyle->currentIndex());
174
175 /* Save the Always On Top setting */
176 saveSetting(SETTING_ALWAYS_ON_TOP, ui.chkAlwaysOnTop->isChecked());
177 if (ui.chkAlwaysOnTop->isChecked()) {
178 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
179 } else {
180 setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
181 }
182 setOpacity(ui.sldrOpacity->value());
183
184 /* Save the line filter values */
185 uint filter = 0;
186 ADD_TO_FILTER(filter, BWGRAPH_LINE_RECV, ui.chkReceiveRate->isChecked());
187 ADD_TO_FILTER(filter, BWGRAPH_LINE_SEND, ui.chkSendRate->isChecked());
189
190
191 /* Update the graph frame settings */
192 ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
193 ui.chkSendRate->isChecked());
194 ui.frmGraph->setGraphStyle((GraphFrame::GraphStyle)ui.cmbGraphStyle->currentIndex());
195
196 /* A change in window flags causes the window to disappear, so make sure
197 * it's still visible. */
198 showNormal();
199}
200
201/** Simply restores the previously saved settings. */
202void
204{
205 /* Hide the settings frame and reset toggle button */
206 showSettingsFrame(false);
207
208 /* Reload the settings */
209 loadSettings();
210}
211
212/** Toggles the Settings pane on and off, changes toggle button text. */
213void
215{
216 static QSize minSize = minimumSize();
217
218 QSize newSize = size();
219 if (show) {
220 /* Extend the bottom of the bandwidth graph and show the settings */
221 ui.frmSettings->setVisible(true);
222 ui.btnToggleSettings->setChecked(true);
223 ui.btnToggleSettings->setText(tr("Hide Settings"));
224
225 /* 6 = vertical spacing between the settings frame and graph frame */
226 newSize.setHeight(newSize.height() + ui.frmSettings->height() + 6);
227 } else {
228 /* Shrink the height of the bandwidth graph and hide the settings */
229 ui.frmSettings->setVisible(false);
230 ui.btnToggleSettings->setChecked(false);
231 ui.btnToggleSettings->setText(tr("Show Settings"));
232
233 /* 6 = vertical spacing between the settings frame and graph frame */
234 newSize.setHeight(newSize.height() - ui.frmSettings->height() - 6);
235 setMinimumSize(minSize);
236 }
237 resize(newSize);
238}
239
240/** Sets the opacity of the Bandwidth Graph window. */
241void
243{
244 qreal newValue = value / 100.0;
245
246 /* Opacity only supported by Mac and Win32 */
247#if defined(Q_WS_MAC)
248 this->setWindowOpacity(newValue);
249 ui.lblPercentOpacity->setText(QString::number(value));
250#elif defined(Q_WS_WIN)
251 if (QSysInfo::WindowsVersion & QSysInfo::WV_NT_based
252 && QSysInfo::WindowsVersion >= QSysInfo::WV_2000) {
253 this->setWindowOpacity(newValue);
254 ui.lblPercentOpacity->setText(QString::number(value));
255 }
256#else
257 Q_UNUSED(newValue);
258#endif
259}
260
#define SETTING_FILTER
#define SETTING_STYLE
#define SETTING_OPACITY
#define BWGRAPH_LINE_RECV
#define DEFAULT_OPACITY
#define DATETIME_FMT
#define DEFAULT_ALWAYS_ON_TOP
#define DEFAULT_FILTER
#define SETTING_ALWAYS_ON_TOP
#define BWGRAPH_LINE_SEND
#define DEFAULT_STYLE
#define ADD_TO_FILTER(f, v, b)
stop errmsg connect(const QHostAddress &address, quint16 port)
virtual void retranslateUi()
Ui::BandwidthGraph ui
void showSettingsFrame(bool show)
void updateGraph(quint64 bytesRead, quint64 bytesWritten)
QStatusBar * _statusBar
void setOpacity(int value)
BandwidthGraph(QStatusBar *st=0, QWidget *parent=0)
bool setEvent(TorEvents::Event e, bool add=true, bool set=true, QString *errmsg=0)
Definition: TorControl.cpp:697
@ Bandwidth
Definition: TorEvents.h:45
static TorControl * torControl()
Definition: Vidalia.h:76
QVariant getSetting(QString name, QVariant defaultValue)
Definition: VidaliaTab.cpp:21
void saveSetting(QString name, QVariant value)
Definition: VidaliaTab.cpp:28
void setTitle(const QString &title)
Definition: VidaliaTab.h:36
bool _onTop
Definition: VidaliaTab.h:66