Main Page · Class Overview · Hierarchy · All Classes
core.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_CORE_H
27 #define QCP_CORE_H
28 
29 #include "global.h"
30 #include "range.h"
31 #include "axis.h"
32 
33 class QCPPainter;
34 class QCPLayer;
36 class QCPAbstractItem;
37 class QCPGraph;
38 class QCPPlotTitle;
39 class QCPLegend;
41 
42 class QCP_LIB_DECL QCustomPlot : public QWidget
43 {
44  Q_OBJECT
46  Q_PROPERTY(QRect viewport READ viewport WRITE setViewport)
47  Q_PROPERTY(QPixmap background READ background WRITE setBackground)
48  Q_PROPERTY(bool backgroundScaled READ backgroundScaled WRITE setBackgroundScaled)
49  Q_PROPERTY(Qt::AspectRatioMode backgroundScaledMode READ backgroundScaledMode WRITE setBackgroundScaledMode)
50  Q_PROPERTY(QCPLayoutGrid* plotLayout READ plotLayout)
51  Q_PROPERTY(bool autoAddPlottableToLegend READ autoAddPlottableToLegend WRITE setAutoAddPlottableToLegend)
52  Q_PROPERTY(int selectionTolerance READ selectionTolerance WRITE setSelectionTolerance)
53  Q_PROPERTY(bool noAntialiasingOnDrag READ noAntialiasingOnDrag WRITE setNoAntialiasingOnDrag)
54  Q_PROPERTY(Qt::KeyboardModifier multiSelectModifier READ multiSelectModifier WRITE setMultiSelectModifier)
56 public:
62  enum LayerInsertMode { limBelow
63  ,limAbove
64  };
65  Q_ENUMS(LayerInsertMode)
66 
67  explicit QCustomPlot(QWidget *parent = 0);
68  virtual ~QCustomPlot();
69 
70  // getters:
71  QRect viewport() const { return mViewport; }
72  QPixmap background() const { return mBackgroundPixmap; }
73  bool backgroundScaled() const { return mBackgroundScaled; }
74  Qt::AspectRatioMode backgroundScaledMode() const { return mBackgroundScaledMode; }
75  QCPLayoutGrid *plotLayout() const { return mPlotLayout; }
76  QCP::AntialiasedElements antialiasedElements() const { return mAntialiasedElements; }
77  QCP::AntialiasedElements notAntialiasedElements() const { return mNotAntialiasedElements; }
78  bool autoAddPlottableToLegend() const { return mAutoAddPlottableToLegend; }
79  const QCP::Interactions interactions() const { return mInteractions; }
80  int selectionTolerance() const { return mSelectionTolerance; }
81  bool noAntialiasingOnDrag() const { return mNoAntialiasingOnDrag; }
82  QCP::PlottingHints plottingHints() const { return mPlottingHints; }
83  Qt::KeyboardModifier multiSelectModifier() const { return mMultiSelectModifier; }
84 
85  // setters:
86  void setViewport(const QRect &rect);
87  void setBackground(const QPixmap &pm);
88  void setBackground(const QPixmap &pm, bool scaled, Qt::AspectRatioMode mode=Qt::KeepAspectRatioByExpanding);
89  void setBackground(const QBrush &brush);
90  void setBackgroundScaled(bool scaled);
91  void setBackgroundScaledMode(Qt::AspectRatioMode mode);
92  void setAntialiasedElements(const QCP::AntialiasedElements &antialiasedElements);
93  void setAntialiasedElement(QCP::AntialiasedElement antialiasedElement, bool enabled=true);
94  void setNotAntialiasedElements(const QCP::AntialiasedElements &notAntialiasedElements);
95  void setNotAntialiasedElement(QCP::AntialiasedElement notAntialiasedElement, bool enabled=true);
96  void setAutoAddPlottableToLegend(bool on);
97  void setInteractions(const QCP::Interactions &interactions);
98  void setInteraction(const QCP::Interaction &interaction, bool enabled=true);
99  void setSelectionTolerance(int pixels);
100  void setNoAntialiasingOnDrag(bool enabled);
101  void setPlottingHints(const QCP::PlottingHints &hints);
102  void setPlottingHint(QCP::PlottingHint hint, bool enabled=true);
103  void setMultiSelectModifier(Qt::KeyboardModifier modifier);
104 
105  // non-property methods:
106  // plottable interface:
107  QCPAbstractPlottable *plottable(int index);
108  QCPAbstractPlottable *plottable();
109  bool addPlottable(QCPAbstractPlottable *plottable);
110  bool removePlottable(QCPAbstractPlottable *plottable);
111  bool removePlottable(int index);
112  int clearPlottables();
113  int plottableCount() const;
114  QList<QCPAbstractPlottable*> selectedPlottables() const;
115  QCPAbstractPlottable *plottableAt(const QPointF &pos, bool onlySelectable=false) const;
116  bool hasPlottable(QCPAbstractPlottable *plottable) const;
117 
118  // specialized interface for QCPGraph:
119  QCPGraph *graph(int index) const;
120  QCPGraph *graph() const;
121  QCPGraph *addGraph(QCPAxis *keyAxis=0, QCPAxis *valueAxis=0);
122  bool removeGraph(QCPGraph *graph);
123  bool removeGraph(int index);
124  int clearGraphs();
125  int graphCount() const;
126  QList<QCPGraph*> selectedGraphs() const;
127 
128  // item interface:
129  QCPAbstractItem *item(int index) const;
130  QCPAbstractItem *item() const;
131  bool addItem(QCPAbstractItem* item);
132  bool removeItem(QCPAbstractItem *item);
133  bool removeItem(int index);
134  int clearItems();
135  int itemCount() const;
136  QList<QCPAbstractItem*> selectedItems() const;
137  QCPAbstractItem *itemAt(const QPointF &pos, bool onlySelectable=false) const;
138  bool hasItem(QCPAbstractItem *item) const;
139 
140  // layer interface:
141  QCPLayer *layer(const QString &name) const;
142  QCPLayer *layer(int index) const;
143  QCPLayer *currentLayer() const;
144  bool setCurrentLayer(const QString &name);
145  bool setCurrentLayer(QCPLayer *layer);
146  int layerCount() const;
147  bool addLayer(const QString &name, QCPLayer *otherLayer=0, LayerInsertMode insertMode=limAbove);
148  bool removeLayer(QCPLayer *layer);
149  bool moveLayer(QCPLayer *layer, QCPLayer *otherLayer, LayerInsertMode insertMode=limAbove);
150 
151  // axis rect/layout interface:
152  int axisRectCount() const;
153  QCPAxisRect* axisRect(int index=0) const;
154  QList<QCPAxisRect*> axisRects() const;
155  QCPLayoutElement* layoutElementAt(const QPointF &pos) const;
156  Q_SLOT void rescaleAxes(bool onlyVisiblePlottables=false);
157 
158  QList<QCPAxis*> selectedAxes() const;
159  QList<QCPLegend*> selectedLegends() const;
160  Q_SLOT void deselectAll();
161 
162  bool savePdf(const QString &fileName, bool noCosmeticPen=false, int width=0, int height=0);
163  bool savePng(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1);
164  bool saveJpg(const QString &fileName, int width=0, int height=0, double scale=1.0, int quality=-1);
165  bool saveBmp(const QString &fileName, int width=0, int height=0, double scale=1.0);
166  bool saveRastered(const QString &fileName, int width, int height, double scale, const char *format, int quality=-1);
167  QPixmap toPixmap(int width=0, int height=0, double scale=1.0);
168  void toPainter(QCPPainter *painter, int width=0, int height=0);
169  Q_SLOT void replot();
170 
171  QCPAxis *xAxis, *yAxis, *xAxis2, *yAxis2;
172  QCPLegend *legend;
173 
174 signals:
175  void mouseDoubleClick(QMouseEvent *event);
176  void mousePress(QMouseEvent *event);
177  void mouseMove(QMouseEvent *event);
178  void mouseRelease(QMouseEvent *event);
179  void mouseWheel(QWheelEvent *event);
180 
181  void plottableClick(QCPAbstractPlottable *plottable, QMouseEvent *event);
182  void plottableDoubleClick(QCPAbstractPlottable *plottable, QMouseEvent *event);
183  void itemClick(QCPAbstractItem *item, QMouseEvent *event);
184  void itemDoubleClick(QCPAbstractItem *item, QMouseEvent *event);
185  void axisClick(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event);
186  void axisDoubleClick(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event);
187  void legendClick(QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event);
188  void legendDoubleClick(QCPLegend *legend, QCPAbstractLegendItem *item, QMouseEvent *event);
189  void titleClick(QMouseEvent *event, QCPPlotTitle *title);
190  void titleDoubleClick(QMouseEvent *event, QCPPlotTitle *title);
191 
192  void selectionChangedByUser();
193  void beforeReplot();
194  void afterReplot();
195 
196 protected:
197  // property members:
198  QRect mViewport;
199  QCPLayoutGrid *mPlotLayout;
200  bool mAutoAddPlottableToLegend;
201  QList<QCPAbstractPlottable*> mPlottables;
202  QList<QCPGraph*> mGraphs; // extra list of plottables also in mPlottables that are of type QCPGraph
203  QList<QCPAbstractItem*> mItems;
204  QList<QCPLayer*> mLayers;
205  QCP::AntialiasedElements mAntialiasedElements, mNotAntialiasedElements;
206  QCP::Interactions mInteractions;
207  int mSelectionTolerance;
208  bool mNoAntialiasingOnDrag;
209  QBrush mBackgroundBrush;
210  QPixmap mBackgroundPixmap;
211  QPixmap mScaledBackgroundPixmap;
212  bool mBackgroundScaled;
213  Qt::AspectRatioMode mBackgroundScaledMode;
214  QCPLayer *mCurrentLayer;
215  QCP::PlottingHints mPlottingHints;
216  Qt::KeyboardModifier mMultiSelectModifier;
217 
218  // non-property members:
219  QPixmap mPaintBuffer;
220  QPoint mMousePressPos;
221  QCPLayoutElement *mMouseEventElement;
222  bool mReplotting;
223 
224  // reimplemented virtual methods:
225  virtual QSize minimumSizeHint() const;
226  virtual QSize sizeHint() const;
227  virtual void paintEvent(QPaintEvent *event);
228  virtual void resizeEvent(QResizeEvent *event);
229  virtual void mouseDoubleClickEvent(QMouseEvent *event);
230  virtual void mousePressEvent(QMouseEvent *event);
231  virtual void mouseMoveEvent(QMouseEvent *event);
232  virtual void mouseReleaseEvent(QMouseEvent *event);
233  virtual void wheelEvent(QWheelEvent *event);
234 
235  // introduced virtual methods:
236  virtual void draw(QCPPainter *painter);
237  virtual void axisRemoved(QCPAxis *axis);
238  virtual void legendRemoved(QCPLegend *legend);
239 
240  // non-virtual methods:
241  void updateLayerIndices() const;
242  QCPLayerable *layerableAt(const QPointF &pos, bool onlySelectable, QVariant *selectionDetails=0) const;
243  void drawBackground(QCPPainter *painter);
244 
245  friend class QCPLegend;
246  friend class QCPAxis;
247  friend class QCPLayer;
248  friend class QCPAxisRect;
249 };
250 
251 #endif // QCP_CORE_H