|
32 | 32 | #include <QWheelEvent> |
33 | 33 | #include <QTimer> |
34 | 34 |
|
| 35 | +#include <QChartView> |
| 36 | +#include <QChart> |
| 37 | +#include <QPieSeries> |
| 38 | +#include <QPieSlice> |
| 39 | +#include <QValueAxis> |
| 40 | + |
35 | 41 | #include <retroshare-gui/RsAutoUpdatePage.h> |
36 | 42 | #include "rshare.h" |
37 | 43 | #include "RSGraphWidget.h" |
@@ -259,32 +265,38 @@ void RSGraphWidget::setTimeScale(float pixels_per_second) |
259 | 265 | _time_scale =pixels_per_second ; |
260 | 266 | } |
261 | 267 |
|
| 268 | +void RSGraphWidget::setViewMode(ViewMode m) |
| 269 | +{ |
| 270 | + _viewMode = m; |
| 271 | +} |
262 | 272 | /** Default contructor */ |
263 | 273 | RSGraphWidget::RSGraphWidget(QWidget *parent) |
264 | 274 | : QFrame(parent) |
265 | 275 | { |
| 276 | + _viewMode = ViewMode::History; |
| 277 | + _mousePressed = false; |
266 | 278 | _source =NULL; |
267 | | - _painter = new QPainter(); |
| 279 | + _painter = new QPainter(); |
268 | 280 |
|
269 | | - /* Initialize graph values */ |
270 | | - _maxPoints = getNumPoints(); |
271 | | - _maxValue = MINUSER_SCALE; |
| 281 | + /* Initialize graph values */ |
| 282 | + _maxPoints = getNumPoints(); |
| 283 | + _maxValue = MINUSER_SCALE; |
272 | 284 |
|
273 | | - _linewidthscale = 1.0f; |
274 | | - _opacity = 0.6 ; |
275 | | - _flags = 0; |
276 | | - _time_scale = 5.0f ; // in pixels per second. |
277 | | - _time_filter = 1.0f ; |
278 | | - _timer = new QTimer ; |
279 | | - QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(updateIfPossible())) ; |
| 285 | + _linewidthscale = 1.0f; |
| 286 | + _opacity = 0.6 ; |
| 287 | + _flags = 0; |
| 288 | + _time_scale = 5.0f ; // in pixels per second. |
| 289 | + _time_filter = 1.0f ; |
| 290 | + _timer = new QTimer ; |
| 291 | + QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(updateIfPossible())) ; |
280 | 292 |
|
281 | 293 |
|
282 | | - _y_scale = 1.0f ; |
283 | | - _timer->start(1000); |
| 294 | + _y_scale = 1.0f ; |
| 295 | + _timer->start(1000); |
284 | 296 |
|
285 | 297 | float FS = QFontMetricsF(font()).height(); |
286 | | - setMinimumHeight(12*FS); |
287 | | - _graph_base = FS*GRAPH_BASE; |
| 298 | + setMinimumHeight(12*FS); |
| 299 | + _graph_base = FS*GRAPH_BASE; |
288 | 300 | } |
289 | 301 |
|
290 | 302 | void RSGraphWidget::updateIfPossible() |
@@ -353,14 +365,16 @@ void RSGraphWidget::paintEvent(QPaintEvent *) |
353 | 365 | } |
354 | 366 | _painter->drawRect(_rec); |
355 | 367 |
|
356 | | - /* Paint the scale */ |
357 | | - paintScale1(); |
358 | | - |
359 | | - /* Plot the data */ |
360 | | - paintData(); |
| 368 | + if(_viewMode == ViewMode::History) |
| 369 | + { |
| 370 | + /* Paint the scale */ |
| 371 | + paintScale1(); |
361 | 372 |
|
362 | | - /* Paint the totals */ |
363 | | - paintTotals(); |
| 373 | + /* Plot the data */ |
| 374 | + paintData(); |
| 375 | + } |
| 376 | + else if(_viewMode == ViewMode::Slice) |
| 377 | + paintTotals(); |
364 | 378 |
|
365 | 379 | // part of the scale that needs to write over the data curves. |
366 | 380 | paintScale2(); |
@@ -592,16 +606,54 @@ void RSGraphWidget::paintDots(const QVector<QPointF>& points, QColor color) |
592 | 606 | /** Paints selected total indicators on the graph. */ |
593 | 607 | void RSGraphWidget::paintTotals() |
594 | 608 | { |
| 609 | +#ifdef UNUSED |
595 | 610 | float FS = QFontMetricsF(font()).height(); |
596 | 611 | //float fact = FS/14.0 ; |
597 | | - |
598 | 612 | //int x = SCALE_WIDTH*fact + FS, y = 0; |
599 | 613 | int rowHeight = FS; |
600 | 614 |
|
601 | 615 | #if !defined(Q_OS_MAC) |
602 | 616 | /* On Mac, we don't need vertical spacing between the text rows. */ |
603 | 617 | rowHeight += 5; |
604 | 618 | #endif |
| 619 | +#endif |
| 620 | + |
| 621 | + auto c = new QtCharts::QChart(); |
| 622 | + c->legend()->setVisible(true); |
| 623 | + c->legend()->setAlignment(Qt::AlignRight); |
| 624 | + |
| 625 | + auto axisY = new QtCharts::QValueAxis(); |
| 626 | + axisY->setTitleText("MB"); |
| 627 | + axisY->setLabelFormat("%.1f"); |
| 628 | + c->addAxis(axisY, Qt::AlignLeft); |
| 629 | + |
| 630 | + std::vector<float> vals,tmp_vals; |
| 631 | + _source->getCumulatedValues(tmp_vals); |
| 632 | + |
| 633 | + for(int i=0;i<_source->n_values();++i) |
| 634 | + if( _masked_entries.find(_source->displayName(i).toStdString()) == _masked_entries.end() ) |
| 635 | + vals.push_back(tmp_vals[i]); |
| 636 | + |
| 637 | + float total = 0.0; |
| 638 | + for(auto v:vals) total += v; |
| 639 | + |
| 640 | + if(total != 0.0) |
| 641 | + for(auto& v:vals) v/=total; |
| 642 | + |
| 643 | + auto pieSeries = new QtCharts::QPieSeries(); |
| 644 | + for(uint i=0;i<vals.size();++i) |
| 645 | + { |
| 646 | + QtCharts::QPieSlice *slice = pieSeries->append(_source->legend(i,vals[i],false),vals[i]); |
| 647 | + slice->setLabelVisible(true); |
| 648 | + } |
| 649 | + |
| 650 | + c->resize(_rec.width(),_rec.height()); |
| 651 | + c->addSeries(pieSeries); |
| 652 | + |
| 653 | + QGraphicsScene scene; |
| 654 | + scene.addItem(c); |
| 655 | + scene.setSceneRect(0, 0, _rec.width(), _rec.height()); |
| 656 | + scene.render(_painter, _rec, _rec); |
605 | 657 | } |
606 | 658 |
|
607 | 659 | /** Returns a formatted string with the correct size suffix. */ |
@@ -773,3 +825,21 @@ void RSGraphWidget::paintLegend() |
773 | 825 | } |
774 | 826 | } |
775 | 827 |
|
| 828 | +// These functions capture the cursor |
| 829 | +void RSGraphWidget::mousePressEvent(QMouseEvent *e) |
| 830 | +{ |
| 831 | + _mousePressed = true; |
| 832 | + QFrame::mousePressEvent(e); |
| 833 | +} |
| 834 | +void RSGraphWidget::mouseMoveEvent(QMouseEvent *e) |
| 835 | +{ |
| 836 | + if(_mousePressed) |
| 837 | + std::cerr << "e->x() = " << e->x() << std::endl; |
| 838 | + QFrame::mouseMoveEvent(e); |
| 839 | +} |
| 840 | +void RSGraphWidget::mouseReleaseEvent(QMouseEvent *e) |
| 841 | +{ |
| 842 | + _mousePressed = false; |
| 843 | + QFrame::mouseReleaseEvent(e); |
| 844 | +} |
| 845 | + |
0 commit comments