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 1563 2006-12-26 06:06:04Z 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   _graphStyle = SolidLine;
00042   
00043   /* Initialize graph values */
00044   _recvData->prepend(0);
00045   _sendData->prepend(0);
00046   _maxPoints = getNumPoints();  
00047   _showRecv = true;
00048   _showSend = true;
00049   _maxValue = MIN_SCALE;
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   QVector<QPointF> recvPoints, sendPoints;
00158 
00159   /* Convert the bandwidth data points to graph points */
00160   recvPoints = pointsFromData(_recvData);
00161   sendPoints = pointsFromData(_sendData);
00162   
00163   if (_graphStyle == AreaGraph) {
00164     /* Plot the bandwidth data as area graphs */
00165     if (_showRecv)
00166       paintIntegral(recvPoints, RECV_COLOR, 0.6);
00167     if (_showSend)
00168       paintIntegral(sendPoints, SEND_COLOR, 0.4);
00169   }
00170   
00171   /* Plot the bandwidth as solid lines. If the graph style is currently an
00172    * area graph, we end up outlining the integrals. */
00173   if (_showRecv)
00174     paintLine(recvPoints, RECV_COLOR);
00175   if (_showSend)
00176     paintLine(sendPoints, SEND_COLOR);
00177 }
00178 
00179 /** Returns a list of points on the bandwidth graph based on the supplied set
00180  * of send or receive values. */
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   /* 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   return points; 
00203 }
00204 
00205 /** Plots an integral using the data points in <b>points</b>. The area will be
00206  * filled in using <b>color</b> and an alpha-blending level of <b>alpha</b>
00207  * (default is opaque). */
00208 void
00209 GraphFrame::paintIntegral(QVector<QPointF> points, QColor color, qreal alpha)
00210 {
00211   /* Save the current brush, plot the integral, and restore the old brush */
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 /** Iterates the input list and draws a line on the graph in the appropriate
00220  * color. */
00221 void
00222 GraphFrame::paintLine(QVector<QPointF> points, QColor color, Qt::PenStyle lineStyle) 
00223 {
00224   /* Save the current brush, plot the line, and restore the old brush */
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 /** Paints selected total indicators on the graph. */
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   /* On Mac, we don't need vertical spacing between the text rows. */
00240   rowHeight += 5;
00241 #endif
00242 
00243   /* If total received is selected */
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   /* If total sent is selected */
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 /** Returns a formatted string with the correct size suffix. */
00263 QString
00264 GraphFrame::totalToStr(qreal total)
00265 {
00266   /* Determine the correct size suffix */
00267   if (total < 1024) {
00268     /* Use KB suffix */
00269     return tr("%1 KB").arg(total, 0, 'f', 2);
00270   } else if (total < 1048576) {
00271     /* Use MB suffix */
00272     return tr("%1 MB").arg(total/1024.0, 0, 'f', 2);
00273   } else {
00274     /* Use GB suffix */
00275     return tr("%1 GB").arg(total/1048576.0, 0, 'f', 2);
00276   }
00277 }
00278 
00279 /** Paints the scale on the graph. */
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   /* Draw the other marks in their correctly scaled locations */
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   /* Draw vertical separator */
00303   _painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom);
00304 }
00305 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3