00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <QtGlobal>
00029
00030 #include "graphframe.h"
00031
00032
00033
00034 GraphFrame::GraphFrame(QWidget *parent)
00035 : QFrame(parent)
00036 {
00037
00038 _recvData = new QList<qreal>();
00039 _sendData = new QList<qreal>();
00040 _painter = new QPainter();
00041
00042
00043 _recvData->prepend(0);
00044 _sendData->prepend(0);
00045 _maxPoints = getNumPoints();
00046 _showRecv = true;
00047 _showSend = true;
00048 _maxValue = MIN_SCALE;
00049
00050 }
00051
00052
00053 GraphFrame::~GraphFrame()
00054 {
00055 delete _painter;
00056 delete _recvData;
00057 delete _sendData;
00058 }
00059
00060
00061
00062 int
00063 GraphFrame::getNumPoints()
00064 {
00065 QDesktopWidget *desktop = QApplication::desktop();
00066 int width = desktop->width();
00067 return width;
00068 }
00069
00070
00071 void
00072 GraphFrame::addPoints(qreal recv, qreal send)
00073 {
00074
00075 if (_sendData->size() == _maxPoints) {
00076 _sendData->removeLast();
00077 _recvData->removeLast();
00078 }
00079
00080
00081 _sendData->prepend(send);
00082 _recvData->prepend(recv);
00083
00084
00085 _totalSend += send;
00086 _totalRecv += recv;
00087
00088
00089 if (send > _maxValue) _maxValue = send;
00090 if (recv > _maxValue) _maxValue = recv;
00091
00092 this->update();
00093 }
00094
00095
00096 void
00097 GraphFrame::resetGraph()
00098 {
00099 _recvData->clear();
00100 _sendData->clear();
00101 _recvData->prepend(0);
00102 _sendData->prepend(0);
00103 _maxValue = MIN_SCALE;
00104 _totalSend = 0;
00105 _totalRecv = 0;
00106 this->update();
00107 }
00108
00109
00110 void
00111 GraphFrame::setShowCounters(bool showRecv, bool showSend)
00112 {
00113 _showRecv = showRecv;
00114 _showSend = showSend;
00115 this->update();
00116 }
00117
00118
00119
00120 void
00121 GraphFrame::paintEvent(QPaintEvent *event)
00122 {
00123 Q_UNUSED(event);
00124
00125
00126 _rec = this->frameRect();
00127
00128
00129 _painter->begin(this);
00130
00131
00132 _painter->setRenderHint(QPainter::Antialiasing);
00133 _painter->setRenderHint(QPainter::TextAntialiasing);
00134
00135
00136 _painter->fillRect(_rec, QBrush(BACK_COLOR));
00137 _painter->drawRect(_rec);
00138
00139
00140 paintScale();
00141
00142 paintData();
00143
00144 paintTotals();
00145
00146
00147 _painter->end();
00148 }
00149
00150
00151
00152
00153
00154 void
00155 GraphFrame::paintData()
00156 {
00157
00158
00159
00160
00161 if (_showRecv) {
00162 paintIntegral(_recvData, RECV_COLOR, 0.6);
00163 }
00164 if (_showSend) {
00165 paintIntegral(_sendData, SEND_COLOR, 0.4);
00166 }
00167
00168
00169 if (_showRecv) {
00170 paintLine(_recvData, RECV_COLOR);
00171 }
00172
00173 if (_showSend) {
00174 paintLine(_sendData, SEND_COLOR);
00175 }
00176 }
00177
00178
00179
00180
00181 void
00182 GraphFrame::paintIntegral(QList<qreal>* list, QColor color, qreal alpha)
00183 {
00184 QVector<QPointF> points;
00185 int x = _rec.width();
00186 int y = _rec.height();
00187 qreal scale = (y - (y/10)) / _maxValue;
00188 qreal currValue;
00189
00190
00191 points << QPointF(x, y);
00192 for (int i = 0; i < list->size(); i++) {
00193 currValue = y - (list->at(i) * scale);
00194 if (x - SCROLL_STEP < SCALE_WIDTH) {
00195 points << QPointF(SCALE_WIDTH, currValue);
00196 break;
00197 }
00198 points << QPointF(x, currValue);
00199 x -= SCROLL_STEP;
00200 }
00201 points << QPointF(SCALE_WIDTH, y);
00202
00203
00204 QBrush oldBrush = _painter->brush();
00205 color.setAlphaF(alpha);
00206 _painter->setBrush(QBrush(color));
00207 _painter->drawPolygon(points.data(), points.size());
00208 _painter->setBrush(oldBrush);
00209 }
00210
00211
00212
00213 void
00214 GraphFrame::paintLine(QList<qreal>* list, QColor color, Qt::PenStyle lineStyle)
00215 {
00216 int x = _rec.width() + SCROLL_STEP;
00217 int y = _rec.height();
00218 qreal scale = (y - (y/10)) / _maxValue;
00219
00220 qreal prevValue = y - (list->at(0) * scale);
00221 qreal currValue;
00222
00223
00224 QPen oldPen = _painter->pen();
00225 _painter->setPen(QPen(color, lineStyle));
00226 for (int i = 0; i < list->size(); ++i) {
00227 currValue = y - (list->at(i) * scale);
00228
00229
00230 if (x - SCROLL_STEP < SCALE_WIDTH) {
00231 _painter->drawLine(QPointF(x, prevValue),
00232 QPointF(SCALE_WIDTH, currValue));
00233 break;
00234 }
00235
00236 _painter->drawLine(QPointF(x, prevValue),
00237 QPointF(x-SCROLL_STEP, currValue));
00238
00239
00240 prevValue = currValue;
00241 x -= SCROLL_STEP;
00242 }
00243 _painter->setPen(oldPen);
00244 }
00245
00246
00247 void
00248 GraphFrame::paintTotals()
00249 {
00250 int x = SCALE_WIDTH + FONT_SIZE, y = 0;
00251 int rowHeight = FONT_SIZE;
00252
00253 #if !defined(Q_WS_MAC)
00254
00255 rowHeight += 5;
00256 #endif
00257
00258
00259 if (_showRecv) {
00260 y = rowHeight;
00261 _painter->setPen(RECV_COLOR);
00262 _painter->drawText(x, y,
00263 tr("Recv: ") + totalToStr(_totalRecv) +
00264 " ("+tr("%1 KB/s").arg(_recvData->first(), 0, 'f', 2)+")");
00265 }
00266
00267
00268 if (_showSend) {
00269 y += rowHeight;
00270 _painter->setPen(SEND_COLOR);
00271 _painter->drawText(x, y,
00272 tr("Sent: ") + totalToStr(_totalSend) +
00273 " ("+tr("%1 KB/s").arg(_sendData->first(), 0, 'f', 2)+")");
00274 }
00275 }
00276
00277
00278 QString
00279 GraphFrame::totalToStr(qreal total)
00280 {
00281
00282 if (total < 1024) {
00283
00284 return tr("%1 KB").arg(total, 0, 'f', 2);
00285 } else if (total < 1048576) {
00286
00287 return tr("%1 MB").arg(total/1024.0, 0, 'f', 2);
00288 } else {
00289
00290 return tr("%1 GB").arg(total/1048576.0, 0, 'f', 2);
00291 }
00292 }
00293
00294
00295 void
00296 GraphFrame::paintScale()
00297 {
00298 qreal markStep = _maxValue * .25;
00299 int top = _rec.y();
00300 int bottom = _rec.height();
00301 qreal paintStep = (bottom - (bottom/10)) / 4;
00302
00303
00304 qreal scale;
00305 qreal pos;
00306 for (int i = 1; i < 5; i++) {
00307 pos = bottom - (i * paintStep);
00308 scale = i * markStep;
00309 _painter->setPen(SCALE_COLOR);
00310 _painter->drawText(QPointF(5, pos+FONT_SIZE),
00311 tr("%1 KB/s").arg(scale, 0, 'f', 2));
00312 _painter->setPen(GRID_COLOR);
00313 _painter->drawLine(QPointF(SCALE_WIDTH, pos),
00314 QPointF(_rec.width(), pos));
00315 }
00316
00317
00318 _painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom);
00319 }
00320