qwt_plot_panner.cpp 6.49 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9
/* -*- 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
 *****************************************************************************/

Bryant's avatar
Bryant committed
10
#include "qwt_plot_panner.h"
pixhawk's avatar
pixhawk committed
11 12
#include "qwt_scale_div.h"
#include "qwt_plot.h"
Bryant's avatar
Bryant committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include "qwt_painter.h"
#include <qbitmap.h>
#include <qstyle.h>
#include <qstyleoption.h>

static QBitmap qwtBorderMask( const QWidget *canvas, const QSize &size )
{
    const QRect r( 0, 0, size.width(), size.height() );

    QPainterPath borderPath;

    ( void )QMetaObject::invokeMethod( 
        const_cast< QWidget *>( canvas ), "borderPath", Qt::DirectConnection,
        Q_RETURN_ARG( QPainterPath, borderPath ), Q_ARG( QRect, r ) );

    if ( borderPath.isEmpty() )
    {
        if ( canvas->contentsRect() == canvas->rect() )
            return QBitmap();

        QBitmap mask( size );
        mask.fill( Qt::color0 );

        QPainter painter( &mask );
        painter.fillRect( canvas->contentsRect(), Qt::color1 );

        return mask;
    }

    QImage image( size, QImage::Format_ARGB32_Premultiplied );
    image.fill( Qt::color0 );

    QPainter painter( &image );
    painter.setClipPath( borderPath );
    painter.fillRect( r, Qt::color1 );

    // now erase the frame

    painter.setCompositionMode( QPainter::CompositionMode_DestinationOut );

    if ( canvas->testAttribute(Qt::WA_StyledBackground ) )
    {
        QStyleOptionFrame opt;
        opt.initFrom(canvas);
        opt.rect = r;
        canvas->style()->drawPrimitive( QStyle::PE_Frame, &opt, &painter, canvas );
    }
    else
    {
        const QVariant borderRadius = canvas->property( "borderRadius" );
        const QVariant frameWidth = canvas->property( "frameWidth" );

        if ( borderRadius.type() == QVariant::Double 
            && frameWidth.type() == QVariant::Int )
        {
            const double br = borderRadius.toDouble();
            const int fw = frameWidth.toInt();
        
            if ( br > 0.0 && fw > 0 )
            {
                painter.setPen( QPen( Qt::color1, fw ) );
                painter.setBrush( Qt::NoBrush );
                painter.setRenderHint( QPainter::Antialiasing, true );

                painter.drawPath( borderPath );
            }
        }
    }

    painter.end();

    const QImage mask = image.createMaskFromColor(
        QColor( Qt::color1 ).rgb(), Qt::MaskOutColor );

    return QBitmap::fromImage( mask );
}
pixhawk's avatar
pixhawk committed
89 90 91 92

class QwtPlotPanner::PrivateData
{
public:
Bryant's avatar
Bryant committed
93 94
    PrivateData()
    {
pixhawk's avatar
pixhawk committed
95 96 97 98 99 100 101 102
        for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
            isAxisEnabled[axis] = true;
    }

    bool isAxisEnabled[QwtPlot::axisCnt];
};

/*!
Bryant's avatar
Bryant committed
103
  \brief A panner for the canvas of a QwtPlot
pixhawk's avatar
pixhawk committed
104 105 106 107 108

  The panner is enabled for all axes

  \param canvas Plot canvas to pan, also the parent object

Bryant's avatar
Bryant committed
109
  \sa setAxisEnabled()
pixhawk's avatar
pixhawk committed
110
*/
Bryant's avatar
Bryant committed
111 112
QwtPlotPanner::QwtPlotPanner( QWidget *canvas ):
    QwtPanner( canvas )
