qwt_plot.h 8.17 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_PLOT_H
#define QWT_PLOT_H

#include "qwt_global.h"
#include "qwt_text.h"
#include "qwt_plot_dict.h"
#include "qwt_scale_map.h"
Bryant's avatar
Bryant committed
17 18 19 20
#include "qwt_interval.h"
#include <qframe.h>
#include <qlist.h>
#include <qvariant.h>
pixhawk's avatar
pixhawk committed
21 22

class QwtPlotLayout;
Bryant's avatar
Bryant committed
23
class QwtAbstractLegend;
pixhawk's avatar
pixhawk committed
24 25 26 27 28 29 30 31 32 33
class QwtScaleWidget;
class QwtScaleEngine;
class QwtScaleDiv;
class QwtScaleDraw;
class QwtTextLabel;

/*!
  \brief A 2-D plotting widget

  QwtPlot is a widget for plotting two-dimensional graphs.
34 35 36
  An unlimited number of plot items can be displayed on
  its canvas. Plot items might be curves (QwtPlotCurve), markers
  (QwtPlotMarker), the grid (QwtPlotGrid), or anything else derived
pixhawk's avatar
pixhawk committed
37 38
  from QwtPlotItem.
  A plot can have up to four axes, with each plot item attached to an x- and
Bryant's avatar
Bryant committed
39
  a y axis. The scales at the axes can be explicitly set (QwtScaleDiv), or
40 41
  are calculated from the plot items, using algorithms (QwtScaleEngine) which
  can be configured separately for each axis.
pixhawk's avatar
pixhawk committed
42

Bryant's avatar
Bryant committed
43 44 45
  The simpleplot example is a good starting point to see how to set up a 
  plot widget.

pixhawk's avatar
pixhawk committed
46 47 48 49 50 51 52 53 54 55
  \image html plot.png

  \par Example
  The following example shows (schematically) the most simple
  way to use QwtPlot. By default, only the left and bottom axes are
  visible and their scales are computed automatically.
  \verbatim
#include <qwt_plot.h>
#include <qwt_plot_curve.h>

Bryant's avatar
Bryant committed
56
QwtPlot *myPlot = new QwtPlot("Two Curves", parent);
pixhawk's avatar
pixhawk committed
57 58 59 60 61

// add curves
QwtPlotCurve *curve1 = new QwtPlotCurve("Curve 1");
QwtPlotCurve *curve2 = new QwtPlotCurve("Curve 2");

Bryant's avatar
Bryant committed
62 63 64
// connect or copy the data to the curves
curve1->setData(...);
curve2->setData(...);
pixhawk's avatar
pixhawk committed
65 66 67 68 69 70 71 72 73 74 75 76

curve1->attach(myPlot);
curve2->attach(myPlot);

// finally, refresh the plot
myPlot->replot();
\endverbatim
*/

class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict
{
    Q_OBJECT
Bryant's avatar
Bryant committed
77 78 79 80 81 82 83 84 85 86

    Q_PROPERTY( QBrush canvasBackground 
        READ canvasBackground WRITE setCanvasBackground )
    Q_PROPERTY( bool autoReplot READ autoReplot WRITE setAutoReplot )

#if 0
    // This property is intended to configure the plot
    // widget from a special dialog in the deigner plugin.
    // Disabled until such a dialog has been implemented.

87
    Q_PROPERTY( QString propertiesDocument
Bryant's avatar
Bryant committed
88 89
        READ grabProperties WRITE applyProperties )
#endif
pixhawk's avatar
pixhawk committed
90 91

public:
Bryant's avatar
Bryant committed
92 93 94 95
    //! \brief Axis index
    enum Axis
    {
        //! Y axis left of the canvas
96
        yLeft,
Bryant's avatar
Bryant committed
97 98

        //! Y axis right of the canvas
99
        yRight,
Bryant's avatar
Bryant committed
100 101

        //! X axis below the canvas
102
        xBottom,
Bryant's avatar
Bryant committed
103 104

        //! X axis above the canvas
105 106
        xTop,

Bryant's avatar
Bryant committed
107
        //! Number of axes
108
        axisCnt
pixhawk's avatar
pixhawk committed
109 110
    };

111
    /*!
Bryant's avatar
Bryant committed
112
        Position of the legend, relative to the canvas.
pixhawk's avatar
pixhawk committed
113

Bryant's avatar
Bryant committed
114
        \sa insertLegend()
pixhawk's avatar
pixhawk committed
115
     */
Bryant's avatar
Bryant committed
116 117 118
    enum LegendPosition
    {
        //! The legend will be left from the QwtPlot::yLeft axis.
pixhawk's avatar
pixhawk committed
119
        LeftLegend,
Bryant's avatar
Bryant committed
120 121

        //! The legend will be right from the QwtPlot::yRight axis.
pixhawk's avatar
pixhawk committed
122
        RightLegend,
Bryant's avatar
Bryant committed
123 124

