/
graphpanelplotwidget.h
726 lines (507 loc) · 21.5 KB
/
graphpanelplotwidget.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR 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 3 of the License, or
(at your option) any later version.
OpenCOR 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, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// Graph panel plot widget
//==============================================================================
#pragma once
//==============================================================================
#include "commonwidget.h"
#include "graphpanelwidgetglobal.h"
//==============================================================================
#include "qwtbegin.h"
#include "qwt_legend.h"
#include "qwt_plot.h"
#include "qwt_plot_curve.h"
#include "qwt_scale_draw.h"
#include "qwt_scale_widget.h"
#include "qwt_symbol.h"
#include "qwt_text.h"
#include "qwtend.h"
//==============================================================================
class QMenu;
//==============================================================================
class QwtLegendLabel;
class QwtPlotDirectPainter;
class QwtPlotGrid;
//==============================================================================
namespace OpenCOR {
namespace GraphPanelWidget {
//==============================================================================
static const double DefaultMinAxis = 0.0;
static const double DefaultMinLogAxis = 1.0;
static const double DefaultMaxAxis = 1000.0;
//==============================================================================
static const QColor DarkBlue = QColor::fromRgb(0, 114, 189);
static const QColor Orange = QColor::fromRgb(217, 83, 25);
static const QColor Yellow = QColor::fromRgb(237, 177, 32);
static const QColor Purple = QColor::fromRgb(126, 47, 142);
static const QColor Green = QColor::fromRgb(119, 172, 48);
static const QColor LightBlue = QColor::fromRgb(77, 190, 238);
static const QColor Red = QColor::fromRgb(162, 20, 47);
//==============================================================================
static const bool DefaultGraphSelected = true;
static const auto DefaultGraphTitle = QStringLiteral("");
static const Qt::PenStyle DefaultGraphLineStyle = Qt::SolidLine;
static const int DefaultGraphLineWidth = 2;
static const QColor DefaultGraphLineColor = DarkBlue;
static const QwtSymbol::Style DefaultGraphSymbolStyle = QwtSymbol::NoSymbol;
static const int DefaultGraphSymbolSize = 8;
static const QColor DefaultGraphSymbolColor = DarkBlue;
static const bool DefaultGraphSymbolFilled = true;
static const QColor DefaultGraphSymbolFillColor = Qt::white;
//==============================================================================
class GRAPHPANELWIDGET_EXPORT GraphPanelPlotGraphProperties
{
public:
explicit GraphPanelPlotGraphProperties(bool pSelected = DefaultGraphSelected,
const QString &pTitle = DefaultGraphTitle,
const Qt::PenStyle &pLineStyle = DefaultGraphLineStyle,
int pLineWidth = DefaultGraphLineWidth,
const QColor &pLineColor = DefaultGraphLineColor,
const QwtSymbol::Style &pSymbolStyle = DefaultGraphSymbolStyle,
int pSymbolSize = DefaultGraphSymbolSize,
const QColor &pSymbolColor = DefaultGraphSymbolColor,
bool pSymbolFilled = DefaultGraphSymbolFilled,
const QColor &pSymbolFillColor = DefaultGraphSymbolFillColor);
bool isSelected() const;
QString title() const;
Qt::PenStyle lineStyle() const;
int lineWidth() const;
QColor lineColor() const;
QwtSymbol::Style symbolStyle() const;
int symbolSize() const;
QColor symbolColor() const;
bool symbolFilled() const;
QColor symbolFillColor() const;
private:
bool mSelected;
QString mTitle;
Qt::PenStyle mLineStyle;
int mLineWidth;
QColor mLineColor;
QwtSymbol::Style mSymbolStyle;
int mSymbolSize;
QColor mSymbolColor;
bool mSymbolFilled;
QColor mSymbolFillColor;
};
//==============================================================================
class GraphPanelPlotGraph;
//==============================================================================
class GraphPanelPlotGraphRun : public QwtPlotCurve
{
public:
explicit GraphPanelPlotGraphRun(GraphPanelPlotGraph *pOwner);
GraphPanelPlotGraph * owner() const;
void setRawSamples(const double *pDataX, const double *pDataY, int pSize);
protected:
void drawLines(QPainter *pPainter, const QwtScaleMap &pMapX,
const QwtScaleMap &pMapY, const QRectF &pCanvasRect,
int pFrom, int pTo) const override;
void drawSymbols(QPainter *pPainter, const QwtSymbol &pSymbol,
const QwtScaleMap &pMapX, const QwtScaleMap &pMapY,
const QRectF &pCanvasRect, int pFrom, int pTo) const override;
private:
GraphPanelPlotGraph *mOwner;
int mSize = 0;
QList<QPair<int, int>> mValidData;
};
//==============================================================================
using GraphPanelPlotGraphRuns = QList<GraphPanelPlotGraphRun *>;
//==============================================================================
class GraphPanelPlotWidget;
class GraphPanelWidget;
//==============================================================================
class GRAPHPANELWIDGET_EXPORT GraphPanelPlotGraph
{
public:
explicit GraphPanelPlotGraph(void *pParameterX, void *pParameterY,
GraphPanelWidget *pOwner);
explicit GraphPanelPlotGraph(GraphPanelWidget *pOwner);
bool isValid() const;
void attach(GraphPanelPlotWidget *pPlot);
void detach();
GraphPanelPlotWidget * plot() const;
int runsCount() const;
void addRun();
void removeRuns();
GraphPanelPlotGraphRun * lastRun() const;
bool isSelected() const;
void setSelected(bool pSelected);
QString fileName() const;
void setFileName(const QString &pFileName);
void * parameterX() const;
void setParameterX(void *pParameterX);
void * parameterY() const;
void setParameterY(void *pParameterY);
QColor color() const;
const QPen & pen() const;
void setPen(const QPen &pPen);
const QwtSymbol * symbol() const;
void setSymbol(const QwtSymbol::Style &pStyle, const QBrush &pBrush,
const QPen &pPen, const QSize &pSize);
QString title() const;
void setTitle(const QString &pTitle);
bool isVisible() const;
void setVisible(bool pVisible);
bool hasData() const;
quint64 dataSize() const;
QwtSeriesData<QPointF> *data(int pRun = -1) const;
void setData(double *pDataX, double *pDataY, quint64 pSize, int pRun = -1);
QRectF boundingRect();
QRectF boundingLogRect();
private:
bool mSelected = true;
QString mFileName;
void *mParameterX;
void *mParameterY;
QColor mColor;
QRectF mBoundingRect;
QMap<GraphPanelPlotGraphRun *, QRectF> mBoundingRects;
QRectF mBoundingLogRect;
QMap<GraphPanelPlotGraphRun *, QRectF> mBoundingLogRects;
GraphPanelPlotWidget *mPlot = nullptr;
GraphPanelPlotGraphRun *mDummyRun = nullptr;
GraphPanelPlotGraphRuns mRuns;
};
//==============================================================================
using GraphPanelPlotGraphs = QList<GraphPanelPlotGraph *>;
//==============================================================================
class GraphPanelPlotOverlayWidget : public QWidget
{
public:
explicit GraphPanelPlotOverlayWidget(GraphPanelPlotWidget *pParent);
void setOriginPoint(const QPoint &pOriginPoint);
void setPoint(const QPoint &pPoint);
QRect zoomRegion() const;
protected:
void paintEvent(QPaintEvent *pEvent) override;
private:
enum class Position {
TopLeft,
TopRight,
BottomLeft,
BottomRight
};
GraphPanelPlotWidget *mOwner;
QPoint mOriginPoint;
QPoint mPoint;
QPoint optimizedPoint(const QPoint &pPoint) const;
void drawCoordinates(QPainter *pPainter, const QPoint &pPoint,
const QColor &pBackgroundColor,
const QColor &pForegroundColor, const int &pFontSize,
int pLineWidth, Position pPosition,
bool pCanMovePosition = true);
};
//==============================================================================
class GraphPanelPlotScaleDraw : public QwtScaleDraw
{
public:
void retranslateUi();
protected:
QwtText label(double pValue) const override;
};
//==============================================================================
class GraphPanelPlotScaleWidget : public QwtScaleWidget
{
public:
void updateLayout();
};
//==============================================================================
class GraphPanelPlotLegendWidget : public QwtLegend
{
Q_OBJECT
public:
explicit GraphPanelPlotLegendWidget(GraphPanelPlotWidget *pParent);
bool isActive() const;
void setActive(bool pActive);
bool isEmpty() const override;
void setChecked(GraphPanelPlotGraph *pGraph, bool pChecked);
void setFontSize(int pFontSize);
void setBackgroundColor(const QColor &pBackgroundColor);
void setForegroundColor(const QColor &pForegroundColor);
void renderLegend(QPainter *pPainter, const QRectF &pRect,
bool pFillBackground) const override;
void setSizeHintWidth(int pSizeHintWidth);
QSize sizeHint() const override;
void addGraph(GraphPanelPlotGraph *pGraph);
void removeGraph(GraphPanelPlotGraph *pGraph);
bool needScrollBar() const;
protected:
void updateWidget(QWidget *pWidget, const QwtLegendData &pLegendData) override;
private:
GraphPanelPlotWidget *mOwner;
bool mActive = false;
int mFontSize;
QColor mBackgroundColor;
QColor mForegroundColor;
QMap<GraphPanelPlotGraph *, QwtLegendLabel *> mLegendLabels;
int mSizeHintWidth = 0;
signals:
void graphToggled(GraphPanelPlotGraph *pGraph);
private slots:
void legendChecked(const QVariant &pItemInfo);
};
//==============================================================================
class GraphPanelWidget;
//==============================================================================
using GraphPanelPlotWidgets = QList<GraphPanelPlotWidget *>;
//==============================================================================
class GRAPHPANELWIDGET_EXPORT GraphPanelPlotWidget : public QwtPlot,
public Core::CommonWidget
{
Q_OBJECT
public:
enum class Action {
None,
Pan,
ShowCoordinates,
Zoom,
ZoomRegion
};
enum class Optimization {
Default,
Linear,
Logarithmic
};
explicit GraphPanelPlotWidget(const GraphPanelPlotWidgets &pNeighbors,
QAction *pSynchronizeXAxisAction,
QAction *pSynchronizeYAxisAction,
GraphPanelWidget *pParent);
~GraphPanelPlotWidget() override;
void retranslateUi() override;
void setActive(bool pActive);
GraphPanelPlotGraphs graphs() const;
bool addGraph(GraphPanelPlotGraph *pGraph);
bool removeGraph(GraphPanelPlotGraph *pGraph);
int graphIndex(GraphPanelPlotGraph *pGraph) const;
bool hasData() const;
bool dataRect(QRectF &pDataRect) const;
bool dataLogRect(QRectF &pDataLogRect) const;
bool isOptimizedAxes() const;
void optimizeAxisX(double &pMin, double &pMax,
Optimization pOptimization = Optimization::Default);
void optimizeAxisY(double &pMin, double &pMax,
Optimization pOptimization = Optimization::Default);
double minX() const;
double maxX() const;
double minY() const;
double maxY() const;
QColor backgroundColor() const;
void setBackgroundColor(const QColor &pBackgroundColor);
int fontSize() const;
void setFontSize(int pFontSize, bool pForceSetting = false);
QColor foregroundColor() const;
void setForegroundColor(const QColor &pForegroundColor);
Qt::PenStyle gridLinesStyle() const;
void setGridLinesStyle(const Qt::PenStyle &pGridLinesStyle);
int gridLinesWidth() const;
void setGridLinesWidth(int pGridLinesWidth);
QColor gridLinesColor() const;
void setGridLinesColor(const QColor &pGridLinesColor);
bool isLegendVisible() const;
void setLegendVisible(bool pLegendActive);
Qt::PenStyle pointCoordinatesStyle() const;
void setPointCoordinatesStyle(const Qt::PenStyle &pPointCoordinatesStyle);
int pointCoordinatesWidth() const;
void setPointCoordinatesWidth(int pPointCoordinatesWidth);
QColor pointCoordinatesColor() const;
void setPointCoordinatesColor(const QColor &pPointCoordinatesColor);
QColor pointCoordinatesFontColor() const;
void setPointCoordinatesFontColor(const QColor &pPointCoordinatesFontColor);
int pointCoordinatesFontSize() const;
void setPointCoordinatesFontSize(int pPointCoordinatesFontSize);
QColor surroundingAreaBackgroundColor() const;
void setSurroundingAreaBackgroundColor(const QColor &pSurroundingAreaBackgroundColor);
QColor surroundingAreaForegroundColor() const;
void setSurroundingAreaForegroundColor(const QColor &pSurroundingAreaForegroundColor);
void setTitle(const QString &pTitle);
int fontSizeAxisX() const;
void setFontSizeAxisX(int pFontSizeAxisX, bool pForceSetting = false);
bool logAxisX() const;
void setLogAxisX(bool pLogAxisX);
QString titleAxisX() const;
void setTitleAxisX(const QString &pTitleAxisX);
int fontSizeAxisY() const;
void setFontSizeAxisY(int pFontSizeAxisY, bool pForceSetting = false);
bool logAxisY() const;
void setLogAxisY(bool pLogAxisY);
QString titleAxisY() const;
void setTitleAxisY(const QString &pTitleAxisY);
Qt::PenStyle zoomRegionStyle() const;
void setZoomRegionStyle(const Qt::PenStyle &pZoomRegionStyle);
int zoomRegionWidth() const;
void setZoomRegionWidth(int pZoomRegionWidth);
QColor zoomRegionColor() const;
void setZoomRegionColor(const QColor &pZoomRegionColor);
QColor zoomRegionFontColor() const;
void setZoomRegionFontColor(const QColor &pZoomRegionFontColor);
int zoomRegionFontSize() const;
void setZoomRegionFontSize(int pZoomRegionFontSize);
bool zoomRegionFilled() const;
void setZoomRegionFilled(bool pZoomRegionFilled);
QColor zoomRegionFillColor() const;
void setZoomRegionFillColor(const QColor &pZoomRegionFillColor);
void setDefaultAxesValues(double pDefaultMinX, double pDefaultMaxX,
double pDefaultMinLogX, double pDefaultMaxLogX,
double pDefaultMinY, double pDefaultMaxY,
double pDefaultMinLogY, double pDefaultMaxLogY);
bool setAxes(double pMinX, double pMaxX, double pMinY, double pMaxY,
bool pSynchronizeAxes, bool pCanReplot, bool pEmitSignal,
bool pForceAxesSetting, bool pSynchronizeXAxis,
bool pSynchronizeYAxis);
bool drawGraphFrom(GraphPanelPlotGraph *pGraph, quint64 pFrom);
GraphPanelPlotWidgets neighbors() const;
void addNeighbor(GraphPanelPlotWidget *pPlot);
void removeNeighbor(GraphPanelPlotWidget *pPlot);
bool hasDirtyAxes() const;
Action action() const;
bool canZoomInX() const;
bool canZoomOutX() const;
bool canZoomInY() const;
bool canZoomOutY() const;
QPointF canvasPoint(const QPoint &pPoint) const;
void updateGui(bool pSingleShot = false, bool pForceAlignment = false);
protected:
void changeEvent(QEvent *pEvent) override;
bool event(QEvent *pEvent) override;
bool eventFilter(QObject *pObject, QEvent *pEvent) override;
void mouseMoveEvent(QMouseEvent *pEvent) override;
void mousePressEvent(QMouseEvent *pEvent) override;
void mouseReleaseEvent(QMouseEvent *pEvent) override;
void paintEvent(QPaintEvent *pEvent) override;
void resizeEvent(QResizeEvent *pEvent) override;
void wheelEvent(QWheelEvent *pEvent) override;
private:
enum class Scaling {
NoScaling,
ScalingIn,
BigScalingIn,
ScalingOut,
BigScalingOut,
CustomScaling
};
GraphPanelWidget *mOwner;
QwtPlotDirectPainter *mDirectPainter;
QColor mBackgroundColor;
QColor mForegroundColor;
Qt::PenStyle mPointCoordinatesStyle = Qt::DashLine;
int mPointCoordinatesWidth = 1;
QColor mPointCoordinatesColor;
QColor mPointCoordinatesFontColor = Qt::white;
int mPointCoordinatesFontSize = 10;
QColor mSurroundingAreaBackgroundColor;
QColor mSurroundingAreaForegroundColor;
Qt::PenStyle mZoomRegionStyle = Qt::SolidLine;
int mZoomRegionWidth = 1;
QColor mZoomRegionColor;
QColor mZoomRegionFontColor = Qt::white;
int mZoomRegionFontSize = 10;
bool mZoomRegionFilled = true;
QColor mZoomRegionFillColor;
bool mLogAxisX = false;
bool mLogAxisY = false;
GraphPanelPlotGraphs mGraphs;
QColor mEnabledBackgroundColor;
QColor mEnabledForegroundColor;
QColor mEnabledSurroundingAreaBackgroundColor;
QColor mEnabledSurroundingAreaForegroundColor;
QColor mEnabledGridLinesColor;
QList<QPen> mEnabledGraphPens;
QList<QBrush> mEnabledGraphSymbolBrushes;
QList<QPen> mEnabledGraphSymbolPens;
Action mAction = Action::None;
QPoint mOriginPoint;
QPoint mPoint;
GraphPanelPlotOverlayWidget *mOverlayWidget;
GraphPanelPlotLegendWidget *mLegend;
bool mCanDirectPaint = true;
bool mCanReplot = true;
bool mCanZoomInX = true;
bool mCanZoomOutX = true;
bool mCanZoomInY = true;
bool mCanZoomOutY = true;
bool mNeedContextMenu = false;
QMenu *mContextMenu;
bool mCanUpdateActions = true;
QAction *mExportToAction;
QAction *mCopyToClipboardAction;
QAction *mSynchronizeXAxisAction;
QAction *mSynchronizeYAxisAction;
QAction *mGraphPanelSettingsAction;
QAction *mGraphsSettingsAction;
QAction *mLegendAction;
QAction *mLogarithmicXAxisAction;
QAction *mLogarithmicYAxisAction;
QAction *mCustomAxesAction;
QAction *mZoomInAction;
QAction *mZoomOutAction;
QAction *mResetZoomAction;
GraphPanelPlotScaleDraw *mAxisX;
GraphPanelPlotScaleDraw *mAxisY;
QwtPlotGrid *mGrid;
bool mOptimizedAxisX = true;
bool mOptimizedAxisY = true;
double mDefaultMinX = DefaultMinAxis;
double mDefaultMaxX = DefaultMaxAxis;
double mDefaultMinY = DefaultMinAxis;
double mDefaultMaxY = DefaultMaxAxis;
double mDefaultMinLogX = DefaultMinLogAxis;
double mDefaultMaxLogX = DefaultMaxAxis;
double mDefaultMinLogY = DefaultMinLogAxis;
double mDefaultMaxLogY = DefaultMaxAxis;
GraphPanelPlotWidgets mNeighbors;
bool mDirtyAxes = false;
void checkAxisValues(bool pLogAxis, double &pMin, double &pMax);
void updateActions();
void resetAction();
QRectF realDataRect();
void optimizeAxis(int pAxisId, double &pMin, double &pMax,
Optimization pOptimization);
void setAxis(int pAxisId, double pMin, double pMax);
bool resetAxes();
bool scaleAxis(Scaling pScaling, bool pCanZoomIn, bool pCanZoomOut,
const QwtScaleMap &pCanvasMap, double pPoint,
double &pMin, double &pMax, double pCustomScaling);
void scaleAxes(const QPoint &pPoint, Scaling pScalingX, Scaling pScalingY,
double pCustomScaling = 0.0);
void setTitleAxis(int pAxisId, const QString &pTitleAxis);
void getBorderDistances(QwtScaleDraw *pScaleDraw,
const QwtScaleMap &pScaleMap, const QFont &pFont,
int &pStart, int &pEnd);
void alignWithNeighbors(bool pCanReplot, bool pForceAlignment = false);
signals:
void axesChanged(double pMinX, double pMaxX, double pMinY, double pMaxY);
void graphPanelSettingsRequested();
void graphsSettingsRequested();
void graphToggled(GraphPanelPlotGraph *pGraph);
void legendToggled();
void logarithmicXAxisToggled();
void logarithmicYAxisToggled();
private slots:
void doUpdateGui(bool pForceAlignment);
void cannotUpdateActions();
void exportTo();
void copyToClipboard();
void customAxes();
void zoomIn();
void zoomOut();
void resetZoom();
};
//==============================================================================
} // namespace GraphPanelWidget
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================