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.h 00024 * \version $Id: graphframe.h 1563 2006-12-26 06:06:04Z edmanm $ 00025 * \brief Graphs a series of send and receive data points 00026 */ 00027 00028 #ifndef _GRAPHFRAME_H 00029 #define _GRAPHFRAME_H 00030 00031 #include <QApplication> 00032 #include <QDesktopWidget> 00033 #include <QFrame> 00034 #include <QPainter> 00035 #include <QPen> 00036 #include <QList> 00037 00038 #define HOR_SPC 2 /** Space between data points */ 00039 #define SCALE_WIDTH 75 /** Width of the scale */ 00040 #define MIN_SCALE 10 /** 10 kB/s is the minimum scale */ 00041 #define SCROLL_STEP 4 /** Horizontal change on graph update */ 00042 00043 #define BACK_COLOR Qt::black 00044 #define SCALE_COLOR Qt::green 00045 #define GRID_COLOR Qt::darkGreen 00046 #define RECV_COLOR Qt::cyan 00047 #define SEND_COLOR Qt::yellow 00048 00049 #define FONT_SIZE 11 00050 00051 00052 class GraphFrame : public QFrame 00053 { 00054 Q_OBJECT 00055 00056 public: 00057 /** Bandwidth graph style. */ 00058 enum GraphStyle { 00059 SolidLine = 0, /**< Plot bandwidth as solid lines. */ 00060 AreaGraph /**< Plot bandwidth as alpha blended area graphs. */ 00061 }; 00062 00063 /** Default Constructor */ 00064 GraphFrame(QWidget *parent = 0); 00065 /** Default Destructor */ 00066 ~GraphFrame(); 00067 00068 /** Add data points. */ 00069 void addPoints(qreal recv, qreal send); 00070 /** Clears the graph. */ 00071 void resetGraph(); 00072 /** Toggles display of data counters. */ 00073 void setShowCounters(bool showRecv, bool showSend); 00074 /** Sets the graph style used to display bandwidth data. */ 00075 void setGraphStyle(GraphStyle style) { _graphStyle = style; } 00076 00077 protected: 00078 /** Overloaded QWidget::paintEvent() */ 00079 void paintEvent(QPaintEvent *event); 00080 00081 private: 00082 /** Gets the width of the desktop, the max # of points. */ 00083 int getNumPoints(); 00084 00085 /** Paints an integral and an outline of that integral for each data set 00086 * (send and/or receive) that is to be displayed. */ 00087 void paintData(); 00088 /** Paints the send/receive totals. */ 00089 void paintTotals(); 00090 /** Paints the scale in the graph. */ 00091 void paintScale(); 00092 /** Returns a formatted string representation of total. */ 00093 QString totalToStr(qreal total); 00094 /** Returns a list of points on the bandwidth graph based on the supplied set 00095 * of send or receive values. */ 00096 QVector<QPointF> pointsFromData(QList<qreal>* list); 00097 /** Paints a line with the data in <b>points</b>. */ 00098 void paintLine(QVector<QPointF> points, QColor color, 00099 Qt::PenStyle lineStyle = Qt::SolidLine); 00100 /** Paints an integral using the supplied data. */ 00101 void paintIntegral(QVector<QPointF> points, QColor color, qreal alpha = 1.0); 00102 00103 /** Style with which the bandwidth data will be graphed. */ 00104 GraphStyle _graphStyle; 00105 /** A QPainter object that handles drawing the various graph elements. */ 00106 QPainter* _painter; 00107 /** Holds the received data points. */ 00108 QList<qreal> *_recvData; 00109 /** Holds the sent data points. */ 00110 QList<qreal> *_sendData; 00111 /** The current dimensions of the graph. */ 00112 QRect _rec; 00113 /** The maximum data value plotted. */ 00114 qreal _maxValue; 00115 /** The maximum number of points to store. */ 00116 int _maxPoints; 00117 /** The total data sent/recv. */ 00118 qreal _totalSend; 00119 qreal _totalRecv; 00120 /** Show the respective lines and counters. */ 00121 bool _showRecv; 00122 bool _showSend; 00123 }; 00124 00125 #endif