Vidalia  0.2.21
GraphFrame.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file GraphFrame.h
13 ** \brief Graphs a series of send and receive data points
14 */
15 
16 #ifndef _GRAPHFRAME_H
17 #define _GRAPHFRAME_H
18 
19 #include <QApplication>
20 #include <QDesktopWidget>
21 #include <QFrame>
22 #include <QPainter>
23 #include <QPen>
24 #include <QList>
25 
26 #define HOR_SPC 2 /** Space between data points */
27 #define MIN_SCALE 10 /** 10 kB/s is the minimum scale */
28 #define SCROLL_STEP 4 /** Horizontal change on graph update */
29 
30 #define BACK_COLOR Qt::black
31 #define SCALE_COLOR Qt::green
32 #define GRID_COLOR Qt::darkGreen
33 #define RECV_COLOR Qt::cyan
34 #define SEND_COLOR Qt::yellow
35 
36 #define FONT_SIZE 11
37 
38 
39 class GraphFrame : public QFrame
40 {
41  Q_OBJECT
42 
43 public:
44  /** Bandwidth graph style. */
45  enum GraphStyle {
46  SolidLine = 0, /**< Plot bandwidth as solid lines. */
47  AreaGraph /**< Plot bandwidth as alpha blended area graphs. */
48  };
49 
50  /** Default Constructor */
51  GraphFrame(QWidget *parent = 0);
52  /** Default Destructor */
53  ~GraphFrame();
54 
55  /** Add data points. */
56  void addPoints(qreal recv, qreal send);
57  /** Clears the graph. */
58  void resetGraph();
59  /** Toggles display of data counters. */
60  void setShowCounters(bool showRecv, bool showSend);
61  /** Sets the graph style used to display bandwidth data. */
62  void setGraphStyle(GraphStyle style) { _graphStyle = style; }
63 
64 protected:
65  /** Overloaded QWidget::paintEvent() */
66  void paintEvent(QPaintEvent *event);
67 
68 private:
69  /** Returns the width in pixels of <b>label</b> using the current painter's
70  * font. */
71  int labelWidth(const QString &label);
72  /** Gets the width of the desktop, the max # of points. */
73  int getNumPoints();
74  /** Paints an integral and an outline of that integral for each data set
75  * (send and/or receive) that is to be displayed. */
76  void paintData();
77  /** Paints the send/receive totals. */
78  void paintTotals();
79  /** Paints the scale in the graph. */
80  void paintScale();
81  /** Returns a formatted string representation of total. */
82  QString totalToStr(qreal total);
83  /** Returns a list of points on the bandwidth graph based on the supplied set
84  * of send or receive values. */
85  QVector<QPointF> pointsFromData(QList<qreal>* list);
86  /** Paints a line with the data in <b>points</b>. */
87  void paintLine(QVector<QPointF> points, QColor color,
88  Qt::PenStyle lineStyle = Qt::SolidLine);
89  /** Paints an integral using the supplied data. */
90  void paintIntegral(QVector<QPointF> points, QColor color, qreal alpha = 1.0);
91 
92  void resizeEvent(QResizeEvent *ev);
93 
94  /** Style with which the bandwidth data will be graphed. */
96  /** A QPainter object that handles drawing the various graph elements. */
97  QPainter* _painter;
98  /** Holds the received data points. */
99  QList<qreal> *_recvData;
100  /** Holds the sent data points. */
101  QList<qreal> *_sendData;
102  /** The current dimensions of the graph. */
103  QRect _rec;
104  /** The maximum data value plotted. */
105  qreal _maxValue;
106  /** The position of the local maximum in the displayed bandwidth */
108  /** The maximum number of points to store. */
110  /** The total data sent/recv. */
111  qreal _totalSend;
112  qreal _totalRecv;
113  /** Show the respective lines and counters. */
114  bool _showRecv;
115  bool _showSend;
116  /** Width (in pixels) of the scale marker area on the left side of the
117  * graph. */
119 };
120 
121 #endif