Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_polar_canvas.cpp
1/******************************************************************************
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
59 class QwtPolarCanvas::PrivateData
60{
61 public:
62 PrivateData()
63 : backingStore( NULL )
64 {
65 }
66
67 ~PrivateData()
68 {
69 delete backingStore;
70 }
71
72 QwtPolarCanvas::PaintAttributes paintAttributes;
73 QPixmap* backingStore;
74};
75
77 QwtPolarCanvas::QwtPolarCanvas( QwtPolarPlot* plot )
78 : QFrame( plot )
79{
80 m_data = new PrivateData;
81
82#ifndef QT_NO_CURSOR
83 setCursor( Qt::CrossCursor );
84#endif
85 setFocusPolicy( Qt::WheelFocus );
86
87 setPaintAttribute( BackingStore, true );
88}
89
91 QwtPolarCanvas::~QwtPolarCanvas()
92{
93 delete m_data;
94}
95
97 QwtPolarPlot* QwtPolarCanvas::plot()
98{
99 return qobject_cast< QwtPolarPlot* >( parent() );
100}
101
103 const QwtPolarPlot* QwtPolarCanvas::plot() const
104{
105 return qobject_cast< QwtPolarPlot* >( parent() );
106}
107
118 void QwtPolarCanvas::setPaintAttribute( PaintAttribute attribute, bool on )
119{
120 if ( bool( m_data->paintAttributes & attribute ) == on )
121 return;
122
123 if ( on )
124 m_data->paintAttributes |= attribute;
125 else
126 m_data->paintAttributes &= ~attribute;
127
128 switch( attribute )
129 {
130 case BackingStore:
131 {
132 if ( on )
133 {
134 if ( m_data->backingStore == NULL )
135 m_data->backingStore = new QPixmap();
136
137 if ( isVisible() )
138 {
139 const QRect cr = contentsRect();
140#if QT_VERSION >= 0x050000
141 *m_data->backingStore = grab( cr );
142#else
143 *m_data->backingStore = QPixmap::grabWidget( this, cr );
144#endif
145 }
146 }
147 else
148 {
149 delete m_data->backingStore;
150 m_data->backingStore = NULL;
151 }
152 break;
153 }
154 }
155}
156
164 bool QwtPolarCanvas::testPaintAttribute( PaintAttribute attribute ) const
165{
166 return ( m_data->paintAttributes & attribute ) != 0;
167}
168
170 const QPixmap* QwtPolarCanvas::backingStore() const
171{
172 return m_data->backingStore;
173}
174
176 void QwtPolarCanvas::invalidateBackingStore()
177{
178 if ( m_data->backingStore )
179 *m_data->backingStore = QPixmap();
180}
181
186 void QwtPolarCanvas::paintEvent( QPaintEvent* event )
187{
188 QPainter painter( this );
189 painter.setClipRegion( event->region() );
190
191 if ( ( m_data->paintAttributes & BackingStore )
192 && m_data->backingStore != NULL )
193 {
194 QPixmap& bs = *m_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, *m_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
310QPoint 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#include "moc_qwt_polar_canvas.cpp"
QwtPainter::fillPixmap
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
Definition qwt_painter.cpp:1301
QwtPointPolar
A point in polar coordinates.
Definition qwt_point_polar.h:29
QwtPointPolar::radius
double radius() const
Returns the radius.
Definition qwt_point_polar.h:102
QwtPointPolar::azimuth
double azimuth() const
Returns the azimuth.
Definition qwt_point_polar.h:108
QwtPointPolar::normalized
QwtPointPolar normalized() const
Definition qwt_point_polar.cpp:118
QwtPolarCanvas::PaintAttributes
QFlags< PaintAttribute > PaintAttributes
Definition qwt_polar_canvas.h:50
QwtPolarCanvas::plot
QwtPolarPlot * plot()
Definition qwt_polar_canvas.cpp:97
QwtPolarCanvas::PaintAttribute
PaintAttribute
Paint attributes.
Definition qwt_polar_canvas.h:41
QwtPolarCanvas::BackingStore
@ BackingStore
Definition qwt_polar_canvas.h:47
QwtPolarCanvas::invalidateBackingStore
void invalidateBackingStore()
Invalidate the internal backing store.
Definition qwt_polar_canvas.cpp:176
QwtPolarCanvas::invTransform
QwtPointPolar invTransform(const QPoint &) const
Definition qwt_polar_canvas.cpp:267
QwtPolarCanvas::transform
QPoint transform(const QwtPointPolar &) const
Definition qwt_polar_canvas.cpp:310
QwtPolarCanvas::paintEvent
virtual void paintEvent(QPaintEvent *) override
Definition qwt_polar_canvas.cpp:186
QwtPolarCanvas::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition qwt_polar_canvas.cpp:164
QwtPolarCanvas::QwtPolarCanvas
QwtPolarCanvas(QwtPolarPlot *)
Constructor.
Definition qwt_polar_canvas.cpp:77
QwtPolarCanvas::~QwtPolarCanvas
virtual ~QwtPolarCanvas()
Destructor.
Definition qwt_polar_canvas.cpp:91
QwtPolarCanvas::resizeEvent
virtual void resizeEvent(QResizeEvent *) override
Definition qwt_polar_canvas.cpp:251
QwtPolarCanvas::backingStore
const QPixmap * backingStore() const
Definition qwt_polar_canvas.cpp:170
QwtPolarCanvas::setPaintAttribute
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
Definition qwt_polar_canvas.cpp:118
QwtPolarPlot
A plotting widget, displaying a polar coordinate system.
Definition qwt_polar_plot.h:47
QwtPolarPlot::scaleMap
QwtScaleMap scaleMap(int scaleId, double radius) const
Definition qwt_polar_plot.cpp:730
QwtPolarPlot::drawCanvas
virtual void drawCanvas(QPainter *, const QRectF &) const
Definition qwt_polar_plot.cpp:930
QwtPolarPlot::updateScale
void updateScale(int scaleId)
Definition qwt_polar_plot.cpp:1040
QwtPolarPlot::plotRect
QRectF plotRect() const
Definition qwt_polar_plot.cpp:1120
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtScaleMap::s1
double s1() const
Definition qwt_scale_map.h:83
QwtScaleMap::s2
double s2() const
Definition qwt_scale_map.h:91
QwtScaleMap::invTransform
double invTransform(double p) const
Definition qwt_scale_map.h:154