Main Page · Class Overview · Hierarchy · All Classes
plottable.h
1 /***************************************************************************
2 ** **
3 ** QCustomPlot, an easy to use, modern plotting widget for Qt **
4 ** Copyright (C) 2011, 2012, 2013, 2014 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.qcustomplot.com/ **
22 ** Date: 07.04.14 **
23 ** Version: 1.2.1 **
24 ****************************************************************************/
25 
26 #ifndef QCP_PLOTTABLE_H
27 #define QCP_PLOTTABLE_H
28 
29 #include "global.h"
30 #include "range.h"
31 #include "layer.h"
32 #include "axis.h"
33 
34 class QCPPainter;
35 
36 class QCP_LIB_DECL QCPAbstractPlottable : public QCPLayerable
37 {
38  Q_OBJECT
40  Q_PROPERTY(QString name READ name WRITE setName)
41  Q_PROPERTY(bool antialiasedFill READ antialiasedFill WRITE setAntialiasedFill)
42  Q_PROPERTY(bool antialiasedScatters READ antialiasedScatters WRITE setAntialiasedScatters)
43  Q_PROPERTY(bool antialiasedErrorBars READ antialiasedErrorBars WRITE setAntialiasedErrorBars)
44  Q_PROPERTY(QPen pen READ pen WRITE setPen)
45  Q_PROPERTY(QPen selectedPen READ selectedPen WRITE setSelectedPen)
46  Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
47  Q_PROPERTY(QBrush selectedBrush READ selectedBrush WRITE setSelectedBrush)
48  Q_PROPERTY(QCPAxis* keyAxis READ keyAxis WRITE setKeyAxis)
49  Q_PROPERTY(QCPAxis* valueAxis READ valueAxis WRITE setValueAxis)
50  Q_PROPERTY(bool selectable READ selectable WRITE setSelectable NOTIFY selectableChanged)
51  Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectionChanged)
53 public:
54  QCPAbstractPlottable(QCPAxis *keyAxis, QCPAxis *valueAxis);
55 
56  // getters:
57  QString name() const { return mName; }
58  bool antialiasedFill() const { return mAntialiasedFill; }
59  bool antialiasedScatters() const { return mAntialiasedScatters; }
60  bool antialiasedErrorBars() const { return mAntialiasedErrorBars; }
61  QPen pen() const { return mPen; }
62  QPen selectedPen() const { return mSelectedPen; }
63  QBrush brush() const { return mBrush; }
64  QBrush selectedBrush() const { return mSelectedBrush; }
65  QCPAxis *keyAxis() const { return mKeyAxis.data(); }
66  QCPAxis *valueAxis() const { return mValueAxis.data(); }
67  bool selectable() const { return mSelectable; }
68  bool selected() const { return mSelected; }
69 
70  // setters:
71  void setName(const QString &name);
72  void setAntialiasedFill(bool enabled);
73  void setAntialiasedScatters(bool enabled);
74  void setAntialiasedErrorBars(bool enabled);
75  void setPen(const QPen &pen);
76  void setSelectedPen(const QPen &pen);
77  void setBrush(const QBrush &brush);
78  void setSelectedBrush(const QBrush &brush);
79  void setKeyAxis(QCPAxis *axis);
80  void setValueAxis(QCPAxis *axis);
81  Q_SLOT void setSelectable(bool selectable);
82  Q_SLOT void setSelected(bool selected);
83 
84  // introduced virtual methods:
85  virtual void clearData() = 0;
86  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const = 0;
87  virtual bool addToLegend();
88  virtual bool removeFromLegend() const;
89 
90  // non-property methods:
91  void rescaleAxes(bool onlyEnlarge=false) const;
92  void rescaleKeyAxis(bool onlyEnlarge=false) const;
93  void rescaleValueAxis(bool onlyEnlarge=false) const;
94 
95 signals:
96  void selectionChanged(bool selected);
97  void selectableChanged(bool selectable);
98 
99 protected:
103  enum SignDomain { sdNegative
104  ,sdBoth
105  ,sdPositive
106  };
107 
108  // property members:
109  QString mName;
110  bool mAntialiasedFill, mAntialiasedScatters, mAntialiasedErrorBars;
111  QPen mPen, mSelectedPen;
112  QBrush mBrush, mSelectedBrush;
113  QPointer<QCPAxis> mKeyAxis, mValueAxis;
114  bool mSelectable, mSelected;
115 
116  // reimplemented virtual methods:
117  virtual QRect clipRect() const;
118  virtual void draw(QCPPainter *painter) = 0;
119  virtual QCP::Interaction selectionCategory() const;
120  void applyDefaultAntialiasingHint(QCPPainter *painter) const;
121  // events:
122  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
123  virtual void deselectEvent(bool *selectionStateChanged);
124 
125  // introduced virtual methods:
126  virtual void drawLegendIcon(QCPPainter *painter, const QRectF &rect) const = 0;
127  virtual QCPRange getKeyRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const = 0;
128  virtual QCPRange getValueRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const = 0;
129 
130  // non-virtual methods:
131  void coordsToPixels(double key, double value, double &x, double &y) const;
132  const QPointF coordsToPixels(double key, double value) const;
133  void pixelsToCoords(double x, double y, double &key, double &value) const;
134  void pixelsToCoords(const QPointF &pixelPos, double &key, double &value) const;
135  QPen mainPen() const;
136  QBrush mainBrush() const;
137  void applyFillAntialiasingHint(QCPPainter *painter) const;
138  void applyScattersAntialiasingHint(QCPPainter *painter) const;
139  void applyErrorBarsAntialiasingHint(QCPPainter *painter) const;
140  double distSqrToLine(const QPointF &start, const QPointF &end, const QPointF &point) const;
141 
142 private:
143  Q_DISABLE_COPY(QCPAbstractPlottable)
144 
145  friend class QCustomPlot;
146  friend class QCPAxis;
147  friend class QCPPlottableLegendItem;
148 };
149 
150 #endif // QCP_PLOTTABLE_H