1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997   Josef Wilgen
4  * Copyright (C) 2002   Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #ifndef QWT_PLOT_H
11 #define QWT_PLOT_H
12 
13 #include <qframe.h>
14 #include "qwt_global.h"
15 #include "qwt_array.h"
16 #include "qwt_text.h"
17 #include "qwt_plot_dict.h"
18 #include "qwt_scale_map.h"
19 #include "qwt_plot_printfilter.h"
20 
21 class QwtPlotLayout;
22 class QwtLegend;
23 class QwtScaleWidget;
24 class QwtScaleEngine;
25 class QwtScaleDiv;
26 class QwtScaleDraw;
27 class QwtTextLabel;
28 class QwtPlotCanvas;
29 class QwtPlotPrintFilter;
30 
31 /*!
32   \brief A 2-D plotting widget
33 
34   QwtPlot is a widget for plotting two-dimensional graphs.
35   An unlimited number of plot items can be displayed on
36   its canvas. Plot items might be curves (QwtPlotCurve), markers
37   (QwtPlotMarker), the grid (QwtPlotGrid), or anything else derived
38   from QwtPlotItem.
39   A plot can have up to four axes, with each plot item attached to an x- and
40   a y axis. The scales at the axes can be explicitely set (QwtScaleDiv), or
41   are calculated from the plot items, using algorithms (QwtScaleEngine) which
42   can be configured separately for each axis.
43 
44   \image html plot.png
45 
46   \par Example
47   The following example shows (schematically) the most simple
48   way to use QwtPlot. By default, only the left and bottom axes are
49   visible and their scales are computed automatically.
50   \verbatim
51 #include <qwt_plot.h>
52 #include <qwt_plot_curve.h>
53 
54 QwtPlot *myPlot = new QwtPlot("Two Curves", parent);
55 
56 // add curves
57 QwtPlotCurve *curve1 = new QwtPlotCurve("Curve 1");
58 QwtPlotCurve *curve2 = new QwtPlotCurve("Curve 2");
59 
60 // copy the data into the curves
61 curve1->setData(...);
62 curve2->setData(...);
63 
64 curve1->attach(myPlot);
65 curve2->attach(myPlot);
66 
67 // finally, refresh the plot
68 myPlot->replot();
69 \endverbatim
70 */
71 
72 class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict
73 {
74     Q_OBJECT
75     Q_PROPERTY( QString propertiesDocument
76         READ grabProperties WRITE applyProperties )
77 
78 public:
79     /*!
80       Axis index
81 
82       - yLeft\n
83       - yRight\n
84       - xBottom\n
85       - xTop\n
86      */
87     enum Axis
88     {
89         yLeft,
90         yRight,
91         xBottom,
92         xTop,
93 
94         axisCnt
95     };
96 
97     /*!
98         Position of the legend, relative to the canvas.
99 
100         - LeftLegend\n
101           The legend will be left from the yLeft axis.
102         - RightLegend\n
103           The legend will be right from the yLeft axis.
104         - BottomLegend\n
105           The legend will be right below the xBottom axis.
106         - TopLegend\n
107           The legend will be between xTop axis and the title.
108         - ExternalLegend\n
109           External means that only the content of the legend
110           will be handled by QwtPlot, but not its geometry.
111           This might be interesting if an application wants to
112           have a legend in an external window ( or on the canvas ).
113 
114         \note In case of ExternalLegend, the legend is not
115               printed by print().
116 
117         \sa insertLegend()
118      */
119     enum LegendPosition
120     {
121         LeftLegend,
122         RightLegend,
123         BottomLegend,
124         TopLegend,
125 
126         ExternalLegend
127     };
128 
129     explicit QwtPlot(QWidget * = NULL);
130     explicit QwtPlot(const QwtText &title, QWidget *p = NULL);
131 #if QT_VERSION < 0x040000
132     explicit QwtPlot(QWidget *, const char* name);
133 #endif
134 
135     virtual ~QwtPlot();
136 
137     void applyProperties(const QString &);
138     QString grabProperties() const;
139 
140     void setAutoReplot(bool tf = true);
141     bool autoReplot() const;
142 
143     void print(QPaintDevice &p,
144         const QwtPlotPrintFilter & = QwtPlotPrintFilter()) const;
145     virtual void print(QPainter *, const QRect &rect,
146         const QwtPlotPrintFilter & = QwtPlotPrintFilter()) const;
147 
148     // Layout
149 
150     QwtPlotLayout *plotLayout();
151     const QwtPlotLayout *plotLayout() const;
152 
153     void setMargin(int margin);
154     int margin() const;
155 
156     // Title
157 
158     void setTitle(const QString &);
159     void setTitle(const QwtText &t);
160     QwtText title() const;
161 
162     QwtTextLabel *titleLabel();
163     const QwtTextLabel *titleLabel() const;
164 
165     // Canvas
166 
167     QwtPlotCanvas *canvas();
168     const QwtPlotCanvas *canvas() const;
169 
170     void setCanvasBackground (const QColor &c);
171     const QColor& canvasBackground() const;
172 
173     void setCanvasLineWidth(int w);
174     int canvasLineWidth() const;
175 
176     virtual QwtScaleMap canvasMap(int axisId) const;
177 
178     double invTransform(int axisId, int pos) const;
179     int transform(int axisId, double value) const;
180 
181     // Axes
182 
183     QwtScaleEngine *axisScaleEngine(int axisId);
184     const QwtScaleEngine *axisScaleEngine(int axisId) const;
185     void setAxisScaleEngine(int axisId, QwtScaleEngine *);
186 
187     void setAxisAutoScale(int axisId);
188     bool axisAutoScale(int axisId) const;
189 
190     void enableAxis(int axisId, bool tf = true);
191     bool axisEnabled(int axisId) const;
192 
193     void setAxisFont(int axisId, const QFont &f);
194     QFont axisFont(int axisId) const;
195 
196     void setAxisScale(int axisId, double min, double max, double step = 0);
197     void setAxisScaleDiv(int axisId, const QwtScaleDiv &);
198     void setAxisScaleDraw(int axisId, QwtScaleDraw *);
199 
200     double axisStepSize(int axisId) const;
201 
202     const QwtScaleDiv *axisScaleDiv(int axisId) const;
203     QwtScaleDiv *axisScaleDiv(int axisId);
204 
205     const QwtScaleDraw *axisScaleDraw(int axisId) const;
206     QwtScaleDraw *axisScaleDraw(int axisId);
207 
208     const QwtScaleWidget *axisWidget(int axisId) const;
209     QwtScaleWidget *axisWidget(int axisId);
210 
211 #if QT_VERSION < 0x040000
212     void setAxisLabelAlignment(int axisId, int);
213 #else
214     void setAxisLabelAlignment(int axisId, Qt::Alignment);
215 #endif
216     void setAxisLabelRotation(int axisId, double rotation);
217 
218     void setAxisTitle(int axisId, const QString &);
219     void setAxisTitle(int axisId, const QwtText &);
220     QwtText axisTitle(int axisId) const;
221 
222     void setAxisMaxMinor(int axisId, int maxMinor);
223     int axisMaxMajor(int axisId) const;
224     void setAxisMaxMajor(int axisId, int maxMajor);
225     int axisMaxMinor(int axisId) const;
226 
227     // Legend
228 
229     void insertLegend(QwtLegend *, LegendPosition = QwtPlot::RightLegend,
230         double ratio = -1.0);
231 
232     QwtLegend *legend();
233     const QwtLegend *legend() const;
234 
235     // Misc
236 
237     virtual void polish();
238     virtual QSize sizeHint() const;
239     virtual QSize minimumSizeHint() const;
240 
241     virtual void updateLayout();
242     virtual void drawCanvas(QPainter *);
243 
244     void updateAxes();
245 
246     virtual bool event(QEvent *);
247 
248 signals:
249     /*!
250       A signal which is emitted when the user has clicked on
251       a legend item, which is in QwtLegend::ClickableItem mode.
252 
253       \param plotItem Corresponding plot item of the
254                  selected legend item
255 
256       \note clicks are disabled as default
257       \sa QwtLegend::setItemMode(), QwtLegend::itemMode()
258      */
259     void legendClicked(QwtPlotItem *plotItem);
260 
261     /*!
262       A signal which is emitted when the user has clicked on
263       a legend item, which is in QwtLegend::CheckableItem mode
264 
265       \param plotItem Corresponding plot item of the
266                  selected legend item
267       \param on True when the legen item is checked
268 
269       \note clicks are disabled as default
270       \sa QwtLegend::setItemMode(), QwtLegend::itemMode()
271      */
272 
273     void legendChecked(QwtPlotItem *plotItem, bool on);
274 
275 public slots:
276     virtual void clear();
277 
278     virtual void replot();
279     void autoRefresh();
280 
281 protected slots:
282     virtual void legendItemClicked();
283     virtual void legendItemChecked(bool);
284 
285 protected:
286     static bool axisValid(int axisId);
287 
288     virtual void drawItems(QPainter *, const QRect &,
289         const QwtScaleMap maps[axisCnt],
290         const QwtPlotPrintFilter &) const;
291 
292     virtual void updateTabOrder();
293 
294     virtual void resizeEvent(QResizeEvent *e);
295 
296     virtual void printLegendItem(QPainter *,
297         const QWidget *, const QRect &) const;
298 
299     virtual void printTitle(QPainter *, const QRect &) const;
300 
301     virtual void printScale(QPainter *, int axisId, int startDist, int endDist,
302         int baseDist, const QRect &) const;
303 
304     virtual void printCanvas(QPainter *,
305         const QRect &boundingRect, const QRect &canvasRect,
306         const QwtScaleMap maps[axisCnt], const QwtPlotPrintFilter &) const;
307 
308     virtual void printLegend(QPainter *, const QRect &) const;
309 
310 private:
311     void initAxesData();
312     void deleteAxesData();
313     void updateScaleDiv();
314 
315     void initPlot(const QwtText &title);
316 
317     class AxisData;
318     AxisData *d_axisData[axisCnt];
319 
320     class PrivateData;
321     PrivateData *d_data;
322 };
323 
324 #endif
325