Repository navigation
Expand file tree
/
Copy pathcore.cpp
More file actions
2875 lines (2409 loc) · 102 KB
/
Copy pathcore.cpp
File metadata and controls
2875 lines (2409 loc) · 102 KB
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
/***************************************************************************
** **
** QCustomPlot, a simple to use, modern plotting widget for Qt **
** Copyright (C) 2011, 2012 Emanuel Eichhammer **
** **
** 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 3 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, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Emanuel Eichhammer **
** Website/Contact: http://www.WorksLikeClockwork.com/ **
** Date: 09.06.12 **
****************************************************************************/
/*! \file */
#include "core.h"
#include "painter.h"
#include "layer.h"
#include "axis.h"
#include "legend.h"
#include "plottable.h"
#include "item.h"
#include "plottables/plottable-graph.h"
/*! \mainpage %QCustomPlot Documentation
Below is a brief overview of and guide to the classes and their relations. If you are new to
QCustomPlot and just want to start using it, it's recommended to look at the examples/tutorials
at
http://www.WorksLikeClockWork.com/index.php/components/qt-plotting-widget
This documentation is especially helpful when you're familiar with the basic concept of how to use
%QCustomPlot and you wish to learn more about specific functionality.
\section simpleoverview Simplified Class Overview
\image latex ClassesOverviewSimplified.png "" width=1.2\textwidth
\image html ClassesOverviewSimplified.png
<center>Simplified diagram of most important classes, view the \ref classoverview "Class Overview" to see a full overview.</center>
The central widget which displays the plottables and axes on its surface is QCustomPlot. Usually,
you don't create the axes yourself, but you use the ones already inside every QCustomPlot
instance (xAxis, yAxis, xAxis2, yAxis2).
\section plottables Plottables
\a Plottables are classes that display any kind of data inside the QCustomPlot. They all derive
from QCPAbstractPlottable. For example, the QCPGraph class is a plottable that displays a graph
inside the plot with different line styles, scatter styles, filling etc.
Since plotting graphs is such a dominant use case, QCustomPlot has a special interface for working
with QCPGraph plottables, that makes it very easy to handle them:\n
You create a new graph with QCustomPlot::addGraph and access them with QCustomPlot::graph.
For all other plottables, you need to use the normal plottable interface:\n
First, you create an instance of the plottable you want, e.g.
\code
QCPCurve *newCurve = new QCPCurve(customPlot->xAxis, customPlot->yAxis);\endcode
add it to the customPlot with QCustomPlot::addPlottable:
\code
customPlot->addPlottable(newCurve);\endcode
and then modify the properties of the newly created plottable via <tt>newCurve</tt>.
Plottables (including graphs) can be retrieved via QCustomPlot::plottable. Since the return type
of that function is the abstract base class of all plottables, QCPAbstractPlottable, you will
probably want to qobject_cast (or dynamic_cast) the returned pointer to the respective plottable
subclass. (As usual, if the cast returns zero, the plottable wasn't of that specific subclass.)
All further interfacing with plottables (e.g how to set data) is specific to the plottable type.
See the documentations of the subclasses: QCPGraph, QCPCurve, QCPBars, QCPStatisticalBox.
\section axes Controlling the Axes
As mentioned, QCustomPlot has four axes by default: \a xAxis (bottom), \a yAxis (left), \a xAxis2
(top), \a yAxis2 (right).
Their range is handled by the simple QCPRange class. You can set the range with the
QCPAxis::setRange function. By default, the axes represent a linear scale. To set a logarithmic
scale, set QCPAxis::setScaleType to QCPAxis::stLogarithmic. The logarithm base can be set freely
with QCPAxis::setScaleLogBase.
By default, an axis automatically creates and labels ticks in a sensible manner, i.e. with a tick
interval that's pleasing to the viewer. See the following functions for tick manipulation:\n
QCPAxis::setTicks, QCPAxis::setAutoTicks, QCPAxis::setAutoTickCount, QCPAxis::setAutoTickStep,
QCPAxis::setTickLabels, QCPAxis::setTickLabelType, QCPAxis::setTickLabelRotation,
QCPAxis::setTickStep, QCPAxis::setTickLength,...
Each axis can be given an axis label (e.g. "Voltage [mV]") with QCPAxis::setLabel.
The distance of an axis backbone to the respective QCustomPlot widget border is called its margin.
Normally, the margins are calculated automatically. To change this, set QCustomPlot::setAutoMargin
to false and set the margins manually with QCustomPlot::setMargin.
\section legend Plot Legend
Every QCustomPlot owns a QCPLegend (as \a legend). That's a small window inside the plot which
lists the plottables with an icon of the plottable line/symbol and a description. The Description
is retrieved from the plottable name (QCPAbstractPlottable::setName). Plottables can be added and
removed from the legend via \ref QCPAbstractPlottable::addToLegend and \ref
QCPAbstractPlottable::removeFromLegend. By default, adding a plottable to QCustomPlot
automatically adds it to the legend, too. This behaviour can be modified with the
QCustomPlot::setAutoAddPlottableToLegend property.
The QCPLegend provides an interface to access, add and remove legend items directly, too. See
QCPLegend::item, QCPLegend::itemWithPlottable, QCPLegend::addItem, QCPLegend::removeItem for
example.
\section userinteraction User Interactions
QCustomPlot currently supports dragging axis ranges with the mouse (\ref
QCustomPlot::setRangeDrag), zooming axis ranges with the mouse wheel (\ref
QCustomPlot::setRangeZoom) and a complete selection mechanism of most objects.
The availability of these interactions is controlled with \ref QCustomPlot::setInteractions. For
details about the interaction system, see the documentation there.
Further, QCustomPlot always emits corresponding signals, when objects are clicked or
doubleClicked. See \ref QCustomPlot::plottableClick, \ref QCustomPlot::plottableDoubleClick
and \ref QCustomPlot::axisClick for example.
\section items Items
Apart from plottables there is another category of plot objects that are important: Items. The
base class of all items is QCPAbstractItem. An item sets itself apart from plottables in that
it's not necessarily bound to any axes. This means it may also be positioned in absolute pixel
coordinates or placed at a relative position on the axis rect. Further it usually doesn't
represent data directly but acts as decoration, emphasis, description etc.
Multiple items can be arranged in a parent-child-hierarchy allowing for dynamical behaviour. For
example, you could place the head of an arrow at a certain plot coordinate, so it always points
to some important part of your data. The tail of the arrow can be fixed at a text label item
which always resides in the top center of the axis rect (independent of where the user drags the
axis ranges).
For a more detailed introduction, see the QCPAbstractItem documentation, and from there the
documentations of the individual built-in items, to find out how to use them.
\section performancetweaks Performance Tweaks
Although QCustomPlot is quite fast, some features like semi-transparent fills and antialiasing
can cause a significant slow down. Here are some thoughts on how to increase performance. By far
the most time is spent in the drawing functions, specifically the drawing of graphs. For maximum
performance, consider the following (most recommended/effective measures first):
\li use Qt 4.8.0 and up. Performance has doubled or tripled with respect to Qt 4.7.4. However they broke QPainter,
drawing pixel precise things, e.g. scatters, isn't possible with Qt 4.8.0/1. So it's a performance vs. plot
quality tradeoff when switching to Qt 4.8.
\li To increase responsiveness during dragging, consider setting \ref QCustomPlot::setNoAntialiasingOnDrag to true.
\li On X11 (linux), avoid the (slow) native drawing system, use raster by supplying
"-graphicssystem raster" as command line argument or calling QApplication::setGraphicsSystem("raster")
before creating the QApplication object.
\li On all operating systems, use OpenGL hardware acceleration by supplying "-graphicssystem
opengl" as command line argument or calling QApplication::setGraphicsSystem("opengl"). If OpenGL
is available, this will slightly decrease the quality of antialiasing, but extremely increase
performance especially with alpha (semi-transparent) fills, much antialiasing and a large
QCustomPlot drawing surface. Note however, that the maximum frame rate might be constrained by
the vertical sync frequency of your monitor (VSync can be disabled in the graphics card driver
configuration). So for simple plots (where the potential framerate is far above 60 frames per
second), OpenGL acceleration might achieve numerically lower frame rates than the other
graphics systems, because they are not capped at the VSync frequency.
\li Avoid any kind of alpha (transparency), especially in fills
\li Avoid any kind of antialiasing, especially in graph lines (see QCustomPlot::setNotAntialiasedElements)
\li Avoid repeatedly setting the complete data set with QCPGraph::setData. Use QCPGraph::addData instead, if most
data points stay unchanged, e.g. in a running measurement.
\li Set the \a copy parameter of the setData functions to false, so only pointers get
transferred. (Relevant only if preparing data maps with a large number of points, i.e. over 10000)
*/
/*! \page classoverview Class Overview
\image latex ClassesOverview.png "Overview of all classes and their relations" width=1.2\textwidth
\image html ClassesOverview.png "Overview of all classes and their relations"
*/
// ================================================================================
// =================== QCustomPlot
// ================================================================================
/*! \class QCustomPlot
\brief The central class of the library, the QWidget which displays the plot and interacts with the user.
For tutorials on how to use QCustomPlot, see the website\n
http://www.WorksLikeClockWork.com/index.php/components/qt-plotting-widget
*/
/* start of documentation of inline functions */
/*! \fn QRect QCustomPlot::viewport() const
Returns the viewport rect of this QCustomPlot instance. The viewport is the area the plot is
drawn in, all mechanisms, e.g. margin caluclation take the viewport to be the outer border of the
plot. The viewport normally is the rect() of the QCustomPlot widget, i.e. a rect with top left
(0, 0) and size of the QCustomPlot widget.
Don't confuse the viewport with the axisRect. An axisRect is the rect defined by two axes, where
the graphs/plottables are drawn in. The viewport is larger and contains also the axes themselves, their
tick numbers, their labels, the plot title etc.
Only when saving to a file (see \ref savePng, savePdf etc.) the viewport is temporarily modified
to allow saving plots with sizes independent of the current widget size.
*/
/* end of documentation of inline functions */
/* start of documentation of signals */
/*! \fn void QCustomPlot::mouseDoubleClick(QMouseEvent *event)
This signal is emitted when the QCustomPlot receives a mouse double click event.
*/
/*! \fn void QCustomPlot::mousePress(QMouseEvent *event)
This signal is emitted when the QCustomPlot receives a mouse press event.
It is emitted before the QCustomPlot handles its range dragging mechanism, so a slot connected to
this signal can still influence the behaviour e.g. with \ref setRangeDrag or \ref
setRangeDragAxes.
*/
/*! \fn void QCustomPlot::mouseMove(QMouseEvent *event)
This signal is emitted when the QCustomPlot receives a mouse move event.
It is emitted before the QCustomPlot handles its range dragging mechanism, so a slot connected to
this signal can still influence the behaviour e.g. with \ref setRangeDrag.
\warning It is discouraged to change the drag-axes with \ref setRangeDragAxes here, because the
dragging starting point was saved the moment the mouse was pressed. Thus it only has a sensible
meaning for the range drag axes that were set at that moment. If you want to change the drag
axes, consider doing this in the \ref mousePress signal instead.
*/
/*! \fn void QCustomPlot::mouseRelease(QMouseEvent *event)
This signal is emitted when the QCustomPlot receives a mouse release event.
It is emitted before the QCustomPlot handles its selection mechanism, so a slot connected to this
signal can still influence the behaviour e.g. with \ref setInteractions or \ref
QCPAbstractPlottable::setSelectable.
*/
/*! \fn void QCustomPlot::mouseWheel(QMouseEvent *event)
This signal is emitted when the QCustomPlot receives a mouse wheel event.
It is emitted before the QCustomPlot handles its range zooming mechanism, so a slot connected to
this signal can still influence the behaviour e.g. with \ref setRangeZoom, \ref setRangeZoomAxes
or \ref setRangeZoomFactor.
*/
/*! \fn void QCustomPlot::plottableClick(QCPAbstractPlottable *plottable, QMouseEvent *event)
This signal is emitted when a plottable is clicked.
\a event is the mouse event that caused the click and \a plottable is the plottable that received
the click.
\see plottableDoubleClick
*/
/*! \fn void QCustomPlot::plottableDoubleClick(QCPAbstractPlottable *plottable, QMouseEvent *event)
This signal is emitted when a plottable is double clicked.
\a event is the mouse event that caused the click and \a plottable is the plottable that received
the click.
\see plottableClick
*/
/*! \fn void QCustomPlot::itemClick(QCPAbstractItem *item, QMouseEvent *event)
This signal is emitted when an item is clicked.
\a event is the mouse event that caused the click and \a item is the item that received the
click.
\see itemDoubleClick
*/
/*! \fn void QCustomPlot::itemDoubleClick(QCPAbstractItem *item, QMouseEvent *event)
This signal is emitted when an item is double clicked.
\a event is the mouse event that caused the click and \a item is the item that received the
click.
\see itemClick
*/
/*! \fn void QCustomPlot::axisClick(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event)
This signal is emitted when an axis is clicked.
\a event is the mouse event that caused the click, \a axis is the axis that received the click and
\a part indicates the part of the axis that was clicked.
\see axisDoubleClick
*/
/*! \fn void QCustomPlot::axisDoubleClick(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event)
This signal is emitted when an axis is double clicked.
\a event is the mouse event that caused the click, \a axis is the axis that received the click and
\a part indicates the part of the axis that was clicked.
\see axisClick
*/
/*! \fn void QCustomPlot::legendClick(QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event)
This signal is emitted when a legend (item) is clicked.
\a event is the mouse event that caused the click, \a legend is the legend that received the
click and \a item is the legend item that received the click. If only the legend and no item is
clicked, \a item is 0 (e.g. a click inside the legend padding, which is not part of any item).
\see legendDoubleClick
*/
/*! \fn void QCustomPlot::legendDoubleClick(QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event)
This signal is emitted when a legend (item) is double clicked.
\a event is the mouse event that caused the click, \a legend is the legend that received the
click and \a item is the legend item that received the click. If only the legend and no item is
clicked, \a item is 0 (e.g. a click inside the legend padding, which is not part of any item).
\see legendClick
*/
/*! \fn void QCustomPlot:: titleClick(QMouseEvent *event)
This signal is emitted when the plot title is clicked.
\a event is the mouse event that caused the click.
\see titleDoubleClick
*/
/*! \fn void QCustomPlot::titleDoubleClick(QMouseEvent *event)
This signal is emitted when the plot title is double clicked.
\a event is the mouse event that caused the click.
\see titleClick
*/
/*! \fn void QCustomPlot::selectionChangedByUser()
This signal is emitted after the user has changed the selection in the QCustomPlot, e.g. by
clicking. It is not emitted, when the selection state of an object has changed programmatically,
e.g. by a direct call to setSelected() on a plottable or by calling \ref deselectAll.
See the documentation of \ref setInteractions for how to find out which objects are currently
selected.
\see setInteractions, QCPAbstractPlottable::selectionChanged, QCPAxis::selectionChanged
*/
/*! \fn void QCustomPlot::beforeReplot()
This signal is emitted immediately before a replot takes place (caused by a call to the slot \ref
replot).
It is safe to mutually connect the replot slot with this signal on two QCustomPlots to make them
replot synchronously (i.e. it won't cause an infinite recursion).
\see replot, afterReplot
*/
/*! \fn void QCustomPlot::afterReplot()
This signal is emitted immediately after a replot has taken place (caused by a call to the slot \ref
replot).
It is safe to mutually connect the replot slot with this signal on two QCustomPlots to make them
replot synchronously (i.e. it won't cause an infinite recursion).
\see replot, beforeReplot
*/
/* end of documentation of signals */
/*!
Constructs a QCustomPlot and sets reasonable default values.
Four axes are created at the bottom, left, top and right sides (xAxis, yAxis, xAxis2, yAxis2),
however, only the bottom and left axes are set to be visible.
The legend is also set to be invisible initially.
*/
QCustomPlot::QCustomPlot(QWidget *parent) :
QWidget(parent),
mDragging(false),
mReplotting(false),
mPlottingHints(QCP::phCacheLabels)
{
setAttribute(Qt::WA_NoMousePropagation);
setAttribute(Qt::WA_OpaquePaintEvent);
setMouseTracking(true);
QLocale currentLocale = locale();
currentLocale.setNumberOptions(QLocale::OmitGroupSeparator);
setLocale(currentLocale);
// create very first layers:
QCPLayer *gridLayer = new QCPLayer(this, "grid");
QCPLayer *mainLayer = new QCPLayer(this, "main");
QCPLayer *axesLayer = new QCPLayer(this, "axes");
mLayers.append(gridLayer);
mLayers.append(mainLayer);
mLayers.append(axesLayer);
setCurrentLayer(mainLayer);
mPaintBuffer = QPixmap(size());
legend = new QCPLegend(this);
legend->setVisible(false);
legend->setLayer(axesLayer);
xAxis = new QCPAxis(this, QCPAxis::atBottom);
yAxis = new QCPAxis(this, QCPAxis::atLeft);
xAxis2 = new QCPAxis(this, QCPAxis::atTop);
yAxis2 = new QCPAxis(this, QCPAxis::atRight);
xAxis2->setGrid(false);
yAxis2->setGrid(false);
xAxis2->setZeroLinePen(Qt::NoPen);
yAxis2->setZeroLinePen(Qt::NoPen);
xAxis2->setVisible(false);
yAxis2->setVisible(false);
xAxis->setLayer(axesLayer);
yAxis->setLayer(axesLayer);
xAxis2->setLayer(axesLayer);
yAxis2->setLayer(axesLayer);
xAxis->mGrid->setLayer(gridLayer);
yAxis->mGrid->setLayer(gridLayer);
xAxis2->mGrid->setLayer(gridLayer);
yAxis2->mGrid->setLayer(gridLayer);
mViewport = rect();
setNoAntialiasingOnDrag(false);
setAutoAddPlottableToLegend(true);
setAxisBackgroundScaled(true);
setAxisBackgroundScaledMode(Qt::KeepAspectRatioByExpanding);
setTitleFont(QFont(font().family(), 14, QFont::Bold));
setTitleColor(Qt::black);
setSelectedTitleFont(QFont(font().family(), 14, QFont::Bold));
setSelectedTitleColor(Qt::blue);
setTitleSelected(false);
setTitle("");
setColor(Qt::white);
#ifdef Q_WS_WIN
setPlottingHint(QCP::phForceRepaint);
#endif
setAntialiasedElements(QCP::aeNone);
setNotAntialiasedElements(QCP::aeNone);
setInteractions(iRangeDrag|iRangeZoom);
setMultiSelectModifier(Qt::ControlModifier);
setRangeDragAxes(xAxis, yAxis);
setRangeZoomAxes(xAxis, yAxis);
setRangeDrag(0);
setRangeZoom(0);
setRangeZoomFactor(0.85);
setSelectionTolerance(8);
setMargin(0, 0, 0, 0); // also initializes the mAxisRect
setAutoMargin(true);
replot();
}
QCustomPlot::~QCustomPlot()
{
clearPlottables();
clearItems();
delete legend;
delete xAxis;
delete yAxis;
delete xAxis2;
delete yAxis2;
qDeleteAll(mLayers);
mLayers.clear();
}
/*!
Returns the range drag axis of the \a orientation provided
\see setRangeDragAxes
*/
QCPAxis *QCustomPlot::rangeDragAxis(Qt::Orientation orientation)
{
return (orientation == Qt::Horizontal ? mRangeDragHorzAxis : mRangeDragVertAxis);
}
/*!
Returns the range zoom axis of the \a orientation provided
\see setRangeZoomAxes
*/
QCPAxis *QCustomPlot::rangeZoomAxis(Qt::Orientation orientation)
{
return (orientation == Qt::Horizontal ? mRangeZoomHorzAxis : mRangeZoomVertAxis);
}
/*!
Returns the range zoom factor of the \a orientation provided
\see setRangeZoomFactor
*/
double QCustomPlot::rangeZoomFactor(Qt::Orientation orientation)
{
return (orientation == Qt::Horizontal ? mRangeZoomFactorHorz : mRangeZoomFactorVert);
}
/*!
Sets the plot title which will be drawn centered at the top of the widget.
The title position is not dependant on the actual position of the axes. However, if
\ref setAutoMargin is set to true, the top margin will be adjusted appropriately,
so the top axis labels/tick labels will not overlap with the title.
\see setTitleFont, setTitleColor
*/
void QCustomPlot::setTitle(const QString &title)
{
mTitle = title;
}
/*!
Sets the font of the plot title
\see setTitleColor, setTitle
*/
void QCustomPlot::setTitleFont(const QFont &font)
{
mTitleFont = font;
}
/*!
Sets the text color of the plot title
\see setTitleFont, setTitle
*/
void QCustomPlot::setTitleColor(const QColor &color)
{
mTitleColor = color;
}
/*!
An alternative way to set the margins, by directly setting the wanted axis rect. The rect
will be translated into appropriate margin values.
\warning Setting the axis rect with this function does not guarantee that the axis rect will stay
like this indefinitely. In QCustomPlot, margins are the fixed values (if \ref setAutoMargin is
false). Hence the axis rect is automatically changed when the widget size changes, but the
margins (distances between axis rect sides and widget/viewport rect sides) stay the same.
\see setMargin
*/
void QCustomPlot::setAxisRect(const QRect &arect)
{
mMarginLeft = arect.left()-mViewport.left();
mMarginRight = mViewport.right()-arect.right();
mMarginTop = arect.top()-mViewport.top();
mMarginBottom = mViewport.bottom()-arect.bottom();
updateAxisRect();
}
/*!
Sets the left margin manually. Will only have effect, if \ref setAutoMargin is set to false.
see \ref setMargin for an explanation of what margins mean in QCustomPlot.
*/
void QCustomPlot::setMarginLeft(int margin)
{
mMarginLeft = margin;
updateAxisRect();
}
/*!
Sets the right margin manually. Will only have effect, if \ref setAutoMargin is set to false.
see \ref setMargin for an explanation of what margins mean in QCustomPlot.
*/
void QCustomPlot::setMarginRight(int margin)
{
mMarginRight = margin;
updateAxisRect();
}
/*!
Sets the top margin manually. Will only have effect, if \ref setAutoMargin is set to false.
see \ref setMargin for an explanation of what margins mean in QCustomPlot.
*/
void QCustomPlot::setMarginTop(int margin)
{
mMarginTop = margin;
updateAxisRect();
}
/*!
Sets the bottom margin manually. Will only have effect, if \ref setAutoMargin is set to false.
see \ref setMargin for an explanation of what margins mean in QCustomPlot.
*/
void QCustomPlot::setMarginBottom(int margin)
{
mMarginBottom = margin;
updateAxisRect();
}
/*!
Sets the margins manually. Will only have effect, if \ref setAutoMargin is set to false.
The margins are the distances in pixels between the axes box and the viewport box.
The viewport box normally is the entire QCustomPlot widget or the entire image, if
using one of the export functions. Positive margin values always mean the axes box
is shrinked, going inward from the sides of the viewport box.
*/
void QCustomPlot::setMargin(int left, int right, int top, int bottom)
{
mMarginLeft = left;
mMarginRight = right;
mMarginTop = top;
mMarginBottom = bottom;
updateAxisRect();
}
/*!
Sets whether the margins are calculated automatically depeding on the sizes
of the tick labels, axis labels, paddings etc.
If disabled, the margins must be set manually with the \a setMargin functions.
\see setMargin, QCPAxis::setLabelPadding, QCPAxis::setTickLabelPadding
*/
void QCustomPlot::setAutoMargin(bool enabled)
{
mAutoMargin = enabled;
}
/*!
Sets the background color of the QCustomPlot widget.
*/
void QCustomPlot::setColor(const QColor &color)
{
mColor = color;
}
/*!
Sets which axis orientation may be range dragged by the user with mouse interaction.
What orientation corresponds to which specific axis can be set with
\ref setRangeDragAxes(QCPAxis *horizontal, QCPAxis *vertical). By
default, the horizontal axis is the bottom axis (xAxis) and the vertical axis
is the left axis (yAxis).
To disable range dragging entirely, pass 0 as \a orientations or remove \ref iRangeDrag from \ref
setInteractions. To enable range dragging for both directions, pass <tt>Qt::Horizontal |
Qt::Vertical</tt> as \a orientations.
In addition to setting \a orientations to a non-zero value, make sure \ref setInteractions
contains \ref iRangeDrag to enable the range dragging interaction.
\see setRangeZoom, setRangeDragAxes, setNoAntialiasingOnDrag
*/
void QCustomPlot::setRangeDrag(Qt::Orientations orientations)
{
mRangeDrag = orientations;
}
/*!
Sets which axis orientation may be zoomed by the user with the mouse wheel. What orientation
corresponds to which specific axis can be set with \ref setRangeZoomAxes(QCPAxis *horizontal,
QCPAxis *vertical). By default, the horizontal axis is the bottom axis (xAxis) and the vertical
axis is the left axis (yAxis).
To disable range zooming entirely, pass 0 as \a orientations or remove \ref iRangeZoom from \ref
setInteractions. To enable range zooming for both directions, pass <tt>Qt::Horizontal |
Qt::Vertical</tt> as \a orientations.
In addition to setting \a orientations to a non-zero value, make sure \ref setInteractions
contains \ref iRangeZoom to enable the range zooming interaction.
\see setRangeZoomFactor, setRangeZoomAxes, setRangeDrag
*/
void QCustomPlot::setRangeZoom(Qt::Orientations orientations)
{
mRangeZoom = orientations;
}
/*!
Sets the axes whose range will be dragged when \ref setRangeDrag enables mouse range dragging
on the QCustomPlot widget.
\see setRangeZoomAxes
*/
void QCustomPlot::setRangeDragAxes(QCPAxis *horizontal, QCPAxis *vertical)
{
if (horizontal)
mRangeDragHorzAxis = horizontal;
if (vertical)
mRangeDragVertAxis = vertical;
}
/*!
Sets the axes whose range will be zoomed when \ref setRangeZoom enables mouse wheel zooming on the
QCustomPlot widget. The two axes can be zoomed with different strengths, when different factors
are passed to \ref setRangeZoomFactor(double horizontalFactor, double verticalFactor).
\see setRangeDragAxes
*/
void QCustomPlot::setRangeZoomAxes(QCPAxis *horizontal, QCPAxis *vertical)
{
if (horizontal)
mRangeZoomHorzAxis = horizontal;
if (vertical)
mRangeZoomVertAxis = vertical;
}
/*!
Sets how strong one rotation step of the mouse wheel zooms, when range zoom was activated with
\ref setRangeZoom. The two parameters \a horizontalFactor and \a verticalFactor provide a way to
let the horizontal axis zoom at different rates than the vertical axis. Which axis is horizontal
and which is vertical, can be set with \ref setRangeZoomAxes.
When the zoom factor is greater than one, scrolling the mouse wheel backwards (towards the user)
will zoom in (make the currently visible range smaller). For zoom factors smaller than one, the
same scrolling direction will zoom out.
*/
void QCustomPlot::setRangeZoomFactor(double horizontalFactor, double verticalFactor)
{
mRangeZoomFactorHorz = horizontalFactor;
mRangeZoomFactorVert = verticalFactor;
}
/*! \overload
Sets both the horizontal and vertical zoom \a factor.
*/
void QCustomPlot::setRangeZoomFactor(double factor)
{
mRangeZoomFactorHorz = factor;
mRangeZoomFactorVert = factor;
}
/*!
Sets which elements are forcibly drawn antialiased as an or combination of QCP::AntialiasedElement.
This overrides the antialiasing settings for whole element groups, normally controlled with the
\a setAntialiasing function on the individual elements. If an element is neither specified in
\ref setAntialiasedElements nor in \ref setNotAntialiasedElements, the antialiasing setting on
each individual element instance is used.
For example, if \a antialiasedElements contains \ref QCP::aePlottables, all plottables will be
drawn antialiased, no matter what the specific QCPAbstractPlottable::setAntialiased value was set
to.
\see setNotAntialiasedElements
*/
void QCustomPlot::setAntialiasedElements(const QCP::AntialiasedElements &antialiasedElements)
{
mAntialiasedElements = antialiasedElements;
// make sure elements aren't in mNotAntialiasedElements and mAntialiasedElements simultaneously:
if ((mNotAntialiasedElements & mAntialiasedElements) != 0)
mNotAntialiasedElements |= ~mAntialiasedElements;
}
/*!
Sets whether the specified \a antialiasedElement is forcibly drawn antialiased.
This overrides the antialiasing settings for whole element groups, normally controlled with the
\a setAntialiasing function on the individual elements. If an element is neither specified in
\ref setAntialiasedElements nor in \ref setNotAntialiasedElements, the antialiasing setting on
each individual element instance is used.
For example, if \a enabled is true and \a antialiasedElement is \ref QCP::aePlottables, all
plottables will be drawn antialiased, no matter what the specific
QCPAbstractPlottable::setAntialiased value was set to.
\see setNotAntialiasedElement
*/
void QCustomPlot::setAntialiasedElement(QCP::AntialiasedElement antialiasedElement, bool enabled)
{
if (!enabled && mAntialiasedElements.testFlag(antialiasedElement))
mAntialiasedElements &= ~antialiasedElement;
else if (enabled && !mAntialiasedElements.testFlag(antialiasedElement))
mAntialiasedElements |= antialiasedElement;
// make sure elements aren't in mNotAntialiasedElements and mAntialiasedElements simultaneously:
if ((mNotAntialiasedElements & mAntialiasedElements) != 0)
mNotAntialiasedElements |= ~mAntialiasedElements;
}
/*!
Sets which elements are forcibly drawn not antialiased as an or combination of
QCP::AntialiasedElement.
This overrides the antialiasing settings for whole element groups, normally controlled with the
\a setAntialiasing function on the individual elements. If an element is neither specified in
\ref setAntialiasedElements nor in \ref setNotAntialiasedElements, the antialiasing setting on
each individual element instance is used.
For example, if \a notAntialiasedElements contains \ref QCP::aePlottables, no plottables will be
drawn antialiased, no matter what the specific QCPAbstractPlottable::setAntialiased value was set
to.
if an element in \a notAntialiasedElements is already set in \ref setAntialiasedElements, it is
removed from there.
\see setAntialiasedElements
*/
void QCustomPlot::setNotAntialiasedElements(const QCP::AntialiasedElements ¬AntialiasedElements)
{
mNotAntialiasedElements = notAntialiasedElements;
// make sure elements aren't in mNotAntialiasedElements and mAntialiasedElements simultaneously:
if ((mNotAntialiasedElements & mAntialiasedElements) != 0)
mAntialiasedElements |= ~mNotAntialiasedElements;
}
/*!
Sets whether the specified \a notAntialiasedElement is forcibly drawn not antialiased.
This overrides the antialiasing settings for whole element groups, normally controlled with the
\a setAntialiasing function on the individual elements. If an element is neither specified in
\ref setAntialiasedElements nor in \ref setNotAntialiasedElements, the antialiasing setting on
each individual element instance is used.
For example, if \a enabled is true and \a notAntialiasedElement is \ref QCP::aePlottables, no
plottables will be drawn antialiased, no matter what the specific
QCPAbstractPlottable::setAntialiased value was set to.
if \a enabled is true and \a notAntialiasedElement is already set with \ref
setAntialiasedElement, it is removed from there.
\see setAntialiasedElement
*/
void QCustomPlot::setNotAntialiasedElement(QCP::AntialiasedElement notAntialiasedElement, bool enabled)
{
if (!enabled && mNotAntialiasedElements.testFlag(notAntialiasedElement))
mNotAntialiasedElements &= ~notAntialiasedElement;
else if (enabled && !mNotAntialiasedElements.testFlag(notAntialiasedElement))
mNotAntialiasedElements |= notAntialiasedElement;
// make sure elements aren't in mNotAntialiasedElements and mAntialiasedElements simultaneously:
if ((mNotAntialiasedElements & mAntialiasedElements) != 0)
mAntialiasedElements |= ~mNotAntialiasedElements;
}
/*!
If set to true, adding a plottable (e.g. a graph) to the QCustomPlot automatically also adds the
newly created plottable to the legend.
\see addPlottable, addGraph, QCPLegend::addItem
*/
void QCustomPlot::setAutoAddPlottableToLegend(bool on)
{
mAutoAddPlottableToLegend = on;
}
/*!
Sets \a pm as the axis background pixmap. The axis background pixmap will be drawn inside the current
axis rect, before anything else (e.g. the axes themselves, grids, graphs, etc.) is drawn.
If the provided pixmap doesn't have the same size as the axis rect, scaling can be enabled with \ref setAxisBackgroundScaled
and the scaling mode (i.e. whether and how the aspect ratio is preserved) can be set with \ref setAxisBackgroundScaledMode.
To set all these options in one call, consider using the overloaded version of this function.
\see setAxisBackgroundScaled, setAxisBackgroundScaledMode
*/
void QCustomPlot::setAxisBackground(const QPixmap &pm)
{
mAxisBackground = pm;
mScaledAxisBackground = QPixmap();
}
/*!
\overload
Allows setting the background pixmap, whether it shall be scaled and how it shall be scaled in one call.
\see setAxisBackground(const QPixmap &pm), setAxisBackgroundScaled, setAxisBackgroundScaledMode
*/
void QCustomPlot::setAxisBackground(const QPixmap &pm, bool scaled, Qt::AspectRatioMode mode)
{
mAxisBackground = pm;
mScaledAxisBackground = QPixmap();
mAxisBackgroundScaled = scaled;
mAxisBackgroundScaledMode = mode;
}
/*!
Sets whether the axis background pixmap shall be scaled to fit the current axis rect or not. If
\a scaled is set to true, you may control whether and how the aspect ratio of the original pixmap is
preserved with \ref setAxisBackgroundScaledMode.
Note that the scaled version of the original pixmap is buffered, so there is no performance penalty
on replots, when enabling the scaling. (Except of course, the axis rect is continuously
changed, but that's not very likely.)
\see setAxisBackground, setAxisBackgroundScaledMode
*/
void QCustomPlot::setAxisBackgroundScaled(bool scaled)
{
mAxisBackgroundScaled = scaled;
}
/*!
If scaling of the axis background pixmap is enabled (\ref setAxisBackgroundScaled), use this function to
define whether and how the aspect ratio of the original pixmap passed to \ref setAxisBackground is preserved.
\see setAxisBackground, setAxisBackgroundScaled
*/
void QCustomPlot::setAxisBackgroundScaledMode(Qt::AspectRatioMode mode)
{
mAxisBackgroundScaledMode = mode;
}
/*!
Sets the possible interactions of this QCustomPlot as an or-combination of \ref Interaction
enums. There are the following types of interactions:
<b>Axis range manipulation</b> is controlled via \ref iRangeDrag and \ref iRangeZoom. When the
respective interaction is enabled, the user may drag axes ranges and zoom with the mouse wheel.
For details how to control which axes the user may drag/zoom and in what orientations, see \ref
setRangeDrag, \ref setRangeZoom, \ref setRangeDragAxes, \ref setRangeZoomAxes.
<b>Plottable selection</b> is controlled by \ref iSelectPlottables. If \ref iSelectPlottables is
set, the user may select plottables (e.g. graphs, curves, bars,...) by clicking on them or in
their vicinity, see \ref setSelectionTolerance. Whether the user can actually select a plottable
can further be restricted with the \ref QCPAbstractPlottable::setSelectable function on the
specific plottable. To find out whether a specific plottable is selected, call
QCPAbstractPlottable::selected(). To retrieve a list of all currently selected plottables, call
\ref selectedPlottables. If you're only interested in QCPGraphs, you may use the convenience
function \ref selectedGraphs.
<b>Item selection</b> is controlled by \ref iSelectItems. If \ref iSelectItems is set, the user
may select items (e.g. QCPItemLine, QCPItemText,...) by clicking on them or in their vicinity. To
find out whether a specific item is selected, call QCPAbstractItem::selected(). To retrieve a
list of all currently selected items, call \ref selectedItems.
<b>Axis selection</b> is controlled with \ref iSelectAxes. If \ref iSelectAxes is set, the user
may select parts of the axes by clicking on them. What parts exactly (e.g. Axis base line, tick
labels, axis label) are selectable can be controlled via \ref QCPAxis::setSelectable for each
axis. To retrieve a list of all axes that currently contain selected parts, call \ref
selectedAxes. Which parts of an axis are selected, can be retrieved with QCPAxis::selected().
<b>Legend selection</b> is controlled with \ref iSelectLegend. If this is set, the user may
select the legend itself or individual items by clicking on them. What parts exactly are
selectable can be controlled via \ref QCPLegend::setSelectable. To find out whether the legend or
any child items are selected, check the value of QCPLegend::selected. To find out which child
items are selected, call \ref QCPLegend::selectedItems.
<b>Plot title selection</b> is controlled with \ref iSelectTitle. If set, the user may select the
plot title by clicking on it. To find out whether the title is currently selected, call
QCustomPlot::titleSelected().
If the selection state has changed by user interaction, the \ref selectionChangedByUser signal is
emitted. Each selectable object additionally emits an individual selectionChanged signal whenever
their selection state has changed, i.e. not only by user interaction.
To allow multiple objects to be selected by holding the modifier set with \ref
setMultiSelectModifier, set the flag \ref iMultiSelect.
\note In addition to the selection mechanism presented here, QCustomPlot always emits
corresponding signals, when an object is clicked or double clicked. see \ref plottableClick and
\ref plottableDoubleClick for example.
\see setInteraction, setSelectionTolerance
*/
void QCustomPlot::setInteractions(const Interactions &interactions)
{
mInteractions = interactions;
}
/*!
Sets the single \a interaction of this QCustomPlot to \a enabled.
For details about the interaction system, see \ref setInteractions.
\see setInteractions
*/
void QCustomPlot::setInteraction(const QCustomPlot::Interaction &interaction, bool enabled)
{
if (!enabled && mInteractions.testFlag(interaction))
mInteractions &= ~interaction;
else if (enabled && !mInteractions.testFlag(interaction))
mInteractions |= interaction;
}
/*!
Sets the tolerance that is used when deciding whether a click on the QCustomPlot surface selects
an object (e.g. a plottable) or not.
If for example the user clicks in the vicinity of the line of a QCPGraph, it's only regarded as a
potential selection when the minimum distance between the click position and the graph line is
smaller than \a pixels. Objects that are defined by an area (e.g. QCPBars) only react to clicks
directly inside the area and ignore this selection tolerance. In other words it only has meaning
for parts of objects that are too thin to exactly hit with a click and thus need such a
tolerance.
\see setInteractions, QCPAbstractPlottable::selectTest
*/