/
graphpanelplotwidget.cpp
2983 lines (2136 loc) · 93 KB
/
graphpanelplotwidget.cpp
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
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 <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// Graph panel plot widget
//==============================================================================
#include "coreguiutils.h"
#include "graphpanelplotwidget.h"
#include "graphpanelwidget.h"
#include "graphpanelwidgetcustomaxesdialog.h"
#include "i18ninterface.h"
//==============================================================================
#include <QApplication>
#include <QClipboard>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QImageWriter>
#include <QMenu>
#include <QPaintEvent>
//==============================================================================
#include <float.h>
//==============================================================================
#include "qwtbegin.h"
#include "qwt_dyngrid_layout.h"
#include "qwt_legend_label.h"
#include "qwt_painter.h"
#include "qwt_plot_canvas.h"
#include "qwt_plot_directpainter.h"
#include "qwt_plot_grid.h"
#include "qwt_plot_layout.h"
#include "qwt_plot_renderer.h"
#include "qwt_scale_engine.h"
#include "qwt_text_label.h"
#include "qwtend.h"
//==============================================================================
namespace OpenCOR {
namespace GraphPanelWidget {
//==============================================================================
GraphPanelPlotGraphProperties::GraphPanelPlotGraphProperties(const QString &pTitle,
const Qt::PenStyle &pLineStyle,
const int &pLineWidth,
const QColor &pLineColor,
const QwtSymbol::Style &pSymbolStyle,
const int &pSymbolSize,
const QColor &pSymbolColor,
const bool &pSymbolFilled,
const QColor &pSymbolFillColor) :
mTitle(pTitle),
mLineStyle(pLineStyle),
mLineWidth(pLineWidth),
mLineColor(pLineColor),
mSymbolStyle(pSymbolStyle),
mSymbolSize(pSymbolSize),
mSymbolColor(pSymbolColor),
mSymbolFilled(pSymbolFilled),
mSymbolFillColor(pSymbolFillColor)
{
}
//==============================================================================
QString GraphPanelPlotGraphProperties::title() const
{
// Return our title
return mTitle;
}
//==============================================================================
Qt::PenStyle GraphPanelPlotGraphProperties::lineStyle() const
{
// Return our line style
return mLineStyle;
}
//==============================================================================
int GraphPanelPlotGraphProperties::lineWidth() const
{
// Return our line width
return mLineWidth;
}
//==============================================================================
QColor GraphPanelPlotGraphProperties::lineColor() const
{
// Return our line colour
return mLineColor;
}
//==============================================================================
QwtSymbol::Style GraphPanelPlotGraphProperties::symbolStyle() const
{
// Return our symbol style
return mSymbolStyle;
}
//==============================================================================
int GraphPanelPlotGraphProperties::symbolSize() const
{
// Return our symbol size
return mSymbolSize;
}
//==============================================================================
QColor GraphPanelPlotGraphProperties::symbolColor() const
{
// Return our symbol colour
return mSymbolColor;
}
//==============================================================================
bool GraphPanelPlotGraphProperties::symbolFilled() const
{
// Return our symbol filled status
return mSymbolFilled;
}
//==============================================================================
QColor GraphPanelPlotGraphProperties::symbolFillColor() const
{
// Return our symbol fill colour
return mSymbolFillColor;
}
//==============================================================================
static const QRectF NoBoundingLogRect = QRectF(0.0, 0.0, -1.0, -1.0);
//==============================================================================
GraphPanelPlotGraph::GraphPanelPlotGraph(void *pParameterX, void *pParameterY) :
QwtPlotCurve(),
mSelected(true),
mFileName(QString()),
mParameterX(pParameterX),
mParameterY(pParameterY),
mBoundingLogRect(NoBoundingLogRect)
{
// Customise ourselves a bit
setLegendAttribute(LegendShowLine);
setLegendAttribute(LegendShowSymbol);
setPen(QPen(Qt::darkBlue, 1.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
setRenderHint(RenderAntialiased);
}
//==============================================================================
bool GraphPanelPlotGraph::isValid() const
{
// Return whether we are valid
return !mFileName.isEmpty()
&& mParameterX && mParameterY;
}
//==============================================================================
bool GraphPanelPlotGraph::isSelected() const
{
// Return whether we are selected
return mSelected;
}
//==============================================================================
void GraphPanelPlotGraph::setSelected(const bool &pSelected)
{
// Set our selected state
mSelected = pSelected;
// Un/check our corresponding legend item
GraphPanelPlotWidget *graphPlot = static_cast<GraphPanelPlotWidget *>(plot());
static_cast<GraphPanelPlotLegendWidget *>(graphPlot->legend())->setChecked(graphPlot->graphIndex(this), pSelected);
}
//==============================================================================
QString GraphPanelPlotGraph::fileName() const
{
// Return our file name
return mFileName;
}
//==============================================================================
void GraphPanelPlotGraph::setFileName(const QString &pFileName)
{
// Set our file name
mFileName = pFileName;
}
//==============================================================================
void * GraphPanelPlotGraph::parameterX() const
{
// Return our parameter X
return mParameterX;
}
//==============================================================================
void GraphPanelPlotGraph::setParameterX(void *pParameterX)
{
// Set our parameter X
mParameterX = pParameterX;
}
//==============================================================================
void * GraphPanelPlotGraph::parameterY() const
{
// Return our parameter Y
return mParameterY;
}
//==============================================================================
void GraphPanelPlotGraph::setParameterY(void *pParameterY)
{
// Set our parameter Y
mParameterY = pParameterY;
}
//==============================================================================
void GraphPanelPlotGraph::setData(double *pDataX, double *pDataY,
const int &pSize)
{
// Set our data, i.e. raw samples
setRawSamples(pDataX, pDataY, pSize);
// Reset our cache version of our log data rectangle
mBoundingLogRect = NoBoundingLogRect;
}
//==============================================================================
QRectF GraphPanelPlotGraph::boundingLogRect()
{
// Return the cached version of our bounding log rectangle, if we have one,
// or compute it and return it
if (mBoundingLogRect == NoBoundingLogRect) {
const QwtSeriesData<QPointF> *crtData = data();
bool needInitMinX = true;
bool needInitMaxX = true;
bool needInitMinY = true;
bool needInitMaxY = true;
double minX = 1.0;
double maxX = 1.0;
double minY = 1.0;
double maxY = 1.0;
for (size_t i = 0, iMax = crtData->size(); i < iMax; ++i) {
QPointF sample = crtData->sample(i);
if (sample.x() > 0.0) {
if (needInitMinX) {
minX = sample.x();
needInitMinX = false;
} else if (sample.x() < minX) {
minX = sample.x();
}
if (needInitMaxX) {
maxX = sample.x();
needInitMaxX = false;
} else if (sample.x() > maxX) {
maxX = sample.x();
}
}
if (sample.y() > 0.0) {
if (needInitMinY) {
minY = sample.y();
needInitMinY = false;
} else if (sample.y() < minY) {
minY = sample.y();
}
if (needInitMaxY) {
maxY = sample.y();
needInitMaxY = false;
} else if (sample.y() > maxY) {
maxY = sample.y();
}
}
}
if (!needInitMinX && !needInitMaxX && !needInitMinY && !needInitMaxY)
mBoundingLogRect = QRectF(minX, minY, maxX-minX, maxY-minY);
}
return mBoundingLogRect;
}
//==============================================================================
GraphPanelPlotOverlayWidget::GraphPanelPlotOverlayWidget(GraphPanelPlotWidget *pParent) :
QWidget(pParent),
mOwner(pParent),
mOriginPoint(QPoint()),
mPoint(QPoint())
{
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
setFocusPolicy(Qt::NoFocus);
}
//==============================================================================
QPoint GraphPanelPlotOverlayWidget::optimisedPoint(const QPoint &pPoint) const
{
// Optimise the given point so that it fits within our owner's ranges
QPoint realPoint = pPoint-mOwner->plotLayout()->canvasRect().topLeft().toPoint();
QwtScaleMap canvasMapX = mOwner->canvasMap(QwtPlot::xBottom);
QwtScaleMap canvasMapY = mOwner->canvasMap(QwtPlot::yLeft);
return QPoint(qMin(qRound(canvasMapX.transform(mOwner->maxX())),
qMax(qRound(canvasMapX.transform(mOwner->minX())),
realPoint.x())),
qMin(qRound(canvasMapY.transform(mOwner->minY())),
qMax(qRound(canvasMapY.transform(mOwner->maxY())),
realPoint.y())));
}
//==============================================================================
void GraphPanelPlotOverlayWidget::paintEvent(QPaintEvent *pEvent)
{
// Accept the event
pEvent->accept();
// Check whether an action is to be carried out
if (mOwner->action() == GraphPanelPlotWidget::None)
return;
// Paint the overlay, if any is needed
QPainter painter(this);
QRectF canvasRect = mOwner->plotLayout()->canvasRect();
painter.translate(canvasRect.x(), canvasRect.y());
switch (mOwner->action()) {
case GraphPanelPlotWidget::ShowCoordinates: {
// Draw the lines that show the coordinates
QPen pen = painter.pen();
pen.setColor(mOwner->pointCoordinatesColor());
pen.setStyle(mOwner->pointCoordinatesStyle());
pen.setWidth(mOwner->pointCoordinatesWidth());
painter.setPen(pen);
QPoint point = optimisedPoint(mPoint);
// Note: see drawCoordinates() as why we use QPoint rather than
// QPointF...
painter.drawLine(0, point.y(), canvasRect.width(), point.y());
painter.drawLine(point.x(), 0, point.x(), canvasRect.height());
// Draw the coordinates of our point
drawCoordinates(&painter, point, mOwner->pointCoordinatesColor(),
mOwner->pointCoordinatesFontColor(),
(mOwner->pointCoordinatesStyle() == Qt::NoPen)?
0:
mOwner->pointCoordinatesWidth(),
BottomRight);
break;
}
case GraphPanelPlotWidget::ZoomRegion: {
// Draw the region to be zoomed in
QRect zoomRegionRect = zoomRegion();
// Note: see drawCoordinates() as why we use QRect rather than QRectF...
if (mOwner->zoomRegionFilled())
painter.fillRect(zoomRegionRect, mOwner->zoomRegionFillColor());
QPen pen = painter.pen();
pen.setJoinStyle(Qt::RoundJoin);
pen.setColor(mOwner->zoomRegionColor());
pen.setStyle(mOwner->zoomRegionStyle());
pen.setWidth(mOwner->zoomRegionWidth());
painter.setPen(pen);
painter.drawRect(zoomRegionRect);
// Draw the two sets of coordinates
drawCoordinates(&painter, zoomRegionRect.topLeft(),
mOwner->zoomRegionColor(),
mOwner->zoomRegionFontColor(),
(mOwner->zoomRegionStyle() == Qt::NoPen)?
0:
mOwner->zoomRegionWidth(),
BottomRight, false);
drawCoordinates(&painter, zoomRegionRect.topLeft()+QPoint(zoomRegionRect.width(), zoomRegionRect.height()),
mOwner->zoomRegionColor(),
mOwner->zoomRegionFontColor(),
(mOwner->zoomRegionStyle() == Qt::NoPen)?
0:
mOwner->zoomRegionWidth(),
TopLeft, false);
break;
}
default:
// Either no action or not an action we know how to handle
;
}
}
//==============================================================================
void GraphPanelPlotOverlayWidget::setOriginPoint(const QPoint &pOriginPoint)
{
// Set our point of origin
mOriginPoint = pOriginPoint;
// Note: unlike for setPoint(), we must not repaint ourselves since a call
// to setOriginPoint() should always be followed by one to setPoint(),
// in which case we don't want to repaint ourselves twice, not least
// because the first time we may not have a correct mPoint value...
}
//==============================================================================
void GraphPanelPlotOverlayWidget::setPoint(const QPoint &pPoint)
{
// Set our point
mPoint = pPoint;
update();
}
//==============================================================================
QRect GraphPanelPlotOverlayWidget::zoomRegion() const
{
// Return the region to be zoomed based on mOriginPoint and mPoint
// Note: by default, we assume that we are already fully zoomed in in both
// directions...
QwtScaleMap canvasMapX = mOwner->canvasMap(QwtPlot::xBottom);
QwtScaleMap canvasMapY = mOwner->canvasMap(QwtPlot::yLeft);
int minX = canvasMapX.transform(mOwner->minX());
int maxX = canvasMapX.transform(mOwner->maxX());
int minY = canvasMapY.transform(mOwner->maxY());
int maxY = canvasMapY.transform(mOwner->minY());
if (mOwner->canZoomInX() || mOwner->canZoomInY()) {
QPoint originPoint = optimisedPoint(mOriginPoint);
QPoint point = optimisedPoint(mPoint);
if (mOwner->canZoomInX()) {
minX = qMin(originPoint.x(), point.x());
maxX = qMax(originPoint.x(), point.x());
}
if (mOwner->canZoomInY()) {
minY = qMin(originPoint.y(), point.y());
maxY = qMax(originPoint.y(), point.y());
}
}
return QRect(minX, minY, maxX-minX, maxY-minY);
}
//==============================================================================
void GraphPanelPlotOverlayWidget::drawCoordinates(QPainter *pPainter,
const QPoint &pPoint,
const QColor &pBackgroundColor,
const QColor &pForegroundColor,
const int &pLineWidth,
const Position &pPosition,
const bool &pCanMovePosition)
{
// Retrieve the size of coordinates as they will appear on the screen,
// which means using the same font as the one used for the axes
// Note #1: normally, pPoint would be a QPointF, but we want the coordinates
// to be drawn relative to something (see paintEvent()) and the
// only way to guarantee that everything will be painted as
// expected is to use QPoint. Indeed, if we were to use QPointF,
// then QPainter would have to do some rounding and though
// everything should be fine (since we always add/subtract a
// rounded number), it happens that it's not always the case.
// Indeed, we should always have a gap of one pixel between the
// coordinates and pPoint, but it could happen that we have either
// no gap, or one or two pixels...
// Note #2: we set the painter's pen's style to a solid line otherwise
// coordinatesRect will be empty if there is no style for showing
// the coordinates of a point or zooming in a region...
pPainter->setFont(mOwner->axisFont(QwtPlot::xBottom));
QPointF point = mOwner->canvasPoint(pPoint);
QString coordinates = QString("X: %1\nY: %2").arg(QLocale().toString(point.x(), 'g', 15),
QLocale().toString(point.y(), 'g', 15));
QPen pen = pPainter->pen();
pen.setStyle(Qt::SolidLine);
pPainter->setPen(pen);
QRect coordinatesRect = pPainter->boundingRect(qApp->desktop()->availableGeometry(), 0, coordinates);
// Determine where the coordinates and its background should be drawn
int shift = (pLineWidth >> 1)+pLineWidth%2;
int plusShift = shift+1;
int minusShift = shift+!(pLineWidth%2);
switch (pPosition) {
case TopLeft:
coordinatesRect.moveTo(pPoint.x()-coordinatesRect.width()-minusShift,
pPoint.y()-coordinatesRect.height()-minusShift);
break;
case TopRight:
coordinatesRect.moveTo(pPoint.x()+plusShift,
pPoint.y()-coordinatesRect.height()-minusShift);
break;
case BottomLeft:
coordinatesRect.moveTo(pPoint.x()-coordinatesRect.width()-minusShift,
pPoint.y()+plusShift);
break;
case BottomRight:
coordinatesRect.moveTo(pPoint.x()+plusShift,
pPoint.y()+plusShift);
break;
}
if (pCanMovePosition) {
QwtScaleMap canvasMapX = mOwner->canvasMap(QwtPlot::xBottom);
QwtScaleMap canvasMapY = mOwner->canvasMap(QwtPlot::yLeft);
QPoint topLeftPoint = QPoint(canvasMapX.transform(mOwner->minX()),
canvasMapY.transform(mOwner->maxY()));
QPoint bottomRightPoint = QPoint(canvasMapX.transform(mOwner->maxX()),
canvasMapY.transform(mOwner->minY()));
if (coordinatesRect.top() < topLeftPoint.y())
coordinatesRect.moveTop(pPoint.y()+plusShift);
else if (coordinatesRect.top()+coordinatesRect.height()-1 > bottomRightPoint.y())
coordinatesRect.moveTop(pPoint.y()-coordinatesRect.height()-minusShift);
if (coordinatesRect.left() < topLeftPoint.x())
coordinatesRect.moveLeft(pPoint.x()+plusShift);
else if (coordinatesRect.left()+coordinatesRect.width()-1 > bottomRightPoint.x())
coordinatesRect.moveLeft(pPoint.x()-coordinatesRect.width()-minusShift);
// Note: the -1 for the else-if tests is because fillRect() below works
// on (0, 0; width-1, height-1)...
}
// Draw a filled rectangle to act as the background for the coordinates
// we are to show
pPainter->fillRect(coordinatesRect, pBackgroundColor);
// Draw the text for the coordinates, using a white pen
pen.setColor(pForegroundColor);
pPainter->setPen(pen);
pPainter->drawText(coordinatesRect, coordinates);
}
//==============================================================================
void GraphPanelPlotScaleDraw::retranslateUi()
{
// Retranslate ourselves by invalidating our cache
invalidateCache();
}
//==============================================================================
QwtText GraphPanelPlotScaleDraw::label(double pValue) const
{
// Return pValue as a string, keeping in mind the current locale
QString label = QLocale().toString(pValue);
QString fullLabel = QLocale().toString(pValue, 'g', 15);
return fullLabel.startsWith(label)?fullLabel:label;
}
//==============================================================================
void GraphPanelPlotScaleWidget::updateLayout()
{
// Our layout has changed, so update our internals
layoutScale();
}
//==============================================================================
GraphPanelPlotLegendWidget::GraphPanelPlotLegendWidget(GraphPanelPlotWidget *pParent) :
QwtLegend(pParent),
mOwner(pParent),
mActive(false),
mFontSize(pParent->fontSize()),
mForegroundColor(pParent->surroundingAreaForegroundColor())
{
// Have our legend items use as much horizontal space as possible
static_cast<QwtDynGridLayout *>(contentsWidget()->layout())->setExpandingDirections(Qt::Horizontal);
// Make our legend items checkable
setDefaultItemMode(QwtLegendData::Checkable);
// Check when someone clicks on a legend item
connect(this, SIGNAL(checked(const QVariant &, bool, int)),
this, SLOT(checked(const QVariant &)));
}
//==============================================================================
bool GraphPanelPlotLegendWidget::isActive() const
{
// Return our active state
return mActive;
}
//==============================================================================
void GraphPanelPlotLegendWidget::setActive(const bool &pActive)
{
// Set our active state
if (pActive != mActive) {
mActive = pActive;
mOwner->updateLegend();
}
}
//==============================================================================
bool GraphPanelPlotLegendWidget::isEmpty() const
{
// Return whether we are empty, based on whether we are active
return sizeHint() == QSize(0, 0);
}
//==============================================================================
void GraphPanelPlotLegendWidget::setChecked(const int &pIndex,
const bool &pChecked)
{
// Un/check the graph which index is given
// Note: the +1 is because the first child is our layout...
static_cast<QwtLegendLabel *>(contentsWidget()->children()[pIndex+1])->setChecked(pChecked);
}
//==============================================================================
void GraphPanelPlotLegendWidget::setFontSize(const int &pFontSize)
{
// Set our font size
if (pFontSize != mFontSize) {
mFontSize = pFontSize;
mOwner->updateLegend();
}
}
//==============================================================================
void GraphPanelPlotLegendWidget::setForegroundColor(const QColor &pForegroundColor)
{
// Set our foreground color
if (pForegroundColor != mForegroundColor) {
mForegroundColor = pForegroundColor;
mOwner->updateLegend();
}
}
//==============================================================================
void GraphPanelPlotLegendWidget::renderLegend(QPainter *pPainter,
const QRectF &pRect,
bool pFillBackground) const
{
// Render our legend
// Note: this is based on QwtLegend::renderLegend() and it fixes a problem
// with the layout when it has its expanding directions set to
// horizontal. More information on this issue can be found at
// http://www.qtcentre.org/threads/68983-Problem-with-QwtDynGridLayout-layoutItems()...
if (!mActive)
return;
if (pFillBackground) {
if (autoFillBackground() || testAttribute(Qt::WA_StyledBackground))
QwtPainter::drawBackgound(pPainter, pRect, this);
}
int top;
int left;
int bottom;
int right;
getContentsMargins(&left, &top, &right, &bottom);
QRect layoutRect = QRect();
layoutRect.setTop(qCeil(pRect.top())+top);
layoutRect.setLeft(qCeil(pRect.left())+left);
layoutRect.setBottom(qFloor(pRect.bottom())-bottom);
layoutRect.setRight(qFloor(pRect.right())-right);
QwtDynGridLayout *legendLayout = static_cast<QwtDynGridLayout *>(contentsWidget()->layout());
QList<QRect> itemRects = legendLayout->layoutItems(layoutRect, legendLayout->columnsForWidth(layoutRect.width()));
if (legendLayout->expandingDirections() & Qt::Horizontal) {
for (int i = 0, iMax = itemRects.size(); i < iMax; ++i)
itemRects[i].adjust(layoutRect.left(), 0, layoutRect.left(), 0);
}
int index = -1;
for (int i = 0, iMax = legendLayout->count(); i < iMax; ++i) {
QWidget *widget = legendLayout->itemAt(i)->widget();
if (widget) {
++index;
pPainter->save();
pPainter->setClipRect(itemRects[index], Qt::IntersectClip);
renderItem(pPainter, widget, itemRects[index], pFillBackground);
pPainter->restore();
}
}
}
//==============================================================================
QSize GraphPanelPlotLegendWidget::sizeHint() const
{
// Determine and return our size hint, based on our active state and on the
// 'raw' size hint of our owner's neigbours' legend, if any
QSize res = mActive?
QwtLegend::isEmpty()?
QSize(0, 0):
QwtLegend::sizeHint():
QSize(0, 0);
foreach (GraphPanelPlotWidget *ownerNeighbor, mOwner->neighbors()) {
GraphPanelPlotLegendWidget *legend = static_cast<GraphPanelPlotLegendWidget *>(ownerNeighbor->legend());
if (legend->isActive())
res.setWidth(qMax(res.width(), legend->QwtLegend::sizeHint().width()));
}
return res;
}
//==============================================================================
void GraphPanelPlotLegendWidget::updateWidget(QWidget *pWidget,
const QwtLegendData &pLegendData)
{
// Default handling
QwtLegend::updateWidget(pWidget, pLegendData);
// Update our visible state
QwtLegendLabel *legendLabel = static_cast<QwtLegendLabel *>(pWidget);
legendLabel->setVisible(mActive);
// Update our font size and foreground colour
QFont newFont = legendLabel->font();
newFont.setPointSize(mFontSize);
legendLabel->setFont(newFont);
QPalette newPalette = legendLabel->palette();
newPalette.setColor(QPalette::Text, mForegroundColor);
legendLabel->setPalette(newPalette);
// Make sure that the width of our owner's neighbours' legend is the same as
// ours
int legendWidth = sizeHint().width();
foreach (GraphPanelPlotWidget *ownerNeighbor, mOwner->neighbors())
ownerNeighbor->setLegendWidth(legendWidth);
// Make sure that updates are enabled
// Note: indeed, when setting its data, QwtLegendLabel (which used by
// QwtLegend) disables itself from updating, and then reenables itself
// if it was originally enabled. Now, the problem is that if one of
// our ancestors decides to temporarily disable updates (e.g. our
// simulation experiment view) then QwtLegendLabel won't reenable
// itself, which means that the legend may not actually be visible in
// some cases (e.g. when opening a SED-ML file)...
pWidget->setUpdatesEnabled(true);
}
//==============================================================================
void GraphPanelPlotLegendWidget::checked(const QVariant &pItemInfo)
{
// Make sure that the corresponding graph panel is selected
// Note: this makes it much easier to handle the graphToggled() signal
// afterwards...
mOwner->setActive(true);
// Let people know that the given graph has been toggled
emit graphToggled(static_cast<GraphPanelPlotGraph *>(mOwner->infoToItem(pItemInfo)));
}
//==============================================================================
static const double DblMinAxis = 1000.0*DBL_MIN;
// Note: normally, we would use DBL_MIN, but this would cause problems with
// QwtPlot (e.g. to create ticks), so instead we use a value that results
// in a range that we know works...
static const double DblMaxAxis = 0.3*DBL_MAX;
// Note: normally, we would use DBL_MAX, but this means that our maximum axis
// range would be 2*DBL_MAX, which would cause problems with QwtPlot (e.g.
// to create ticks), so instead we use a value that results in a range
// that we know works...
static const double MinAxis = -DblMaxAxis;
static const double MinLogAxis = DblMinAxis ;
static const double MaxAxis = DblMaxAxis;
static const double MaxAxisRange = MaxAxis-MinAxis;
static const double MaxLogAxisRange = MaxAxis-MinLogAxis;
static const double MinAxisRange = DblMinAxis;
//==============================================================================
GraphPanelPlotWidget::GraphPanelPlotWidget(const GraphPanelPlotWidgets &pNeighbors,
QAction *pSynchronizeXAxisAction,
QAction *pSynchronizeYAxisAction,
GraphPanelWidget *pParent) :
QwtPlot(pParent),
Core::CommonWidget(this),
mOwner(pParent),
mBackgroundColor(QColor()),
mPointCoordinatesStyle(Qt::DashLine),
mPointCoordinatesWidth(1),
mPointCoordinatesColor(QColor()),
mPointCoordinatesFontColor(Qt::white),
mSurroundingAreaBackgroundColor(QColor()),
mSurroundingAreaForegroundColor(QColor()),
mZoomRegionStyle(Qt::SolidLine),
mZoomRegionWidth(1),
mZoomRegionColor(QColor()),
mZoomRegionFontColor(Qt::white),
mZoomRegionFilled(true),
mZoomRegionFillColor(QColor()),
mLogAxisX(false),
mLogAxisY(false),
mGraphs(GraphPanelPlotGraphs()),
mAction(None),
mOriginPoint(QPoint()),
mPoint(QPoint()),
mLegend(new GraphPanelPlotLegendWidget(this)),
mCanDirectPaint(true),
mCanReplot(true),
mCanZoomInX(true),
mCanZoomOutX(true),
mCanZoomInY(true),
mCanZoomOutY(true),
mNeedContextMenu(false),
mCanUpdateActions(true),
mSynchronizeXAxisAction(pSynchronizeXAxisAction),
mSynchronizeYAxisAction(pSynchronizeYAxisAction),
mDefaultMinX(DefaultMinAxis),
mDefaultMaxX(DefaultMaxAxis),
mDefaultMinY(DefaultMinAxis),
mDefaultMaxY(DefaultMaxAxis),
mDefaultMinLogX(DefaultMinLogAxis),
mDefaultMaxLogX(DefaultMaxAxis),
mDefaultMinLogY(DefaultMinLogAxis),
mDefaultMaxLogY(DefaultMaxAxis),
mNeighbors(pNeighbors)
{
// Keep track of when our grand parent (i.e. a GraphPanelsWidget object)
// gets destroyed
// Note: indeed, when that happens, all of its children (i.e.
// GraphPanelWidget objects) will tell their plot's neighbours (i.e.
// objects like this one) that they are not going to be their
// neighbour anymore (see removeNeighbor()). This will, in turn,
// involve updating our actions (see updateActions()), something that
// we cannot do when our grand parent gets destroyed...
connect(pParent->parent(), SIGNAL(destroyed(QObject *)),
this, SLOT(cannotUpdateActions()));
// Get ourselves a direct painter