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
22static 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
30static 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
59class 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
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
88}
89
92{
93 delete m_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( 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
165{
166 return ( m_data->paintAttributes & attribute ) != 0;
167}
168
170const QPixmap* QwtPolarCanvas::backingStore() const
171{
172 return m_data->backingStore;
173}
174
177{
178 if ( m_data->backingStore )
179 *m_data->backingStore = QPixmap();
180}
181
186void 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
251void 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
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"
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
A point in polar coordinates.
double radius() const
Returns the radius.
double azimuth() const
Returns the azimuth.
QwtPointPolar normalized() const
QFlags< PaintAttribute > PaintAttributes
QwtPolarPlot * plot()
PaintAttribute
Paint attributes.
void invalidateBackingStore()
Invalidate the internal backing store.
QwtPointPolar invTransform(const QPoint &) const
QPoint transform(const QwtPointPolar &) const
virtual void paintEvent(QPaintEvent *) override
bool testPaintAttribute(PaintAttribute) const
QwtPolarCanvas(QwtPolarPlot *)
Constructor.
virtual ~QwtPolarCanvas()
Destructor.
virtual void resizeEvent(QResizeEvent *) override
const QPixmap * backingStore() const
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
A plotting widget, displaying a polar coordinate system.
QwtScaleMap scaleMap(int scaleId, double radius) const
virtual void drawCanvas(QPainter *, const QRectF &) const
void updateScale(int scaleId)
QRectF plotRect() const
A scale map.
double transform(double s) const
double s1() const
double s2() const
double invTransform(double p) const