Skip to content

Commit 8a6866d

Browse files
committed
SpeedPlotView: Simplify code using PointData struct
1 parent b599a8e commit 8a6866d

3 files changed

Lines changed: 64 additions & 100 deletions

File tree

src/gui/properties/speedplotview.cpp

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
SpeedPlotView::SpeedPlotView(QWidget *parent)
3636
: QGraphicsView(parent)
37+
, m_data5Min(MIN5_BUF_SIZE)
38+
, m_data30Min(MIN30_BUF_SIZE)
39+
, m_data6Hour(HOUR6_BUF_SIZE)
3740
, m_viewablePointsCount(MIN5_SEC)
3841
, m_counter30Min(-1)
3942
, m_counter6Hour(-1)
@@ -67,26 +70,6 @@ SpeedPlotView::SpeedPlotView(QWidget *parent)
6770
greenPen.setStyle(Qt::DotLine);
6871
m_properties[TRACKER_UP] = GraphProperties(tr("Tracker Upload"), bluePen);
6972
m_properties[TRACKER_DOWN] = GraphProperties(tr("Tracker Download"), greenPen);
70-
71-
initBuffers(MIN5_BUF_SIZE, m_xData5Min, m_yData5Min);
72-
initBuffers(MIN30_BUF_SIZE, m_xData30Min, m_yData30Min);
73-
initBuffers(HOUR6_BUF_SIZE, m_xData6Hour, m_yData6Hour);
74-
}
75-
76-
void SpeedPlotView::initBuffers(int capacity, boost::circular_buffer<uint> &xBuffer,
77-
QMap<SpeedPlotView::GraphID, boost::circular_buffer<int> > &yBuffers)
78-
{
79-
xBuffer = boost::circular_buffer<uint>(capacity);
80-
yBuffers[UP] = boost::circular_buffer<int>(capacity);
81-
yBuffers[DOWN] = boost::circular_buffer<int>(capacity);
82-
yBuffers[PAYLOAD_UP] = boost::circular_buffer<int>(capacity);
83-
yBuffers[PAYLOAD_DOWN] = boost::circular_buffer<int>(capacity);
84-
yBuffers[OVERHEAD_UP] = boost::circular_buffer<int>(capacity);
85-
yBuffers[OVERHEAD_DOWN] = boost::circular_buffer<int>(capacity);
86-
yBuffers[DHT_UP] = boost::circular_buffer<int>(capacity);
87-
yBuffers[DHT_DOWN] = boost::circular_buffer<int>(capacity);
88-
yBuffers[TRACKER_UP] = boost::circular_buffer<int>(capacity);
89-
yBuffers[TRACKER_DOWN] = boost::circular_buffer<int>(capacity);
9073
}
9174

9275
void SpeedPlotView::setGraphEnable(GraphID id, bool enable)
@@ -95,35 +78,32 @@ void SpeedPlotView::setGraphEnable(GraphID id, bool enable)
9578
this->viewport()->update();
9679
}
9780

98-
void SpeedPlotView::pushXPoint(uint x)
81+
void SpeedPlotView::pushPoint(SpeedPlotView::PointData point)
9982
{
100-
++m_counter30Min;
101-
m_counter30Min %= 3;
102-
++m_counter6Hour;
103-
m_counter6Hour %= 6;
83+
m_counter30Min = (m_counter30Min + 1) % 3;
84+
m_counter6Hour = (m_counter6Hour + 1) % 6;
10485

105-
m_xData5Min.push_back(x);
106-
107-
if (m_counter30Min == 0)
108-
m_xData30Min.push_back(x);
109-
110-
if (m_counter6Hour == 0)
111-
m_xData6Hour.push_back(x);
112-
}
86+
m_data5Min.push_back(point);
11387

114-
void SpeedPlotView::pushYPoint(GraphID id, int y)
115-
{
116-
m_yData5Min[id].push_back(y);
117-
118-
if (m_counter30Min == 0)
119-
m_yData30Min[id].push_back(y);
120-
else
121-
m_yData30Min[id].back() = (m_yData30Min[id].back() * m_counter30Min + y) / (m_counter30Min + 1);
88+
if (m_counter30Min == 0) {
89+
m_data30Min.push_back(point);
90+
}
91+
else {
92+
m_data30Min.back().x = (m_data30Min.back().x * m_counter30Min + point.x) / (m_counter30Min + 1);
93+
for (int id = UP; id < NB_GRAPHS; ++id) {
94+
m_data30Min.back().y[id] = (m_data30Min.back().y[id] * m_counter30Min + point.y[id]) / (m_counter30Min + 1);
95+
}
96+
}
12297

123-
if (m_counter6Hour == 0)
124-
m_yData6Hour[id].push_back(y);
125-
else
126-
m_yData6Hour[id].back() = (m_yData6Hour[id].back() * m_counter6Hour + y) / (m_counter6Hour + 1);
98+
if (m_counter6Hour == 0) {
99+
m_data6Hour.push_back(point);
100+
}
101+
else {
102+
m_data6Hour.back().x = (m_data6Hour.back().x * m_counter6Hour + point.x) / (m_counter6Hour + 1);
103+
for (int id = UP; id < NB_GRAPHS; ++id) {
104+
m_data6Hour.back().y[id] = (m_data6Hour.back().y[id] * m_counter6Hour + point.y[id]) / (m_counter6Hour + 1);
105+
}
106+
}
127107
}
128108

