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: 11.10.14 **
23 ** Version: 1.3.0-beta **
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*> mChildrenX, mChildrenY;
58 
59  // introduced virtual methods:
60  virtual QCPItemPosition *toQCPItemPosition() { return 0; }
61 
62  // non-virtual methods:
63  void addChildX(QCPItemPosition* pos); // called from pos when this anchor is set as parent
64  void removeChildX(QCPItemPosition *pos); // called from pos when its parent anchor is reset or pos deleted
65  void addChildY(QCPItemPosition* pos); // called from pos when this anchor is set as parent
66  void removeChildY(QCPItemPosition *pos); // called from pos when its parent anchor is reset or pos deleted
67 
68 private:
69  Q_DISABLE_COPY(QCPItemAnchor)
70 
71  friend class QCPItemPosition;
72 };
73 
74 
75 
76 class QCP_LIB_DECL QCPItemPosition : public QCPItemAnchor
77 {
78 public:
85  enum PositionType { ptAbsolute
86  ,ptViewportRatio
87 
88 
89  ,ptAxisRectRatio
90 
91 
92  ,ptPlotCoords
93  };
94 
95  QCPItemPosition(QCustomPlot *parentPlot, QCPAbstractItem *parentItem, const QString name);
96  virtual ~QCPItemPosition();
97 
98  // getters:
99  PositionType type() const { return typeX(); }
100  PositionType typeX() const { return mPositionTypeX; }
101  PositionType typeY() const { return mPositionTypeY; }
102  QCPItemAnchor *parentAnchor() const { return parentAnchorX(); }
103  QCPItemAnchor *parentAnchorX() const { return mParentAnchorX; }
104  QCPItemAnchor *parentAnchorY() const { return mParentAnchorY; }
105  double key() const { return mKey; }
106  double value() const { return mValue; }
107  QPointF coords() const { return QPointF(mKey, mValue); }
108  QCPAxis *keyAxis() const { return mKeyAxis.data(); }
109  QCPAxis *valueAxis() const { return mValueAxis.data(); }
110  QCPAxisRect *axisRect() const;
111  virtual QPointF pixelPoint() const;
112 
113  // setters:
114  void setType(PositionType type);
115  void setTypeX(PositionType type);
116  void setTypeY(PositionType type);
117  bool setParentAnchor(QCPItemAnchor *parentAnchor, bool keepPixelPosition=false);
118  bool setParentAnchorX(QCPItemAnchor *parentAnchor, bool keepPixelPosition=false);
119  bool setParentAnchorY(QCPItemAnchor *parentAnchor, bool keepPixelPosition=false);
120  void setCoords(double key, double value);
121  void setCoords(const QPointF &coords);
122  void setAxes(QCPAxis* keyAxis, QCPAxis* valueAxis);
123  void setAxisRect(QCPAxisRect *axisRect);
124  void setPixelPoint(const QPointF &pixelPoint);
125 
126 protected:
127  // property members:
128  PositionType mPositionTypeX, mPositionTypeY;
129  QPointer<QCPAxis> mKeyAxis, mValueAxis;
130  QPointer<QCPAxisRect> mAxisRect;
131  double mKey, mValue;
132  QCPItemAnchor *mParentAnchorX, *mParentAnchorY;
133 
134  // reimplemented virtual methods:
135  virtual QCPItemPosition *toQCPItemPosition() { return this; }
136 
137 private:
138  Q_DISABLE_COPY(QCPItemPosition)
139 
140 };
141 
142 
143 class QCP_LIB_DECL QCPAbstractItem : public QCPLayerable
144 {
145  Q_OBJECT
147  Q_PROPERTY(bool clipToAxisRect READ clipToAxisRect WRITE setClipToAxisRect)
148  Q_PROPERTY(QCPAxisRect* clipAxisRect READ clipAxisRect WRITE setClipAxisRect)
149  Q_PROPERTY(bool selectable READ selectable WRITE setSelectable NOTIFY selectableChanged)
150  Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectionChanged)
152 public:
153  QCPAbstractItem(QCustomPlot *parentPlot);
154  virtual ~QCPAbstractItem();
155 
156  // getters:
157  bool clipToAxisRect() const { return mClipToAxisRect; }
158  QCPAxisRect *clipAxisRect() const;
159  bool selectable() const { return mSelectable; }
160  bool selected() const { return mSelected; }
161 
162  // setters:
163  void setClipToAxisRect(bool clip);
164  void setClipAxisRect(QCPAxisRect *rect);
165  Q_SLOT void setSelectable(bool selectable);
166  Q_SLOT void setSelected(bool selected);
167 
168  // reimplemented virtual methods:
169  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const = 0;
170 
171  // non-virtual methods:
172  QList<QCPItemPosition*> positions() const { return mPositions; }
173  QList<QCPItemAnchor*> anchors() const { return mAnchors; }
174  QCPItemPosition *position(const QString &name) const;
175  QCPItemAnchor *anchor(const QString &name) const;
176  bool hasAnchor(const QString &name) const;
177 
178 signals:
179  void selectionChanged(bool selected);
180  void selectableChanged(bool selectable);
181 
182 protected:
183  // property members:
184  bool mClipToAxisRect;
185  QPointer<QCPAxisRect> mClipAxisRect;
186  QList<QCPItemPosition*> mPositions;
187  QList<QCPItemAnchor*> mAnchors;
188  bool mSelectable, mSelected;
189 
190  // reimplemented virtual methods:
191  virtual QCP::Interaction selectionCategory() const;
192  virtual QRect clipRect() const;
193  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
194  virtual void draw(QCPPainter *painter) = 0;
195  // events:
196  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
197  virtual void deselectEvent(bool *selectionStateChanged);
198 
199  // introduced virtual methods:
200  virtual QPointF anchorPixelPoint(int anchorId) const;
201 
202  // non-virtual methods:
203  double distSqrToLine(const QPointF &start, const QPointF &end, const QPointF &point) const;
204  double rectSelectTest(const QRectF &rect, const QPointF &pos, bool filledRect) const;
205  QCPItemPosition *createPosition(const QString &name);
206  QCPItemAnchor *createAnchor(const QString &name, int anchorId);
207 
208 private:
209  Q_DISABLE_COPY(QCPAbstractItem)
210 
211  friend class QCustomPlot;
212  friend class QCPItemAnchor;
213 };
214 
215 #endif // QCP_ITEM_H