[4271] | 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 | #include "qwt_plot_panner.h"
|
---|
| 11 | #include "qwt_scale_div.h"
|
---|
| 12 | #include "qwt_plot.h"
|
---|
[8127] | 13 | #include "qwt_painter.h"
|
---|
| 14 | #include <qbitmap.h>
|
---|
| 15 | #include <qstyle.h>
|
---|
| 16 | #include <qstyleoption.h>
|
---|
[4271] | 17 |
|
---|
[8127] | 18 | static QBitmap qwtBorderMask( const QWidget *canvas, const QSize &size )
|
---|
| 19 | {
|
---|
| 20 | const QRect r( 0, 0, size.width(), size.height() );
|
---|
| 21 |
|
---|
| 22 | QPainterPath borderPath;
|
---|
| 23 |
|
---|
| 24 | ( void )QMetaObject::invokeMethod(
|
---|
| 25 | const_cast< QWidget *>( canvas ), "borderPath", Qt::DirectConnection,
|
---|
| 26 | Q_RETURN_ARG( QPainterPath, borderPath ), Q_ARG( QRect, r ) );
|
---|
| 27 |
|
---|
| 28 | if ( borderPath.isEmpty() )
|
---|
| 29 | {
|
---|
| 30 | if ( canvas->contentsRect() == canvas->rect() )
|
---|
| 31 | return QBitmap();
|
---|
| 32 |
|
---|
| 33 | QBitmap mask( size );
|
---|
| 34 | mask.fill( Qt::color0 );
|
---|
| 35 |
|
---|
| 36 | QPainter painter( &mask );
|
---|
| 37 | painter.fillRect( canvas->contentsRect(), Qt::color1 );
|
---|
| 38 |
|
---|
| 39 | return mask;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | QImage image( size, QImage::Format_ARGB32_Premultiplied );
|
---|
| 43 | image.fill( Qt::color0 );
|
---|
| 44 |
|
---|
| 45 | QPainter painter( &image );
|
---|
| 46 | painter.setClipPath( borderPath );
|
---|
| 47 | painter.fillRect( r, Qt::color1 );
|
---|
| 48 |
|
---|
| 49 | // now erase the frame
|
---|
| 50 |
|
---|
| 51 | painter.setCompositionMode( QPainter::CompositionMode_DestinationOut );
|
---|
| 52 |
|
---|
| 53 | if ( canvas->testAttribute(Qt::WA_StyledBackground ) )
|
---|
| 54 | {
|
---|
| 55 | QStyleOptionFrame opt;
|
---|
| 56 | opt.initFrom(canvas);
|
---|
| 57 | opt.rect = r;
|
---|
| 58 | canvas->style()->drawPrimitive( QStyle::PE_Frame, &opt, &painter, canvas );
|
---|
| 59 | }
|
---|
| 60 | else
|
---|
| 61 | {
|
---|
| 62 | const QVariant borderRadius = canvas->property( "borderRadius" );
|
---|
| 63 | const QVariant frameWidth = canvas->property( "frameWidth" );
|
---|
| 64 |
|
---|
| 65 | if ( borderRadius.type() == QVariant::Double
|
---|
| 66 | && frameWidth.type() == QVariant::Int )
|
---|
| 67 | {
|
---|
| 68 | const double br = borderRadius.toDouble();
|
---|
| 69 | const int fw = frameWidth.toInt();
|
---|
| 70 |
|
---|
| 71 | if ( br > 0.0 && fw > 0 )
|
---|
| 72 | {
|
---|
| 73 | painter.setPen( QPen( Qt::color1, fw ) );
|
---|
| 74 | painter.setBrush( Qt::NoBrush );
|
---|
| 75 | painter.setRenderHint( QPainter::Antialiasing, true );
|
---|
| 76 |
|
---|
| 77 | painter.drawPath( borderPath );
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | painter.end();
|
---|
| 83 |
|
---|
| 84 | const QImage mask = image.createMaskFromColor(
|
---|
| 85 | QColor( Qt::color1 ).rgb(), Qt::MaskOutColor );
|
---|
| 86 |
|
---|
| 87 | return QBitmap::fromImage( mask );
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[4271] | 90 | class QwtPlotPanner::PrivateData
|
---|
| 91 | {
|
---|
| 92 | public:
|
---|
| 93 | PrivateData()
|
---|
| 94 | {
|
---|
| 95 | for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
|
---|
| 96 | isAxisEnabled[axis] = true;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | bool isAxisEnabled[QwtPlot::axisCnt];
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | /*!
|
---|
[8127] | 103 | \brief A panner for the canvas of a QwtPlot
|
---|
[4271] | 104 |
|
---|
| 105 | The panner is enabled for all axes
|
---|
| 106 |
|
---|
| 107 | \param canvas Plot canvas to pan, also the parent object
|
---|
| 108 |
|
---|
| 109 | \sa setAxisEnabled()
|
---|
| 110 | */
|
---|
[8127] | 111 | QwtPlotPanner::QwtPlotPanner( QWidget *canvas ):
|
---|
[4271] | 112 | QwtPanner( canvas )
|
---|
| 113 | {
|
---|
| 114 | d_data = new PrivateData();
|
---|
| 115 |
|
---|
| 116 | connect( this, SIGNAL( panned( int, int ) ),
|
---|
| 117 | SLOT( moveCanvas( int, int ) ) );
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | //! Destructor
|
---|
| 121 | QwtPlotPanner::~QwtPlotPanner()
|
---|
| 122 | {
|
---|
| 123 | delete d_data;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /*!
|
---|
| 127 | \brief En/Disable an axis
|
---|
| 128 |
|
---|
| 129 | Axes that are enabled will be synchronized to the
|
---|
| 130 | result of panning. All other axes will remain unchanged.
|
---|
| 131 |
|
---|
| 132 | \param axis Axis, see QwtPlot::Axis
|
---|
| 133 | \param on On/Off
|
---|
| 134 |
|
---|
| 135 | \sa isAxisEnabled(), moveCanvas()
|
---|
| 136 | */
|
---|
| 137 | void QwtPlotPanner::setAxisEnabled( int axis, bool on )
|
---|
| 138 | {
|
---|
| 139 | if ( axis >= 0 && axis < QwtPlot::axisCnt )
|
---|
| 140 | d_data->isAxisEnabled[axis] = on;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /*!
|
---|
| 144 | Test if an axis is enabled
|
---|
| 145 |
|
---|
| 146 | \param axis Axis, see QwtPlot::Axis
|
---|
| 147 | \return True, if the axis is enabled
|
---|
| 148 |
|
---|
| 149 | \sa setAxisEnabled(), moveCanvas()
|
---|
| 150 | */
|
---|
| 151 | bool QwtPlotPanner::isAxisEnabled( int axis ) const
|
---|
| 152 | {
|
---|
| 153 | if ( axis >= 0 && axis < QwtPlot::axisCnt )
|
---|
| 154 | return d_data->isAxisEnabled[axis];
|
---|
| 155 |
|
---|
| 156 | return true;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | //! Return observed plot canvas
|
---|
[8127] | 160 | QWidget *QwtPlotPanner::canvas()
|
---|
[4271] | 161 | {
|
---|
[8127] | 162 | return parentWidget();
|
---|
[4271] | 163 | }
|
---|
| 164 |
|
---|
| 165 | //! Return Observed plot canvas
|
---|
[8127] | 166 | const QWidget *QwtPlotPanner::canvas() const
|
---|
[4271] | 167 | {
|
---|
[8127] | 168 | return parentWidget();
|
---|
[4271] | 169 | }
|
---|
| 170 |
|
---|
| 171 | //! Return plot widget, containing the observed plot canvas
|
---|
| 172 | QwtPlot *QwtPlotPanner::plot()
|
---|
| 173 | {
|
---|
[8127] | 174 | QWidget *w = canvas();
|
---|
[4271] | 175 | if ( w )
|
---|
[8127] | 176 | w = w->parentWidget();
|
---|
[4271] | 177 |
|
---|
[8127] | 178 | return qobject_cast<QwtPlot *>( w );
|
---|
[4271] | 179 | }
|
---|
| 180 |
|
---|
| 181 | //! Return plot widget, containing the observed plot canvas
|
---|
| 182 | const QwtPlot *QwtPlotPanner::plot() const
|
---|
| 183 | {
|
---|
[8127] | 184 | const QWidget *w = canvas();
|
---|
[4271] | 185 | if ( w )
|
---|
[8127] | 186 | w = w->parentWidget();
|
---|
[4271] | 187 |
|
---|
[8127] | 188 | return qobject_cast<const QwtPlot *>( w );
|
---|
[4271] | 189 | }
|
---|
| 190 |
|
---|
| 191 | /*!
|
---|
| 192 | Adjust the enabled axes according to dx/dy
|
---|
| 193 |
|
---|
| 194 | \param dx Pixel offset in x direction
|
---|
| 195 | \param dy Pixel offset in y direction
|
---|
| 196 |
|
---|
| 197 | \sa QwtPanner::panned()
|
---|
| 198 | */
|
---|
| 199 | void QwtPlotPanner::moveCanvas( int dx, int dy )
|
---|
| 200 | {
|
---|
| 201 | if ( dx == 0 && dy == 0 )
|
---|
| 202 | return;
|
---|
| 203 |
|
---|
| 204 | QwtPlot *plot = this->plot();
|
---|
| 205 | if ( plot == NULL )
|
---|
| 206 | return;
|
---|
| 207 |
|
---|
| 208 | const bool doAutoReplot = plot->autoReplot();
|
---|
| 209 | plot->setAutoReplot( false );
|
---|
| 210 |
|
---|
| 211 | for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
|
---|
| 212 | {
|
---|
| 213 | if ( !d_data->isAxisEnabled[axis] )
|
---|
| 214 | continue;
|
---|
| 215 |
|
---|
| 216 | const QwtScaleMap map = plot->canvasMap( axis );
|
---|
| 217 |
|
---|
[8127] | 218 | const double p1 = map.transform( plot->axisScaleDiv( axis ).lowerBound() );
|
---|
| 219 | const double p2 = map.transform( plot->axisScaleDiv( axis ).upperBound() );
|
---|
[4271] | 220 |
|
---|
| 221 | double d1, d2;
|
---|
| 222 | if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
|
---|
| 223 | {
|
---|
| 224 | d1 = map.invTransform( p1 - dx );
|
---|
| 225 | d2 = map.invTransform( p2 - dx );
|
---|
| 226 | }
|
---|
| 227 | else
|
---|
| 228 | {
|
---|
| 229 | d1 = map.invTransform( p1 - dy );
|
---|
| 230 | d2 = map.invTransform( p2 - dy );
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | plot->setAxisScale( axis, d1, d2 );
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | plot->setAutoReplot( doAutoReplot );
|
---|
| 237 | plot->replot();
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /*!
|
---|
[8127] | 241 | Calculate a mask from the border path of the canvas
|
---|
| 242 |
|
---|
| 243 | \return Mask as bitmap
|
---|
| 244 | \sa QwtPlotCanvas::borderPath()
|
---|
[4271] | 245 | */
|
---|
| 246 | QBitmap QwtPlotPanner::contentsMask() const
|
---|
| 247 | {
|
---|
| 248 | if ( canvas() )
|
---|
[8127] | 249 | return qwtBorderMask( canvas(), size() );
|
---|
[4271] | 250 |
|
---|
| 251 | return QwtPanner::contentsMask();
|
---|
| 252 | }
|
---|
[8127] | 253 |
|
---|
| 254 | /*!
|
---|
| 255 | \return Pixmap with the content of the canvas
|
---|
| 256 | */
|
---|
| 257 | QPixmap QwtPlotPanner::grab() const
|
---|
| 258 | {
|
---|
| 259 | const QWidget *cv = canvas();
|
---|
| 260 | if ( cv && cv->inherits( "QGLWidget" ) )
|
---|
| 261 | {
|
---|
| 262 | // we can't grab from a QGLWidget
|
---|
| 263 |
|
---|
| 264 | QPixmap pm( cv->size() );
|
---|
| 265 | QwtPainter::fillPixmap( cv, pm );
|
---|
| 266 |
|
---|
| 267 | QPainter painter( &pm );
|
---|
| 268 | const_cast<QwtPlot *>( plot() )->drawCanvas( &painter );
|
---|
| 269 |
|
---|
| 270 | return pm;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | return QwtPanner::grab();
|
---|
| 274 | }
|
---|
| 275 |
|
---|