129109
void SpeedPlotView::setViewableLastPoints(TimePeriod period)
@@ -156,49 +136,33 @@ void SpeedPlotView::replot()
156136
this->viewport()->update();
157137
}
158138

159-
boost::circular_buffer<uint> &SpeedPlotView::getCurrentXData()
139+
boost::circular_buffer<SpeedPlotView::PointData> &SpeedPlotView::getCurrentData()
160140
{
161141
switch (m_period) {
162142
default:
163143
case SpeedPlotView::MIN1:
164144
case SpeedPlotView::MIN5:
165-
return m_xData5Min;
145+
return m_data5Min;
166146
case SpeedPlotView::MIN30:
167-
return m_xData30Min;
147+
return m_data30Min;
168148
case SpeedPlotView::HOUR6:
169-
return m_xData6Hour;
170-
}
171-
}
172-
173-
QMap<SpeedPlotView::GraphID, boost::circular_buffer<int> > &SpeedPlotView::getCurrentYData()
174-
{
175-
switch (m_period) {
176-
default:
177-
case SpeedPlotView::MIN1:
178-
case SpeedPlotView::MIN5:
179-
return m_yData5Min;
180-
case SpeedPlotView::MIN30:
181-
return m_yData30Min;
182-
case SpeedPlotView::HOUR6:
183-
return m_yData6Hour;
149+
return m_data6Hour;
184150
}
185151
}
186152

187153
int SpeedPlotView::maxYValue()
188154
{
189-
QMap<GraphID, boost::circular_buffer<int> > &yData = getCurrentYData();
155+
boost::circular_buffer<PointData> &queue = getCurrentData();
190156

191157
int maxYValue = 0;
192-
for (QMap<GraphID, boost::circular_buffer<int> >::const_iterator it = yData.begin(); it != yData.end(); ++it) {
158+
for (int id = UP; id < NB_GRAPHS; ++id) {
193159

194-
if (!m_properties[it.key()].m_enable)
160+
if (!m_properties[static_cast<GraphID>(id)].m_enable)
195161
continue;
196162

197-
const boost::circular_buffer<int> &queue = it.value();
198-
199163
for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) {
200-
if (queue[i] > maxYValue)
201-
maxYValue = queue[i];
164+
if (queue[i].y[id] > maxYValue)
165+
maxYValue = queue[i].y[id];
202166
}
203167
}
204168

@@ -269,25 +233,24 @@ void SpeedPlotView::paintEvent(QPaintEvent *)
269233
double yMultiplier = (maxY == 0) ? 0.0 : double(rect.height()) / maxY;
270234
double xTickSize = double(rect.width()) / m_viewablePointsCount;
271235

272-
QMap<GraphID, boost::circular_buffer<int> > &yData = getCurrentYData();
236+
boost::circular_buffer<PointData> &queue = getCurrentData();
273237

274-
for (QMap<GraphID, boost::circular_buffer<int> >::const_iterator it = yData.begin(); it != yData.end(); ++it) {
238+
for (int id = UP; id < NB_GRAPHS; ++id) {
275239

276-
if (!m_properties[it.key()].m_enable)
240+
if (!m_properties[static_cast<GraphID>(id)].m_enable)
277241
continue;
278242

279-
const boost::circular_buffer<int> &queue = it.value();
280243
QVector<QPoint> points;
281244

282245
for (int i = queue.size() - 1, j = 0; i >= 0 && j <= m_viewablePointsCount; --i, ++j) {
283246

284247
int new_x = rect.right() - j * xTickSize;
285-
int new_y = rect.bottom() - queue[i] * yMultiplier;
248+
int new_y = rect.bottom() - queue[i].y[id] * yMultiplier;
286249

287250
points.push_back(QPoint(new_x, new_y));
288251
}
289252

290-
painter.setPen(m_properties[it.key()].m_pen);
253+
painter.setPen(m_properties[static_cast<GraphID>(id)].m_pen);
291254
painter.drawPolyline(points.data(), points.size());
292255
}
293256

src/gui/properties/speedplotview.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ class SpeedPlotView : public QGraphicsView
6464
HOUR6
6565
};
6666

67+
struct PointData
68+
{
69+
uint x;
70+
int y[NB_GRAPHS];
71+
};
72+
6773
explicit SpeedPlotView(QWidget *parent = 0);
6874

6975
void setGraphEnable(GraphID id, bool enable);
7076

71-
void pushXPoint(uint x);
72-
void pushYPoint(GraphID id, int y);
77+
void pushPoint(PointData point);
7378

7479
void setViewableLastPoints(TimePeriod period);
7580

@@ -104,12 +109,9 @@ class SpeedPlotView : public QGraphicsView
104109
bool m_enable;
105110
};
106111

