mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-21 23:40:26 -04:00
improved RSGraph class. Used it for Turtle statistics and bandwidth statistics as well
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7612 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
bd96859704
commit
8c1bf3cf8d
16 changed files with 267 additions and 145 deletions
|
@ -65,6 +65,32 @@ void RSGraphSource::start()
|
|||
|
||||
int RSGraphSource::n_values() const { return _points.size() ; }
|
||||
|
||||
QString RSGraphSource::displayName(int i) const
|
||||
{
|
||||
std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it = _points.begin();
|
||||
|
||||
int n=0;
|
||||
for(it = _points.begin();it!=_points.end() && n<i;++it,++n) ;
|
||||
|
||||
if(n != i)
|
||||
return QString("[error]");
|
||||
|
||||
return QString::fromStdString(it->first) ;
|
||||
}
|
||||
|
||||
QString RSGraphSource::displayValue(float v) const
|
||||
{
|
||||
return QString::number(v,'g',2) + " " + unitName() ;
|
||||
}
|
||||
|
||||
void RSGraphSource::getCurrentValues(std::vector<float>& vals) const
|
||||
{
|
||||
std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it = _points.begin();
|
||||
|
||||
for(it = _points.begin();it!=_points.end();++it)
|
||||
vals.push_back(it->second.back().second) ;
|
||||
}
|
||||
|
||||
void RSGraphSource::getDataPoints(int index,std::vector<QPointF>& pts) const
|
||||
{
|
||||
pts.clear() ;
|
||||
|
@ -222,6 +248,9 @@ void RSGraphWidget::paintEvent(QPaintEvent *)
|
|||
/* Paint the rsDHT/allDHT totals */
|
||||
paintTotals();
|
||||
|
||||
if(_flags & RSGRAPH_FLAGS_SHOW_LEGEND)
|
||||
paintLegend() ;
|
||||
|
||||
/* Stop the painter */
|
||||
_painter->end();
|
||||
}
|
||||
|
@ -229,7 +258,7 @@ void RSGraphWidget::paintEvent(QPaintEvent *)
|
|||
QColor RSGraphWidget::getColor(int i)
|
||||
{
|
||||
// shuffle the colors a little bit
|
||||
int h = (i*44497)%359 ;
|
||||
int h = (i*86243)%359 ;
|
||||
|
||||
return QColor::fromHsv(h,128+127*(i&1),255) ;
|
||||
}
|
||||
|
@ -430,8 +459,10 @@ void RSGraphWidget::paintScale()
|
|||
|
||||
scale = pixelsToValue(i * paintStep);
|
||||
|
||||
QString text = _source->displayValue(scale) ;
|
||||
|
||||
_painter->setPen(SCALE_COLOR);
|
||||
_painter->drawText(QPointF(5, pos+0.5*FONT_SIZE), tr("%1 %2").arg(scale, 0, 'f', _precision_digits).arg(unit_name));
|
||||
_painter->drawText(QPointF(5, pos+0.5*FONT_SIZE), text);
|
||||
_painter->setPen(GRID_COLOR);
|
||||
_painter->drawLine(QPointF(SCALE_WIDTH, pos), QPointF(_rec.width(), pos));
|
||||
}
|
||||
|
@ -440,3 +471,26 @@ void RSGraphWidget::paintScale()
|
|||
_painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom);
|
||||
}
|
||||
|
||||
void RSGraphWidget::paintLegend()
|
||||
{
|
||||
int bottom = _rec.height();
|
||||
|
||||
std::vector<float> vals ;
|
||||
_source->getCurrentValues(vals) ;
|
||||
|
||||
for(uint i=0;i<vals.size();++i)
|
||||
{
|
||||
qreal paintStep = 4+FONT_SIZE;
|
||||
qreal pos = 20+i*paintStep;
|
||||
QString text = _source->displayName(i) + " (" + _source->displayValue(vals[i]) + " )";
|
||||
|
||||
QPen oldPen = _painter->pen();
|
||||
_painter->setPen(QPen(getColor(i), Qt::SolidLine));
|
||||
_painter->drawLine(QPointF(SCALE_WIDTH+10.0, pos), QPointF(SCALE_WIDTH+30.0, pos));
|
||||
_painter->setPen(oldPen);
|
||||
|
||||
_painter->setPen(SCALE_COLOR);
|
||||
_painter->drawText(QPointF(SCALE_WIDTH + 40,pos + 0.5*FONT_SIZE), text) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -59,19 +59,29 @@ public:
|
|||
void stop() ;
|
||||
void clear() ;
|
||||
|
||||
virtual int n_values() const ;
|
||||
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 ;
|
||||
|
||||
// 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 ;
|
||||
|
||||
// returns the name to give to the nth entry in the graph
|
||||
virtual QString displayName(int index) const ;
|
||||
|
||||
// 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) ;
|
||||
|
||||
virtual QString unitName() const =0;// overload to give your own unit name (KB/s, Users, etc)
|
||||
|
||||
protected slots:
|
||||
// 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()
|
||||
|
@ -102,6 +112,7 @@ class RSGraphWidget: public QFrame
|
|||
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
|
||||
static const uint32_t RSGRAPH_FLAGS_SHOW_LEGEND = 0x0010 ;// show legend in the graph
|
||||
|
||||
/** Bandwidth graph style. */
|
||||
enum GraphStyle
|
||||
|
@ -149,6 +160,8 @@ class RSGraphWidget: public QFrame
|
|||
/** Paints the rsdht/alldht totals. */
|
||||
void paintTotals();
|
||||
/** Paints the scale in the graph. */
|
||||
void paintLegend();
|
||||
/** Paints the scale in the graph. */
|
||||
void paintScale();
|
||||
|
||||
QColor getColor(int i) ;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue