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_painter.h"
14 #include <qbitmap.h>
15 #include <qstyle.h>
16 #include <qstyleoption.h>
17 
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 
91 {
92 public:
94  {
95  for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
96  isAxisEnabled[axis] = true;
97  }
98 
100 };
101 
112  QwtPanner( canvas )
113 {
114  d_data = new PrivateData();
115 
116  connect( this, SIGNAL( panned( int, int ) ),
117  SLOT( moveCanvas( int, int ) ) );
118 }
119 
122 {
123  delete d_data;
124 }
125 
137 void QwtPlotPanner::setAxisEnabled( int axis, bool on )
138 {
139  if ( axis >= 0 && axis < QwtPlot::axisCnt )
140  d_data->isAxisEnabled[axis] = on;
141 }
142 
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 
161 {
162  return parentWidget();
163 }
164 
166 const QWidget *QwtPlotPanner::canvas() const
167 {
168  return parentWidget();
169 }
170 
173 {
174  QWidget *w = canvas();
175  if ( w )
176  w = w->parentWidget();
177 
178  return qobject_cast<QwtPlot *>( w );
179 }
180 
183 {
184  const QWidget *w = canvas();
185  if ( w )
186  w = w->parentWidget();
187 
188  return qobject_cast<const QwtPlot *>( w );
189 }
190 
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 
218  const double p1 = map.transform( plot->axisScaleDiv( axis ).lowerBound() );
219  const double p2 = map.transform( plot->axisScaleDiv( axis ).upperBound() );
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 
247 {
248  if ( canvas() )
249  return qwtBorderMask( canvas(), size() );
250 
251  return QwtPanner::contentsMask();
252 }
253 
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 
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:105
virtual QBitmap contentsMask() const
virtual QBitmap contentsMask() const
Calculate a mask for the contents of the panned widget.
Definition: qwt_panner.cpp:295
virtual ~QwtPlotPanner()
Destructor.
Number of axes.
Definition: qwt_plot.h:108
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition: qwt_plot.cpp:314
QwtPanner provides panning of a widget.
Definition: qwt_panner.h:35
virtual void replot()
Redraw the plot.
Definition: qwt_plot.cpp:546
A 2-D plotting widget.
Definition: qwt_plot.h:74
void setAxisEnabled(int axis, bool on)
En/Disable an axis.
double upperBound() const
virtual QPixmap grab() const
bool autoReplot
Definition: qwt_plot.h:80
double lowerBound() const
virtual QwtScaleMap canvasMap(int axisId) const
Definition: qwt_plot.cpp:790
void panned(int dx, int dy)
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:30
double invTransform(double p) const
virtual QPixmap grab() const
Definition: qwt_panner.cpp:304
QWidget * canvas()
Return observed plot canvas.
virtual void moveCanvas(int dx, int dy)
uintptr_t size
TFSIMD_FORCE_INLINE const tfScalar & w() const
bool isAxisEnabled[QwtPlot::axisCnt]
double transform(double s) const
PrivateData * d_data
void setAxisScale(int axisId, double min, double max, double step=0)
Disable autoscaling and specify a fixed scale for a selected axis.
X axis below the canvas.
Definition: qwt_plot.h:102


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17