2014-10-12 17:12:58 -04:00
|
|
|
/****************************************************************
|
|
|
|
* This file is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 RetroShare Team
|
|
|
|
* Copyright (c) 2006-2007, crypton
|
|
|
|
* Copyright (c) 2006, Matt Edman, Justin Hipple
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
#include <map>
|
2014-10-17 15:47:05 -04:00
|
|
|
#include <set>
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QFrame>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#define HOR_SPC 2 /** Space between data points */
|
|
|
|
#define SCALE_WIDTH 75 /** Width of the scale */
|
|
|
|
#define MINUSER_SCALE 2000 /** 2000 users is the minimum scale */
|
|
|
|
#define SCROLL_STEP 4 /** Horizontal change on graph update */
|
|
|
|
|
|
|
|
#define BACK_COLOR Qt::white
|
|
|
|
#define SCALE_COLOR Qt::black
|
2014-10-14 16:25:23 -04:00
|
|
|
#define GRID_COLOR Qt::lightGray
|
2014-10-12 17:12:58 -04:00
|
|
|
#define RSDHT_COLOR Qt::magenta
|
|
|
|
#define ALLDHT_COLOR Qt::yellow
|
|
|
|
|
|
|
|
#define FONT_SIZE 11
|
|
|
|
|
|
|
|
// This class provides a source value that the graph can retrieve on demand.
|
|
|
|
// In order to use your own source, derive from RSGraphSource and overload the value() method.
|
|
|
|
//
|
|
|
|
class RSGraphSource: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
RSGraphSource();
|
|
|
|
virtual ~RSGraphSource() ;
|
|
|
|
|
|
|
|
void start() ;
|
|
|
|
void stop() ;
|
|
|
|
void clear() ;
|
2014-10-17 15:47:05 -04:00
|
|
|
void reset() ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-15 18:00:49 -04:00
|
|
|
virtual QString unitName() const { return "" ; }// overload to give your own unit name (KB/s, Users, etc)
|
|
|
|
|
|
|
|
int n_values() const ;
|
|
|
|
|
|
|
|
// Might be overloaded in order to show a fancy digit number with adaptive units.
|
|
|
|
// The default is to return v + unitName()
|
|
|
|
virtual QString displayValue(float v) const ;
|
|
|
|
|
|
|
|
// return the vector of last values up to date
|
|
|
|
virtual void getCurrentValues(std::vector<float>& vals) const ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-16 15:17:17 -04:00
|
|
|
// returns what to display in the legend. Derive this to show additional info.
|
|
|
|
virtual QString legend(int i,float v) const ;
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
// Returns the n^th interpolated value at the given time in floating point seconds backward.
|
|
|
|
virtual void getDataPoints(int index, std::vector<QPointF>& pts) const ;
|
|
|
|
|
2014-10-15 18:00:49 -04:00
|
|
|
// returns the name to give to the nth entry in the graph
|
|
|
|
virtual QString displayName(int index) const ;
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
// Sets the maximum time for keeping values. Units=seconds.
|
|
|
|
void setCollectionTimeLimit(qint64 msecs) ;
|
|
|
|
|
|
|
|
// Sets the time period for collecting new values. Units=milliseconds.
|
|
|
|
void setCollectionTimePeriod(qint64 msecs) ;
|
|
|
|
|
2014-10-16 15:17:17 -04:00
|
|
|
void setDigits(int d) { _digits = d ;}
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
protected slots:
|
2014-10-13 18:05:50 -04:00
|
|
|
// Calls the internal source for a new data points; called by the timer. You might want to overload this
|
|
|
|
// if the collection system needs it. Otherwise, the default method will call getValues()
|
2014-10-12 17:12:58 -04:00
|
|
|
void update() ;
|
2015-02-04 05:56:19 -05:00
|
|
|
void updateIfPossible() ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
|
|
|
protected:
|
2014-10-13 18:05:50 -04:00
|
|
|
virtual void getValues(std::map<std::string,float>& values) const = 0 ;// overload this in your own class to fill in the values you want to display.
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
qint64 getTime() const ; // returns time in ms since RS has started
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
// Storage of collected events. The string is any string used to represent the collected data.
|
|
|
|
|
|
|
|
std::map<std::string, std::list<std::pair<qint64,float> > > _points ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
|
|
|
QTimer *_timer ;
|
|
|
|
|
|
|
|
qint64 _time_limit_msecs ;
|
|
|
|
qint64 _update_period_msecs ;
|
|
|
|
qint64 _time_orig_msecs ;
|
2014-10-16 15:17:17 -04:00
|
|
|
int _digits ;
|
2014-10-12 17:12:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class RSGraphWidget: public QFrame
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2014-10-14 16:25:23 -04:00
|
|
|
static const uint32_t RSGRAPH_FLAGS_AUTO_SCALE_Y = 0x0001 ;// automatically adjust Y scale
|
|
|
|
static const uint32_t RSGRAPH_FLAGS_LOG_SCALE_Y = 0x0002 ;// log scale in Y
|
|
|
|
static const uint32_t RSGRAPH_FLAGS_ALWAYS_COLLECT = 0x0004 ;// keep collecting while not displayed
|
|
|
|
static const uint32_t RSGRAPH_FLAGS_PAINT_STYLE_PLAIN = 0x0008 ;// use plain / line drawing style
|
2014-10-15 18:00:49 -04:00
|
|
|
static const uint32_t RSGRAPH_FLAGS_SHOW_LEGEND = 0x0010 ;// show legend in the graph
|
2014-10-12 17:12:58 -04:00
|
|
|
/** Bandwidth graph style. */
|
|
|
|
enum GraphStyle
|
|
|
|
{
|
|
|
|
SolidLine = 0, /**< Plot bandwidth as solid lines. */
|
|
|
|
AreaGraph = 1 /**< Plot bandwidth as alpha blended area graphs. */
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Default Constructor */
|
|
|
|
RSGraphWidget(QWidget *parent = 0);
|
|
|
|
/** Default Destructor */
|
|
|
|
~RSGraphWidget();
|
|
|
|
|
|
|
|
// sets the update interval period.
|
|
|
|
//
|
|
|
|
void setTimerPeriod(int miliseconds) ;
|
|
|
|
void addSource(RSGraphSource *gs) ;
|
|
|
|
void setTimeScale(float pixels_per_second) ;
|
|
|
|
|
|
|
|
/** Add data points. */
|
|
|
|
//void addPoints(qreal rsDHT, qreal allDHT);
|
|
|
|
/** Clears the graph. */
|
|
|
|
void resetGraph();
|
|
|
|
/** Toggles display of data counters. */
|
2014-10-17 15:47:05 -04:00
|
|
|
//void setShowCounters(bool showRSDHT, bool showALLDHT);
|
|
|
|
|
|
|
|
void setShowEntry(uint32_t entry, bool show) ;
|
|
|
|
void setCurvesOpacity(float f) ;
|
2014-10-14 16:25:23 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
void setFlags(uint32_t flag) { _flags |= flag ; }
|
|
|
|
void resetFlags(uint32_t flag) { _flags &= ~flag ; }
|
2014-10-12 17:12:58 -04:00
|
|
|
protected:
|
|
|
|
/** Overloaded QWidget::paintEvent() */
|
|
|
|
void paintEvent(QPaintEvent *event);
|
|
|
|
|
|
|
|
protected slots:
|
2015-02-04 05:56:19 -05:00
|
|
|
void updateIfPossible() ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
virtual void wheelEvent(QWheelEvent *e);
|
2014-10-12 17:12:58 -04:00
|
|
|
private:
|
|
|
|
/** Gets the width of the desktop, the max # of points. */
|
|
|
|
int getNumPoints();
|
|
|
|
|
|
|
|
/** Paints an integral and an outline of that integral for each data set
|
|
|
|
* (rsdht and/or alldht) that is to be displayed. */
|
|
|
|
void paintData();
|
|
|
|
/** Paints the rsdht/alldht totals. */
|
|
|
|
void paintTotals();
|
|
|
|
/** Paints the scale in the graph. */
|
2014-10-15 18:00:49 -04:00
|
|
|
void paintLegend();
|
|
|
|
/** Paints the scale in the graph. */
|
2014-10-14 16:25:23 -04:00
|
|
|
void paintScale();
|
|
|
|
|
|
|
|
QColor getColor(int i) ;
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
/** Returns a formatted string representation of total. */
|
|
|
|
QString totalToStr(qreal total);
|
|
|
|
/** Returns a list of points on the bandwidth graph based on the supplied set
|
|
|
|
* of rsdht or alldht values. */
|
2014-10-13 18:05:50 -04:00
|
|
|
void pointsFromData(const std::vector<QPointF>& values, QVector<QPointF> &points ) ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
|
|
|
/** Paints a line with the data in <b>points</b>. */
|
|
|
|
void paintLine(const QVector<QPointF>& points, QColor color,
|
|
|
|
Qt::PenStyle lineStyle = Qt::SolidLine);
|
|
|
|
/** Paints an integral using the supplied data. */
|
|
|
|
void paintIntegral(const QVector<QPointF>& points, QColor color, qreal alpha = 1.0);
|
|
|
|
|
|
|
|
/** A QPainter object that handles drawing the various graph elements. */
|
|
|
|
QPainter* _painter;
|
|
|
|
/** The current dimensions of the graph. */
|
|
|
|
QRect _rec;
|
|
|
|
/** The maximum data value plotted. */
|
|
|
|
qreal _maxValue;
|
2014-10-13 18:05:50 -04:00
|
|
|
/** The maximum number of points to store. */
|
|
|
|
qreal _y_scale ;
|
2014-10-17 15:47:05 -04:00
|
|
|
qreal _opacity ;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
|
|
|
qreal pixelsToValue(qreal) ;
|
|
|
|
qreal valueToPixels(qreal) ;
|
|
|
|
int _maxPoints;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
std::set<std::string> _masked_entries ;
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
qreal _time_scale ; // horizontal scale in pixels per sec.
|
|
|
|
|
|
|
|
/** Show the respective lines and counters. */
|
|
|
|
//bool _showRSDHT;
|
|
|
|
//bool _showALLDHT;
|
|
|
|
|
|
|
|
uint32_t _flags ;
|
|
|
|
QTimer *_timer ;
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
RSGraphSource *_source ;
|
2014-10-12 17:12:58 -04:00
|
|
|
};
|
|
|
|
|