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