        //! The legend will be below the footer 
pixhawk's avatar
pixhawk committed
125
        BottomLegend,
126

Bryant's avatar
Bryant committed
127 128
        //! The legend will be above the title
        TopLegend
pixhawk's avatar
pixhawk committed
129 130
    };

Bryant's avatar
Bryant committed
131 132
    explicit QwtPlot( QWidget * = NULL );
    explicit QwtPlot( const QwtText &title, QWidget * = NULL );
pixhawk's avatar
pixhawk committed
133 134 135

    virtual ~QwtPlot();

Bryant's avatar
Bryant committed
136
    void applyProperties( const QString & );
pixhawk's avatar
pixhawk committed
137 138
    QString grabProperties() const;

Bryant's avatar
Bryant committed
139
    void setAutoReplot( bool = true );
pixhawk's avatar
pixhawk committed
140 141 142 143
    bool autoReplot() const;

    // Layout

Bryant's avatar
Bryant committed
144 145
    void setPlotLayout( QwtPlotLayout * );

pixhawk's avatar
pixhawk committed
146 147 148 149 150
    QwtPlotLayout *plotLayout();
    const QwtPlotLayout *plotLayout() const;

    // Title

Bryant's avatar
Bryant committed
151 152
    void setTitle( const QString & );
    void setTitle( const QwtText &t );
pixhawk's avatar
pixhawk committed
153 154 155 156 157
    QwtText title() const;

    QwtTextLabel *titleLabel();
    const QwtTextLabel *titleLabel() const;

Bryant's avatar
Bryant committed
158 159 160 161 162 163 164 165 166
    // Footer

    void setFooter( const QString & );
    void setFooter( const QwtText &t );
    QwtText footer() const;

    QwtTextLabel *footerLabel();
    const QwtTextLabel *footerLabel() const;

pixhawk's avatar
pixhawk committed
167 168
    // Canvas

Bryant's avatar
Bryant committed
169
    void setCanvas( QWidget * );
pixhawk's avatar
pixhawk committed
170

Bryant's avatar
Bryant committed
171 172
    QWidget *canvas();
    const QWidget *canvas() const;
pixhawk's avatar
pixhawk committed
173

Bryant's avatar
Bryant committed
174 175
    void setCanvasBackground( const QBrush & );
    QBrush canvasBackground() const;
pixhawk's avatar
pixhawk committed
176

Bryant's avatar
Bryant committed
177
    virtual QwtScaleMap canvasMap( int axisId ) const;
pixhawk's avatar
pixhawk committed
178

Bryant's avatar
Bryant committed
179 180
    double invTransform( int axisId, int pos ) const;
    double transform( int axisId, double value ) const;
pixhawk's avatar
pixhawk committed
181 182 183

    // Axes

Bryant's avatar
Bryant committed
184 185 186
    QwtScaleEngine *axisScaleEngine( int axisId );
    const QwtScaleEngine *axisScaleEngine( int axisId ) const;
    void setAxisScaleEngine( int axisId, QwtScaleEngine * );
pixhawk's avatar
pixhawk committed
187

Bryant's avatar
Bryant committed
188 189
    void setAxisAutoScale( int axisId, bool on = true );
    bool axisAutoScale( int axisId ) const;
pixhawk's avatar
pixhawk committed
190

Bryant's avatar
Bryant committed
191 192
    void enableAxis( int axisId, bool tf = true );
    bool axisEnabled( int axisId ) const;
pixhawk's avatar
pixhawk committed
193

Bryant's avatar
Bryant committed
194 195
    void setAxisFont( int axisId, const QFont &f );
    QFont axisFont( int axisId ) const;
pixhawk's avatar
pixhawk committed
196

Bryant's avatar
Bryant committed
197 198 199
    void setAxisScale( int axisId, double min, double max, double step = 0 );
    void setAxisScaleDiv( int axisId, const QwtScaleDiv & );
    void setAxisScaleDraw( int axisId, QwtScaleDraw * );
pixhawk's avatar
pixhawk committed
200

Bryant's avatar
Bryant committed
201 202
    double axisStepSize( int axisId ) const;
    QwtInterval axisInterval( int axisId ) const;
pixhawk's avatar
pixhawk committed
203

Bryant's avatar
Bryant committed
204
    const QwtScaleDiv &axisScaleDiv( int axisId ) const;
pixhawk's avatar
pixhawk committed
205

Bryant's avatar
Bryant committed
206 207
    const QwtScaleDraw *axisScaleDraw( int axisId ) const;
    QwtScaleDraw *axisScaleDraw( int axisId );
pixhawk's avatar
pixhawk committed
208

Bryant's avatar
Bryant committed
209 210
    const QwtScaleWidget *axisWidget( int axisId ) const;
    QwtScaleWidget *axisWidget( int axisId );
pixhawk's avatar
pixhawk committed
211

Bryant's avatar
Bryant committed
212 213 214 215 216 217
    void setAxisLabelAlignment( int axisId, Qt::Alignment );
    void setAxisLabelRotation( int axisId, double rotation );

