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.
|
|
|
|
****************************************************************/
|
|
|
|
|
2014-10-12 17:38:27 -04:00
|
|
|
#ifndef WINDOWS_SYS
|
2014-10-12 17:12:58 -04:00
|
|
|
#include <sys/times.h>
|
2014-10-12 17:38:27 -04:00
|
|
|
#endif
|
2014-10-12 17:12:58 -04:00
|
|
|
|
|
|
|
#include <iostream>
|
2014-10-13 18:05:50 -04:00
|
|
|
#include <math.h>
|
2014-10-12 17:12:58 -04:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDateTime>
|
2015-03-21 14:40:21 -04:00
|
|
|
#include <QWheelEvent>
|
2014-10-12 17:12:58 -04:00
|
|
|
#include <QTimer>
|
|
|
|
|
2015-02-04 05:56:19 -05:00
|
|
|
#include <retroshare-gui/RsAutoUpdatePage.h>
|
2014-10-12 17:12:58 -04:00
|
|
|
#include "RSGraphWidget.h"
|
|
|
|
|
2015-02-13 14:34:38 -05:00
|
|
|
#if QT_VERSION < 0x040700
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
static qint64 getCurrentMSecsSinceEpoch()
|
|
|
|
{
|
|
|
|
timeval tv ;
|
|
|
|
gettimeofday(&tv,NULL) ;
|
|
|
|
|
|
|
|
return (qint64)tv.tv_sec + (qint64)tv.tv_usec/1000 ;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
RSGraphSource::RSGraphSource()
|
|
|
|
{
|
|
|
|
_time_limit_msecs = 10*1000 ;
|
|
|
|
_update_period_msecs = 1*1000 ;
|
2015-02-13 14:34:38 -05:00
|
|
|
#if QT_VERSION < 0x040700
|
|
|
|
_time_orig_msecs = getCurrentMSecsSinceEpoch() ;
|
|
|
|
#else
|
2014-10-12 17:12:58 -04:00
|
|
|
_time_orig_msecs = QDateTime::currentMSecsSinceEpoch() ;
|
2015-02-13 14:34:38 -05:00
|
|
|
#endif
|
2014-10-12 17:12:58 -04:00
|
|
|
_timer = new QTimer ;
|
2014-10-16 15:17:17 -04:00
|
|
|
_digits = 2 ;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2015-02-04 05:56:19 -05:00
|
|
|
QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(updateIfPossible())) ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RSGraphSource::~RSGraphSource()
|
|
|
|
{
|
|
|
|
stop() ;
|
|
|
|
delete _timer ;
|
|
|
|
}
|
|
|
|
void RSGraphSource::clear()
|
|
|
|
{
|
|
|
|
_points.clear() ;
|
|
|
|
}
|
|
|
|
void RSGraphSource::stop()
|
|
|
|
{
|
|
|
|
_timer->stop() ;
|
|
|
|
}
|
|
|
|
void RSGraphSource::start()
|
|
|
|
{
|
|
|
|
_timer->stop();
|
|
|
|
_timer->start((int)(_update_period_msecs)) ;
|
|
|
|
}
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
int RSGraphSource::n_values() const { return _points.size() ; }
|
|
|
|
|
2014-10-15 18:00:49 -04:00
|
|
|
QString RSGraphSource::displayName(int i) const
|
|
|
|
{
|
|
|
|
int n=0;
|
2015-06-14 08:48:12 -04:00
|
|
|
for(std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it = _points.begin();it!=_points.end() ;++it)
|
|
|
|
if(n++ == i)
|
|
|
|
return QString::fromStdString(it->first) ;
|
2014-10-15 18:00:49 -04:00
|
|
|
|
2015-06-14 08:48:12 -04:00
|
|
|
return QString("[error]");
|
2014-10-15 18:00:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString RSGraphSource::displayValue(float v) const
|
|
|
|
{
|
2014-10-16 15:17:17 -04:00
|
|
|
return QString::number(v,'f',_digits) + " " + unitName() ;
|
2014-10-15 18:00:49 -04:00
|
|
|
}
|
|
|
|
|
2015-05-15 09:12:46 -04:00
|
|
|
void RSGraphSource::getCurrentValues(std::vector<QPointF>& vals) const
|
2014-10-15 18:00:49 -04:00
|
|
|
{
|
|
|
|
std::map<std::string,std::list<std::pair<qint64,float> > >::const_iterator it = _points.begin();
|
2015-05-15 09:12:46 -04:00
|
|
|
qint64 now = getTime() ;
|
2014-10-15 18:00:49 -04:00
|
|
|
|
|
|
|
for(it = _points.begin();it!=_points.end();++it)
|
2015-05-15 09:12:46 -04:00
|
|
|
vals.push_back(QPointF( (now - it->second.back().first)/1000.0f,it->second.back().second)) ;
|
2014-10-15 18:00:49 -04:00
|
|
|
}
|
|
|
|
|
2014-10-16 15:17:17 -04:00
|
|
|
QString RSGraphSource::legend(int i,float v) const
|
|
|
|
{
|
|
|
|
return displayName(i) + " (" + displayValue(v) + " )";
|
|
|
|
}
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
void RSGraphSource::getDataPoints(int index,std::vector<QPointF>& pts) const
|
|
|
|
{
|
|
|
|
pts.clear() ;
|
|
|
|
qint64 now = getTime() ;
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
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<index;++it,++n) ;
|
|
|
|
|
|
|
|
if(n != index)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
for(std::list<std::pair<qint64,float> >::const_iterator it2=it->second.begin();it2!=it->second.end();++it2)
|
|
|
|
pts.push_back(QPointF( (now - (*it2).first)/1000.0f,(*it2).second)) ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
void RSGraphWidget::setShowEntry(uint32_t entry,bool b)
|
|
|
|
{
|
|
|
|
if(b)
|
|
|
|
{
|
|
|
|
std::set<std::string>::iterator it = _masked_entries.find(_source->displayName(entry).toStdString()) ;
|
|
|
|
if(it != _masked_entries.end())
|
|
|
|
_masked_entries.erase(it) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_masked_entries.insert(_source->displayName(entry).toStdString()) ;
|
|
|
|
}
|
|
|
|
|
2015-07-09 23:24:39 -04:00
|
|
|
void RSGraphWidget::setSource(RSGraphSource *gs)
|
2014-10-12 17:12:58 -04:00
|
|
|
{
|
2014-10-13 18:05:50 -04:00
|
|
|
if(_source != NULL)
|
|
|
|
delete _source ;
|
|
|
|
|
|
|
|
_source = gs ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 RSGraphSource::getTime() const
|
|
|
|
{
|
2015-02-13 14:34:38 -05:00
|
|
|
#if QT_VERSION < 0x040700
|
|
|
|
return getCurrentMSecsSinceEpoch() - _time_orig_msecs ;
|
|
|
|
#else
|
2014-10-12 17:12:58 -04:00
|
|
|
return QDateTime::currentMSecsSinceEpoch() - _time_orig_msecs ;
|
2015-02-13 14:34:38 -05:00
|
|
|
#endif
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
2015-02-04 05:56:19 -05:00
|
|
|
void RSGraphSource::updateIfPossible()
|
|
|
|
{
|
|
|
|
if(RsAutoUpdatePage::eventsLocked())
|
|
|
|
return ;
|
|
|
|
|
|
|
|
update() ;
|
|
|
|
}
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
void RSGraphSource::update()
|
|
|
|
{
|
2014-10-13 18:05:50 -04:00
|
|
|
std::map<std::string,float> vals ;
|
2014-10-12 17:12:58 -04:00
|
|
|
getValues(vals) ;
|
|
|
|
|
|
|
|
qint64 ms = getTime() ;
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
for(std::map<std::string,float>::iterator it=vals.begin();it!=vals.end();++it)
|
|
|
|
{
|
|
|
|
std::list<std::pair<qint64,float> >& lst(_points[it->first]) ;
|
|
|
|
|
|
|
|
lst.push_back(std::make_pair(ms,it->second)) ;
|
|
|
|
|
|
|
|
for(std::list<std::pair<qint64,float> >::iterator it2=lst.begin();it2!=lst.end();)
|
|
|
|
if( ms - (*it2).first > _time_limit_msecs)
|
|
|
|
{
|
|
|
|
//std::cerr << " removing old value with time " << (*it).first/1000.0f << std::endl;
|
|
|
|
it2 = lst.erase(it2) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break ;
|
|
|
|
}
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
// remove empty lists
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
for(std::map<std::string,std::list<std::pair<qint64,float> > >::iterator it=_points.begin();it!=_points.end();)
|
|
|
|
if(it->second.empty())
|
|
|
|
{
|
|
|
|
std::map<std::string,std::list<std::pair<qint64,float> > >::iterator tmp(it) ;
|
|
|
|
++tmp;
|
|
|
|
_points.erase(it) ;
|
|
|
|
it=tmp ;
|
|
|
|
}
|
2014-10-12 17:12:58 -04:00
|
|
|
else
|
2014-10-13 18:05:50 -04:00
|
|
|
++it ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
2015-07-12 00:04:18 -04:00
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
void RSGraphSource::reset()
|
|
|
|
{
|
|
|
|
_points.clear() ;
|
|
|
|
}
|
2014-10-12 17:12:58 -04:00
|
|
|
|
|
|
|
void RSGraphSource::setCollectionTimeLimit(qint64 s) { _time_limit_msecs = s ; }
|
|
|
|
void RSGraphSource::setCollectionTimePeriod(qint64 s) { _update_period_msecs = s ; }
|
|
|
|
|
|
|
|
void RSGraphWidget::setTimeScale(float pixels_per_second)
|
|
|
|
{
|
|
|
|
_time_scale =pixels_per_second ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Default contructor */
|
|
|
|
RSGraphWidget::RSGraphWidget(QWidget *parent)
|
|
|
|
: QFrame(parent)
|
|
|
|
{
|
2014-10-13 18:05:50 -04:00
|
|
|
_source =NULL;
|
2014-10-12 17:12:58 -04:00
|
|
|
_painter = new QPainter();
|
|
|
|
|
|
|
|
/* Initialize graph values */
|
|
|
|
_maxPoints = getNumPoints();
|
|
|
|
_maxValue = MINUSER_SCALE;
|
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
_opacity = 0.6 ;
|
2014-10-13 18:05:50 -04:00
|
|
|
_flags = 0;
|
2014-10-12 17:12:58 -04:00
|
|
|
_time_scale = 5.0f ; // in pixels per second.
|
|
|
|
_timer = new QTimer ;
|
2015-02-04 05:56:19 -05:00
|
|
|
QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(updateIfPossible())) ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
_y_scale = 1.0f ;
|
2014-10-12 17:12:58 -04:00
|
|
|
_timer->start(1000);
|
2015-06-25 16:11:39 -04:00
|
|
|
|
|
|
|
float FS = QFontMetricsF(font()).height();
|
|
|
|
setMinimumHeight(12*FS);
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
2015-02-04 05:56:19 -05:00
|
|
|
void RSGraphWidget::updateIfPossible()
|
|
|
|
{
|
|
|
|
if(RsAutoUpdatePage::eventsLocked())
|
|
|
|
return ;
|
|
|
|
|
|
|
|
if(!isVisible())
|
|
|
|
return ;
|
|
|
|
|
|
|
|
update() ;
|
|
|
|
}
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
/** Default destructor */
|
|
|
|
RSGraphWidget::~RSGraphWidget()
|
|
|
|
{
|
2014-10-13 18:05:50 -04:00
|
|
|
delete _painter;
|
|
|
|
delete _source ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the width of the desktop, which is the maximum number of points
|
|
|
|
* we can plot in the graph. */
|
|
|
|
int
|
|
|
|
RSGraphWidget::getNumPoints()
|
|
|
|
{
|
|
|
|
QDesktopWidget *desktop = QApplication::desktop();
|
|
|
|
int width = desktop->width();
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clears the graph. */
|
|
|
|
void
|
|
|
|
RSGraphWidget::resetGraph()
|
|
|
|
{
|
2014-10-17 15:47:05 -04:00
|
|
|
_source->reset() ;
|
2014-10-12 17:12:58 -04:00
|
|
|
_maxValue = MINUSER_SCALE;
|
2015-02-04 05:56:19 -05:00
|
|
|
updateIfPossible();
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Toggles display of respective graph lines and counters. */
|
|
|
|
//void
|
|
|
|
//DhtGraph::setShowCounters(bool showRSDHT, bool showALLDHT)
|
|
|
|
//{
|
|
|
|
// _showRSDHT = showRSDHT;
|
|
|
|
// _showALLDHT = showALLDHT;
|
|
|
|
// this->update();
|
|
|
|
//}
|
|
|
|
|
|
|
|
/** Overloads default QWidget::paintEvent. Draws the actual
|
|
|
|
* bandwidth graph. */
|
|
|
|
void RSGraphWidget::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
//std::cerr << "In paint event!" << std::endl;
|
|
|
|
|
|
|
|
/* Set current graph dimensions */
|
|
|
|
_rec = this->frameRect();
|
|
|
|
|
|
|
|
/* Start the painter */
|
|
|
|
_painter->begin(this);
|
|
|
|
|
|
|
|
/* We want antialiased lines and text */
|
|
|
|
_painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
_painter->setRenderHint(QPainter::TextAntialiasing);
|
|
|
|
|
|
|
|
/* Fill in the background */
|
|
|
|
_painter->fillRect(_rec, QBrush(BACK_COLOR));
|
|
|
|
_painter->drawRect(_rec);
|
|
|
|
|
|
|
|
/* Paint the scale */
|
2015-05-15 09:35:51 -04:00
|
|
|
paintScale1();
|
2014-10-12 17:12:58 -04:00
|
|
|
/* Plot the rsDHT/allDHT data */
|
|
|
|
paintData();
|
|
|
|
/* Paint the rsDHT/allDHT totals */
|
|
|
|
paintTotals();
|
|
|
|
|
2015-05-15 09:35:51 -04:00
|
|
|
// part of the scale that needs to write over the data curves.
|
|
|
|
paintScale2();
|
|
|
|
|
2014-10-15 18:00:49 -04:00
|
|
|
if(_flags & RSGRAPH_FLAGS_SHOW_LEGEND)
|
|
|
|
paintLegend() ;
|
|
|
|
|
2014-10-12 17:12:58 -04:00
|
|
|
/* Stop the painter */
|
|
|
|
_painter->end();
|
|
|
|
}
|
|
|
|
|
2015-06-25 16:11:39 -04:00
|
|
|
QSizeF RSGraphWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
|
|
|
|
{
|
|
|
|
float FS = QFontMetricsF(font()).height();
|
|
|
|
float fact = FS/14.0 ;
|
|
|
|
|
|
|
|
switch(which)
|
|
|
|
{
|
|
|
|
defaut:
|
|
|
|
case Qt::MinimumSize:
|
|
|
|
case Qt::PreferredSize:
|
|
|
|
return QSizeF(70*FS,12*FS);
|
|
|
|
case Qt::MaximumSize:
|
|
|
|
return QSizeF(700*FS,120*FS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 16:25:23 -04:00
|
|
|
QColor RSGraphWidget::getColor(int i)
|
|
|
|
{
|
|
|
|
// shuffle the colors a little bit
|
2014-10-15 18:00:49 -04:00
|
|
|
int h = (i*86243)%359 ;
|
2014-10-14 16:25:23 -04:00
|
|
|
|
|
|
|
return QColor::fromHsv(h,128+127*(i&1),255) ;
|
|
|
|
}
|
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
void RSGraphWidget::setCurvesOpacity(float f)
|
|
|
|
{
|
|
|
|
_opacity = f ;
|
|
|
|
}
|
2014-10-12 17:12:58 -04:00
|
|
|
/** Paints an integral and an outline of that integral for each data set (rsdht
|
|
|
|
* and/or alldht) that is to be displayed. The integrals will be drawn first,
|
|
|
|
* followed by the outlines, since we want the area of overlapping integrals
|
|
|
|
* to blend, but not the outlines of those integrals. */
|
|
|
|
void RSGraphWidget::paintData()
|
|
|
|
{
|
|
|
|
/* Convert the bandwidth data points to graph points */
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
if(_source == NULL)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
const RSGraphSource& source(*_source) ;
|
|
|
|
_maxValue = 0.0f ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
for(int i=0;i<source.n_values();++i)
|
2014-10-17 15:47:05 -04:00
|
|
|
if( _masked_entries.find(source.displayName(i).toStdString()) == _masked_entries.end() )
|
|
|
|
{
|
|
|
|
std::vector<QPointF> values ;
|
|
|
|
source.getDataPoints(i,values) ;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
QVector<QPointF> points ;
|
|
|
|
pointsFromData(values,points) ;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
/* Plot the bandwidth data as area graphs */
|
|
|
|
if (_flags & RSGRAPH_FLAGS_PAINT_STYLE_PLAIN)
|
|
|
|
paintIntegral(points, getColor(i), _opacity);
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2014-10-17 15:47:05 -04:00
|
|
|
/* Plot the bandwidth as solid lines. If the graph style is currently an
|
2014-10-12 17:12:58 -04:00
|
|
|
* area graph, we end up outlining the integrals. */
|
2014-10-17 15:47:05 -04:00
|
|
|
paintLine(points, getColor(i));
|
|
|
|
}
|
2014-10-13 18:05:50 -04:00
|
|
|
if(_maxValue > 0.0f)
|
|
|
|
if(_flags & RSGRAPH_FLAGS_LOG_SCALE_Y)
|
|
|
|
_y_scale = _rec.height()*0.8 / log(_maxValue) ;
|
|
|
|
else
|
|
|
|
_y_scale = _rec.height()*0.8/_maxValue ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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 RSGraphWidget::pointsFromData(const std::vector<QPointF>& values,QVector<QPointF>& points)
|
2014-10-12 17:12:58 -04:00
|
|
|
{
|
|
|
|
points.clear();
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
int x = _rec.width();
|
|
|
|
int y = _rec.height();
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
float time_step = 1.0f ; // number of seconds per pixel
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
/* Translate all data points to points on the graph frame */
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-05-15 08:40:47 -04:00
|
|
|
// take 0 as the origin, otherwise the different curves are not aligned properly
|
|
|
|
float last = 0;//values.back().x();
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
//std::cerr << "Got " << values.size() << " values for index 0" << std::endl;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-06-24 16:55:09 -04:00
|
|
|
float FS = QFontMetricsF(font()).height();
|
|
|
|
float fact = FS/14.0 ;
|
|
|
|
|
|
|
|
float last_px = SCALE_WIDTH*fact ;
|
2014-10-13 18:05:50 -04:00
|
|
|
float last_py = 0.0f ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-07-17 16:13:31 -04:00
|
|
|
// float min_x_no_data_threshold = 1.5 ; // 1.5 sec.
|
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
for (uint i = 0; i < values.size(); ++i)
|
2015-03-21 14:40:21 -04:00
|
|
|
{
|
|
|
|
//std::cerr << "Value: (" << values[i].x() << " , " << values[i].y() << ")" << std::endl;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
// compute point in pixels
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
qreal px = x - (values[i].x()-last)*_time_scale ;
|
|
|
|
qreal py = y - valueToPixels(values[i].y()) ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-07-17 16:13:31 -04:00
|
|
|
if(px >= SCALE_WIDTH*fact && last_px < SCALE_WIDTH*fact)
|
2015-03-21 14:40:21 -04:00
|
|
|
{
|
2015-06-24 16:55:09 -04:00
|
|
|
float alpha = (SCALE_WIDTH*fact - last_px)/(px - last_px) ;
|
|
|
|
float ipx = SCALE_WIDTH*fact ;
|
2015-03-21 14:40:21 -04:00
|
|
|
float ipy = (1-alpha)*last_py + alpha*py ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
points << QPointF(ipx,y) ;
|
|
|
|
points << QPointF(ipx,ipy) ;
|
|
|
|
}
|
|
|
|
else if(i==0)
|
2015-07-17 16:13:31 -04:00
|
|
|
{
|
|
|
|
if(px < SCALE_WIDTH*fact)
|
|
|
|
points << QPointF(SCALE_WIDTH*fact,py) ;
|
|
|
|
else
|
2015-03-21 14:40:21 -04:00
|
|
|
points << QPointF(px,y) ;
|
2015-07-17 16:13:31 -04:00
|
|
|
}
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-06-24 16:55:09 -04:00
|
|
|
if(px < SCALE_WIDTH*fact)
|
2015-03-21 14:40:21 -04:00
|
|
|
continue ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
_maxValue = std::max(_maxValue,values[i].y()) ;
|
2014-10-17 15:47:05 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
// remove midle point when 3 consecutive points have the same value.
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
if(points.size() > 1 && points[points.size()-2].y() == points.back().y() && points.back().y() == py)
|
|
|
|
points.pop_back() ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-07-17 16:13:31 -04:00
|
|
|
// if(fabs(px - last_px)/_time_scale > min_x_no_data_threshold)
|
|
|
|
// {
|
|
|
|
// points << QPointF(last_px,y) ;
|
|
|
|
// points << QPointF(px,y) ;
|
|
|
|
// }
|
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
points << QPointF(px,py) ;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
if(i==values.size()-1)
|
|
|
|
points << QPointF(px,y) ;
|
2015-07-17 16:13:31 -04:00
|
|
|
|
|
|
|
last_px = px ;
|
|
|
|
last_py = py ;
|
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
}
|
2014-10-13 18:05:50 -04:00
|
|
|
}
|
2014-10-12 17:12:58 -04:00
|
|
|
|
2014-10-13 18:05:50 -04:00
|
|
|
qreal RSGraphWidget::valueToPixels(qreal val)
|
|
|
|
{
|
|
|
|
if(_flags & RSGRAPH_FLAGS_LOG_SCALE_Y)
|
|
|
|
return log(std::max((qreal)1.0,val))*_y_scale ;
|
|
|
|
else
|
|
|
|
return val*_y_scale ;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal RSGraphWidget::pixelsToValue(qreal val)
|
|
|
|
{
|
|
|
|
if(_flags & RSGRAPH_FLAGS_LOG_SCALE_Y)
|
|
|
|
return exp(val / _y_scale) ;
|
|
|
|
else
|
|
|
|
return val/_y_scale ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Plots an integral using the data points in <b>points</b>. The area will be
|
|
|
|
* filled in using <b>color</b> and an alpha-blending level of <b>alpha</b>
|
|
|
|
* (default is opaque). */
|
|
|
|
void RSGraphWidget::paintIntegral(const QVector<QPointF>& points, QColor color, qreal alpha)
|
|
|
|
{
|
|
|
|
/* Save the current brush, plot the integral, and restore the old brush */
|
|
|
|
QBrush oldBrush = _painter->brush();
|
|
|
|
color.setAlphaF(alpha);
|
|
|
|
_painter->setBrush(QBrush(color));
|
|
|
|
_painter->drawPolygon(points.data(), points.size());
|
|
|
|
_painter->setBrush(oldBrush);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Iterates the input list and draws a line on the graph in the appropriate
|
|
|
|
* color. */
|
|
|
|
void RSGraphWidget::paintLine(const QVector<QPointF>& points, QColor color, Qt::PenStyle lineStyle)
|
|
|
|
{
|
|
|
|
/* Save the current brush, plot the line, and restore the old brush */
|
|
|
|
QPen oldPen = _painter->pen();
|
|
|
|
_painter->setPen(QPen(color, lineStyle));
|
|
|
|
_painter->drawPolyline(points.data(), points.size());
|
|
|
|
_painter->setPen(oldPen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Paints selected total indicators on the graph. */
|
|
|
|
void RSGraphWidget::paintTotals()
|
|
|
|
{
|
2015-06-24 16:55:09 -04:00
|
|
|
float FS = QFontMetricsF(font()).height();
|
|
|
|
float fact = FS/14.0 ;
|
|
|
|
|
|
|
|
int x = SCALE_WIDTH*fact + FS, y = 0;
|
|
|
|
int rowHeight = FS;
|
2014-10-12 17:12:58 -04:00
|
|
|
|
|
|
|
#if !defined(Q_WS_MAC)
|
|
|
|
/* On Mac, we don't need vertical spacing between the text rows. */
|
|
|
|
rowHeight += 5;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a formatted string with the correct size suffix. */
|
|
|
|
QString RSGraphWidget::totalToStr(qreal total)
|
|
|
|
{
|
|
|
|
/* Determine the correct size suffix */
|
|
|
|
if (total < 1024) {
|
|
|
|
/* Use KB suffix */
|
|
|
|
return tr("%1 KB").arg(total, 0, 'f', 2);
|
|
|
|
} else if (total < 1048576) {
|
|
|
|
/* Use MB suffix */
|
|
|
|
return tr("%1 MB").arg(total/1024.0, 0, 'f', 2);
|
|
|
|
} else {
|
|
|
|
/* Use GB suffix */
|
|
|
|
return tr("%1 GB").arg(total/1048576.0, 0, 'f', 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Paints the scale on the graph. */
|
2015-05-15 09:35:51 -04:00
|
|
|
void RSGraphWidget::paintScale1()
|
2014-10-12 17:12:58 -04:00
|
|
|
{
|
2015-06-24 16:55:09 -04:00
|
|
|
float FS = QFontMetricsF(font()).height();
|
|
|
|
float fact = FS/14.0 ;
|
|
|
|
|
|
|
|
int top = _rec.y();
|
2015-03-21 14:40:21 -04:00
|
|
|
int bottom = _rec.height();
|
|
|
|
qreal paintStep = (bottom - (bottom/10)) / 4;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
/* Draw the other marks in their correctly scaled locations */
|
|
|
|
qreal scale;
|
|
|
|
qreal pos;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
if(_source == NULL)
|
|
|
|
return ;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
QString unit_name = _source->unitName() ;
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
for (int i = 1; i < 5; i++)
|
|
|
|
{
|
|
|
|
pos = bottom - (i * paintStep);
|
2014-10-13 18:05:50 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
scale = pixelsToValue(i * paintStep);
|
2014-10-15 18:00:49 -04:00
|
|
|
|
2015-03-21 14:40:21 -04:00
|
|
|
QString text = _source->displayValue(scale) ;
|
|
|
|
|
|
|
|
_painter->setPen(SCALE_COLOR);
|
2015-06-24 16:55:09 -04:00
|
|
|
_painter->drawText(QPointF(5*fact, pos+0.5*FS), text);
|
2015-03-21 14:40:21 -04:00
|
|
|
_painter->setPen(GRID_COLOR);
|
2015-06-24 16:55:09 -04:00
|
|
|
_painter->drawLine(QPointF(SCALE_WIDTH*fact, pos), QPointF(_rec.width(), pos));
|
2015-03-21 14:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Draw vertical separator */
|
2015-06-24 16:55:09 -04:00
|
|
|
_painter->drawLine(SCALE_WIDTH*fact, top, SCALE_WIDTH*fact, bottom);
|
2015-05-15 09:35:51 -04:00
|
|
|
}
|
2015-03-21 14:40:21 -04:00
|
|
|
|
2015-05-15 09:35:51 -04:00
|
|
|
void RSGraphWidget::paintScale2()
|
|
|
|
{
|
|
|
|
// draw time below the graph
|
2015-03-21 14:40:21 -04:00
|
|
|
|
2015-06-24 16:55:09 -04:00
|
|
|
float FS = QFontMetricsF(font()).height();
|
|
|
|
float fact = FS/14.0 ;
|
|
|
|
|
2015-05-15 09:35:51 -04:00
|
|
|
int bottom = _rec.height();
|
2015-06-24 16:55:09 -04:00
|
|
|
static const int npix = 100*fact ;
|
2015-03-21 14:40:21 -04:00
|
|
|
|
2015-06-24 16:55:09 -04:00
|
|
|
for(int i=_rec.width();i>SCALE_WIDTH*fact;i-=npix)
|
2015-05-15 09:35:51 -04:00
|
|
|
{
|
2015-06-24 16:55:09 -04:00
|
|
|
qreal pos = bottom - FS;
|
2015-03-21 14:40:21 -04:00
|
|
|
|
2015-05-15 09:35:51 -04:00
|
|
|
int seconds = (_rec.width()-i)/_time_scale ; // pixels / (pixels per second) => seconds
|
|
|
|
QString text = QString::number(seconds)+ " secs";
|
2015-03-21 14:40:21 -04:00
|
|
|
|
2015-05-15 09:35:51 -04:00
|
|
|
_painter->setPen(SCALE_COLOR);
|
2015-06-24 16:55:09 -04:00
|
|
|
_painter->drawText(QPointF(i, _rec.height()-0.5*FS), text);
|
2015-05-15 09:35:51 -04:00
|
|
|
}
|
2015-03-21 14:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RSGraphWidget::wheelEvent(QWheelEvent *e)
|
|
|
|
{
|
|
|
|
if(e->delta() > 0)
|
|
|
|
_time_scale *= 1.1 ;
|
|
|
|
else
|
|
|
|
_time_scale /= 1.1 ;
|
|
|
|
|
|
|
|
update() ;
|
2014-10-12 17:12:58 -04:00
|
|
|
}
|
|
|
|
|
2014-10-15 18:00:49 -04:00
|
|
|
void RSGraphWidget::paintLegend()
|
|
|
|
{
|
|
|
|
int bottom = _rec.height();
|
|
|
|
|
2015-05-15 09:12:46 -04:00
|
|
|
std::vector<QPointF> vals ;
|
2014-10-15 18:00:49 -04:00
|
|
|
_source->getCurrentValues(vals) ;
|
2015-05-15 09:12:46 -04:00
|
|
|
int j=0;
|
2014-10-15 18:00:49 -04:00
|
|
|
|
2015-06-24 16:55:09 -04:00
|
|
|
float FS = QFontMetricsF(font()).height();
|
|
|
|
float fact = FS/14.0 ;
|
|
|
|
|
2014-10-15 18:00:49 -04:00
|
|
|
for(uint i=0;i<vals.size();++i)
|
2015-05-15 08:40:47 -04:00
|
|
|
if( _masked_entries.find(_source->displayName(i).toStdString()) == _masked_entries.end() )
|
2015-05-15 09:12:46 -04:00
|
|
|
{
|
2015-06-24 16:55:09 -04:00
|
|
|
if( _rec.width() - (vals[i].x()-0)*_time_scale < SCALE_WIDTH*fact )
|
2015-05-15 09:12:46 -04:00
|
|
|
continue ;
|
2014-10-15 18:00:49 -04:00
|
|
|
|
2015-06-24 16:55:09 -04:00
|
|
|
qreal paintStep = 4*fact+FS;
|
|
|
|
qreal pos = 15*fact+j*paintStep;
|
2015-05-15 09:12:46 -04:00
|
|
|
QString text = _source->legend(i,vals[i].y()) ;
|
2014-10-15 18:00:49 -04:00
|
|
|
|
2015-05-15 09:12:46 -04:00
|
|
|
QPen oldPen = _painter->pen();
|
|
|
|
_painter->setPen(QPen(getColor(i), Qt::SolidLine));
|
2015-06-25 16:11:39 -04:00
|
|
|
_painter->drawLine(QPointF(SCALE_WIDTH*fact+10.0*fact, pos+FS/3), QPointF(SCALE_WIDTH*fact+30.0*fact, pos+FS/3));
|
2015-05-15 09:12:46 -04:00
|
|
|
_painter->setPen(oldPen);
|
|
|
|
|
|
|
|
_painter->setPen(SCALE_COLOR);
|
2015-06-24 16:55:09 -04:00
|
|
|
_painter->drawText(QPointF(SCALE_WIDTH *fact+ 40*fact,pos + 0.5*FS), text) ;
|
2015-05-15 09:12:46 -04:00
|
|
|
|
|
|
|
++j ;
|
|
|
|
}
|
2014-10-15 18:00:49 -04:00
|
|
|
}
|
|
|
|
|