107-
boost::circular_buffer<uint> m_xData5Min;
108-
boost::circular_buffer<uint> m_xData30Min;
109-
boost::circular_buffer<uint> m_xData6Hour;
110-
QMap<GraphID, boost::circular_buffer<int> > m_yData5Min;
111-
QMap<GraphID, boost::circular_buffer<int> > m_yData30Min;
112-
QMap<GraphID, boost::circular_buffer<int> > m_yData6Hour;
112+
boost::circular_buffer<PointData> m_data5Min;
113+
boost::circular_buffer<PointData> m_data30Min;
114+
boost::circular_buffer<PointData> m_data6Hour;
113115
QMap<GraphID, GraphProperties> m_properties;
114116

115117
TimePeriod m_period;
@@ -120,11 +122,7 @@ class SpeedPlotView : public QGraphicsView
120122

121123
int maxYValue();
122124

123-
void initBuffers(int capacity, boost::circular_buffer<uint> &xBuffer,
124-
QMap<SpeedPlotView::GraphID, boost::circular_buffer<int> > &yBuffers);
125-
126-
boost::circular_buffer<uint> &getCurrentXData();
127-
QMap<SpeedPlotView::GraphID, boost::circular_buffer<int> > &getCurrentYData();
125+
boost::circular_buffer<PointData> &getCurrentData();
128126
};
129127

130128
#endif // SPEEDPLOTVIEW_H

src/gui/properties/speedwidget.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,20 @@ void SpeedWidget::update()
138138

139139
BitTorrent::SessionStatus btStatus = BitTorrent::Session::instance()->status();
140140

141-
m_plot->pushXPoint(QDateTime::currentDateTime().toTime_t());
142-
m_plot->pushYPoint(SpeedPlotView::UP, btStatus.uploadRate());
143-
m_plot->pushYPoint(SpeedPlotView::DOWN, btStatus.downloadRate());
144-
m_plot->pushYPoint(SpeedPlotView::PAYLOAD_UP, btStatus.payloadUploadRate());
145-
m_plot->pushYPoint(SpeedPlotView::PAYLOAD_DOWN, btStatus.payloadDownloadRate());
146-
m_plot->pushYPoint(SpeedPlotView::OVERHEAD_UP, btStatus.ipOverheadUploadRate());
147-
m_plot->pushYPoint(SpeedPlotView::OVERHEAD_DOWN, btStatus.ipOverheadDownloadRate());
148-
m_plot->pushYPoint(SpeedPlotView::DHT_UP, btStatus.dhtUploadRate());
149-
m_plot->pushYPoint(SpeedPlotView::DHT_DOWN, btStatus.dhtDownloadRate());
150-
m_plot->pushYPoint(SpeedPlotView::TRACKER_UP, btStatus.trackerUploadRate());
151-
m_plot->pushYPoint(SpeedPlotView::TRACKER_DOWN, btStatus.trackerDownloadRate());
141+
SpeedPlotView::PointData point;
142+
point.x = QDateTime::currentDateTime().toTime_t();
143+
point.y[SpeedPlotView::UP] = btStatus.uploadRate();
144+
point.y[SpeedPlotView::DOWN] = btStatus.downloadRate();
145+
point.y[SpeedPlotView::PAYLOAD_UP] = btStatus.payloadUploadRate();
146+
point.y[SpeedPlotView::PAYLOAD_DOWN] = btStatus.payloadDownloadRate();
147+
point.y[SpeedPlotView::OVERHEAD_UP] = btStatus.ipOverheadUploadRate();
148+
point.y[SpeedPlotView::OVERHEAD_DOWN] = btStatus.ipOverheadDownloadRate();
149+
point.y[SpeedPlotView::DHT_UP] = btStatus.dhtUploadRate();
150+
point.y[SpeedPlotView::DHT_DOWN] = btStatus.dhtDownloadRate();
151+
point.y[SpeedPlotView::TRACKER_UP] = btStatus.trackerUploadRate();
152+
point.y[SpeedPlotView::TRACKER_DOWN] = btStatus.trackerDownloadRate();
153+
154+
m_plot->pushPoint(point);
152155

153156
QMetaObject::invokeMethod(this, "graphUpdate", Qt::QueuedConnection);
154157
Utils::Misc::msleep(1000);

0 commit comments

Comments
 (0)