    void setAxisTitle( int axisId, const QString & );
    void setAxisTitle( int axisId, const QwtText & );
    QwtText axisTitle( int axisId ) const;
pixhawk's avatar
pixhawk committed
218

Bryant's avatar
Bryant committed
219 220
    void setAxisMaxMinor( int axisId, int maxMinor );
    int axisMaxMinor( int axisId ) const;
pixhawk's avatar
pixhawk committed
221

Bryant's avatar
Bryant committed
222 223
    void setAxisMaxMajor( int axisId, int maxMajor );
    int axisMaxMajor( int axisId ) const;
pixhawk's avatar
pixhawk committed
224

225
    // Legend
pixhawk's avatar
pixhawk committed
226

Bryant's avatar
Bryant committed
227 228 229 230 231
    void insertLegend( QwtAbstractLegend *, 
        LegendPosition = QwtPlot::RightLegend, double ratio = -1.0 );

    QwtAbstractLegend *legend();
    const QwtAbstractLegend *legend() const;
pixhawk's avatar
pixhawk committed
232

Bryant's avatar
Bryant committed
233 234
    void updateLegend();
    void updateLegend( const QwtPlotItem * );
pixhawk's avatar
pixhawk committed
235 236 237 238 239 240 241

    // Misc

    virtual QSize sizeHint() const;
    virtual QSize minimumSizeHint() const;

    virtual void updateLayout();
Bryant's avatar
Bryant committed
242
    virtual void drawCanvas( QPainter * );
pixhawk's avatar
pixhawk committed
243

Bryant's avatar
Bryant committed
244 245
    void updateAxes();
    void updateCanvasMargins();
pixhawk's avatar
pixhawk committed
246

Bryant's avatar
Bryant committed
247 248 249
    virtual void getCanvasMarginsHint( 
        const QwtScaleMap maps[], const QRectF &canvasRect,
        double &left, double &top, double &right, double &bottom) const;
pixhawk's avatar
pixhawk committed
250

Bryant's avatar
Bryant committed
251 252
    virtual bool event( QEvent * );
    virtual bool eventFilter( QObject *, QEvent * );
pixhawk's avatar
pixhawk committed
253

Bryant's avatar
Bryant committed
254 255
    virtual void drawItems( QPainter *, const QRectF &,
        const QwtScaleMap maps[axisCnt] ) const;
pixhawk's avatar
pixhawk committed
256

Bryant's avatar
Bryant committed
257 258
    virtual QVariant itemToInfo( QwtPlotItem * ) const;
    virtual QwtPlotItem *infoToItem( const QVariant & ) const;
pixhawk's avatar
pixhawk committed
259

Bryant's avatar
Bryant committed
260 261 262
Q_SIGNALS:
    /*!
      A signal indicating, that an item has been attached/detached
pixhawk's avatar
pixhawk committed
263

Bryant's avatar
Bryant committed
264 265
      \param plotItem Plot item
      \param on Attached/Detached
pixhawk's avatar
pixhawk committed
266
     */
Bryant's avatar
Bryant committed
267
    void itemAttached( QwtPlotItem *plotItem, bool on );
pixhawk's avatar
pixhawk committed
268

Bryant's avatar
Bryant committed
269 270 271
    /*!
      A signal with the attributes how to update 
      the legend entries for a plot item.
pixhawk's avatar
pixhawk committed
272

Bryant's avatar
Bryant committed
273 274 275 276 277 278 279 280
      \param itemInfo Info about a plot item, build from itemToInfo()
      \param data Attributes of the entries ( usually <= 1 ) for
                  the plot item.

      \sa itemToInfo(), infoToItem(), QwtAbstractLegend::updateLegend()
     */
    void legendDataChanged( const QVariant &itemInfo, 
        const QList<QwtLegendData> &data );
pixhawk's avatar
pixhawk committed
281

Bryant's avatar
Bryant committed
282
public Q_SLOTS:
pixhawk's avatar
pixhawk committed
283 284 285 286
    virtual void replot();
    void autoRefresh();

protected:
Bryant's avatar
Bryant committed
287
    static bool axisValid( int axisId );
pixhawk's avatar
pixhawk committed
288

Bryant's avatar
Bryant committed
289
    virtual void resizeEvent( QResizeEvent *e );
pixhawk's avatar
pixhawk committed
290

Bryant's avatar
Bryant committed
291 292 293
private Q_SLOTS:
    void updateLegendItems( const QVariant &itemInfo,
        const QList<QwtLegendData> &data );
pixhawk's avatar
pixhawk committed
294 295

private:
Bryant's avatar
Bryant committed
296 297 298
    friend class QwtPlotItem;
    void attachItem( QwtPlotItem *, bool );

pixhawk's avatar
pixhawk committed
299 300 301 302
    void initAxesData();
    void deleteAxesData();
    void updateScaleDiv();

Bryant's avatar
Bryant committed
303
    void initPlot( const QwtText &title );
pixhawk's avatar
pixhawk committed
304 305 306 307 308 309 310 311 312

    class AxisData;
    AxisData *d_axisData[axisCnt];

    class PrivateData;
    PrivateData *d_data;
};

#endif