Vidalia 0.2.15
GraphFrame.h
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file GraphFrame.h
00013 ** \brief Graphs a series of send and receive data points
00014 */
00015 
00016 #ifndef _GRAPHFRAME_H
00017 #define _GRAPHFRAME_H
00018 
00019 #include <QApplication>
00020 #include <QDesktopWidget>
00021 #include <QFrame>
00022 #include <QPainter>
00023 #include <QPen>
00024 #include <QList>
00025 
00026 #define HOR_SPC       2   /** Space between data points */
00027 #define MIN_SCALE     10  /** 10 kB/s is the minimum scale */
00028 #define SCROLL_STEP   4   /** Horizontal change on graph update */
00029 
00030 #define BACK_COLOR    Qt::black
00031 #define SCALE_COLOR   Qt::green
00032 #define GRID_COLOR    Qt::darkGreen
00033 #define RECV_COLOR    Qt::cyan
00034 #define SEND_COLOR    Qt::yellow
00035 
00036 #define FONT_SIZE     11
00037 
00038 
00039 class GraphFrame : public QFrame
00040 {
00041   Q_OBJECT
00042 
00043 public:
00044   /** Bandwidth graph style. */
00045   enum GraphStyle {
00046     SolidLine = 0,  /**< Plot bandwidth as solid lines. */
00047     AreaGraph       /**< Plot bandwidth as alpha blended area graphs. */
00048   };
00049   
00050   /** Default Constructor */
00051   GraphFrame(QWidget *parent = 0);
00052   /** Default Destructor */
00053   ~GraphFrame();
00054 
00055   /** Add data points. */
00056   void addPoints(qreal recv, qreal send);
00057   /** Clears the graph. */
00058   void resetGraph();
00059   /** Toggles display of data counters. */
00060   void setShowCounters(bool showRecv, bool showSend);
00061   /** Sets the graph style used to display bandwidth data. */
00062   void setGraphStyle(GraphStyle style) { _graphStyle = style; }
00063 
00064 protected:
00065   /** Overloaded QWidget::paintEvent() */
00066   void paintEvent(QPaintEvent *event);
00067 
00068 private:
00069   /** Returns the width in pixels of <b>label</b> using the current painter's
00070    * font. */
00071   int labelWidth(const QString &label);
00072   /** Gets the width of the desktop, the max # of points. */
00073   int getNumPoints();
00074   /** Paints an integral and an outline of that integral for each data set
00075    * (send and/or receive) that is to be displayed. */
00076   void paintData();
00077   /** Paints the send/receive totals. */
00078   void paintTotals();
00079   /** Paints the scale in the graph. */
00080   void paintScale();
00081   /** Returns a formatted string representation of total. */
00082   QString totalToStr(qreal total);
00083   /** Returns a list of points on the bandwidth graph based on the supplied set
00084    * of send or receive values. */
00085   QVector<QPointF> pointsFromData(QList<qreal>* list);
00086   /** Paints a line with the data in <b>points</b>. */
00087   void paintLine(QVector<QPointF> points, QColor color, 
00088                  Qt::PenStyle lineStyle = Qt::SolidLine);
00089   /** Paints an integral using the supplied data. */
00090   void paintIntegral(QVector<QPointF> points, QColor color, qreal alpha = 1.0);
00091 
00092   void resizeEvent(QResizeEvent *ev);
00093 
00094   /** Style with which the bandwidth data will be graphed. */
00095   GraphStyle _graphStyle;
00096   /** A QPainter object that handles drawing the various graph elements. */
00097   QPainter* _painter;
00098   /** Holds the received data points. */
00099   QList<qreal> *_recvData;
00100   /** Holds the sent data points. */
00101   QList<qreal> *_sendData;
00102   /** The current dimensions of the graph. */
00103   QRect _rec;
00104   /** The maximum data value plotted. */
00105   qreal _maxValue;
00106   /** The position of the local maximum in the displayed bandwidth */
00107   int _maxPosition;
00108   /** The maximum number of points to store. */
00109   int _maxPoints;
00110   /** The total data sent/recv. */
00111   qreal _totalSend;
00112   qreal _totalRecv;
00113   /** Show the respective lines and counters. */
00114   bool _showRecv;
00115   bool _showSend;
00116   /** Width (in pixels) of the scale marker area on the left side of the
00117    * graph. */
00118   int _scaleWidth;
00119 };
00120 
00121 #endif