pixhawk's avatar
pixhawk committed
113 114 115
{
    d_data = new PrivateData();

Bryant's avatar
Bryant committed
116 117
    connect( this, SIGNAL( panned( int, int ) ),
        SLOT( moveCanvas( int, int ) ) );
pixhawk's avatar
pixhawk committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

//! Destructor
QwtPlotPanner::~QwtPlotPanner()
{
    delete d_data;
}

/*!
   \brief En/Disable an axis

   Axes that are enabled will be synchronized to the
   result of panning. All other axes will remain unchanged.

   \param axis Axis, see QwtPlot::Axis
   \param on On/Off

Bryant's avatar
Bryant committed
135
   \sa isAxisEnabled(), moveCanvas()
pixhawk's avatar
pixhawk committed
136
*/
Bryant's avatar
Bryant committed
137
void QwtPlotPanner::setAxisEnabled( int axis, bool on )
pixhawk's avatar
pixhawk committed
138 139 140 141 142 143 144 145 146 147
{
    if ( axis >= 0 && axis < QwtPlot::axisCnt )
        d_data->isAxisEnabled[axis] = on;
}

/*!
   Test if an axis is enabled

   \param axis Axis, see QwtPlot::Axis
   \return True, if the axis is enabled
148

Bryant's avatar
Bryant committed
149
   \sa setAxisEnabled(), moveCanvas()
pixhawk's avatar
pixhawk committed
150
*/
Bryant's avatar
Bryant committed
151
bool QwtPlotPanner::isAxisEnabled( int axis ) const
pixhawk's avatar
pixhawk committed
152 153 154 155 156 157 158 159
{
    if ( axis >= 0 && axis < QwtPlot::axisCnt )
        return d_data->isAxisEnabled[axis];

    return true;
}

//! Return observed plot canvas
Bryant's avatar
Bryant committed
160
QWidget *QwtPlotPanner::canvas()
pixhawk's avatar
pixhawk committed
161
{
Bryant's avatar
Bryant committed
162
    return parentWidget();
pixhawk's avatar
pixhawk committed
163 164 165
}

//! Return Observed plot canvas
Bryant's avatar
Bryant committed
166
const QWidget *QwtPlotPanner::canvas() const
pixhawk's avatar
pixhawk committed
167
{
Bryant's avatar
Bryant committed
168
    return parentWidget();
pixhawk's avatar
pixhawk committed
169 170 171 172 173
}

//! Return plot widget, containing the observed plot canvas
QwtPlot *QwtPlotPanner::plot()
{
Bryant's avatar
Bryant committed
174 175 176
    QWidget *w = canvas();
    if ( w )
        w = w->parentWidget();
pixhawk's avatar
pixhawk committed
177

Bryant's avatar
Bryant committed
178
    return qobject_cast<QwtPlot *>( w );
pixhawk's avatar
pixhawk committed
179 180 181 182 183
}

//! Return plot widget, containing the observed plot canvas
const QwtPlot *QwtPlotPanner::plot() const
{
Bryant's avatar
Bryant committed
184 185 186 187 188
    const QWidget *w = canvas();
    if ( w )
        w = w->parentWidget();

    return qobject_cast<const QwtPlot *>( w );
pixhawk's avatar
pixhawk committed
189 190 191 192 193 194 195 196 197 198
}

/*!
   Adjust the enabled axes according to dx/dy

   \param dx Pixel offset in x direction
   \param dy Pixel offset in y direction

   \sa QwtPanner::panned()
*/
Bryant's avatar
Bryant committed
199
void QwtPlotPanner::moveCanvas( int dx, int dy )
pixhawk's avatar
pixhawk committed
200 201 202 203
{
    if ( dx == 0 && dy == 0 )
        return;

Bryant's avatar
Bryant committed
204
    QwtPlot *plot = this->plot();
pixhawk's avatar
pixhawk committed
205 206
    if ( plot == NULL )
        return;
207

pixhawk's avatar
pixhawk committed
208
    const bool doAutoReplot = plot->autoReplot();
Bryant's avatar
Bryant committed
209
    plot->setAutoReplot( false );
pixhawk's avatar
pixhawk committed
210

Bryant's avatar
Bryant committed
211 212
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
    {
pixhawk's avatar
pixhawk committed
213 214 215
        if ( !d_data->isAxisEnabled[axis] )
            continue;

Bryant's avatar
Bryant committed
216
        const QwtScaleMap map = plot->canvasMap( axis );
pixhawk's avatar
pixhawk committed
217

Bryant's avatar
Bryant committed
218 219
        const double p1 = map.transform( plot->axisScaleDiv( axis ).lowerBound() );
        const double p2 = map.transform( plot->axisScaleDiv( axis ).upperBound() );
pixhawk's avatar
pixhawk committed
220 221

        double d1, d2;
Bryant's avatar
Bryant committed
222 223 224 225 226 227 228 229 230
        if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
        {
            d1 = map.invTransform( p1 - dx );
            d2 = map.invTransform( p2 - dx );
        }
        else
        {
            d1 = map.invTransform( p1 - dy );
            d2 = map.invTransform( p2 - dy );
pixhawk's avatar
pixhawk committed
231 232
        }

Bryant's avatar
Bryant committed
233
        plot->setAxisScale( axis, d1, d2 );
pixhawk's avatar
pixhawk committed
234 235
    }

Bryant's avatar
Bryant committed
236
    plot->setAutoReplot( doAutoReplot );
pixhawk's avatar
pixhawk committed
237 238
    plot->replot();
}
Bryant's avatar
Bryant committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

/*!
   Calculate a mask from the border path of the canvas

   \return Mask as bitmap
   \sa QwtPlotCanvas::borderPath()
*/
QBitmap QwtPlotPanner::contentsMask() const
{
    if ( canvas() )
        return qwtBorderMask( canvas(), size() );

    return QwtPanner::contentsMask();
}

/*!
   \return Pixmap with the content of the canvas
 */
QPixmap QwtPlotPanner::grab() const
{   
    const QWidget *cv = canvas();
    if ( cv && cv->inherits( "QGLWidget" ) )
    {
        // we can't grab from a QGLWidget

        QPixmap pm( cv->size() );
        QwtPainter::fillPixmap( cv, pm );

        QPainter painter( &pm );
        const_cast<QwtPlot *>( plot() )->drawCanvas( &painter );

        return pm;
    }

    return QwtPanner::grab();
}