Skip to content
Snippets Groups Projects
qwt_plot.h 8.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • pixhawk's avatar
    pixhawk committed
    /* -*- 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
    #include "qwt_interval.h"
    #include <qframe.h>
    #include <qlist.h>
    #include <qvariant.h>
    
    pixhawk's avatar
    pixhawk committed
    
    class QwtPlotLayout;
    
    Bryant's avatar
    Bryant committed
    class QwtAbstractLegend;
    
    pixhawk's avatar
    pixhawk committed
    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.
    
      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
      from QwtPlotItem.
      A plot can have up to four axes, with each plot item attached to an x- and
    
    Bryant's avatar
    Bryant committed
      a y axis. The scales at the axes can be explicitly set (QwtScaleDiv), or
    
      are calculated from the plot items, using algorithms (QwtScaleEngine) which
      can be configured separately for each axis.
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
      The simpleplot example is a good starting point to see how to set up a 
      plot widget.
    
    
    pixhawk's avatar
    pixhawk committed
      \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
    QwtPlot *myPlot = new QwtPlot("Two Curves", parent);
    
    pixhawk's avatar
    pixhawk committed
    
    // add curves
    QwtPlotCurve *curve1 = new QwtPlotCurve("Curve 1");
    QwtPlotCurve *curve2 = new QwtPlotCurve("Curve 2");
    
    
    Bryant's avatar
    Bryant committed
    // connect or copy the data to the curves
    curve1->setData(...);
    curve2->setData(...);
    
    pixhawk's avatar
    pixhawk committed
    
    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
    
        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.
    
    
        Q_PROPERTY( QString propertiesDocument
    
    Bryant's avatar
    Bryant committed
            READ grabProperties WRITE applyProperties )
    #endif
    
    pixhawk's avatar
    pixhawk committed
    
    public:
    
    Bryant's avatar
    Bryant committed
        //! \brief Axis index
        enum Axis
        {
            //! Y axis left of the canvas
    
    Bryant's avatar
    Bryant committed
    
            //! Y axis right of the canvas
    
    Bryant's avatar
    Bryant committed
    
            //! X axis below the canvas
    
    Bryant's avatar
    Bryant committed
    
            //! X axis above the canvas
    
    Bryant's avatar
    Bryant committed
            //! Number of axes
    
    pixhawk's avatar
    pixhawk committed
        };
    
    
    Bryant's avatar
    Bryant committed
            Position of the legend, relative to the canvas.
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
            \sa insertLegend()
    
    pixhawk's avatar
    pixhawk committed
         */
    
    Bryant's avatar
    Bryant committed
        enum LegendPosition
        {
            //! The legend will be left from the QwtPlot::yLeft axis.
    
    pixhawk's avatar
    pixhawk committed
            LeftLegend,
    
    Bryant's avatar
    Bryant committed
    
            //! The legend will be right from the QwtPlot::yRight axis.
    
    pixhawk's avatar
    pixhawk committed
            RightLegend,
    
    Bryant's avatar
    Bryant committed
    
            //! The legend will be below the footer 
    
    pixhawk's avatar
    pixhawk committed
            BottomLegend,
    
    Bryant's avatar
    Bryant committed
            //! The legend will be above the title
            TopLegend
    
    pixhawk's avatar
    pixhawk committed
        };
    
    
    Bryant's avatar
    Bryant committed
        explicit QwtPlot( QWidget * = NULL );
        explicit QwtPlot( const QwtText &title, QWidget * = NULL );
    
    pixhawk's avatar
    pixhawk committed
    
        virtual ~QwtPlot();
    
    
    Bryant's avatar
    Bryant committed
        void applyProperties( const QString & );
    
    pixhawk's avatar
    pixhawk committed
        QString grabProperties() const;
    
    
    Bryant's avatar
    Bryant committed
        void setAutoReplot( bool = true );
    
    pixhawk's avatar
    pixhawk committed
        bool autoReplot() const;
    
        // Layout
    
    
    Bryant's avatar
    Bryant committed
        void setPlotLayout( QwtPlotLayout * );
    
    
    pixhawk's avatar
    pixhawk committed
        QwtPlotLayout *plotLayout();
        const QwtPlotLayout *plotLayout() const;
    
        // Title
    
    
    Bryant's avatar
    Bryant committed
        void setTitle( const QString & );
        void setTitle( const QwtText &t );
    
    pixhawk's avatar
    pixhawk committed
        QwtText title() const;
    
        QwtTextLabel *titleLabel();
        const QwtTextLabel *titleLabel() const;
    
    
    Bryant's avatar
    Bryant committed
        // Footer
    
        void setFooter( const QString & );
        void setFooter( const QwtText &t );
        QwtText footer() const;
    
        QwtTextLabel *footerLabel();
        const QwtTextLabel *footerLabel() const;
    
    
    pixhawk's avatar
    pixhawk committed
        // Canvas
    
    
    Bryant's avatar
    Bryant committed
        void setCanvas( QWidget * );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        QWidget *canvas();
        const QWidget *canvas() const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void setCanvasBackground( const QBrush & );
        QBrush canvasBackground() const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        virtual QwtScaleMap canvasMap( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        double invTransform( int axisId, int pos ) const;
        double transform( int axisId, double value ) const;
    
    pixhawk's avatar
    pixhawk committed
    
        // Axes
    
    
    Bryant's avatar
    Bryant committed
        QwtScaleEngine *axisScaleEngine( int axisId );
        const QwtScaleEngine *axisScaleEngine( int axisId ) const;
        void setAxisScaleEngine( int axisId, QwtScaleEngine * );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void setAxisAutoScale( int axisId, bool on = true );
        bool axisAutoScale( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void enableAxis( int axisId, bool tf = true );
        bool axisEnabled( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void setAxisFont( int axisId, const QFont &f );
        QFont axisFont( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        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
    
    
    Bryant's avatar
    Bryant committed
        double axisStepSize( int axisId ) const;
        QwtInterval axisInterval( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        const QwtScaleDiv &axisScaleDiv( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        const QwtScaleDraw *axisScaleDraw( int axisId ) const;
        QwtScaleDraw *axisScaleDraw( int axisId );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        const QwtScaleWidget *axisWidget( int axisId ) const;
        QwtScaleWidget *axisWidget( int axisId );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        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
    
    
    Bryant's avatar
    Bryant committed
        void setAxisMaxMinor( int axisId, int maxMinor );
        int axisMaxMinor( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void setAxisMaxMajor( int axisId, int maxMajor );
        int axisMaxMajor( int axisId ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void insertLegend( QwtAbstractLegend *, 
            LegendPosition = QwtPlot::RightLegend, double ratio = -1.0 );
    
        QwtAbstractLegend *legend();
        const QwtAbstractLegend *legend() const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void updateLegend();
        void updateLegend( const QwtPlotItem * );
    
    pixhawk's avatar
    pixhawk committed
    
        // Misc
    
        virtual QSize sizeHint() const;
        virtual QSize minimumSizeHint() const;
    
        virtual void updateLayout();
    
    Bryant's avatar
    Bryant committed
        virtual void drawCanvas( QPainter * );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        void updateAxes();
        void updateCanvasMargins();
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        virtual void getCanvasMarginsHint( 
            const QwtScaleMap maps[], const QRectF &canvasRect,
            double &left, double &top, double &right, double &bottom) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        virtual bool event( QEvent * );
        virtual bool eventFilter( QObject *, QEvent * );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        virtual void drawItems( QPainter *, const QRectF &,
            const QwtScaleMap maps[axisCnt] ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        virtual QVariant itemToInfo( QwtPlotItem * ) const;
        virtual QwtPlotItem *infoToItem( const QVariant & ) const;
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
    Q_SIGNALS:
        /*!
          A signal indicating, that an item has been attached/detached
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
          \param plotItem Plot item
          \param on Attached/Detached
    
    pixhawk's avatar
    pixhawk committed
         */
    
    Bryant's avatar
    Bryant committed
        void itemAttached( QwtPlotItem *plotItem, bool on );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        /*!
          A signal with the attributes how to update 
          the legend entries for a plot item.
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
          \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
    
    
    Bryant's avatar
    Bryant committed
    public Q_SLOTS:
    
    pixhawk's avatar
    pixhawk committed
        virtual void replot();
        void autoRefresh();
    
    protected:
    
    Bryant's avatar
    Bryant committed
        static bool axisValid( int axisId );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
        virtual void resizeEvent( QResizeEvent *e );
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
    private Q_SLOTS:
        void updateLegendItems( const QVariant &itemInfo,
            const QList<QwtLegendData> &data );
    
    pixhawk's avatar
    pixhawk committed
    
    private:
    
    Bryant's avatar
    Bryant committed
        friend class QwtPlotItem;
        void attachItem( QwtPlotItem *, bool );
    
    
    pixhawk's avatar
    pixhawk committed
        void initAxesData();
        void deleteAxesData();
        void updateScaleDiv();
    
    
    Bryant's avatar
    Bryant committed
        void initPlot( const QwtText &title );
    
    pixhawk's avatar
    pixhawk committed
    
        class AxisData;
        AxisData *d_axisData[axisCnt];
    
        class PrivateData;
        PrivateData *d_data;
    };
    
    #endif