Main Page · Class Overview · Hierarchy · All Classes
item.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_ITEM_H
27 #define QCP_ITEM_H
28 
29 #include "global.h"
30 #include "layer.h"
31 #include "axis.h"
32 
33 class QCPPainter;
34 class QCustomPlot;
35 class QCPItemPosition;
36 class QCPAbstractItem;
37 class QCPAxisRect;
38 
39 class QCP_LIB_DECL QCPItemAnchor
40 {
41 public:
42  QCPItemAnchor(QCustomPlot *parentPlot, QCPAbstractItem *parentItem, const QString name, int anchorId=-1);
43  virtual ~QCPItemAnchor();
44 
45  // getters:
46  QString name() const { return mName; }
47  virtual QPointF pixelPoint() const;
48 
49 protected:
50  // property members:
51  QString mName;
52 
53  // non-property members:
54  QCustomPlot *mParentPlot;
55  QCPAbstractItem *mParentItem;
56  int mAnchorId;
57  QSet<QCPItemPosition*> mChildren;
58 
59  // introduced virtual methods:
60  virtual QCPItemPosition *toQCPItemPosition() { return 0; }
61 
62  // non-virtual methods:
63  void addChild(QCPItemPosition* pos); // called from pos when this anchor is set as parent
64  void removeChild(QCPItemPosition *pos); // called from pos when its parent anchor is reset or pos deleted
65 
66 private:
67  Q_DISABLE_COPY(QCPItemAnchor)
68 
69  friend class QCPItemPosition;
70 };
71 
72 
73 
74 class QCP_LIB_DECL QCPItemPosition : public QCPItemAnchor
75 {
76 public:
83  enum PositionType { ptAbsolute
84  ,ptViewportRatio
85 
86 
87  ,ptAxisRectRatio
88 
89 
90  ,ptPlotCoords
91  };
92 
93  QCPItemPosition(QCustomPlot *parentPlot, QCPAbstractItem *parentItem, const QString name);
94  virtual ~QCPItemPosition();
95 
96  // getters:
97  PositionType type() const { return mPositionType; }
98  QCPItemAnchor *parentAnchor() const { return mParentAnchor; }
99  double key() const { return mKey; }
100  double value() const { return mValue; }
101  QPointF coords() const { return QPointF(mKey, mValue); }
102  QCPAxis *keyAxis() const { return mKeyAxis.data(); }
103  QCPAxis *valueAxis() const { return mValueAxis.data(); }
104  QCPAxisRect *axisRect() const;
105  virtual QPointF pixelPoint() const;
106 
107  // setters:
108  void setType(PositionType type);
109  bool setParentAnchor(QCPItemAnchor *parentAnchor, bool keepPixelPosition=false);
110  void setCoords(double key, double value);
111  void setCoords(const QPointF &coords);
112  void setAxes(QCPAxis* keyAxis, QCPAxis* valueAxis);
113  void setAxisRect(QCPAxisRect *axisRect);
114  void setPixelPoint(const QPointF &pixelPoint);
115 
116 protected:
117  // property members:
118  PositionType mPositionType;
119  QPointer<QCPAxis> mKeyAxis, mValueAxis;
120  QPointer<QCPAxisRect> mAxisRect;
121  double mKey, mValue;
122  QCPItemAnchor *mParentAnchor;
123 
124  // reimplemented virtual methods:
125  virtual QCPItemPosition *toQCPItemPosition() { return this; }
126 
127 private:
128  Q_DISABLE_COPY(QCPItemPosition)
129 
130 };
131 
132 
133 class QCP_LIB_DECL QCPAbstractItem : public QCPLayerable
134 {
135  Q_OBJECT
137  Q_PROPERTY(bool clipToAxisRect READ clipToAxisRect WRITE setClipToAxisRect)
138  Q_PROPERTY(QCPAxisRect* clipAxisRect READ clipAxisRect WRITE setClipAxisRect)
139  Q_PROPERTY(bool selectable READ selectable WRITE setSelectable NOTIFY selectableChanged)
140  Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectionChanged)
142 public:
143  QCPAbstractItem(QCustomPlot *parentPlot);
144  virtual ~QCPAbstractItem();
145 
146  // getters:
147  bool clipToAxisRect() const { return mClipToAxisRect; }
148  QCPAxisRect *clipAxisRect() const;
149  bool selectable() const { return mSelectable; }
150  bool selected() const { return mSelected; }
151 
152  // setters:
153  void setClipToAxisRect(bool clip);
154  void setClipAxisRect(QCPAxisRect *rect);
155  Q_SLOT void setSelectable(bool selectable);
156  Q_SLOT void setSelected(bool selected);
157 
158  // reimplemented virtual methods:
159  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const = 0;
160 
161  // non-virtual methods:
162  QList<QCPItemPosition*> positions() const { return mPositions; }
163  QList<QCPItemAnchor*> anchors() const { return mAnchors; }
164  QCPItemPosition *position(const QString &name) const;
165  QCPItemAnchor *anchor(const QString &name) const;
166  bool hasAnchor(const QString &name) const;
167 
168 signals:
169  void selectionChanged(bool selected);
170  void selectableChanged(bool selectable);
171 
172 protected:
173  // property members:
174  bool mClipToAxisRect;
175  QPointer<QCPAxisRect> mClipAxisRect;
176  QList<QCPItemPosition*> mPositions;
177  QList<QCPItemAnchor*> mAnchors;
178  bool mSelectable, mSelected;
179 
180  // reimplemented virtual methods:
181  virtual QCP::Interaction selectionCategory() const;
182  virtual QRect clipRect() const;
183  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
184  virtual void draw(QCPPainter *painter) = 0;
185  // events:
186  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
187  virtual void deselectEvent(bool *selectionStateChanged);
188 
189  // introduced virtual methods:
190  virtual QPointF anchorPixelPoint(int anchorId) const;
191 
192  // non-virtual methods:
193  double distSqrToLine(const QPointF &start, const QPointF &end, const QPointF &point) const;
194  double rectSelectTest(const QRectF &rect, const QPointF &pos, bool filledRect) const;
195  QCPItemPosition *createPosition(const QString &name);
196  QCPItemAnchor *createAnchor(const QString &name, int anchorId);
197 
198 private:
199  Q_DISABLE_COPY(QCPAbstractItem)
200 
201  friend class QCustomPlot;
202  friend class QCPItemAnchor;
203 };
204 
205 #endif // QCP_ITEM_H