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 _graphStyle = SolidLine;
00042
00043
00044 _recvData->prepend(0);
00045 _sendData->prepend(0);
00046 _maxPoints = getNumPoints();
00047 _showRecv = true;
00048 _showSend = true;
00049 _maxValue = MIN_SCALE;
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 QVector<QPointF> recvPoints, sendPoints;
00158
00159
00160 recvPoints = pointsFromData(_recvData);
00161 sendPoints = pointsFromData(_sendData);
00162
00163 if (_graphStyle == AreaGraph) {
00164
00165 if (_showRecv)
00166 paintIntegral(recvPoints, RECV_COLOR, 0.6);
00167 if (_showSend)
00168 paintIntegral(sendPoints, SEND_COLOR, 0.4);
00169 }
00170
00171
00172
00173 if (_showRecv)
00174 paintLine(recvPoints, RECV_COLOR);
00175 if (_showSend)
00176 paintLine(sendPoints, SEND_COLOR);
00177 }
00178
00179
00180
00181 QVector<QPointF>
00182 GraphFrame::pointsFromData(QList<qreal>* list)
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 return points;
00203 }
00204
00205
00206
00207
00208 void
00209 GraphFrame::paintIntegral(QVector<QPointF> points, QColor color, qreal alpha)
00210 {
00211
00212 QBrush oldBrush = _painter->brush();
00213 color.setAlphaF(alpha);
00214 _painter->setBrush(QBrush(color));
00215 _painter->drawPolygon(points.data(), points.size());
00216 _painter->setBrush(oldBrush);
00217 }
00218
00219
00220
00221 void
00222 GraphFrame::paintLine(QVector<QPointF> points, QColor color, Qt::PenStyle lineStyle)
00223 {
00224
00225 QPen oldPen = _painter->pen();
00226 _painter->setPen(QPen(color, lineStyle));
00227 _painter->drawPolyline(points.data(), points.size());
00228 _painter->setPen(oldPen);
00229 }
00230
00231
00232 void
00233 GraphFrame::paintTotals()
00234 {
00235 int x = SCALE_WIDTH + FONT_SIZE, y = 0;
00236 int rowHeight = FONT_SIZE;
00237
00238 #if !defined(Q_WS_MAC)
00239
00240 rowHeight += 5;
00241 #endif
00242
00243
00244 if (_showRecv) {
00245 y = rowHeight;
00246 _painter->setPen(RECV_COLOR);
00247 _painter->drawText(x, y,
00248 tr("Recv: ") + totalToStr(_totalRecv) +
00249 " ("+tr("%1 KB/s").arg(_recvData->first(), 0, 'f', 2)+")");
00250 }
00251
00252
00253 if (_showSend) {
00254 y += rowHeight;
00255 _painter->setPen(SEND_COLOR);
00256 _painter->drawText(x, y,
00257 tr("Sent: ") + totalToStr(_totalSend) +
00258 " ("+tr("%1 KB/s").arg(_sendData->first(), 0, 'f', 2)+")");
00259 }
00260 }
00261
00262
00263 QString
00264 GraphFrame::totalToStr(qreal total)
00265 {
00266
00267 if (total < 1024) {
00268
00269 return tr("%1 KB").arg(total, 0, 'f', 2);
00270 } else if (total < 1048576) {
00271
00272 return tr("%1 MB").arg(total/1024.0, 0, 'f', 2);
00273 } else {
00274
00275 return tr("%1 GB").arg(total/1048576.0, 0, 'f', 2);
00276 }
00277 }
00278
00279
00280 void
00281 GraphFrame::paintScale()
00282 {
00283 qreal markStep = _maxValue * .25;
00284 int top = _rec.y();
00285 int bottom = _rec.height();
00286 qreal paintStep = (bottom - (bottom/10)) / 4;
00287
00288
00289 qreal scale;
00290 qreal pos;
00291 for (int i = 1; i < 5; i++) {
00292 pos = bottom - (i * paintStep);
00293 scale = i * markStep;
00294 _painter->setPen(SCALE_COLOR);
00295 _painter->drawText(QPointF(5, pos+FONT_SIZE),
00296 tr("%1 KB/s").arg(scale, 0, 'f', 2));
00297 _painter->setPen(GRID_COLOR);
00298 _painter->drawLine(QPointF(SCALE_WIDTH, pos),
00299 QPointF(_rec.width(), pos));
00300 }
00301
00302
00303 _painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom);
00304 }
00305