qwt_polar_canvas.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * QwtPolar Widget Library
3  * Copyright (C) 2008 Uwe Rathmann
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the Qwt License, Version 1.0
7  *****************************************************************************/
8 
9 #include "qwt_polar_canvas.h"
10 #include "qwt_polar_plot.h"
11 #include "qwt_painter.h"
12 
13 #include <qpainter.h>
14 #include <qevent.h>
15 #include <qpixmap.h>
16 #include <qstyle.h>
17 #include <qstyleoption.h>
18 #ifdef Q_WS_X11
19 #include <qx11info_x11.h>
20 #endif
21 
22 static inline void qwtDrawStyledBackground(
23  QWidget *widget, QPainter *painter )
24 {
25  QStyleOption opt;
26  opt.initFrom( widget );
27  widget->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, widget );
28 }
29 
30 static QWidget *qwtBackgroundWidget( QWidget *w )
31 {
32  if ( w->parentWidget() == NULL )
33  return w;
34 
35  if ( w->autoFillBackground() )
36  {
37  const QBrush brush = w->palette().brush( w->backgroundRole() );
38  if ( brush.color().alpha() > 0 )
39  return w;
40  }
41 
42  if ( w->testAttribute( Qt::WA_StyledBackground ) )
43  {
44  QImage image( 1, 1, QImage::Format_ARGB32 );
45  image.fill( Qt::transparent );
46 
47  QPainter painter( &image );
48  painter.translate( -w->rect().center() );
49  qwtDrawStyledBackground( w, &painter );
50  painter.end();
51 
52  if ( qAlpha( image.pixel( 0, 0 ) ) != 0 )
53  return w;
54  }
55 
56  return qwtBackgroundWidget( w->parentWidget() );
57 }
58 
60 {
61 public:
63  backingStore( NULL )
64  {
65  }
66 
68  {
69  delete backingStore;
70  }
71 
73  QPixmap *backingStore;
74 };
75 
78  QFrame( plot )
79 {
80  d_data = new PrivateData;
81 
82 #ifndef QT_NO_CURSOR
83  setCursor( Qt::CrossCursor );
84 #endif
85  setFocusPolicy( Qt::WheelFocus );
86 
88 }
89 
92 {
93  delete d_data;
94 }
95 
98 {
99  return qobject_cast<QwtPolarPlot *>( parent() );
100 }
101 
104 {
105  return qobject_cast<QwtPolarPlot *>( parent() );
106 }
107 
119 {
120  if ( bool( d_data->paintAttributes & attribute ) == on )
121  return;
122 
123  if ( on )
124  d_data->paintAttributes |= attribute;
125  else
126  d_data->paintAttributes &= ~attribute;
127 
128  switch( attribute )
129  {
130  case BackingStore:
131  {
132  if ( on )
133  {
134  if ( d_data->backingStore == NULL )
135  d_data->backingStore = new QPixmap();
136 
137  if ( isVisible() )
138  {
139  const QRect cr = contentsRect();
140 #if QT_VERSION >= 0x050000
141  *d_data->backingStore = grab( cr );
142 #else
143  *d_data->backingStore = QPixmap::grabWidget( this, cr );
144 #endif
145  }
146  }
147  else
148  {
149  delete d_data->backingStore;
150  d_data->backingStore = NULL;
151  }
152  break;
153  }
154  }
155 }
156 
165 {
166  return ( d_data->paintAttributes & attribute ) != 0;
167 }
168 
170 const QPixmap *QwtPolarCanvas::backingStore() const
171 {
172  return d_data->backingStore;
173 }
174 
177 {
178  if ( d_data->backingStore )
179  *d_data->backingStore = QPixmap();
180 }
181 
186 void QwtPolarCanvas::paintEvent( QPaintEvent *event )
187 {
188  QPainter painter( this );
189  painter.setClipRegion( event->region() );
190 
192  && d_data->backingStore != NULL )
193  {
194  QPixmap &bs = *d_data->backingStore;
195  if ( bs.size() != size() )
196  {
197  bs = QPixmap( size() );
198 #ifdef Q_WS_X11
199  if ( bs.x11Info().screen() != x11Info().screen() )
200  bs.x11SetScreen( x11Info().screen() );
201 #endif
202 
203  QPainter p;
204 
205  if ( testAttribute( Qt::WA_StyledBackground ) )
206  {
207  p.begin( &bs );
208  qwtDrawStyledBackground( this, &p );
209  }
210  else
211  {
212  if ( autoFillBackground() )
213  {
214  p.begin( &bs );
215  p.fillRect( rect(), palette().brush( backgroundRole() ) );
216  }
217  else
218  {
219  QWidget *bgWidget = qwtBackgroundWidget( plot() );
220 
221  QwtPainter::fillPixmap( bgWidget, bs,
222  mapTo( bgWidget, rect().topLeft() ) );
223 
224  p.begin( &bs );
225  }
226  }
227 
228  plot()->drawCanvas( &p, contentsRect() );
229 
230  if ( frameWidth() > 0 )
231  drawFrame( &p );
232  }
233 
234  painter.drawPixmap( 0, 0, *d_data->backingStore );
235  }
236  else
237  {
238  qwtDrawStyledBackground( this, &painter );
239 
240  plot()->drawCanvas( &painter, contentsRect() );
241 
242  if ( frameWidth() > 0 )
243  drawFrame( &painter );
244  }
245 }
246 
251 void QwtPolarCanvas::resizeEvent( QResizeEvent *event )
252 {
253  QFrame::resizeEvent( event );
254 
255  for ( int scaleId = 0; scaleId < QwtPolar::ScaleCount; scaleId++ )
256  plot()->updateScale( scaleId );
257 }
258 
267 QwtPointPolar QwtPolarCanvas::invTransform( const QPoint &pos ) const
268 {
269  const QwtPolarPlot *pl = plot();
270 
271  const QwtScaleMap azimuthMap = pl->scaleMap( QwtPolar::Azimuth );
272  const QwtScaleMap radialMap = pl->scaleMap( QwtPolar::Radius );
273 
274  const QPointF center = pl->plotRect().center();
275 
276  double dx = pos.x() - center.x();
277  double dy = -( pos.y() - center.y() );
278 
279  const QwtPointPolar polarPos = QwtPointPolar( QPoint( dx, dy ) ).normalized();
280 
281  double azimuth = azimuthMap.invTransform( polarPos.azimuth() );
282 
283  // normalize the azimuth
284  double min = azimuthMap.s1();
285  double max = azimuthMap.s2();
286  if ( max < min )
287  qSwap( min, max );
288 
289  if ( azimuth < min )
290  {
291  azimuth += max - min;
292  }
293  else if ( azimuth > max )
294  {
295  azimuth -= max - min;
296  }
297 
298  const double radius = radialMap.invTransform( polarPos.radius() );
299 
300  return QwtPointPolar( azimuth, radius );
301 }
302 
310 QPoint QwtPolarCanvas::transform( const QwtPointPolar &polarPos ) const
311 {
312  const QwtPolarPlot *pl = plot();
313 
314  const QwtScaleMap azimuthMap = pl->scaleMap( QwtPolar::Azimuth );
315  const QwtScaleMap radialMap = pl->scaleMap( QwtPolar::Radius );
316 
317  const double radius = radialMap.transform( polarPos.radius() );
318  const double azimuth = azimuthMap.transform( polarPos.azimuth() );
319 
320  const QPointF pos = qwtPolar2Pos(
321  pl->plotRect().center(), radius, azimuth );
322 
323  return pos.toPoint();
324 }
325 
326 #if QWT_MOC_INCLUDE
327 #include "moc_qwt_polar_canvas.cpp"
328 #endif
A plotting widget, displaying a polar coordinate system.
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
bool testPaintAttribute(PaintAttribute) const
A point in polar coordinates.
QwtPolarCanvas(QwtPolarPlot *)
Constructor.
virtual ~QwtPolarCanvas()
Destructor.
double s1() const
Definition: qwt_scale_map.h:83
void updateScale(int scaleId)
QPoint qwtPolar2Pos(const QPoint &pole, double radius, double angle)
QRectF plotRect() const
Azimuth.
Definition: qwt_polar.h:36
double radius() const
Returns the radius.
static QWidget * qwtBackgroundWidget(QWidget *w)
QwtPointPolar invTransform(const QPoint &) const
QwtPolarCanvas::PaintAttributes paintAttributes
virtual void paintEvent(QPaintEvent *) QWT_OVERRIDE
void invalidateBackingStore()
Invalidate the internal backing store.
virtual void drawCanvas(QPainter *, const QRectF &) const
virtual void resizeEvent(QResizeEvent *) QWT_OVERRIDE
static void qwtDrawStyledBackground(QWidget *widget, QPainter *painter)
double azimuth() const
Returns the azimuth.
QwtScaleMap scaleMap(int scaleId, double radius) const
QPoint transform(const QwtPointPolar &) const
double s2() const
Definition: qwt_scale_map.h:91
Radius.
Definition: qwt_polar.h:39
#define min(A, B)
Definition: Log.c:64
#define max(A, B)
Definition: Socket.h:88
const QPixmap * backingStore() const
PrivateData * d_data
QwtPolarPlot * plot()
A scale map.
Definition: qwt_scale_map.h:26
double invTransform(double p) const
Number of scales.
Definition: qwt_polar.h:80
QFlags< PaintAttribute > PaintAttributes
Paint attributes.
double transform(double s) const
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
PaintAttribute
Paint attributes.
QwtPointPolar normalized() const


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