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"
|
---|
13 | #include "qwt_painter.h"
|
---|
14 | #include <qbitmap.h>
|
---|
15 | #include <qstyle.h>
|
---|
16 | #include <qstyleoption.h>
|
---|
17 | #include <qpainterpath.h>
|
---|
18 |
|
---|
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 |
|
---|
29 | static QBitmap qwtBorderMask( const QWidget *canvas, const QSize &size )
|
---|
30 | {
|
---|
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 |
|
---|
41 | const QRect r( 0, 0, size.width(), size.height() );
|
---|
42 |
|
---|
43 | QPainterPath borderPath;
|
---|
44 |
|
---|
45 | ( void )QMetaObject::invokeMethod(
|
---|
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 |
|
---|
54 | #if QT_VERSION >= 0x050000
|
---|
55 | QBitmap mask( size * pixelRatio );
|
---|
56 | mask.setDevicePixelRatio( pixelRatio );
|
---|
57 | #else
|
---|
58 | QBitmap mask( size );
|
---|
59 | #endif
|
---|
60 | mask.fill( Qt::color0 );
|
---|
61 |
|
---|
62 | QPainter painter( &mask );
|
---|
63 | painter.fillRect( canvas->contentsRect(), Qt::color1 );
|
---|
64 |
|
---|
65 | return mask;
|
---|
66 | }
|
---|
67 |
|
---|
68 | #if QT_VERSION >= 0x050000
|
---|
69 | QImage image( size * pixelRatio, QImage::Format_ARGB32_Premultiplied );
|
---|
70 | image.setDevicePixelRatio( pixelRatio );
|
---|
71 | #else
|
---|
72 | QImage image( size, QImage::Format_ARGB32_Premultiplied );
|
---|
73 | #endif
|
---|
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 |
|
---|
96 | if ( borderRadius.type() == QVariant::Double
|
---|
97 | && frameWidth.type() == QVariant::Int )
|
---|
98 | {
|
---|
99 | const double br = borderRadius.toDouble();
|
---|
100 | const int fw = frameWidth.toInt();
|
---|
101 |
|
---|
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 |
|
---|
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 | /*!
|
---|
134 | \brief A panner for the canvas of a QwtPlot
|
---|
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 | */
|
---|
142 | QwtPlotPanner::QwtPlotPanner( QWidget *canvas ):
|
---|
143 | QwtPanner( canvas )
|
---|
144 | {
|
---|
145 | d_data = new PrivateData();
|
---|
146 |
|
---|
147 | connect( this, SIGNAL(panned(int,int)),
|
---|
148 | SLOT(moveCanvas(int,int)) );
|
---|
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
|
---|
191 | QWidget *QwtPlotPanner::canvas()
|
---|
192 | {
|
---|
193 | return parentWidget();
|
---|
194 | }
|
---|
195 |
|
---|
196 | //! Return Observed plot canvas
|
---|
197 | const QWidget *QwtPlotPanner::canvas() const
|
---|
198 | {
|
---|
199 | return parentWidget();
|
---|
200 | }
|
---|
201 |
|
---|
202 | //! Return plot widget, containing the observed plot canvas
|
---|
203 | QwtPlot *QwtPlotPanner::plot()
|
---|
204 | {
|
---|
205 | QWidget *w = canvas();
|
---|
206 | if ( w )
|
---|
207 | w = w->parentWidget();
|
---|
208 |
|
---|
209 | return qobject_cast<QwtPlot *>( w );
|
---|
210 | }
|
---|
211 |
|
---|
212 | //! Return plot widget, containing the observed plot canvas
|
---|
213 | const QwtPlot *QwtPlotPanner::plot() const
|
---|
214 | {
|
---|
215 | const QWidget *w = canvas();
|
---|
216 | if ( w )
|
---|
217 | w = w->parentWidget();
|
---|
218 |
|
---|
219 | return qobject_cast<const QwtPlot *>( w );
|
---|
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 |
|
---|
249 | const double p1 = map.transform( plot->axisScaleDiv( axis ).lowerBound() );
|
---|
250 | const double p2 = map.transform( plot->axisScaleDiv( axis ).upperBound() );
|
---|
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 | /*!
|
---|
272 | Calculate a mask from the border path of the canvas
|
---|
273 |
|
---|
274 | \return Mask as bitmap
|
---|
275 | \sa QwtPlotCanvas::borderPath()
|
---|
276 | */
|
---|
277 | QBitmap QwtPlotPanner::contentsMask() const
|
---|
278 | {
|
---|
279 | if ( canvas() )
|
---|
280 | return qwtBorderMask( canvas(), size() );
|
---|
281 |
|
---|
282 | return QwtPanner::contentsMask();
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*!
|
---|
286 | \return Pixmap with the content of the canvas
|
---|
287 | */
|
---|
288 | QPixmap QwtPlotPanner::grab() const
|
---|
289 | {
|
---|
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();
|
---|
305 | }
|
---|
306 |
|
---|