qwt_plot_panner.cpp
Go to the documentation of this file.
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_scale_map.h"
14 #include "qwt_painter.h"
15 
16 #include <qbitmap.h>
17 #include <qstyle.h>
18 #include <qstyleoption.h>
19 #include <qpainter.h>
20 #include <qpainterpath.h>
21 
22 static QBitmap qwtBorderMask( const QWidget *canvas, const QSize &size )
23 {
24 #if QT_VERSION >= 0x050000
25  const qreal pixelRatio = QwtPainter::devicePixelRatio( canvas );
26 #endif
27 
28  const QRect r( 0, 0, size.width(), size.height() );
29 
30  QPainterPath borderPath;
31 
32  ( void )QMetaObject::invokeMethod(
33  const_cast< QWidget *>( canvas ), "borderPath", Qt::DirectConnection,
34  Q_RETURN_ARG( QPainterPath, borderPath ), Q_ARG( QRect, r ) );
35 
36  if ( borderPath.isEmpty() )
37  {
38  if ( canvas->contentsRect() == canvas->rect() )
39  return QBitmap();
40 
41 #if QT_VERSION >= 0x050000
42  QBitmap mask( size * pixelRatio );
43  mask.setDevicePixelRatio( pixelRatio );
44 #else
45  QBitmap mask( size );
46 #endif
47  mask.fill( Qt::color0 );
48 
49  QPainter painter( &mask );
50  painter.fillRect( canvas->contentsRect(), Qt::color1 );
51 
52  return mask;
53  }
54 
55 #if QT_VERSION >= 0x050000
56  QImage image( size * pixelRatio, QImage::Format_ARGB32_Premultiplied );
57  image.setDevicePixelRatio( pixelRatio );
58 #else
59  QImage image( size, QImage::Format_ARGB32_Premultiplied );
60 #endif
61  image.fill( Qt::color0 );
62 
63  QPainter painter( &image );
64  painter.setClipPath( borderPath );
65  painter.fillRect( r, Qt::color1 );
66 
67  // now erase the frame
68 
69  painter.setCompositionMode( QPainter::CompositionMode_DestinationOut );
70 
71  if ( canvas->testAttribute(Qt::WA_StyledBackground ) )
72  {
73  QStyleOptionFrame opt;
74  opt.initFrom(canvas);
75  opt.rect = r;
76  canvas->style()->drawPrimitive( QStyle::PE_Frame, &opt, &painter, canvas );
77  }
78  else
79  {
80  const QVariant borderRadius = canvas->property( "borderRadius" );
81  const QVariant frameWidth = canvas->property( "frameWidth" );
82 
83  if ( borderRadius.type() == QVariant::Double
84  && frameWidth.type() == QVariant::Int )
85  {
86  const double br = borderRadius.toDouble();
87  const int fw = frameWidth.toInt();
88 
89  if ( br > 0.0 && fw > 0 )
90  {
91  painter.setPen( QPen( Qt::color1, fw ) );
92  painter.setBrush( Qt::NoBrush );
93  painter.setRenderHint( QPainter::Antialiasing, true );
94 
95  painter.drawPath( borderPath );
96  }
97  }
98  }
99 
100  painter.end();
101 
102  const QImage mask = image.createMaskFromColor(
103  QColor( Qt::color1 ).rgb(), Qt::MaskOutColor );
104 
105  return QBitmap::fromImage( mask );
106 }
107 
109 {
110 public:
112  {
113  for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
114  isAxisEnabled[axis] = true;
115  }
116 
118 };
119 
130  QwtPanner( canvas )
131 {
132  d_data = new PrivateData();
133 
134  connect( this, SIGNAL(panned(int,int)),
135  SLOT(moveCanvas(int,int)) );
136 }
137 
140 {
141  delete d_data;
142 }
143 
155 void QwtPlotPanner::setAxisEnabled( int axis, bool on )
156 {
157  if ( axis >= 0 && axis < QwtPlot::axisCnt )
158  d_data->isAxisEnabled[axis] = on;
159 }
160 
169 bool QwtPlotPanner::isAxisEnabled( int axis ) const
170 {
171  if ( axis >= 0 && axis < QwtPlot::axisCnt )
172  return d_data->isAxisEnabled[axis];
173 
174  return true;
175 }
176 
179 {
180  return parentWidget();
181 }
182 
184 const QWidget *QwtPlotPanner::canvas() const
185 {
186  return parentWidget();
187 }
188 
191 {
192  QWidget *w = canvas();
193  if ( w )
194  w = w->parentWidget();
195 
196  return qobject_cast<QwtPlot *>( w );
197 }
198 
201 {
202  const QWidget *w = canvas();
203  if ( w )
204  w = w->parentWidget();
205 
206  return qobject_cast<const QwtPlot *>( w );
207 }
208 
217 void QwtPlotPanner::moveCanvas( int dx, int dy )
218 {
219  if ( dx == 0 && dy == 0 )
220  return;
221 
222  QwtPlot *plot = this->plot();
223  if ( plot == NULL )
224  return;
225 
226  const bool doAutoReplot = plot->autoReplot();
227  plot->setAutoReplot( false );
228 
229  for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
230  {
231  if ( !d_data->isAxisEnabled[axis] )
232  continue;
233 
234  const QwtScaleMap map = plot->canvasMap( axis );
235 
236  const double p1 = map.transform( plot->axisScaleDiv( axis ).lowerBound() );
237  const double p2 = map.transform( plot->axisScaleDiv( axis ).upperBound() );
238 
239  double d1, d2;
240  if ( axis == QwtPlot::xBottom || axis == QwtPlot::xTop )
241  {
242  d1 = map.invTransform( p1 - dx );
243  d2 = map.invTransform( p2 - dx );
244  }
245  else
246  {
247  d1 = map.invTransform( p1 - dy );
248  d2 = map.invTransform( p2 - dy );
249  }
250 
251  plot->setAxisScale( axis, d1, d2 );
252  }
253 
254  plot->setAutoReplot( doAutoReplot );
255  plot->replot();
256 }
257 
265 {
266  if ( canvas() )
267  return qwtBorderMask( canvas(), size() );
268 
269  return QwtPanner::contentsMask();
270 }
271 
275 QPixmap QwtPlotPanner::grab() const
276 {
277  const QWidget *cv = canvas();
278  if ( cv && cv->inherits( "QGLWidget" ) )
279  {
280  // we can't grab from a QGLWidget
281 
282  QPixmap pm( cv->size() );
283  QwtPainter::fillPixmap( cv, pm );
284 
285  QPainter painter( &pm );
286  const_cast<QwtPlot *>( plot() )->drawCanvas( &painter );
287 
288  return pm;
289  }
290 
291  return QwtPanner::grab();
292 }
293 
294 #if QWT_MOC_INCLUDE
295 #include "moc_qwt_plot_panner.cpp"
296 #endif
QwtPlotPanner(QWidget *)
A panner for the canvas of a QwtPlot.
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
const QwtScaleDiv & axisScaleDiv(int axisId) const
Return the scale division of a specified axis.
bool isAxisEnabled(int axis) const
X axis above the canvas.
Definition: qwt_plot.h:106
virtual QBitmap contentsMask() const
Calculate a mask for the contents of the panned widget.
Definition: qwt_panner.cpp:297
virtual ~QwtPlotPanner()
Destructor.
Number of axes.
Definition: qwt_plot.h:109
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition: qwt_plot.cpp:307
QwtPanner provides panning of a widget.
Definition: qwt_panner.h:35
virtual void replot()
Redraw the plot.
Definition: qwt_plot.cpp:539
virtual QPixmap grab() const QWT_OVERRIDE
A 2-D plotting widget.
Definition: qwt_plot.h:75
void setAxisEnabled(int axis, bool on)
En/Disable an axis.
double upperBound() const
bool autoReplot
Definition: qwt_plot.h:81
double lowerBound() const
static qreal devicePixelRatio(const QPaintDevice *)
virtual QBitmap contentsMask() const QWT_OVERRIDE
virtual QwtScaleMap canvasMap(int axisId) const
Definition: qwt_plot.cpp:786
void panned(int dx, int dy)
j template void())
Definition: json.hpp:3707
QwtPlot * plot()
Return plot widget, containing the observed plot canvas.
static QBitmap qwtBorderMask(const QWidget *canvas, const QSize &size)
A scale map.
Definition: qwt_scale_map.h:26
double invTransform(double p) const
virtual QPixmap grab() const
Definition: qwt_panner.cpp:306
QWidget * canvas()
Return observed plot canvas.
void setAxisScale(int axisId, double min, double max, double stepSize=0)
Disable autoscaling and specify a fixed scale for a selected axis.
virtual void moveCanvas(int dx, int dy)
bool isAxisEnabled[QwtPlot::axisCnt]
double transform(double s) const
PrivateData * d_data
X axis below the canvas.
Definition: qwt_plot.h:103


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10