graphframe.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file graphframe.cpp
00024  * \version $Id: graphframe.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Graphs a series of send and receive data points
00026  */
00027 
00028 #include <QtGlobal>
00029 
00030 #include "graphframe.h"
00031 
00032 
00033 /** Default contructor */
00034 GraphFrame::GraphFrame(QWidget *parent)
00035 : QFrame(parent)
00036 {
00037   /* Create Graph Frame related objects */
00038   _recvData = new QList<qreal>();
00039   _sendData = new QList<qreal>();
00040   _painter = new QPainter();
00041   
00042   /* Initialize graph values */
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 /** Default destructor */
00053 GraphFrame::~GraphFrame()
00054 {
00055   delete _painter;
00056   delete _recvData;
00057   delete _sendData;
00058 }
00059 
00060 /** Gets the width of the desktop, which is the maximum number of points 
00061  * we can plot in the graph. */
00062 int
00063 GraphFrame::getNumPoints()
00064 {
00065   QDesktopWidget *desktop = QApplication::desktop();
00066   int width = desktop->width();
00067   return width;
00068 }
00069 
00070 /** Adds new data points to the graph. */
00071 void
00072 GraphFrame::addPoints(qreal recv, qreal send)
00073 {
00074   /* If maximum number of points plotted, remove oldest */
00075   if (_sendData->size() == _maxPoints) {
00076     _sendData->removeLast();
00077     _recvData->removeLast();
00078   }
00079 
00080   /* Add the points to their respective lists */
00081   _sendData->prepend(send);
00082   _recvData->prepend(recv);
00083 
00084   /* Add to the total counters */
00085   _totalSend += send;
00086   _totalRecv += recv;
00087   
00088   /* Check for a new maximum value */
00089   if (send > _maxValue) _maxValue = send;
00090   if (recv > _maxValue) _maxValue = recv;
00091 
00092   this->update();
00093 }
00094 
00095 /** Clears the graph. */
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 /** Toggles display of respective graph lines and counters. */
00110 void
00111 GraphFrame::setShowCounters(bool showRecv, bool showSend)
00112 {
00113   _showRecv = showRecv;
00114   _showSend = showSend;
00115   this->update();
00116 }
00117 
00118 /** Overloads default QWidget::paintEvent. Draws the actual 
00119  * bandwidth graph. */
00120 void
00121 GraphFrame::paintEvent(QPaintEvent *event)
00122 {
00123   Q_UNUSED(event);
00124 
00125   /* Set current graph dimensions */
00126   _rec = this->frameRect();
00127   
00128   /* Start the painter */
00129   _painter->begin(this);
00130   
00131   /* We want antialiased lines and text */
00132   _painter->setRenderHint(QPainter::Antialiasing);
00133   _painter->setRenderHint(QPainter::TextAntialiasing);
00134   
00135   /* Fill in the background */
00136   _painter->fillRect(_rec, QBrush(BACK_COLOR));
00137   _painter->drawRect(_rec);
00138 
00139   /* Paint the scale */
00140   paintScale();
00141   /* Plot the send/receive data */
00142   paintData();
00143   /* Paint the send/recv totals */
00144   paintTotals();
00145 
00146   /* Stop the painter */
00147   _painter->end();
00148 }
00149 
00150 /** Paints an integral and an outline of that integral for each data set (send
00151  * and/or receive) that is to be displayed. The integrals will be drawn first,
00152  * followed by the outlines, since we want the area of overlapping integrals
00153  * to blend, but not the outlines of those integrals. */
00154 void
00155 GraphFrame::paintData()
00156 {
00157   /* Draw the integrals using an alpha-blending, giving the background
00158    * integral a little more weight than the foreground. This could probably be
00159    * tweaked more to make overlapping more apparent, including tweaking the 
00160    * colors`. */
00161   if (_showRecv) {
00162     paintIntegral(_recvData, RECV_COLOR, 0.6);
00163   }
00164   if (_showSend) {
00165     paintIntegral(_sendData, SEND_COLOR, 0.4);
00166   }
00167   
00168   /* Outline the integrals in their appropriate colors. */
00169   if (_showRecv) {
00170     paintLine(_recvData, RECV_COLOR);
00171   }
00172   /* If show send rate is selected */
00173   if (_showSend) {
00174     paintLine(_sendData, SEND_COLOR);
00175   }
00176 }
00177 
00178 /** Plots an integral using the data points in <b>list</b>. The area will be
00179  * filled in using <b>color</b> and an alpha-blending level of <b>alpha</b>
00180  * (default is opaque). */
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   /* Translate all data points to points on the graph frame */
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   /* Save the current brush, plot the integral, and restore the old brush */
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 /** Iterates the input list and draws a line on the graph in the appropriate
00212  * color. */
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   /* Save the current pen, set the new pen and plot the data lines */
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     /* Don't draw past the scale */
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     /* Update for next iteration */
00240     prevValue = currValue;
00241     x -= SCROLL_STEP;
00242   }
00243   _painter->setPen(oldPen);
00244 }
00245 
00246 /** Paints selected total indicators on the graph. */
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   /* On Mac, we don't need vertical spacing between the text rows. */
00255   rowHeight += 5;
00256 #endif
00257 
00258   /* If total received is selected */
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   /* If total sent is selected */
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 /** Returns a formatted string with the correct size suffix. */
00278 QString
00279 GraphFrame::totalToStr(qreal total)
00280 {
00281   /* Determine the correct size suffix */
00282   if (total < 1024) {
00283     /* Use KB suffix */
00284     return tr("%1 KB").arg(total, 0, 'f', 2);
00285   } else if (total < 1048576) {
00286     /* Use MB suffix */
00287     return tr("%1 MB").arg(total/1024.0, 0, 'f', 2);
00288   } else {
00289     /* Use GB suffix */
00290     return tr("%1 GB").arg(total/1048576.0, 0, 'f', 2);
00291   }
00292 }
00293 
00294 /** Paints the scale on the graph. */
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   /* Draw the other marks in their correctly scaled locations */
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   /* Draw vertical separator */
00318   _painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom);
00319 }
00320 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0