.Simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
qcustomplot.h
Go to the documentation of this file.
1 /***************************************************************************
2 ** **
3 ** QCustomPlot, a simple to use, modern plotting widget for Qt **
4 ** Copyright (C) 2012 Emanuel Eichhammer **
5 ** **
6 ** This program is free software: you can redistribute it and/or modify **
7 ** it under the terms of the GNU General Public License as published by **
8 ** the Free Software Foundation, either version 3 of the License, or **
9 ** (at your option) any later version. **
10 ** **
11 ** This program is distributed in the hope that it will be useful, **
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
14 ** GNU General Public License for more details. **
15 ** **
16 ** You should have received a copy of the GNU General Public License **
17 ** along with this program. If not, see http://www.gnu.org/licenses/. **
18 ** **
19 ****************************************************************************
20 ** Author: Emanuel Eichhammer **
21 ** Website/Contact: http://www.WorksLikeClockwork.com/ **
22 ** Date: 09.06.12 **
23 ****************************************************************************/
24 
27 #ifndef QCUSTOMPLOT_H
28 #define QCUSTOMPLOT_H
29 
30 #include <QObject>
31 #include <QWidget>
32 #include <QPainter>
33 #include <QPaintEvent>
34 #include <QPixmap>
35 #include <QVector>
36 #include <QString>
37 #include <QPrinter>
38 #include <QDateTime>
39 #include <QMultiMap>
40 #include <QFlags>
41 #include <QDebug>
42 #include <QVector2D>
43 #include <QStack>
44 #include <qmath.h>
45 #include <limits>
46 
47 namespace QCPlot {
48 
49 // decl definitions for shared library compilation/usage:
50 #if defined(QCUSTOMPLOT_COMPILE_LIBRARY)
51 # define QCP_LIB_DECL Q_DECL_EXPORT
52 #elif defined(QCUSTOMPLOT_USE_LIBRARY)
53 # define QCP_LIB_DECL Q_DECL_IMPORT
54 #else
55 # define QCP_LIB_DECL
56 #endif
57 
58  class QCustomPlot;
59  class QCPLegend;
60  class QCPRange;
61  class QCPLayerable;
62  class QCPAbstractItem;
63  class QCPItemPosition;
64  class QCPAxis;
65  class QCPData;
66 
70  namespace QCP
71  {
98  };
99 
110  enum AntialiasedElement { aeAxes = 0x0001
111  ,aeGrid = 0x0002
112  ,aeSubGrid = 0x0004
113  ,aeLegend = 0x0008
114  ,aeLegendItems = 0x0010
115  ,aePlottables = 0x0020
116  ,aeItems = 0x0040
117  ,aeScatters = 0x0080
118  ,aeErrorBars = 0x0100
119  ,aeFills = 0x0200
120  ,aeZeroLine = 0x0400
121  ,aeAll = 0xFFFF
122  ,aeNone = 0x0000
123  };
124  Q_DECLARE_FLAGS(AntialiasedElements, AntialiasedElement)
125 
126 
130  enum PlottingHint { phNone = 0x000
131  ,phFastPolylines = 0x001
132  ,phForceRepaint = 0x002
134  };
136  Q_DECLARE_FLAGS(PlottingHints, PlottingHint)
137  }
138 
139  Q_DECLARE_OPERATORS_FOR_FLAGS(QCP::AntialiasedElements)
140  Q_DECLARE_OPERATORS_FOR_FLAGS(QCP::PlottingHints)
141 
142  class QCP_LIB_DECL QCPData
143  {
144  public:
145  QCPData();
146  QCPData(double key, double value);
147  double key, value;
148  double keyErrorPlus, keyErrorMinus;
149  double valueErrorPlus, valueErrorMinus;
150  };
151 
159  typedef QMap<double, QCPData> QCPDataMap;
160  typedef QMapIterator<double, QCPData> QCPDataMapIterator;
161  typedef QMutableMapIterator<double, QCPData> QCPDataMutableMapIterator;
162 
163  class QCP_LIB_DECL QCPCurveData
164  {
165  public:
166  QCPCurveData();
167  QCPCurveData(double t, double key, double value);
168  double t, key, value;
169  };
170 
179  typedef QMap<double, QCPCurveData> QCPCurveDataMap;
180  typedef QMapIterator<double, QCPCurveData> QCPCurveDataMapIterator;
181  typedef QMutableMapIterator<double, QCPCurveData> QCPCurveDataMutableMapIterator;
182 
183  class QCP_LIB_DECL QCPBarData
184  {
185  public:
186  QCPBarData();
187  QCPBarData(double key, double value);
188  double key, value;
189  };
190 
198  typedef QMap<double, QCPBarData> QCPBarDataMap;
199  typedef QMapIterator<double, QCPBarData> QCPBarDataMapIterator;
200  typedef QMutableMapIterator<double, QCPBarData> QCPBarDataMutableMapIterator;
201 
202  class QCP_LIB_DECL QCPPainter : public QPainter
203  {
204  public:
205  QCPPainter();
206  QCPPainter(QPaintDevice *device);
207  ~QCPPainter();
208 
209  // getters:
210  QPixmap scatterPixmap() const { return mScatterPixmap; }
211  bool antialiasing() const { return testRenderHint(QPainter::Antialiasing); }
212  bool pdfExportMode() const { return mPdfExportMode; }
213  bool scaledExportMode() const { return mScaledExportMode; }
214 
215  // setters:
216  void setScatterPixmap(const QPixmap pm);
217  void setAntialiasing(bool enabled);
218  void setPdfExportMode(bool enabled);
219  void setScaledExportMode(bool enabled);
220 
221  // methods hiding non-virtual base class functions (QPainter bug workarounds):
222  void setPen(const QPen &pen);
223  void setPen(const QColor &color);
224  void setPen(Qt::PenStyle penStyle);
225  void drawLine(const QLineF &line);
226  void drawLine(const QPointF &p1, const QPointF &p2) {drawLine(QLineF(p1, p2));}
227  void save();
228  void restore();
229 
230  // helpers:
231  void fixScaledPen();
232  void drawScatter(double x, double y, double size, QCP::ScatterStyle style);
233 
234  protected:
235  QPixmap mScatterPixmap;
236  bool mScaledExportMode;
237  bool mPdfExportMode;
238  bool mIsAntialiasing;
239  QStack<bool> mAntialiasingStack;
240  };
241 
242  class QCP_LIB_DECL QCPLineEnding
243  {
244  public:
256  enum EndingStyle { esNone
257  ,esFlatArrow
258  ,esSpikeArrow
259  ,esLineArrow
260  ,esDisc
261  ,esSquare
262  ,esDiamond
263  ,esBar
264  };
265 
266  QCPLineEnding();
267  QCPLineEnding(EndingStyle style, double width=8, double length=10, bool inverted=false);
268 
269  // getters:
270  EndingStyle style() const { return mStyle; }
271  double width() const { return mWidth; }
272  double length() const { return mLength; }
273  bool inverted() const { return mInverted; }
274 
275  // setters:
276  void setStyle(EndingStyle style);
277  void setWidth(double width);
278  void setLength(double length);
279  void setInverted(bool inverted);
280 
281  // non-property methods:
282  double boundingDistance() const;
283  void draw(QCPPainter *painter, const QVector2D &pos, const QVector2D &dir) const;
284  void draw(QCPPainter *painter, const QVector2D &pos, double angle) const;
285 
286  protected:
287  EndingStyle mStyle;
288  double mWidth, mLength;
289  bool mInverted;
290  };
291 
292  class QCP_LIB_DECL QCPLayer
293  {
294  public:
295  QCPLayer(QCustomPlot* parentPlot, const QString &layerName);
296  ~QCPLayer();
297 
298  // getters:
299  QCustomPlot *parentPlot() const { return mParentPlot; }
300  QString name() const { return mName; }
301  int index() const;
302  QList<QCPLayerable*> children() const { return mChildren; }
303 
304  protected:
305  QCustomPlot *mParentPlot;
306  QString mName;
307  QList<QCPLayerable*> mChildren;
308 
309  void addChild(QCPLayerable *layerable, bool prepend);
310  void removeChild(QCPLayerable *layerable);
311 
312  private:
313  Q_DISABLE_COPY(QCPLayer)
314 
315  friend class QCPLayerable;
316  };
317 
318  class QCP_LIB_DECL QCPLayerable : public QObject
319  {
320  Q_OBJECT
321  public:
322  QCPLayerable(QCustomPlot *parentPlot);
323  ~QCPLayerable();
324 
325  // getters:
326  bool visible() const { return mVisible; }
327  QCustomPlot *parentPlot() const { return mParentPlot; }
328  QCPLayer *layer() const { return mLayer; }
329  bool antialiased() const { return mAntialiased; }
330 
331  // setters:
332  void setVisible(bool on);
333  bool setLayer(QCPLayer *layer);
334  bool setLayer(const QString &layerName);
335  void setAntialiased(bool enabled);
336 
337  protected:
338  bool mVisible;
339  QCustomPlot *mParentPlot;
340  QCPLayer *mLayer;
341  bool mAntialiased;
342 
343  // non-property methods:
344  bool moveToLayer(QCPLayer *layer, bool prepend);
345 
346  void applyAntialiasingHint(QCPPainter *painter, bool localAntialiased, QCP::AntialiasedElement overrideElement) const;
347  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const = 0;
348  virtual QRect clipRect() const;
349  virtual void draw(QCPPainter *painter) = 0;
350 
351  private:
352  Q_DISABLE_COPY(QCPLayerable)
353 
354  friend class QCustomPlot;
355  };
356 
357  class QCP_LIB_DECL QCPAbstractPlottable : public QCPLayerable
358  {
359  Q_OBJECT
360  public:
361  QCPAbstractPlottable(QCPAxis *keyAxis, QCPAxis *valueAxis);
362  virtual ~QCPAbstractPlottable() {}
363 
364  // getters:
365  QString name() const { return mName; }
366  bool antialiasedFill() const { return mAntialiasedFill; }
367  bool antialiasedScatters() const { return mAntialiasedScatters; }
368  bool antialiasedErrorBars() const { return mAntialiasedErrorBars; }
369  QPen pen() const { return mPen; }
370  QPen selectedPen() const { return mSelectedPen; }
371  QBrush brush() const { return mBrush; }
372  QBrush selectedBrush() const { return mSelectedBrush; }
373  QCPAxis *keyAxis() const { return mKeyAxis; }
374  QCPAxis *valueAxis() const { return mValueAxis; }
375  bool selectable() const { return mSelectable; }
376  bool selected() const { return mSelected; }
377 
378  // setters:
379  void setName(const QString &name);
380  void setAntialiasedFill(bool enabled);
381  void setAntialiasedScatters(bool enabled);
382  void setAntialiasedErrorBars(bool enabled);
383  void setPen(const QPen &pen);
384  void setSelectedPen(const QPen &pen);
385  void setBrush(const QBrush &brush);
386  void setSelectedBrush(const QBrush &brush);
387  void setKeyAxis(QCPAxis *axis);
388  void setValueAxis(QCPAxis *axis);
389  void setSelectable(bool selectable);
390  void setSelected(bool selected);
391 
392  // non-property methods:
393  void rescaleAxes(bool onlyEnlarge=false) const;
394  void rescaleKeyAxis(bool onlyEnlarge=false) const;
395  void rescaleValueAxis(bool onlyEnlarge=false) const;
396  virtual void clearData() = 0;
397  virtual double selectTest(const QPointF &pos) const = 0;
398  virtual bool addToLegend();
399  virtual bool removeFromLegend() const;
400 
401  signals:
402  void selectionChanged(bool selected);
403 
404  protected:
408  enum SignDomain { sdNegative
409  ,sdBoth
410  ,sdPositive
411  };
412  QString mName;
413  bool mAntialiasedFill, mAntialiasedScatters, mAntialiasedErrorBars;
414  QPen mPen, mSelectedPen;
415  QBrush mBrush, mSelectedBrush;
416  QCPAxis *mKeyAxis, *mValueAxis;
417  bool mSelected, mSelectable;
418 
419  virtual QRect clipRect() const;
420  virtual void draw(QCPPainter *painter) = 0;
421  virtual void drawLegendIcon(QCPPainter *painter, const QRect &rect) const = 0;
422  virtual QCPRange getKeyRange(bool &validRange, SignDomain inSignDomain=sdBoth) const = 0;
423  virtual QCPRange getValueRange(bool &validRange, SignDomain inSignDomain=sdBoth) const = 0;
424 
425  // painting and coordinate transformation helpers:
426  void coordsToPixels(double key, double value, double &x, double &y) const;
427  const QPointF coordsToPixels(double key, double value) const;
428  void pixelsToCoords(double x, double y, double &key, double &value) const;
429  void pixelsToCoords(const QPointF &pixelPos, double &key, double &value) const;
430  QPen mainPen() const;
431  QBrush mainBrush() const;
432  void applyDefaultAntialiasingHint(QCPPainter *painter) const;
433  void applyFillAntialiasingHint(QCPPainter *painter) const;
434  void applyScattersAntialiasingHint(QCPPainter *painter) const;
435  void applyErrorBarsAntialiasingHint(QCPPainter *painter) const;
436 
437  // selection test helpers:
438  double distSqrToLine(const QPointF &start, const QPointF &end, const QPointF &point) const;
439 
440  private:
441  Q_DISABLE_COPY(QCPAbstractPlottable)
442 
443  friend class QCustomPlot;
444  friend class QCPPlottableLegendItem;
445  };
446 
447  class QCP_LIB_DECL QCPGraph : public QCPAbstractPlottable
448  {
449  Q_OBJECT
450  public:
456  enum LineStyle { lsNone
457  ,lsLine
459  ,lsStepLeft
460  ,lsStepRight
461  ,lsStepCenter
462  ,lsImpulse
463  };
464  Q_ENUMS(LineStyle)
468  enum ErrorType { etNone
469  ,etKey
470  ,etValue
471  ,etBoth
472  };
473  Q_ENUMS(ErrorType)
474 
475  explicit QCPGraph(QCPAxis *keyAxis, QCPAxis *valueAxis);
476  virtual ~QCPGraph();
477 
478  // getters:
479  const QCPDataMap *data() const { return mData; }
480  LineStyle lineStyle() const { return mLineStyle; }
481  QCP::ScatterStyle scatterStyle() const { return mScatterStyle; }
482  double scatterSize() const { return mScatterSize; }
483  const QPixmap scatterPixmap() const { return mScatterPixmap; }
484  ErrorType errorType() const { return mErrorType; }
485  QPen errorPen() const { return mErrorPen; }
486  double errorBarSize() const { return mErrorBarSize; }
487  bool errorBarSkipSymbol() const { return mErrorBarSkipSymbol; }
488  QCPGraph *channelFillGraph() const { return mChannelFillGraph; }
489 
490  // setters:
491  void setData(QCPDataMap *data, bool copy=false);
492  void setData(const QVector<double> &key, const QVector<double> &value);
493  void setDataKeyError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyError);
494  void setDataKeyError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyErrorMinus, const QVector<double> &keyErrorPlus);
495  void setDataValueError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &valueError);
496  void setDataValueError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &valueErrorMinus, const QVector<double> &valueErrorPlus);
497  void setDataBothError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyError, const QVector<double> &valueError);
498  void setDataBothError(const QVector<double> &key, const QVector<double> &value, const QVector<double> &keyErrorMinus, const QVector<double> &keyErrorPlus, const QVector<double> &valueErrorMinus, const QVector<double> &valueErrorPlus);
499  void setLineStyle(LineStyle ls);
500  void setScatterStyle(QCP::ScatterStyle ss);
501  void setScatterSize(double size);
502  void setScatterPixmap(const QPixmap &pixmap);
503  void setErrorType(ErrorType errorType);
504  void setErrorPen(const QPen &pen);
505  void setErrorBarSize(double size);
506  void setErrorBarSkipSymbol(bool enabled);
507  void setChannelFillGraph(QCPGraph *targetGraph);
508 
509  // non-property methods:
510  void addData(const QCPDataMap &dataMap);
511  void addData(const QCPData &data);
512  void addData(double key, double value);
513  void addData(const QVector<double> &keys, const QVector<double> &values);
514  void removeDataBefore(double key);
515  void removeDataAfter(double key);
516  void removeData(double fromKey, double toKey);
517  void removeData(double key);
518  virtual void clearData();
519  virtual double selectTest(const QPointF &pos) const;
523  virtual void rescaleAxes(bool onlyEnlarge, bool includeErrorBars) const; // overloads base class interface
524  virtual void rescaleKeyAxis(bool onlyEnlarge, bool includeErrorBars) const; // overloads base class interface
525  virtual void rescaleValueAxis(bool onlyEnlarge, bool includeErrorBars) const; // overloads base class interface
526 
527  protected:
528  QCPDataMap *mData;
529  QPen mErrorPen;
530  LineStyle mLineStyle;
531  QCP::ScatterStyle mScatterStyle;
532  double mScatterSize;
533  QPixmap mScatterPixmap;
534  ErrorType mErrorType;
535  double mErrorBarSize;
536  bool mErrorBarSkipSymbol;
537  QCPGraph *mChannelFillGraph;
538 
539  virtual void draw(QCPPainter *painter);
540  virtual void drawLegendIcon(QCPPainter *painter, const QRect &rect) const;
541 
542  // functions to generate plot data points in pixel coordinates:
543  void getPlotData(QVector<QPointF> *lineData, QVector<QCPData> *pointData) const;
544  // plot style specific functions to generate plot data, used by getPlotData:
545  void getScatterPlotData(QVector<QCPData> *pointData) const;
546  void getLinePlotData(QVector<QPointF> *lineData, QVector<QCPData> *pointData) const;
547  void getStepLeftPlotData(QVector<QPointF> *lineData, QVector<QCPData> *pointData) const;
548  void getStepRightPlotData(QVector<QPointF> *lineData, QVector<QCPData> *pointData) const;
549  void getStepCenterPlotData(QVector<QPointF> *lineData, QVector<QCPData> *pointData) const;
550  void getImpulsePlotData(QVector<QPointF> *lineData, QVector<QCPData> *pointData) const;
551 
552  // helper functions for drawing:
553  void drawFill(QCPPainter *painter, QVector<QPointF> *lineData) const;
554  void drawScatterPlot(QCPPainter *painter, QVector<QCPData> *pointData) const;
555  void drawLinePlot(QCPPainter *painter, QVector<QPointF> *lineData) const;
556  void drawImpulsePlot(QCPPainter *painter, QVector<QPointF> *lineData) const;
557  void drawError(QCPPainter *painter, double x, double y, const QCPData &data) const;
558 
559  // helper functions:
560  void getVisibleDataBounds(QCPDataMap::const_iterator &lower, QCPDataMap::const_iterator &upper, int &count) const;
561  void addFillBasePoints(QVector<QPointF> *lineData) const;
562  void removeFillBasePoints(QVector<QPointF> *lineData) const;
563  QPointF lowerFillBasePoint(double lowerKey) const;
564  QPointF upperFillBasePoint(double upperKey) const;
565  const QPolygonF getChannelFillPolygon(const QVector<QPointF> *lineData) const;
566  int findIndexBelowX(const QVector<QPointF> *data, double x) const;
567  int findIndexAboveX(const QVector<QPointF> *data, double x) const;
568  int findIndexBelowY(const QVector<QPointF> *data, double y) const;
569  int findIndexAboveY(const QVector<QPointF> *data, double y) const;
570  double pointDistance(const QPointF &pixelPoint) const;
571  virtual QCPRange getKeyRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
572  virtual QCPRange getValueRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
573  virtual QCPRange getKeyRange(bool &validRange, SignDomain inSignDomain, bool includeErrors) const; // overloads base class interface
574  virtual QCPRange getValueRange(bool &validRange, SignDomain inSignDomain, bool includeErrors) const; // overloads base class interface
575 
576  friend class QCustomPlot;
577  friend class QCPLegend;
578  };
579 
580  class QCP_LIB_DECL QCPCurve : public QCPAbstractPlottable
581  {
582  Q_OBJECT
583  public:
589  enum LineStyle { lsNone,
590  lsLine
591  };
592  explicit QCPCurve(QCPAxis *keyAxis, QCPAxis *valueAxis);
593  virtual ~QCPCurve();
594 
595  // getters:
596  const QCPCurveDataMap *data() const { return mData; }
597  QCP::ScatterStyle scatterStyle() const { return mScatterStyle; }
598  double scatterSize() const { return mScatterSize; }
599  QPixmap scatterPixmap() const { return mScatterPixmap; }
600  LineStyle lineStyle() const { return mLineStyle; }
601 
602  // setters:
603  void setData(QCPCurveDataMap *data, bool copy=false);
604  void setData(const QVector<double> &t, const QVector<double> &key, const QVector<double> &value);
605  void setData(const QVector<double> &key, const QVector<double> &value);
606  void setScatterStyle(QCP::ScatterStyle style);
607  void setScatterSize(double size);
608  void setScatterPixmap(const QPixmap &pixmap);
609  void setLineStyle(LineStyle style);
610 
611  // non-property methods:
612  void addData(const QCPCurveDataMap &dataMap);
613  void addData(const QCPCurveData &data);
614  void addData(double t, double key, double value);
615  void addData(double key, double value);
616  void addData(const QVector<double> &ts, const QVector<double> &keys, const QVector<double> &values);
617  void removeDataBefore(double t);
618  void removeDataAfter(double t);
619  void removeData(double fromt, double tot);
620  void removeData(double t);
621  virtual void clearData();
622  virtual double selectTest(const QPointF &pos) const;
623 
624  protected:
625  QCPCurveDataMap *mData;
626  QCP::ScatterStyle mScatterStyle;
627  double mScatterSize;
628  QPixmap mScatterPixmap;
629  LineStyle mLineStyle;
630 
631  virtual void draw(QCPPainter *painter);
632  virtual void drawLegendIcon(QCPPainter *painter, const QRect &rect) const;
633  // drawing helpers:
634  virtual void drawScatterPlot(QCPPainter *painter, const QVector<QPointF> *pointData) const;
635 
636  // helper functions:
637  void getCurveData(QVector<QPointF> *lineData) const;
638  double pointDistance(const QPointF &pixelPoint) const;
639 
640  QPointF outsideCoordsToPixels(double key, double value, int region) const;
641  virtual QCPRange getKeyRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
642  virtual QCPRange getValueRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
643 
644  friend class QCustomPlot;
645  friend class QCPLegend;
646  };
647 
648  class QCP_LIB_DECL QCPBars : public QCPAbstractPlottable
649  {
650  Q_OBJECT
651  public:
652  explicit QCPBars(QCPAxis *keyAxis, QCPAxis *valueAxis);
653  virtual ~QCPBars();
654 
655  // getters:
656  double width() const { return mWidth; }
657  QCPBars *barBelow() const { return mBarBelow; }
658  QCPBars *barAbove() const { return mBarAbove; }
659  const QCPBarDataMap *data() const { return mData; }
660 
661  // setters:
662  void setWidth(double width);
663  void setData(QCPBarDataMap *data, bool copy=false);
664  void setData(const QVector<double> &key, const QVector<double> &value);
665 
666  // non-property methods:
667  void moveBelow(QCPBars *bars);
668  void moveAbove(QCPBars *bars);
669  void addData(const QCPBarDataMap &dataMap);
670  void addData(const QCPBarData &data);
671  void addData(double key, double value);
672  void addData(const QVector<double> &keys, const QVector<double> &values);
673  void removeDataBefore(double key);
674  void removeDataAfter(double key);
675  void removeData(double fromKey, double toKey);
676  void removeData(double key);
677  virtual void clearData();
678  virtual double selectTest(const QPointF &pos) const;
679 
680  protected:
681  QCPBarDataMap *mData;
682  double mWidth;
683  QCPBars *mBarBelow, *mBarAbove;
684 
685  virtual void draw(QCPPainter *painter);
686  virtual void drawLegendIcon(QCPPainter *painter, const QRect &rect) const;
687 
688  QPolygonF getBarPolygon(double key, double value) const;
689  double getBaseValue(double key, bool positive) const;
690  static void connectBars(QCPBars* lower, QCPBars* upper);
691  virtual QCPRange getKeyRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
692  virtual QCPRange getValueRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
693 
694  friend class QCustomPlot;
695  friend class QCPLegend;
696  };
697 
698  class QCP_LIB_DECL QCPStatisticalBox : public QCPAbstractPlottable
699  {
700  Q_OBJECT
701  public:
702  explicit QCPStatisticalBox(QCPAxis *keyAxis, QCPAxis *valueAxis);
703  virtual ~QCPStatisticalBox();
704 
705  // getters:
706  double key() const { return mKey; }
707  double minimum() const { return mMinimum; }
708  double lowerQuartile() const { return mLowerQuartile; }
709  double median() const { return mMedian; }
710  double upperQuartile() const { return mUpperQuartile; }
711  double maximum() const { return mMaximum; }
712  QVector<double> outliers() const { return mOutliers; }
713  double width() const { return mWidth; }
714  double whiskerWidth() const { return mWhiskerWidth; }
715  QPen whiskerPen() const { return mWhiskerPen; }
716  QPen whiskerBarPen() const { return mWhiskerBarPen; }
717  QPen medianPen() const { return mMedianPen; }
718  double outlierSize() const { return mOutlierSize; }
719  QPen outlierPen() const { return mOutlierPen; }
720  QCP::ScatterStyle outlierStyle() const { return mOutlierStyle; }
721 
722  // setters:
723  void setKey(double key);
724  void setMinimum(double value);
725  void setLowerQuartile(double value);
726  void setMedian(double value);
727  void setUpperQuartile(double value);
728  void setMaximum(double value);
729  void setOutliers(const QVector<double> &values);
730  void setData(double key, double minimum, double lowerQuartile, double median, double upperQuartile, double maximum);
731  void setWidth(double width);
732  void setWhiskerWidth(double width);
733  void setWhiskerPen(const QPen &pen);
734  void setWhiskerBarPen(const QPen &pen);
735  void setMedianPen(const QPen &pen);
736  void setOutlierSize(double pixels);
737  void setOutlierPen(const QPen &pen);
738  void setOutlierStyle(QCP::ScatterStyle style);
739 
740  // non-property methods:
741  virtual void clearData();
742  virtual double selectTest(const QPointF &pos) const;
743 
744  protected:
745  QVector<double> mOutliers;
746  double mKey, mMinimum, mLowerQuartile, mMedian, mUpperQuartile, mMaximum;
747  double mWidth;
748  double mWhiskerWidth;
749  double mOutlierSize;
750  QPen mWhiskerPen, mWhiskerBarPen, mOutlierPen, mMedianPen;
751  QCP::ScatterStyle mOutlierStyle;
752 
753  virtual void draw(QCPPainter *painter);
754  virtual void drawLegendIcon(QCPPainter *painter, const QRect &rect) const;
755 
756  virtual void drawQuartileBox(QCPPainter *painter, QRectF *quartileBox=0) const;
757  virtual void drawMedian(QCPPainter *painter) const;
758  virtual void drawWhiskers(QCPPainter *painter) const;
759  virtual void drawOutliers(QCPPainter *painter) const;
760  virtual QCPRange getKeyRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
761  virtual QCPRange getValueRange(bool &validRange, SignDomain inSignDomain=sdBoth) const;
762 
763  friend class QCustomPlot;
764  friend class QCPLegend;
765  };
766 
767  class QCP_LIB_DECL QCPItemAnchor
768  {
769  public:
770  QCPItemAnchor(QCustomPlot *parentPlot, QCPAbstractItem *parentItem, const QString name, int anchorId=-1);
771  virtual ~QCPItemAnchor();
772 
773  QString name() const { return mName; }
774  virtual QPointF pixelPoint() const;
775 
776  protected:
777  QCustomPlot *mParentPlot;
778  QCPAbstractItem *mParentItem;
779  int mAnchorId;
780  QString mName;
781  // non-property members:
782  QSet<QCPItemPosition*> mChildren;
783 
784  void addChild(QCPItemPosition* pos); // called from pos when this anchor is set as parent
785  void removeChild(QCPItemPosition *pos); // called from pos when its parent anchor is reset or pos deleted
786 
787  private:
788  Q_DISABLE_COPY(QCPItemAnchor)
789 
790  friend class QCPItemPosition;
791  };
792 
793  class QCP_LIB_DECL QCPItemPosition : public QCPItemAnchor
794  {
795  public:
802  enum PositionType { ptAbsolute
803  ,ptViewportRatio
804  ,ptAxisRectRatio
805  ,ptPlotCoords
806  };
807 
808  QCPItemPosition(QCustomPlot *parentPlot, QCPAbstractItem *parentItem, const QString name);
809  virtual ~QCPItemPosition();
810 
811  // getters:
812  PositionType type() const { return mPositionType; }
813  QCPItemAnchor *parentAnchor() const { return mParentAnchor; }
814  double key() const { return mKey; }
815  double value() const { return mValue; }
816  QPointF coords() const { return QPointF(mKey, mValue); }
817  QCPAxis *keyAxis() const { return mKeyAxis; }
818  QCPAxis *valueAxis() const { return mValueAxis; }
819  virtual QPointF pixelPoint() const;
820 
821  // setters:
822  void setType(PositionType type);
823  bool setParentAnchor(QCPItemAnchor *parentAnchor, bool keepPixelPosition=false);
824  void setCoords(double key, double value);
825  void setCoords(const QPointF &coords);
826  void setAxes(QCPAxis* keyAxis, QCPAxis* valueAxis);
827  void setPixelPoint(const QPointF &pixelPoint);
828 
829  protected:
830  PositionType mPositionType;
831  QCPAxis *mKeyAxis, *mValueAxis;
832  double mKey, mValue;
833  QCPItemAnchor *mParentAnchor;
834 
835  private:
836  Q_DISABLE_COPY(QCPItemPosition)
837 
838  };
839 
840  class QCP_LIB_DECL QCPAbstractItem : public QCPLayerable
841  {
842  Q_OBJECT
843  public:
844  QCPAbstractItem(QCustomPlot *parentPlot);
845  virtual ~QCPAbstractItem();
846 
847  // getters:
848  bool clipToAxisRect() const { return mClipToAxisRect; }
849  QCPAxis *clipKeyAxis() const { return mClipKeyAxis; }
850  QCPAxis *clipValueAxis() const { return mClipValueAxis; }
851  bool selectable() const { return mSelectable; }
852  bool selected() const { return mSelected; }
853 
854  // setters:
855  void setClipToAxisRect(bool clip);
856  void setClipAxes(QCPAxis *keyAxis, QCPAxis *valueAxis);
857  void setClipKeyAxis(QCPAxis *axis);
858  void setClipValueAxis(QCPAxis *axis);
859  void setSelectable(bool selectable);
860  void setSelected(bool selected);
861 
862  // non-property methods:
863  virtual double selectTest(const QPointF &pos) const = 0;
864  QList<QCPItemPosition*> positions() const { return mPositions; }
865  QList<QCPItemAnchor*> anchors() const { return mAnchors; }
866  QCPItemPosition *position(const QString &name) const;
867  QCPItemAnchor *anchor(const QString &name) const;
868  bool hasAnchor(const QString &name) const;
869 
870  protected:
871  bool mClipToAxisRect;
872  QCPAxis *mClipKeyAxis, *mClipValueAxis;
873  bool mSelectable, mSelected;
874  QList<QCPItemPosition*> mPositions;
875  QList<QCPItemAnchor*> mAnchors;
876 
877  virtual QRect clipRect() const;
878  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
879  virtual void draw(QCPPainter *painter) = 0;
880 
881  // helper functions for subclasses:
882  double distSqrToLine(const QPointF &start, const QPointF &end, const QPointF &point) const;
883  double rectSelectTest(const QRectF &rect, const QPointF &pos, bool filledRect) const;
884 
885  // anchor/position interface:
886  virtual QPointF anchorPixelPoint(int anchorId) const;
887  QCPItemPosition *createPosition(const QString &name);
888  QCPItemAnchor *createAnchor(const QString &name, int anchorId);
889 
890  signals:
891  void selectionChanged(bool selected);
892 
893  private:
894  Q_DISABLE_COPY(QCPAbstractItem)
895 
896  friend class QCustomPlot;
897  friend class QCPItemAnchor;
898  };
899 
900  class QCP_LIB_DECL QCPItemStraightLine : public QCPAbstractItem
901  {
902  Q_OBJECT
903  public:
904  QCPItemStraightLine(QCustomPlot *parentPlot);
905  virtual ~QCPItemStraightLine();
906 
907  // getters:
908  QPen pen() const { return mPen; }
909  QPen selectedPen() const { return mSelectedPen; }
910 
911  // setters;
912  void setPen(const QPen &pen);
913  void setSelectedPen(const QPen &pen);
914 
915  // non-property methods:
916  virtual double selectTest(const QPointF &pos) const;
917 
918  QCPItemPosition * const point1;
919  QCPItemPosition * const point2;
920 
921  protected:
922  QPen mPen, mSelectedPen;
923 
924  virtual void draw(QCPPainter *painter);
925 
926  // helper functions:
927  double distToStraightLine(const QVector2D &point1, const QVector2D &vec, const QVector2D &point) const;
928  QLineF getRectClippedStraightLine(const QVector2D &point1, const QVector2D &vec, const QRect &rect) const;
929  QPen mainPen() const;
930  };
931 
932  class QCP_LIB_DECL QCPItemLine : public QCPAbstractItem
933  {
934  Q_OBJECT
935  public:
936  QCPItemLine(QCustomPlot *parentPlot);
937  virtual ~QCPItemLine();
938 
939  // getters:
940  QPen pen() const { return mPen; }
941  QPen selectedPen() const { return mSelectedPen; }
942  QCPLineEnding head() const { return mHead; }
943  QCPLineEnding tail() const { return mTail; }
944 
945  // setters;
946  void setPen(const QPen &pen);
947  void setSelectedPen(const QPen &pen);
948  void setHead(const QCPLineEnding &head);
949  void setTail(const QCPLineEnding &tail);
950 
951  // non-property methods:
952  virtual double selectTest(const QPointF &pos) const;
953 
954  QCPItemPosition * const start;
955  QCPItemPosition * const end;
956 
957  protected:
958  QPen mPen, mSelectedPen;
959  QCPLineEnding mHead, mTail;
960 
961  virtual void draw(QCPPainter *painter);
962 
963  // helper functions:
964  QLineF getRectClippedLine(const QVector2D &start, const QVector2D &end, const QRect &rect) const;
965  QPen mainPen() const;
966  };
967 
968  class QCP_LIB_DECL QCPItemEllipse : public QCPAbstractItem
969  {
970  Q_OBJECT
971  public:
972  QCPItemEllipse(QCustomPlot *parentPlot);
973  virtual ~QCPItemEllipse();
974 
975  // getters:
976  QPen pen() const { return mPen; }
977  QPen selectedPen() const { return mSelectedPen; }
978  QBrush brush() const { return mBrush; }
979  QBrush selectedBrush() const { return mSelectedBrush; }
980 
981  // setters;
982  void setPen(const QPen &pen);
983  void setSelectedPen(const QPen &pen);
984  void setBrush(const QBrush &brush);
985  void setSelectedBrush(const QBrush &brush);
986 
987  // non-property methods:
988  virtual double selectTest(const QPointF &pos) const;
989 
990  QCPItemPosition * const topLeft;
991  QCPItemPosition * const bottomRight;
992  QCPItemAnchor * const topLeftRim;
993  QCPItemAnchor * const top;
994  QCPItemAnchor * const topRightRim;
995  QCPItemAnchor * const right;
996  QCPItemAnchor * const bottomRightRim;
997  QCPItemAnchor * const bottom;
998  QCPItemAnchor * const bottomLeftRim;
999  QCPItemAnchor * const left;
1000 
1001  protected:
1002  enum AnchorIndex {aiTopLeftRim, aiTop, aiTopRightRim, aiRight, aiBottomRightRim, aiBottom, aiBottomLeftRim, aiLeft};
1003  QPen mPen, mSelectedPen;
1004  QBrush mBrush, mSelectedBrush;
1005 
1006  virtual void draw(QCPPainter *painter);
1007  virtual QPointF anchorPixelPoint(int anchorId) const;
1008 
1009  // helper functions:
1010  QPen mainPen() const;
1011  QBrush mainBrush() const;
1012  };
1013 
1014  class QCP_LIB_DECL QCPItemRect : public QCPAbstractItem
1015  {
1016  Q_OBJECT
1017  public:
1018  QCPItemRect(QCustomPlot *parentPlot);
1019  virtual ~QCPItemRect();
1020 
1021  // getters:
1022  QPen pen() const { return mPen; }
1023  QPen selectedPen() const { return mSelectedPen; }
1024  QBrush brush() const { return mBrush; }
1025  QBrush selectedBrush() const { return mSelectedBrush; }
1026 
1027  // setters;
1028  void setPen(const QPen &pen);
1029  void setSelectedPen(const QPen &pen);
1030  void setBrush(const QBrush &brush);
1031  void setSelectedBrush(const QBrush &brush);
1032 
1033  // non-property methods:
1034  virtual double selectTest(const QPointF &pos) const;
1035 
1036  QCPItemPosition * const topLeft;
1037  QCPItemPosition * const bottomRight;
1038  QCPItemAnchor * const top;
1039  QCPItemAnchor * const topRight;
1040  QCPItemAnchor * const right;
1041  QCPItemAnchor * const bottom;
1042  QCPItemAnchor * const bottomLeft;
1043  QCPItemAnchor * const left;
1044 
1045  protected:
1046  enum AnchorIndex {aiTop, aiTopRight, aiRight, aiBottom, aiBottomLeft, aiLeft};
1047  QPen mPen, mSelectedPen;
1048  QBrush mBrush, mSelectedBrush;
1049 
1050  virtual void draw(QCPPainter *painter);
1051  virtual QPointF anchorPixelPoint(int anchorId) const;
1052 
1053  // helper functions:
1054  QPen mainPen() const;
1055  QBrush mainBrush() const;
1056  };
1057 
1058  class QCP_LIB_DECL QCPItemPixmap : public QCPAbstractItem
1059  {
1060  Q_OBJECT
1061  public:
1062  QCPItemPixmap(QCustomPlot *parentPlot);
1063  virtual ~QCPItemPixmap();
1064 
1065  // getters:
1066  QPixmap pixmap() const { return mPixmap; }
1067  bool scaled() const { return mScaled; }
1068  Qt::AspectRatioMode aspectRatioMode() const { return mAspectRatioMode; }
1069  QPen pen() const { return mPen; }
1070  QPen selectedPen() const { return mSelectedPen; }
1071 
1072  // setters;
1073  void setPixmap(const QPixmap &pixmap);
1074  void setScaled(bool scaled, Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio);
1075  void setPen(const QPen &pen);
1076  void setSelectedPen(const QPen &pen);
1077 
1078  // non-property methods:
1079  virtual double selectTest(const QPointF &pos) const;
1080 
1081  QCPItemPosition * const topLeft;
1082  QCPItemPosition * const bottomRight;
1083  QCPItemAnchor * const top;
1084  QCPItemAnchor * const topRight;
1085  QCPItemAnchor * const right;
1086  QCPItemAnchor * const bottom;
1087  QCPItemAnchor * const bottomLeft;
1088  QCPItemAnchor * const left;
1089 
1090  protected:
1091  enum AnchorIndex {aiTop, aiTopRight, aiRight, aiBottom, aiBottomLeft, aiLeft};
1092  QPixmap mPixmap;
1093  QPixmap mScaledPixmap;
1094  bool mScaled;
1095  Qt::AspectRatioMode mAspectRatioMode;
1096  QPen mPen, mSelectedPen;
1097 
1098  virtual void draw(QCPPainter *painter);
1099  virtual QPointF anchorPixelPoint(int anchorId) const;
1100 
1101  // helper functions:
1102  void updateScaledPixmap(QRect finalRect=QRect(), bool flipHorz=false, bool flipVert=false);
1103  QRect getFinalRect(bool *flippedHorz=0, bool *flippedVert=0) const;
1104  QPen mainPen() const;
1105  };
1106 
1107  class QCP_LIB_DECL QCPItemText : public QCPAbstractItem
1108  {
1109  Q_OBJECT
1110  public:
1111  QCPItemText(QCustomPlot *parentPlot);
1112  virtual ~QCPItemText();
1113 
1114  // getters:
1115  QColor color() const { return mColor; }
1116  QColor selectedColor() const { return mSelectedColor; }
1117  QPen pen() const { return mPen; }
1118  QPen selectedPen() const { return mSelectedPen; }
1119  QBrush brush() const { return mBrush; }
1120  QBrush selectedBrush() const { return mSelectedBrush; }
1121  QFont font() const { return mFont; }
1122  QFont selectedFont() const { return mSelectedFont; }
1123  QString text() const { return mText; }
1124  Qt::Alignment positionAlignment() const { return mPositionAlignment; }
1125  Qt::Alignment textAlignment() const { return mTextAlignment; }
1126  double rotation() const { return mRotation; }
1127  QMargins padding() const { return mPadding; }
1128 
1129  // setters;
1130  void setColor(const QColor &color);
1131  void setSelectedColor(const QColor &color);
1132  void setPen(const QPen &pen);
1133  void setSelectedPen(const QPen &pen);
1134  void setBrush(const QBrush &brush);
1135  void setSelectedBrush(const QBrush &brush);
1136  void setFont(const QFont &font);
1137  void setSelectedFont(const QFont &font);
1138  void setText(const QString &text);
1139  void setPositionAlignment(Qt::Alignment alignment);
1140  void setTextAlignment(Qt::Alignment alignment);
1141  void setRotation(double degrees);
1142  void setPadding(const QMargins &padding);
1143 
1144  // non-property methods:
1145  virtual double selectTest(const QPointF &pos) const;
1146 
1147  QCPItemPosition * const position;
1148  QCPItemAnchor * const topLeft;
1149  QCPItemAnchor * const top;
1150  QCPItemAnchor * const topRight;
1151  QCPItemAnchor * const right;
1152  QCPItemAnchor * const bottomRight;
1153  QCPItemAnchor * const bottom;
1154  QCPItemAnchor * const bottomLeft;
1155  QCPItemAnchor * const left;
1156 
1157  protected:
1158  enum AnchorIndex {aiTopLeft, aiTop, aiTopRight, aiRight, aiBottomRight, aiBottom, aiBottomLeft, aiLeft};
1159  QColor mColor, mSelectedColor;
1160  QPen mPen, mSelectedPen;
1161  QBrush mBrush, mSelectedBrush;
1162  QFont mFont, mSelectedFont;
1163  QString mText;
1164  Qt::Alignment mPositionAlignment;
1165  Qt::Alignment mTextAlignment;
1166  double mRotation;
1167  QMargins mPadding;
1168 
1169  virtual void draw(QCPPainter *painter);
1170  virtual QPointF anchorPixelPoint(int anchorId) const;
1171 
1172  // helper functions:
1173  QPointF getTextDrawPoint(const QPointF &pos, const QRectF &rect, Qt::Alignment positionAlignment) const;
1174  QFont mainFont() const;
1175  QColor mainColor() const;
1176  QPen mainPen() const;
1177  QBrush mainBrush() const;
1178  };
1179 
1180  class QCP_LIB_DECL QCPItemCurve : public QCPAbstractItem
1181  {
1182  Q_OBJECT
1183  public:
1184  QCPItemCurve(QCustomPlot *parentPlot);
1185  virtual ~QCPItemCurve();
1186 
1187  // getters:
1188  QPen pen() const { return mPen; }
1189  QPen selectedPen() const { return mSelectedPen; }
1190  QCPLineEnding head() const { return mHead; }
1191  QCPLineEnding tail() const { return mTail; }
1192 
1193  // setters;
1194  void setPen(const QPen &pen);
1195  void setSelectedPen(const QPen &pen);
1196  void setHead(const QCPLineEnding &head);
1197  void setTail(const QCPLineEnding &tail);
1198 
1199  // non-property methods:
1200  virtual double selectTest(const QPointF &pos) const;
1201 
1202  QCPItemPosition * const start;
1203  QCPItemPosition * const startDir;
1204  QCPItemPosition * const endDir;
1205  QCPItemPosition * const end;
1206 
1207  protected:
1208  QPen mPen, mSelectedPen;
1209  QCPLineEnding mHead, mTail;
1210 
1211  virtual void draw(QCPPainter *painter);
1212 
1213  // helper functions:
1214  QPen mainPen() const;
1215  };
1216 
1217  class QCP_LIB_DECL QCPItemBracket : public QCPAbstractItem
1218  {
1219  Q_OBJECT
1220  public:
1221  enum BracketStyle { bsSquare
1222  ,bsRound
1223  ,bsCurly
1224  ,bsCalligraphic
1225  };
1226 
1227  QCPItemBracket(QCustomPlot *parentPlot);
1228  virtual ~QCPItemBracket();
1229 
1230  // getters:
1231  QPen pen() const { return mPen; }
1232  QPen selectedPen() const { return mSelectedPen; }
1233  double length() const { return mLength; }
1234  BracketStyle style() const { return mStyle; }
1235 
1236  // setters;
1237  void setPen(const QPen &pen);
1238  void setSelectedPen(const QPen &pen);
1239  void setLength(double length);
1240  void setStyle(BracketStyle style);
1241 
1242  // non-property methods:
1243  virtual double selectTest(const QPointF &pos) const;
1244 
1245  QCPItemPosition * const left;
1246  QCPItemPosition * const right;
1247  QCPItemAnchor * const center;
1248 
1249  protected:
1250  enum AnchorIndex {aiCenter};
1251  QPen mPen, mSelectedPen;
1252  double mLength;
1253  BracketStyle mStyle;
1254 
1255  virtual void draw(QCPPainter *painter);
1256  virtual QPointF anchorPixelPoint(int anchorId) const;
1257 
1258  // helper functions:
1259  QPen mainPen() const;
1260  };
1261 
1262  class QCP_LIB_DECL QCPItemTracer : public QCPAbstractItem
1263  {
1264  Q_OBJECT
1265  public:
1271  enum TracerStyle { tsNone
1272  ,tsPlus
1273  ,tsCrosshair
1274  ,tsCircle
1275  ,tsSquare
1276  };
1277  Q_ENUMS(TracerStyle)
1278 
1279  QCPItemTracer(QCustomPlot *parentPlot);
1280  virtual ~QCPItemTracer();
1281 
1282  // getters:
1283  QPen pen() const { return mPen; }
1284  QPen selectedPen() const { return mSelectedPen; }
1285  QBrush brush() const { return mBrush; }
1286  QBrush selectedBrush() const { return mSelectedBrush; }
1287  double size() const { return mSize; }
1288  TracerStyle style() const { return mStyle; }
1289  QCPGraph *graph() const { return mGraph; }
1290  double graphKey() const { return mGraphKey; }
1291  bool interpolating() const { return mInterpolating; }
1292 
1293  // setters;
1294  void setPen(const QPen &pen);
1295  void setSelectedPen(const QPen &pen);
1296  void setBrush(const QBrush &brush);
1297  void setSelectedBrush(const QBrush &brush);
1298  void setSize(double size);
1299  void setStyle(TracerStyle style);
1300  void setGraph(QCPGraph *graph);
1301  void setGraphKey(double key);
1302  void setInterpolating(bool enabled);
1303 
1304  // non-property methods:
1305  virtual double selectTest(const QPointF &pos) const;
1306  void updatePosition();
1307 
1308  QCPItemPosition * const position;
1309 
1310  protected:
1311  QPen mPen, mSelectedPen;
1312  QBrush mBrush, mSelectedBrush;
1313  double mSize;
1314  TracerStyle mStyle;
1315  QCPGraph *mGraph;
1316  double mGraphKey;
1317  bool mInterpolating;
1318 
1319  virtual void draw(QCPPainter *painter);
1320 
1321  // helper functions:
1322  QPen mainPen() const;
1323  QBrush mainBrush() const;
1324  };
1325 
1326  class QCP_LIB_DECL QCPRange
1327  {
1328  public:
1329  double lower, upper;
1330  QCPRange();
1331  QCPRange(double lower, double upper);
1332  double size() const;
1333  double center() const;
1334  void normalize();
1335  QCPRange sanitizedForLogScale() const;
1336  QCPRange sanitizedForLinScale() const;
1337  bool contains(double value) const;
1338 
1339  static bool validRange(double lower, double upper);
1340  static bool validRange(const QCPRange &range);
1341  static const double minRange; //1e-280;
1342  static const double maxRange; //1e280;
1343  };
1344 
1345  class QCP_LIB_DECL QCPAbstractLegendItem : public QObject
1346  {
1347  Q_OBJECT
1348  public:
1350  virtual ~QCPAbstractLegendItem() {}
1351 
1352  // getters:
1353  bool antialiased() const { return mAntialiased; }
1354  QFont font() const { return mFont; }
1355  QColor textColor() const { return mTextColor; }
1356  QFont selectedFont() const { return mSelectedFont; }
1357  QColor selectedTextColor() const { return mSelectedTextColor; }
1358  bool selectable() const { return mSelectable; }
1359  bool selected() const { return mSelected; }
1360 
1361  // setters:
1362  void setAntialiased(bool enabled);
1363  void setFont(const QFont &font);
1364  void setTextColor(const QColor &color);
1365  void setSelectedFont(const QFont &font);
1366  void setSelectedTextColor(const QColor &color);
1367  void setSelectable(bool selectable);
1368  void setSelected(bool selected);
1369 
1370  signals:
1371  void selectionChanged(bool selected);
1372 
1373  protected:
1374  QCPLegend *mParentLegend;
1375  bool mAntialiased;
1376  QFont mFont;
1377  QColor mTextColor;
1378  QFont mSelectedFont;
1379  QColor mSelectedTextColor;
1380  bool mSelectable, mSelected;
1381 
1382  virtual void draw(QCPPainter *painter, const QRect &rect) const = 0;
1383  virtual QSize size(const QSize &targetSize) const = 0;
1384  void applyAntialiasingHint(QCPPainter *painter) const;
1385 
1386  private:
1387  Q_DISABLE_COPY(QCPAbstractLegendItem)
1388 
1389  friend class QCPLegend;
1390  };
1391 
1392  class QCP_LIB_DECL QCPPlottableLegendItem : public QCPAbstractLegendItem
1393  {
1394  Q_OBJECT
1395  public:
1397  virtual ~QCPPlottableLegendItem() {}
1398 
1399  // getters:
1400  QCPAbstractPlottable *plottable() { return mPlottable; }
1401  bool textWrap() const { return mTextWrap; }
1402 
1403  // setters:
1404  void setTextWrap(bool wrap);
1405 
1406  protected:
1407  QCPAbstractPlottable *mPlottable;
1408  bool mTextWrap;
1409 
1410  QPen getIconBorderPen() const;
1411  QColor getTextColor() const;
1412  QFont getFont() const;
1413 
1414  virtual void draw(QCPPainter *painter, const QRect &rect) const;
1415  virtual QSize size(const QSize &targetSize) const;
1416  };
1417 
1418  class QCP_LIB_DECL QCPLegend : public QCPLayerable
1419  {
1420  Q_OBJECT
1421  public:
1425  enum PositionStyle { psManual
1426  ,psTopLeft
1427  ,psTop
1428  ,psTopRight
1429  ,psRight
1430  ,psBottomRight
1431  ,psBottom
1432  ,psBottomLeft
1433  ,psLeft
1434  };
1435  Q_ENUMS(PositionStyle)
1436 
1437 
1440  enum SelectablePart { spNone = 0
1441  ,spLegendBox = 0x001
1442  ,spItems = 0x002
1443  };
1444  Q_ENUMS(SelectablePart)
1445  Q_DECLARE_FLAGS(SelectableParts, SelectablePart)
1446 
1447  explicit QCPLegend(QCustomPlot *parentPlot);
1448  virtual ~QCPLegend();
1449 
1450  // getters:
1451  QPen borderPen() const { return mBorderPen; }
1452  QBrush brush() const { return mBrush; }
1453  QFont font() const { return mFont; }
1454  QColor textColor() const { return mTextColor; }
1455  PositionStyle positionStyle() const { return mPositionStyle; }
1456  QPoint position() const { return mPosition; }
1457  bool autoSize() const { return mAutoSize; }
1458  QSize size() const { return mSize; }
1459  QSize minimumSize() const { return mMinimumSize; }
1460  int paddingLeft() const { return mPaddingLeft; }
1461  int paddingRight() const { return mPaddingRight; }
1462  int paddingTop() const { return mPaddingTop; }
1463  int paddingBottom() const { return mPaddingBottom; }
1464  int marginLeft() const { return mMarginLeft; }
1465  int marginRight() const { return mMarginRight; }
1466  int marginTop() const { return mMarginTop; }
1467  int marginBottom() const { return mMarginBottom; }
1468  int itemSpacing() const { return mItemSpacing; }
1469  QSize iconSize() const { return mIconSize; }
1470  int iconTextPadding() const { return mIconTextPadding; }
1471  QPen iconBorderPen() const { return mIconBorderPen; }
1472  SelectableParts selectable() const { return mSelectable; }
1473  SelectableParts selected() const { return mSelected; }
1474  QPen selectedBorderPen() const { return mSelectedBorderPen; }
1475  QPen selectedIconBorderPen() const { return mSelectedIconBorderPen; }
1476  QBrush selectedBrush() const { return mSelectedBrush; }
1477  QFont selectedFont() const { return mSelectedFont; }
1478  QColor selectedTextColor() const { return mSelectedTextColor; }
1479 
1480  // setters:
1481  void setBorderPen(const QPen &pen);
1482  void setBrush(const QBrush &brush);
1483  void setFont(const QFont &font);
1484  void setTextColor(const QColor &color);
1485  void setPositionStyle(PositionStyle legendPositionStyle);
1486  void setPosition(const QPoint &pixelPosition);
1487  void setAutoSize(bool on);
1488  void setSize(const QSize &size);
1489  void setSize(int width, int height);
1490  void setMinimumSize(const QSize &size);
1491  void setMinimumSize(int width, int height);
1492  void setPaddingLeft(int padding);
1493  void setPaddingRight(int padding);
1494  void setPaddingTop(int padding);
1495  void setPaddingBottom(int padding);
1496  void setPadding(int left, int right, int top, int bottom);
1497  void setMarginLeft(int margin);
1498  void setMarginRight(int margin);
1499  void setMarginTop(int margin);
1500  void setMarginBottom(int margin);
1501  void setMargin(int left, int right, int top, int bottom);
1502  void setItemSpacing(int spacing);
1503  void setIconSize(const QSize &size);
1504  void setIconSize(int width, int height);
1505  void setIconTextPadding(int padding);
1506  void setIconBorderPen(const QPen &pen);
1507  void setSelectable(const SelectableParts &selectable);
1508  void setSelected(const SelectableParts &selected);
1509  void setSelectedBorderPen(const QPen &pen);
1510  void setSelectedIconBorderPen(const QPen &pen);
1511  void setSelectedBrush(const QBrush &brush);
1512  void setSelectedFont(const QFont &font);
1513  void setSelectedTextColor(const QColor &color);
1514 
1515  // non-property methods:
1516  QCPAbstractLegendItem *item(int index) const;
1517  QCPPlottableLegendItem *itemWithPlottable(const QCPAbstractPlottable *plottable) const;
1518  int itemCount() const;
1519  bool hasItem(QCPAbstractLegendItem *item) const;
1520  bool hasItemWithPlottable(const QCPAbstractPlottable *plottable) const;
1521  bool addItem(QCPAbstractLegendItem *item);
1522  bool removeItem(int index);
1523  bool removeItem(QCPAbstractLegendItem *item);
1524  void clearItems();
1525  QList<QCPAbstractLegendItem*> selectedItems() const;
1526  void reArrange();
1527 
1528  bool selectTestLegend(const QPointF &pos) const;
1529  QCPAbstractLegendItem *selectTestItem(const QPoint pos) const;
1530 
1531  signals:
1532  void selectionChanged(QCPLegend::SelectableParts selection);
1533 
1534  protected:
1535  // simple properties with getters and setters:
1536  QPen mBorderPen, mIconBorderPen;
1537  QBrush mBrush;
1538  QFont mFont;
1539  QColor mTextColor;
1540  QPoint mPosition;
1541  QSize mSize, mMinimumSize, mIconSize;
1542  PositionStyle mPositionStyle;
1543  bool mAutoSize;
1544  int mPaddingLeft, mPaddingRight, mPaddingTop, mPaddingBottom;
1545  int mMarginLeft, mMarginRight, mMarginTop, mMarginBottom;
1546  int mItemSpacing, mIconTextPadding;
1547  SelectableParts mSelected, mSelectable;
1548  QPen mSelectedBorderPen, mSelectedIconBorderPen;
1549  QBrush mSelectedBrush;
1550  QFont mSelectedFont;
1551  QColor mSelectedTextColor;
1552 
1553  // internal or not explicitly exposed properties:
1554  QList<QCPAbstractLegendItem*> mItems;
1555  QMap<QCPAbstractLegendItem*, QRect> mItemBoundingBoxes;
1556 
1557  virtual void updateSelectionState();
1558  virtual bool handleLegendSelection(QMouseEvent *event, bool additiveSelection, bool &modified);
1559  // introduced methods:
1560  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
1561  virtual void draw(QCPPainter *painter);
1562  virtual void calculateAutoSize();
1563  virtual void calculateAutoPosition();
1564 
1565  // drawing helpers:
1566  QPen getBorderPen() const;
1567  QBrush getBrush() const;
1568 
1569  private:
1570  Q_DISABLE_COPY(QCPLegend)
1571 
1572  friend class QCustomPlot;
1573  friend class QCPAbstractLegendItem;
1574  };
1575  Q_DECLARE_OPERATORS_FOR_FLAGS(QCPLegend::SelectableParts)
1576 
1577  class QCP_LIB_DECL QCPGrid : public QCPLayerable
1578  {
1579  Q_OBJECT
1580  public:
1581  QCPGrid(QCPAxis *parentAxis);
1582  ~QCPGrid();
1583 
1584  // getters:
1585  bool subGridVisible() const { return mSubGridVisible; }
1586  bool antialiasedSubGrid() const { return mAntialiasedSubGrid; }
1587  bool antialiasedZeroLine() const { return mAntialiasedZeroLine; }
1588  QPen pen() const { return mPen; }
1589  QPen subGridPen() const { return mSubGridPen; }
1590  QPen zeroLinePen() const { return mZeroLinePen; }
1591 
1592  // setters:
1593  void setSubGridVisible(bool visible);
1594  void setAntialiasedSubGrid(bool enabled);
1595  void setAntialiasedZeroLine(bool enabled);
1596  void setPen(const QPen &pen);
1597  void setSubGridPen(const QPen &pen);
1598  void setZeroLinePen(const QPen &pen);
1599 
1600  protected:
1601  QCPAxis *mParentAxis;
1602  bool mSubGridVisible;
1603  bool mAntialiasedSubGrid, mAntialiasedZeroLine;
1604  QPen mPen, mSubGridPen, mZeroLinePen;
1605 
1606  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
1607  virtual void draw(QCPPainter *painter);
1608  // drawing helpers:
1609  void drawGridLines(QCPPainter *painter) const;
1610  void drawSubGridLines(QCPPainter *painter) const;
1611 
1612  friend class QCPAxis;
1613  };
1614 
1615  class QCP_LIB_DECL QCPAxis : public QCPLayerable
1616  {
1617  Q_OBJECT
1619  Q_PROPERTY(AxisType axisType READ axisType WRITE setAxisType)
1620  Q_PROPERTY(ScaleType scaleType READ scaleType WRITE setScaleType)
1621  Q_PROPERTY(double scaleLogBase READ scaleLogBase WRITE setScaleLogBase)
1622  Q_PROPERTY(QRect axisRect READ axisRect WRITE setAxisRect)
1623  Q_PROPERTY(QCPRange range READ range WRITE setRange)
1624  Q_PROPERTY(bool grid READ grid WRITE setGrid)
1625  Q_PROPERTY(bool subGrid READ subGrid WRITE setSubGrid)
1626  Q_PROPERTY(bool autoTicks READ autoTicks WRITE setAutoTicks)
1627  Q_PROPERTY(int autoTickCount READ autoTickCount WRITE setAutoTickCount)
1628  Q_PROPERTY(bool autoTickLabels READ autoTickLabels WRITE setAutoTickLabels)
1629  Q_PROPERTY(bool autoTickStep READ autoTickStep WRITE setAutoTickStep)
1630  Q_PROPERTY(bool autoSubTicks READ autoSubTicks WRITE setAutoSubTicks)
1631  Q_PROPERTY(bool ticks READ ticks WRITE setTicks)
1632  Q_PROPERTY(bool tickLabels READ tickLabels WRITE setTickLabels)
1633  Q_PROPERTY(int tickLabelPadding READ tickLabelPadding WRITE setTickLabelPadding)
1634  Q_PROPERTY(LabelType tickLabelType READ tickLabelType WRITE setTickLabelType)
1635  Q_PROPERTY(QFont tickLabelFont READ tickLabelFont WRITE setTickLabelFont)
1636  Q_PROPERTY(double tickLabelRotation READ tickLabelRotation WRITE setTickLabelRotation)
1637  Q_PROPERTY(QString dateTimeFormat READ dateTimeFormat WRITE setDateTimeFormat)
1638  Q_PROPERTY(QString numberFormat READ numberFormat WRITE setNumberFormat)
1639  Q_PROPERTY(double tickStep READ tickStep WRITE setTickStep)
1640  Q_PROPERTY(QVector<double> tickVector READ tickVector WRITE setTickVector)
1641  Q_PROPERTY(QVector<QString> tickVectorLabels READ tickVectorLabels WRITE setTickVectorLabels)
1642  Q_PROPERTY(int subTickCount READ subTickCount WRITE setSubTickCount)
1643  Q_PROPERTY(QPen basePen READ basePen WRITE setBasePen)
1644  Q_PROPERTY(QPen gridPen READ gridPen WRITE setGridPen)
1645  Q_PROPERTY(QPen subGridPen READ subGridPen WRITE setSubGridPen)
1646  Q_PROPERTY(QPen tickPen READ tickPen WRITE setTickPen)
1647  Q_PROPERTY(QPen subTickPen READ subTickPen WRITE setSubTickPen)
1648  Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont)
1649  Q_PROPERTY(QString label READ label WRITE setLabel)
1650  Q_PROPERTY(int labelPadding READ labelPadding WRITE setLabelPadding)
1652  public:
1658  enum AxisType { atLeft
1659  ,atRight
1660  ,atTop
1661  ,atBottom
1662  };
1663  Q_ENUMS(AxisType)
1670  enum LabelType { ltNumber
1671  ,ltDateTime
1672  };
1673  Q_ENUMS(LabelType)
1678  enum ScaleType { stLinear
1679  ,stLogarithmic
1680  };
1681  Q_ENUMS(ScaleType)
1686  enum SelectablePart { spNone = 0
1687  ,spAxis = 0x001
1688  ,spTickLabels = 0x002
1689  ,spAxisLabel = 0x004
1690  };
1691  Q_ENUMS(SelectablePart)
1692  Q_DECLARE_FLAGS(SelectableParts, SelectablePart)
1693 
1694  explicit QCPAxis(QCustomPlot *parentPlot, AxisType type);
1695  virtual ~QCPAxis();
1696 
1697  // getters:
1698  AxisType axisType() const { return mAxisType; }
1699  QRect axisRect() const { return mAxisRect; }
1700  ScaleType scaleType() const { return mScaleType; }
1701  double scaleLogBase() const { return mScaleLogBase; }
1702  const QCPRange range() const { return mRange; }
1703  bool rangeReversed() const { return mRangeReversed; }
1704  bool antialiasedGrid() const { return mGrid->antialiased(); }
1705  bool antialiasedSubGrid() const { return mGrid->antialiasedSubGrid(); }
1706  bool antialiasedZeroLine() const { return mGrid->antialiasedZeroLine(); }
1707  bool grid() const { return mGrid->visible(); }
1708  bool subGrid() const { return mGrid->subGridVisible(); }
1709  bool autoTicks() const { return mAutoTicks; }
1710  int autoTickCount() const { return mAutoTickCount; }
1711  bool autoTickLabels() const { return mAutoTickLabels; }
1712  bool autoTickStep() const { return mAutoTickStep; }
1713  bool autoSubTicks() const { return mAutoSubTicks; }
1714  bool ticks() const { return mTicks; }
1715  bool tickLabels() const { return mTickLabels; }
1716  int tickLabelPadding() const { return mTickLabelPadding; }
1717  LabelType tickLabelType() const { return mTickLabelType; }
1718  QFont tickLabelFont() const { return mTickLabelFont; }
1719  QColor tickLabelColor() const { return mTickLabelColor; }
1720  double tickLabelRotation() const { return mTickLabelRotation; }
1721  QString dateTimeFormat() const { return mDateTimeFormat; }
1722  QString numberFormat() const;
1723  int numberPrecision() const { return mNumberPrecision; }
1724  double tickStep() const { return mTickStep; }
1725  QVector<double> tickVector() const { return mTickVector; }
1726  QVector<QString> tickVectorLabels() const { return mTickVectorLabels; }
1727  int tickLengthIn() const { return mTickLengthIn; }
1728  int tickLengthOut() const { return mTickLengthOut; }
1729  int subTickCount() const { return mSubTickCount; }
1730  int subTickLengthIn() const { return mSubTickLengthIn; }
1731  int subTickLengthOut() const { return mSubTickLengthOut; }
1732  QPen basePen() const { return mBasePen; }
1733  QPen gridPen() const { return mGrid->pen(); }
1734  QPen subGridPen() const { return mGrid->subGridPen(); }
1735  QPen zeroLinePen() const { return mGrid->zeroLinePen(); }
1736  QPen tickPen() const { return mTickPen; }
1737  QPen subTickPen() const { return mSubTickPen; }
1738  QFont labelFont() const { return mLabelFont; }
1739  QColor labelColor() const { return mLabelColor; }
1740  QString label() const { return mLabel; }
1741  int labelPadding() const { return mLabelPadding; }
1742  int padding() const { return mPadding; }
1743  SelectableParts selected() const { return mSelected; }
1744  SelectableParts selectable() const { return mSelectable; }
1745  QFont selectedTickLabelFont() const { return mSelectedTickLabelFont; }
1746  QFont selectedLabelFont() const { return mSelectedLabelFont; }
1747  QColor selectedTickLabelColor() const { return mSelectedTickLabelColor; }
1748  QColor selectedLabelColor() const { return mSelectedLabelColor; }
1749  QPen selectedBasePen() const { return mSelectedBasePen; }
1750  QPen selectedTickPen() const { return mSelectedTickPen; }
1751  QPen selectedSubTickPen() const { return mSelectedSubTickPen; }
1752 
1753  // setters:
1754  void setScaleType(ScaleType type);
1755  void setScaleLogBase(double base);
1756  void setRange(double lower, double upper);
1757  void setRange(double position, double size, Qt::AlignmentFlag alignment);
1758  void setRangeLower(double lower);
1759  void setRangeUpper(double upper);
1760  void setRangeReversed(bool reversed);
1761  void setAntialiasedGrid(bool enabled);
1762  void setAntialiasedSubGrid(bool enabled);
1763  void setAntialiasedZeroLine(bool enabled);
1764  void setGrid(bool show);
1765  void setSubGrid(bool show);
1766  void setAutoTicks(bool on);
1767  void setAutoTickCount(int approximateCount);
1768  void setAutoTickLabels(bool on);
1769  void setAutoTickStep(bool on);
1770  void setAutoSubTicks(bool on);
1771  void setTicks(bool show);
1772  void setTickLabels(bool show);
1773  void setTickLabelPadding(int padding);
1774  void setTickLabelType(LabelType type);
1775  void setTickLabelFont(const QFont &font);
1776  void setTickLabelColor(const QColor &color);
1777  void setTickLabelRotation(double degrees);
1778  void setDateTimeFormat(const QString &format);
1779  void setNumberFormat(const QString &formatCode);
1780  void setNumberPrecision(int precision);
1781  void setTickStep(double step);
1782  void setTickVector(const QVector<double> &vec);
1783  void setTickVectorLabels(const QVector<QString> &vec);
1784  void setTickLength(int inside, int outside=0);
1785  void setSubTickCount(int count);
1786  void setSubTickLength(int inside, int outside=0);
1787  void setBasePen(const QPen &pen);
1788  void setGridPen(const QPen &pen);
1789  void setSubGridPen(const QPen &pen);
1790  void setZeroLinePen(const QPen &pen);
1791  void setTickPen(const QPen &pen);
1792  void setSubTickPen(const QPen &pen);
1793  void setLabelFont(const QFont &font);
1794  void setLabelColor(const QColor &color);
1795  void setLabel(const QString &str);
1796  void setLabelPadding(int padding);
1797  void setPadding(int padding);
1798  void setSelectedTickLabelFont(const QFont &font);
1799  void setSelectedLabelFont(const QFont &font);
1800  void setSelectedTickLabelColor(const QColor &color);
1801  void setSelectedLabelColor(const QColor &color);
1802  void setSelectedBasePen(const QPen &pen);
1803  void setSelectedTickPen(const QPen &pen);
1804  void setSelectedSubTickPen(const QPen &pen);
1805 
1806  // non-property methods:
1807  Qt::Orientation orientation() const { return mOrientation; }
1808  void moveRange(double diff);
1809  void scaleRange(double factor, double center);
1810  void setScaleRatio(const QCPAxis *otherAxis, double ratio=1.0);
1811  double pixelToCoord(double value) const;
1812  double coordToPixel(double value) const;
1813  SelectablePart selectTest(const QPointF &pos) const;
1814 
1815  public slots:
1816  // slot setters:
1817  void setRange(const QCPRange &range);
1818  void setSelectable(const QCPAxis::SelectableParts &selectable);
1819  void setSelected(const QCPAxis::SelectableParts &selected);
1820 
1821  signals:
1822  void ticksRequest();
1823  void rangeChanged(const QCPRange &newRange);
1824  void selectionChanged(QCPAxis::SelectableParts selection);
1825 
1826  protected:
1827  // simple properties with getters and setters:
1828  QVector<double> mTickVector;
1829  QVector<QString> mTickVectorLabels;
1830  QCPRange mRange;
1831  QString mDateTimeFormat;
1832  QString mLabel;
1833  QRect mAxisRect;
1834  QPen mBasePen, mTickPen, mSubTickPen;
1835  QFont mTickLabelFont, mLabelFont;
1836  QColor mTickLabelColor, mLabelColor;
1837  LabelType mTickLabelType;
1838  ScaleType mScaleType;
1839  AxisType mAxisType;
1840  double mTickStep;
1841  double mScaleLogBase, mScaleLogBaseLogInv;
1842  int mSubTickCount, mTickLengthIn, mTickLengthOut, mSubTickLengthIn, mSubTickLengthOut;
1843  int mAutoTickCount;
1844  int mTickLabelPadding, mLabelPadding, mPadding;
1845  double mTickLabelRotation;
1846  bool mTicks, mTickLabels, mAutoTicks, mAutoTickLabels, mAutoTickStep, mAutoSubTicks;
1847  bool mRangeReversed;
1848  SelectableParts mSelectable, mSelected;
1849  QFont mSelectedTickLabelFont, mSelectedLabelFont;
1850  QColor mSelectedTickLabelColor, mSelectedLabelColor;
1851  QPen mSelectedBasePen, mSelectedTickPen, mSelectedSubTickPen;
1852  QRect mAxisSelectionBox, mTickLabelsSelectionBox, mLabelSelectionBox;
1853 
1854  // internal or not explicitly exposed properties:
1855  QCPGrid *mGrid;
1856  QVector<double> mSubTickVector;
1857  QChar mExponentialChar, mPositiveSignChar;
1858  int mNumberPrecision;
1859  char mNumberFormatChar;
1860  bool mNumberBeautifulPowers, mNumberMultiplyCross;
1861  Qt::Orientation mOrientation;
1862  int mLowestVisibleTick, mHighestVisibleTick;
1863 
1864  // internal setters:
1865  void setAxisType(AxisType type);
1866  void setAxisRect(const QRect &rect);
1867 
1868  // introduced methods:
1869  virtual void setupTickVectors();
1870  virtual void generateAutoTicks();
1871  virtual int calculateAutoSubTickCount(double tickStep) const;
1872  virtual int calculateMargin() const;
1873  virtual bool handleAxisSelection(QMouseEvent *event, bool additiveSelection, bool &modified);
1874 
1875  // drawing:
1876  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
1877  virtual void draw(QCPPainter *painter);
1878  virtual void drawTickLabel(QCPPainter *painter, double position, int distanceToAxis, const QString &text, QSize *tickLabelsSize);
1879  virtual void getMaxTickLabelSize(const QFont &font, const QString &text, QSize *tickLabelsSize) const;
1880 
1881  // basic non virtual helpers:
1882  void visibleTickBounds(int &lowIndex, int &highIndex) const;
1883  double baseLog(double value) const;
1884  double basePow(double value) const;
1885 
1886  // helpers to get the right pen/font depending on selection state:
1887  QPen getBasePen() const;
1888  QPen getTickPen() const;
1889  QPen getSubTickPen() const;
1890  QFont getTickLabelFont() const;
1891  QFont getLabelFont() const;
1892  QColor getTickLabelColor() const;
1893  QColor getLabelColor() const;
1894 
1895  private:
1896  Q_DISABLE_COPY(QCPAxis)
1897 
1898  friend class QCustomPlot;
1899  friend class QCPGrid;
1900  };
1901  Q_DECLARE_OPERATORS_FOR_FLAGS(QCPAxis::SelectableParts)
1902 
1903  class QCP_LIB_DECL QCustomPlot : public QWidget
1904  {
1905  Q_OBJECT
1907  Q_PROPERTY(QString title READ title WRITE setTitle)
1908  Q_PROPERTY(QRect axisRect READ axisRect WRITE setAxisRect)
1909  Q_PROPERTY(int marginLeft READ marginLeft WRITE setMarginLeft)
1910  Q_PROPERTY(int marginRight READ marginRight WRITE setMarginRight)
1911  Q_PROPERTY(int marginTop READ marginTop WRITE setMarginTop)
1912  Q_PROPERTY(int marginBottom READ marginBottom WRITE setMarginBottom)
1913  Q_PROPERTY(int autoMargin READ autoMargin WRITE setAutoMargin)
1914  Q_PROPERTY(QColor color READ color WRITE setColor)
1915  Q_PROPERTY(Qt::Orientations rangeDrag READ rangeDrag WRITE setRangeDrag)
1916  Q_PROPERTY(Qt::Orientations rangeZoom READ rangeZoom WRITE setRangeZoom)
1918  public:
1925  enum Interaction { iRangeDrag = 0x001
1926  ,iRangeZoom = 0x002
1927  ,iMultiSelect = 0x004
1928  ,iSelectTitle = 0x008
1929  ,iSelectPlottables = 0x010
1930  ,iSelectAxes = 0x020
1931  ,iSelectLegend = 0x040
1932  ,iSelectItems = 0x080
1933  };
1934  Q_ENUMS(Interaction)
1935  Q_DECLARE_FLAGS(Interactions, Interaction)
1941  enum LayerInsertMode { limBelow
1942  ,limAbove
1943  };
1944  Q_ENUMS(LayerInsertMode)
1945 
1946  explicit QCustomPlot(QWidget *parent = 0);
1947  virtual ~QCustomPlot();
1948 
1949  // getters:
1950  QString title() const { return mTitle; }
1951  QFont titleFont() const { return mTitleFont; }
1952  QColor titleColor() const { return mTitleColor; }
1953  QRect axisRect() const { return mAxisRect; }
1954  QRect viewport() const { return mViewport; }
1955  int marginLeft() const { return mMarginLeft; }
1956  int marginRight() const { return mMarginRight; }
1957  int marginTop() const { return mMarginTop; }
1958  int marginBottom() const { return mMarginBottom; }
1959  bool autoMargin() const { return mAutoMargin; }
1960  QColor color() const { return mColor; }
1961  Qt::Orientations rangeDrag() const { return mRangeDrag; }
1962  Qt::Orientations rangeZoom() const { return mRangeZoom; }
1963  QCPAxis *rangeDragAxis(Qt::Orientation orientation);
1964  QCPAxis *rangeZoomAxis(Qt::Orientation orientation);
1965  double rangeZoomFactor(Qt::Orientation orientation);
1966  QCP::AntialiasedElements antialiasedElements() const { return mAntialiasedElements; }
1967  QCP::AntialiasedElements notAntialiasedElements() const { return mNotAntialiasedElements; }
1968  bool autoAddPlottableToLegend() const { return mAutoAddPlottableToLegend; }
1969  QPixmap axisBackground() const { return mAxisBackground; }
1970  bool axisBackgroundScaled() const { return mAxisBackgroundScaled; }
1971  Qt::AspectRatioMode axisBackgroundScaledMode() const { return mAxisBackgroundScaledMode; }
1972  const Interactions interactions() const { return mInteractions; }
1973  int selectionTolerance() const { return mSelectionTolerance; }
1974  QFont selectedTitleFont() const { return mSelectedTitleFont; }
1975  QColor selectedTitleColor() const { return mSelectedTitleColor; }
1976  bool titleSelected() const { return mTitleSelected; }
1977  bool noAntialiasingOnDrag() const { return mNoAntialiasingOnDrag; }
1978  QCP::PlottingHints plottingHints() const { return mPlottingHints; }
1979  Qt::KeyboardModifier multiSelectModifier() const { return mMultiSelectModifier; }
1980 
1981  // setters:
1982  void setTitle(const QString &title);
1983  void setTitleFont(const QFont &font);
1984  void setTitleColor(const QColor &color);
1985  void setAxisRect(const QRect &arect);
1986  void setMarginLeft(int margin);
1987  void setMarginRight(int margin);
1988  void setMarginTop(int margin);
1989  void setMarginBottom(int margin);
1990  void setMargin(int left, int right, int top, int bottom);
1991  void setAutoMargin(bool enabled);
1992  void setColor(const QColor &color);
1993  void setRangeDrag(Qt::Orientations orientations);
1994  void setRangeZoom(Qt::Orientations orientations);
1995  void setRangeDragAxes(QCPAxis *horizontal, QCPAxis *vertical);
1996  void setRangeZoomAxes(QCPAxis *horizontal, QCPAxis *vertical);
1997  void setRangeZoomFactor(double horizontalFactor, double verticalFactor);
1998  void setRangeZoomFactor(double factor);
1999  void setAntialiasedElements(const QCP::AntialiasedElements &antialiasedElements);
2000  void setAntialiasedElement(QCP::AntialiasedElement antialiasedElement, bool enabled=true);
2001  void setNotAntialiasedElements(const QCP::AntialiasedElements &notAntialiasedElements);
2002  void setNotAntialiasedElement(QCP::AntialiasedElement notAntialiasedElement, bool enabled=true);
2003  void setAutoAddPlottableToLegend(bool on);
2004  void setAxisBackground(const QPixmap &pm);
2005  void setAxisBackground(const QPixmap &pm, bool scaled, Qt::AspectRatioMode mode=Qt::KeepAspectRatioByExpanding);
2006  void setAxisBackgroundScaled(bool scaled);
2007  void setAxisBackgroundScaledMode(Qt::AspectRatioMode mode);
2008  void setInteractions(const Interactions &interactions);
2009  void setInteraction(const Interaction &interaction, bool enabled=true);
2010  void setSelectionTolerance(int pixels);
2011  void setSelectedTitleFont(const QFont &font);
2012  void setSelectedTitleColor(const QColor &color);
2013  void setTitleSelected(bool selected);
2014  void setNoAntialiasingOnDrag(bool enabled);
2015  void setPlottingHints(const QCP::PlottingHints &hints);
2016  void setPlottingHint(QCP::PlottingHint hint, bool enabled=true);
2017  void setMultiSelectModifier(Qt::KeyboardModifier modifier);
2018 
2019  // non-property methods:
2020  // plottable interface:
2021  QCPAbstractPlottable *plottable(int index);
2022  QCPAbstractPlottable *plottable();
2023  bool addPlottable(QCPAbstractPlottable *plottable);
2024  bool removePlottable(QCPAbstractPlottable *plottable);
2025  bool removePlottable(int index);
2026  int clearPlottables();
2027  int plottableCount() const;
2028  QList<QCPAbstractPlottable*> selectedPlottables() const;
2029  QCPAbstractPlottable *plottableAt(const QPointF &pos, bool onlySelectable=false) const;
2030  bool hasPlottable(QCPAbstractPlottable *plottable) const;
2031 
2032  // specialized interface for QCPGraph:
2033  QCPGraph *graph(int index) const;
2034  QCPGraph *graph() const;
2035  QCPGraph *addGraph(QCPAxis *keyAxis=0, QCPAxis *valueAxis=0);
2036  bool removeGraph(QCPGraph *graph);
2037  bool removeGraph(int index);
2038  int clearGraphs();
2039  int graphCount() const;
2040  QList<QCPGraph*> selectedGraphs() const;
2041 
2042  // item interface:
2043  QCPAbstractItem *item(int index) const;
2044  QCPAbstractItem *item() const;
2045  bool addItem(QCPAbstractItem* item);
2046  bool removeItem(QCPAbstractItem *item);
2047  bool removeItem(int index);
2048  int clearItems();
2049  int itemCount() const;
2050  QList<QCPAbstractItem*> selectedItems() const;
2051  QCPAbstractItem *itemAt(const QPointF &pos, bool onlySelectable=false) const;
2052 
2053  // layer interface:
2054  QCPLayer *layer(const QString &name) const;
2055  QCPLayer *layer(int index) const;
2056  QCPLayer *currentLayer() const;
2057  bool setCurrentLayer(const QString &name);
2058  bool setCurrentLayer(QCPLayer *layer);
2059  int layerCount() const;
2060  bool addLayer(const QString &name, QCPLayer *otherLayer=0, LayerInsertMode insertMode=limAbove);
2061  bool removeLayer(QCPLayer *layer);
2062  bool moveLayer(QCPLayer *layer, QCPLayer *otherLayer, LayerInsertMode insertMode=limAbove);
2063 
2064  QList<QCPAxis*> selectedAxes() const;
2065  QList<QCPLegend*> selectedLegends() const;
2066  void setupFullAxesBox();
2067  bool savePdf(const QString &fileName, bool noCosmeticPen=false, int width=0, int height=0);
2068  bool savePng(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1);
2069  bool saveJpg(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1);
2070  bool saveBmp(const QString &fileName, int width=0, int height=0, double scale=1.0);
2071  bool saveRastered(const QString &fileName, int width, int height, double scale, const char *format, int quality=-1);
2072 
2073  QCPAxis *xAxis, *yAxis, *xAxis2, *yAxis2;
2074  QCPLegend *legend;
2075 
2076  public slots:
2077  void deselectAll();
2078  void replot();
2079  void rescaleAxes();
2080 
2081  signals:
2082  void mouseDoubleClick(QMouseEvent *event);
2083  void mousePress(QMouseEvent *event);
2084  void mouseMove(QMouseEvent *event);
2085  void mouseRelease(QMouseEvent *event);
2086  void mouseWheel(QWheelEvent *event);
2087 
2088  void plottableClick(QCPAbstractPlottable *plottable, QMouseEvent *event);
2089  void plottableDoubleClick(QCPAbstractPlottable *plottable, QMouseEvent *event);
2090  void itemClick(QCPAbstractItem *item, QMouseEvent *event);
2091  void itemDoubleClick(QCPAbstractItem *item, QMouseEvent *event);
2092  void axisClick(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event);
2093  void axisDoubleClick(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event);
2094  void legendClick(QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event);
2095  void legendDoubleClick(QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event);
2096  void titleClick(QMouseEvent *event);
2097  void titleDoubleClick(QMouseEvent *event);
2098 
2099  void selectionChangedByUser();
2100  void beforeReplot();
2101  void afterReplot();
2102 
2103  protected:
2104  QString mTitle;
2105  QFont mTitleFont, mSelectedTitleFont;
2106  QColor mTitleColor, mSelectedTitleColor;
2107  QRect mViewport;
2108  QRect mAxisRect;
2109  int mMarginLeft, mMarginRight, mMarginTop, mMarginBottom;
2110  bool mAutoMargin, mAutoAddPlottableToLegend;
2111  QColor mColor;
2112  QList<QCPAbstractPlottable*> mPlottables;
2113  QList<QCPGraph*> mGraphs; // extra list of items also in mPlottables that are of type QCPGraph
2114  QList<QCPAbstractItem*> mItems;
2115  QList<QCPLayer*> mLayers;
2116  Qt::Orientations mRangeDrag, mRangeZoom;
2117  QCPAxis *mRangeDragHorzAxis, *mRangeDragVertAxis, *mRangeZoomHorzAxis, *mRangeZoomVertAxis;
2118  double mRangeZoomFactorHorz, mRangeZoomFactorVert;
2119  bool mDragging;
2120  QCP::AntialiasedElements mAntialiasedElements, mNotAntialiasedElements;
2121  QPixmap mAxisBackground;
2122  bool mAxisBackgroundScaled;
2123  Qt::AspectRatioMode mAxisBackgroundScaledMode;
2124  Interactions mInteractions;
2125  int mSelectionTolerance;
2126  bool mTitleSelected;
2127  QRect mTitleBoundingBox;
2128  bool mNoAntialiasingOnDrag;
2129  // not explicitly exposed properties:
2130  QPixmap mPaintBuffer;
2131  QPoint mDragStart;
2132  QCPRange mDragStartHorzRange, mDragStartVertRange;
2133  QPixmap mScaledAxisBackground;
2134  bool mReplotting;
2135  QCP::AntialiasedElements mAADragBackup, mNotAADragBackup;
2136  QCPLayer *mCurrentLayer;
2137  QCP::PlottingHints mPlottingHints;
2138  Qt::KeyboardModifier mMultiSelectModifier;
2139 
2140  // reimplemented methods:
2141  virtual QSize minimumSizeHint() const;
2142  virtual void paintEvent(QPaintEvent *event);
2143  virtual void resizeEvent(QResizeEvent *event);
2144  virtual void mouseDoubleClickEvent(QMouseEvent *event);
2145  virtual void mousePressEvent(QMouseEvent *event);
2146  virtual void mouseMoveEvent(QMouseEvent *event);
2147  virtual void mouseReleaseEvent(QMouseEvent *event);
2148  virtual void wheelEvent(QWheelEvent *event);
2149  // event helpers:
2150  virtual bool handlePlottableSelection(QMouseEvent *event, bool additiveSelection, bool &modified);
2151  virtual bool handleItemSelection(QMouseEvent *event, bool additiveSelection, bool &modified);
2152  virtual bool handleAxisSelection(QMouseEvent *event, bool additiveSelection, bool &modified);
2153  virtual bool handleTitleSelection(QMouseEvent *event, bool additiveSelection, bool &modified);
2154 
2155  // introduced methods:
2156  virtual void draw(QCPPainter *painter);
2157  virtual void drawAxisBackground(QCPPainter *painter);
2158 
2159  // helpers:
2160  void updateAxisRect();
2161  bool selectTestTitle(const QPointF &pos) const;
2162  friend class QCPLegend;
2163  friend class QCPAxis;
2164  friend class QCPLayer;
2165  };
2166  Q_DECLARE_OPERATORS_FOR_FLAGS(QCustomPlot::Interactions)
2167 }
2168 Q_DECLARE_TYPEINFO(QCPlot::QCPData, Q_MOVABLE_TYPE);
2169 Q_DECLARE_TYPEINFO(QCPlot::QCPBarData, Q_MOVABLE_TYPE);
2170 Q_DECLARE_TYPEINFO(QCPlot::QCPCurveData, Q_MOVABLE_TYPE);
2171 Q_DECLARE_TYPEINFO(QCPlot::QCPLineEnding, Q_MOVABLE_TYPE);
2172 Q_DECLARE_TYPEINFO(QCPlot::QCPRange, Q_MOVABLE_TYPE);
2173 #endif // QCUSTOMPLOT_H
a square which is not filled, with a plus inside
Definition: qcustomplot.h:93
A curved line from one point to another.
Definition: qcustomplot.h:1180
PositionStyle
Defines where the legend is positioned inside the QCustomPlot axis rect.
Definition: qcustomplot.h:1425
static const double maxRange
Maximum values (negative and positive) the range will accept in range-changing functions.
Definition: qcustomplot.h:1342
0x0002 Grid lines
Definition: qcustomplot.h:111
A layer that may contain objects, to control the rendering order.
Definition: qcustomplot.h:292
LineStyle
Defines how the graph&#39;s line is represented visually in the plot.
Definition: qcustomplot.h:456
The abstract base class for all data representing objects in a plot.
Definition: qcustomplot.h:357
SelectablePart
Defines the selectable parts of an axis.
Definition: qcustomplot.h:1686
ScaleType
Defines the scale of an axis.
Definition: qcustomplot.h:1678
static const double minRange
Minimum range size (upper - lower) the range changing functions will accept.
Definition: qcustomplot.h:1341
an equilateral triangle which is not filled, standing on corner
Definition: qcustomplot.h:91
0x0200 Borders of fills (e.g. under or between graphs)
Definition: qcustomplot.h:119
0x0008 Legend box
Definition: qcustomplot.h:113
a square which is not filled, with a cross inside
Definition: qcustomplot.h:92
a square which is not filled
Definition: qcustomplot.h:87
A plottable representing a single statistical box in a plot.
Definition: qcustomplot.h:698
LayerInsertMode
Defines how a layer should be inserted relative to a specified other layer.
Definition: qcustomplot.h:1941
Responsible for drawing the grid of a QCPAxis.
Definition: qcustomplot.h:1577
a circle which is not filled, with a plus inside
Definition: qcustomplot.h:95
Holds the data of one single data point for QCPGraph.
Definition: qcustomplot.h:142
a circle which is not filled, with one vertical and two downward diagonal lines
Definition: qcustomplot.h:96
PositionType
Defines the ways an item position can be specified.
Definition: qcustomplot.h:802
The central class of the library, the QWidget which displays the plot and interacts with the user...
Definition: qcustomplot.h:1903
The abstract base class for all items in a plot.
Definition: qcustomplot.h:840
An anchor of an item to which positions can be attached to.
Definition: qcustomplot.h:767
QCPBars * barAbove() const
Returns the bars plottable that is directly above this bars plottable.
Definition: qcustomplot.h:658
a diamond which is not filled
Definition: qcustomplot.h:88
void rescaleValueAxis(bool onlyEnlarge=false) const
Rescales the value axis of the plottable so the whole plottable is visible.
Definition: qcustomplot.cpp:8588
Holds the data of one single data point for QCPCurve.
Definition: qcustomplot.h:163
QMap< double, QCPCurveData > QCPCurveDataMap
Container for storing QCPCurveData items in a sorted fashion.
Definition: qcustomplot.h:179
0x0040 Main lines of items
Definition: qcustomplot.h:116
QPainter subclass used internally.
Definition: qcustomplot.h:202
QList< QCPLayerable * > children() const
Returns a list of all layerables on this layer.
Definition: qcustomplot.h:302
0x0001 Axis base line and tick marks
Definition: qcustomplot.h:110
A plottable representing a bar chart in a plot.
Definition: qcustomplot.h:648
QMap< double, QCPBarData > QCPBarDataMap
Container for storing QCPBarData items in a sorted fashion.
Definition: qcustomplot.h:198
LineStyle
Defines how the curve&#39;s line is represented visually in the plot.
Definition: qcustomplot.h:589
ScatterStyle
Defines the symbol used for scatter points.
Definition: qcustomplot.h:81
0xFFFF All elements
Definition: qcustomplot.h:121
A straight line that spans infinitely in both directions.
Definition: qcustomplot.h:900
TracerStyle
The different visual appearances a tracer item can have.
Definition: qcustomplot.h:1271
A plottable representing a parametric curve in a plot.
Definition: qcustomplot.h:580
QList< QCPItemAnchor * > anchors() const
Returns all anchors of the item in a list.
Definition: qcustomplot.h:865
A rectangle.
Definition: qcustomplot.h:1014
Qt::Orientation orientation() const
Returns the orientation of the axis.
Definition: qcustomplot.h:1807
QList< QCPItemPosition * > positions() const
Returns all positions of the item in a list.
Definition: qcustomplot.h:864
a cross (x)
Definition: qcustomplot.h:83
The abstract base class for all items in a QCPLegend.
Definition: qcustomplot.h:1345
0x0020 Main lines of plottables (excluding error bars, see element aeErrorBars)
Definition: qcustomplot.h:115
a circle which is not filled
Definition: qcustomplot.h:85
SelectablePart
Defines the selectable parts of a legend.
Definition: qcustomplot.h:1440
A line from one point to another.
Definition: qcustomplot.h:932
PlottingHint
Defines plotting hints that control various aspects of the quality and speed of plotting.
Definition: qcustomplot.h:130
Manages a single axis inside a QCustomPlot.
Definition: qcustomplot.h:1615
Holds the data of one single data point (one bar) for QCPBars.
Definition: qcustomplot.h:183
AxisType
Defines at which side of the axis rect the axis will appear.
Definition: qcustomplot.h:1658
void rescaleKeyAxis(bool onlyEnlarge=false) const
Rescales the key axis of the plottable so the whole plottable is visible.
Definition: qcustomplot.cpp:8562
0x0000 No elements
Definition: qcustomplot.h:122
Interaction
Defines the mouse interactions possible with QCustomPlot.
Definition: qcustomplot.h:1925
AntialiasedElement
Defines what elements of a plot can be forcibly drawn antialiased/not antialiased.
Definition: qcustomplot.h:110
0x0004 Sub grid lines
Definition: qcustomplot.h:112
a custom pixmap specified by setScatterPixmap, centered on the data point coordinates ...
Definition: qcustomplot.h:97
Manages the position of an item.
Definition: qcustomplot.h:793
A plottable representing a graph in a plot.
Definition: qcustomplot.h:447
A legend item representing a plottable with an icon and the plottable name.
Definition: qcustomplot.h:1392
LabelType
When automatic tick label generation is enabled (setAutoTickLabels), defines how the numerical value ...
Definition: qcustomplot.h:1670
a circle which is not filled, with a cross inside
Definition: qcustomplot.h:94
void rescaleAxes(bool onlyEnlarge=false) const
Rescales the key and value axes associated with this plottable to contain all displayed data...
Definition: qcustomplot.cpp:8551
0x0400 Zero-lines, see QCPAxis::setZeroLinePen
Definition: qcustomplot.h:120
An arbitrary pixmap.
Definition: qcustomplot.h:1058
no scatter symbols are drawn (e.g. in QCPGraph, data only represented with lines) ...
Definition: qcustomplot.h:81
0x0080 Scatter symbols of plottables (excluding scatter symbols of type ssPixmap) ...
Definition: qcustomplot.h:117
a plus (+)
Definition: qcustomplot.h:84
A bracket for referencing/highlighting certain parts in the plot.
Definition: qcustomplot.h:1217
A text label.
Definition: qcustomplot.h:1107
QRect viewport() const
Returns the viewport rect of this QCustomPlot instance.
Definition: qcustomplot.h:1954
QCPBars * barBelow() const
Returns the bars plottable that is directly below this bars plottable.
Definition: qcustomplot.h:657
Handles the different ending decorations for line-like items.
Definition: qcustomplot.h:242
QMap< double, QCPData > QCPDataMap
Container for storing QCPData items in a sorted fashion.
Definition: qcustomplot.h:159
Manages a legend inside a QCustomPlot.
Definition: qcustomplot.h:1418
0x0010 Legend items
Definition: qcustomplot.h:114
Base class for all objects that can be placed on layers.
Definition: qcustomplot.h:318
ErrorType
Defines what kind of error bars are drawn for each data point.
Definition: qcustomplot.h:468
0x000 No hints are set
Definition: qcustomplot.h:130
SignDomain
Represents negative and positive sign domain for passing to getKeyRange and getValueRange.
Definition: qcustomplot.h:408
a circle which is filled with the color of the pen (not the brush!)
Definition: qcustomplot.h:86
a star with eight arms, i.e. a combination of cross and plus
Definition: qcustomplot.h:89
Represents the range an axis is encompassing.
Definition: qcustomplot.h:1326
an equilateral triangle which is not filled, standing on baseline
Definition: qcustomplot.h:90
0x001 Graph/Curve lines are drawn with a faster method. This reduces the quality &lt; especially of the ...
Definition: qcustomplot.h:131
No line is drawn between data points (e.g. only scatters)
Definition: qcustomplot.h:589
EndingStyle
Defines the type of ending decoration for line-like items, e.g.
Definition: qcustomplot.h:256
Item that sticks to QCPGraph data points.
Definition: qcustomplot.h:1262
0x0100 Error bars
Definition: qcustomplot.h:118
0x002 causes an immediate repaint() instead of a soft update() when QCustomPlot::replot() is called...
Definition: qcustomplot.h:133
An ellipse.
Definition: qcustomplot.h:968
BracketStyle
Definition: qcustomplot.h:1221
a single pixel
Definition: qcustomplot.h:82