Main Page · Class Overview · Hierarchy · All Classes
plottable-bars.h
Go to the documentation of this file.
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 ****************************************************************************/
26 #ifndef QCP_PLOTTABLE_BARS_H
27 #define QCP_PLOTTABLE_BARS_H
28 
29 #include "../global.h"
30 #include "../range.h"
31 #include "../plottable.h"
32 
33 class QCPPainter;
34 class QCPAxis;
35 class QCPBars;
36 
37 class QCP_LIB_DECL QCPBarsGroup : public QObject
38 {
39  Q_OBJECT
41  Q_PROPERTY(SpacingType spacingType READ spacingType WRITE setSpacingType)
42  Q_PROPERTY(double spacing READ spacing WRITE setSpacing)
44 public:
51  enum SpacingType { stAbsolute
52  ,stAxisRectRatio
53  ,stPlotCoords
54  };
55  QCPBarsGroup(QCustomPlot *parentPlot);
56  ~QCPBarsGroup();
57 
58  // getters:
59  SpacingType spacingType() const { return mSpacingType; }
60  double spacing() const { return mSpacing; }
61 
62  // setters:
63  void setSpacingType(SpacingType spacingType);
64  void setSpacing(double spacing);
65 
66  // non-virtual methods:
67  QList<QCPBars*> bars() const { return mBars; }
68  QCPBars* bars(int index) const;
69  int size() const { return mBars.size(); }
70  bool isEmpty() const { return mBars.isEmpty(); }
71  void clear();
72  bool contains(QCPBars *bars) const { return mBars.contains(bars); }
73  void append(QCPBars *bars);
74  void insert(int i, QCPBars *bars);
75  void remove(QCPBars *bars);
76 
77 protected:
78  // non-property members:
79  QCustomPlot *mParentPlot;
80  SpacingType mSpacingType;
81  double mSpacing;
82  QList<QCPBars*> mBars;
83 
84  // non-virtual methods:
85  void registerBars(QCPBars *bars);
86  void unregisterBars(QCPBars *bars);
87 
88  // virtual methods:
89  double keyPixelOffset(const QCPBars *bars, double keyCoord);
90  double getPixelSpacing(const QCPBars *bars, double keyCoord);
91 
92 private:
93  Q_DISABLE_COPY(QCPBarsGroup)
94 
95  friend class QCPBars;
96 };
97 
98 
99 class QCP_LIB_DECL QCPBarData
100 {
101 public:
102  QCPBarData();
103  QCPBarData(double key, double value);
104  double key, value;
105 };
106 Q_DECLARE_TYPEINFO(QCPBarData, Q_MOVABLE_TYPE);
107 
115 typedef QMap<double, QCPBarData> QCPBarDataMap;
116 typedef QMapIterator<double, QCPBarData> QCPBarDataMapIterator;
117 typedef QMutableMapIterator<double, QCPBarData> QCPBarDataMutableMapIterator;
118 
119 
120 class QCP_LIB_DECL QCPBars : public QCPAbstractPlottable
121 {
122  Q_OBJECT
124  Q_PROPERTY(double width READ width WRITE setWidth)
125  Q_PROPERTY(WidthType widthType READ widthType WRITE setWidthType)
126  Q_PROPERTY(QCPBarsGroup* barsGroup READ barsGroup WRITE setBarsGroup)
127  Q_PROPERTY(double baseValue READ baseValue WRITE setBaseValue)
128  Q_PROPERTY(QCPBars* barBelow READ barBelow)
129  Q_PROPERTY(QCPBars* barAbove READ barAbove)
131 public:
138  enum WidthType { wtAbsolute
139  ,wtAxisRectRatio
140  ,wtPlotCoords
141  };
142  Q_ENUMS(WidthType)
143 
144  explicit QCPBars(QCPAxis *keyAxis, QCPAxis *valueAxis);
145  virtual ~QCPBars();
146 
147  // getters:
148  double width() const { return mWidth; }
149  WidthType widthType() const { return mWidthType; }
150  QCPBarsGroup *barsGroup() const { return mBarsGroup; }
151  double baseValue() const { return mBaseValue; }
152  QCPBars *barBelow() const { return mBarBelow.data(); }
153  QCPBars *barAbove() const { return mBarAbove.data(); }
154  QCPBarDataMap *data() const { return mData; }
155 
156  // setters:
157  void setWidth(double width);
158  void setWidthType(WidthType widthType);
159  void setBarsGroup(QCPBarsGroup *barsGroup);
160  void setBaseValue(double baseValue);
161  void setData(QCPBarDataMap *data, bool copy=false);
162  void setData(const QVector<double> &key, const QVector<double> &value);
163 
164  // non-property methods:
165  void moveBelow(QCPBars *bars);
166  void moveAbove(QCPBars *bars);
167  void addData(const QCPBarDataMap &dataMap);
168  void addData(const QCPBarData &data);
169  void addData(double key, double value);
170  void addData(const QVector<double> &keys, const QVector<double> &values);
171  void removeDataBefore(double key);
172  void removeDataAfter(double key);
173  void removeData(double fromKey, double toKey);
174  void removeData(double key);
175 
176  // reimplemented virtual methods:
177  virtual void clearData();
178  virtual double selectTest(const QPointF &pos, bool onlySelectable, QVariant *details=0) const;
179 
180 protected:
181  // property members:
182  QCPBarDataMap *mData;
183  double mWidth;
184  WidthType mWidthType;
185  QCPBarsGroup *mBarsGroup;
186  double mBaseValue;
187  QPointer<QCPBars> mBarBelow, mBarAbove;
188 
189  // reimplemented virtual methods:
190  virtual void draw(QCPPainter *painter);
191  virtual void drawLegendIcon(QCPPainter *painter, const QRectF &rect) const;
192  virtual QCPRange getKeyRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const;
193  virtual QCPRange getValueRange(bool &foundRange, SignDomain inSignDomain=sdBoth) const;
194 
195  // non-virtual methods:
196  void getVisibleDataBounds(QCPBarDataMap::const_iterator &lower, QCPBarDataMap::const_iterator &upperEnd) const;
197  QPolygonF getBarPolygon(double key, double value) const;
198  void getPixelWidth(double key, double &lower, double &upper) const;
199  double getStackedBaseValue(double key, bool positive) const;
200  static void connectBars(QCPBars* lower, QCPBars* upper);
201 
202  friend class QCustomPlot;
203  friend class QCPLegend;
204  friend class QCPBarsGroup;
205 };
206 
207 #endif // QCP_PLOTTABLE_BARS_H