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