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