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 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: 04.11.13 **
23 ** Version: 1.1.0 **
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  ,ptAxisRectRatio
86  ,ptPlotCoords
87  };
88 
89  QCPItemPosition(QCustomPlot *parentPlot, QCPAbstractItem *parentItem, const QString name);
90  virtual ~QCPItemPosition();
91 
92  // getters:
93  PositionType type() const { return mPositionType; }
94  QCPItemAnchor *parentAnchor() const { return mParentAnchor; }
95  double key() const { return mKey; }
96  double value() const { return mValue; }
97  QPointF coords() const { return QPointF(mKey, mValue); }
98  QCPAxis *keyAxis() const { return mKeyAxis.data(); }
99  QCPAxis *valueAxis() const { return mValueAxis.data(); }
100  QCPAxisRect *axisRect() const;
101  virtual QPointF pixelPoint() const;
102 
103  // setters:
104  void setType(PositionType type);
105  bool setParentAnchor(QCPItemAnchor *parentAnchor, bool keepPixelPosition=false);
106  void setCoords(double key, double value);
107  void setCoords(const QPointF &coords);
108  void setAxes(QCPAxis* keyAxis, QCPAxis* valueAxis);
109  void setAxisRect(QCPAxisRect *axisRect);
110  void setPixelPoint(const QPointF &pixelPoint);
111 
112 protected:
113  // property members:
114  PositionType mPositionType;
115  QPointer<QCPAxis> mKeyAxis, mValueAxis;
116  QPointer<QCPAxisRect> mAxisRect;
117  double mKey, mValue;
118  QCPItemAnchor *mParentAnchor;
119 
120  // reimplemented virtual methods:
121  virtual QCPItemPosition *toQCPItemPosition() { return this; }
122 
123 private:
124  Q_DISABLE_COPY(QCPItemPosition)
125 
126 };
127 
128 
129 class QCP_LIB_DECL QCPAbstractItem : public QCPLayerable
130 {
131  Q_OBJECT
133  Q_PROPERTY(bool clipToAxisRect READ clipToAxisRect WRITE setClipToAxisRect)
134  Q_PROPERTY(QCPAxisRect* clipAxisRect READ clipAxisRect WRITE setClipAxisRect)
135  Q_PROPERTY(bool selectable READ selectable WRITE setSelectable)
136  Q_PROPERTY(bool selected READ selected WRITE setSelected)
138 public:
139  QCPAbstractItem(QCustomPlot *parentPlot);
140  virtual ~QCPAbstractItem();
141 
142  // getters:
143  bool clipToAxisRect() const { return mClipToAxisRect; }
144  QCPAxisRect *clipAxisRect() const;
145  bool selectable() const { return mSelectable; }
146  bool selected() const { return mSelected; }
147 
148  // setters:
149  void setClipToAxisRect(bool clip);
150  void setClipAxisRect(QCPAxisRect *rect);
151  void setSelectable(bool selectable);
152  void setSelected(bool selected);
153 
154  // reimplemented virtual methods:
155  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const = 0;
156 
157  // non-virtual methods:
158  QList<QCPItemPosition*> positions() const { return mPositions; }
159  QList<QCPItemAnchor*> anchors() const { return mAnchors; }
160  QCPItemPosition *position(const QString &name) const;
161  QCPItemAnchor *anchor(const QString &name) const;
162  bool hasAnchor(const QString &name) const;
163 
164 signals:
165  void selectionChanged(bool selected);
166 
167 protected:
168  // property members:
169  bool mClipToAxisRect;
170  QPointer<QCPAxisRect> mClipAxisRect;
171  QList<QCPItemPosition*> mPositions;
172  QList<QCPItemAnchor*> mAnchors;
173  bool mSelectable, mSelected;
174 
175  // reimplemented virtual methods:
176  virtual QCP::Interaction selectionCategory() const;
177  virtual QRect clipRect() const;
178  virtual void applyDefaultAntialiasingHint(QCPPainter *painter) const;
179  virtual void draw(QCPPainter *painter) = 0;
180  // events:
181  virtual void selectEvent(QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged);
182  virtual void deselectEvent(bool *selectionStateChanged);
183 
184  // introduced virtual methods:
185  virtual QPointF anchorPixelPoint(int anchorId) const;
186 
187  // non-virtual methods:
188  double distSqrToLine(const QPointF &start, const QPointF &end, const QPointF &point) const;
189  double rectSelectTest(const QRectF &rect, const QPointF &pos, bool filledRect) const;
190  QCPItemPosition *createPosition(const QString &name);
191  QCPItemAnchor *createAnchor(const QString &name, int anchorId);
192 
193 private:
194  Q_DISABLE_COPY(QCPAbstractItem)
195 
196  friend class QCustomPlot;
197  friend class QCPItemAnchor;
198 };
199 
200 #endif // QCP_ITEM_H