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