[4272] | 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"
|
---|
[8127] | 11 | #include <qwt_painter.h>
|
---|
[4272] | 12 | #include <qpainter.h>
|
---|
| 13 | #include <qevent.h>
|
---|
| 14 | #include <qpixmap.h>
|
---|
| 15 | #include <qstyle.h>
|
---|
| 16 | #include <qstyleoption.h>
|
---|
| 17 | #ifdef Q_WS_X11
|
---|
| 18 | #include <qx11info_x11.h>
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | static inline void qwtDrawStyledBackground(
|
---|
| 22 | QWidget *widget, QPainter *painter )
|
---|
| 23 | {
|
---|
| 24 | QStyleOption opt;
|
---|
| 25 | opt.initFrom( widget );
|
---|
| 26 | widget->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, widget );
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | static QWidget *qwtBackgroundWidget( QWidget *w )
|
---|
| 30 | {
|
---|
| 31 | if ( w->parentWidget() == NULL )
|
---|
| 32 | return w;
|
---|
| 33 |
|
---|
| 34 | if ( w->autoFillBackground() )
|
---|
| 35 | {
|
---|
| 36 | const QBrush brush = w->palette().brush( w->backgroundRole() );
|
---|
| 37 | if ( brush.color().alpha() > 0 )
|
---|
| 38 | return w;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | if ( w->testAttribute( Qt::WA_StyledBackground ) )
|
---|
| 42 | {
|
---|
| 43 | QImage image( 1, 1, QImage::Format_ARGB32 );
|
---|
| 44 | image.fill( Qt::transparent );
|
---|
| 45 |
|
---|
| 46 | QPainter painter( &image );
|
---|
| 47 | painter.translate( -w->rect().center() );
|
---|
| 48 | qwtDrawStyledBackground( w, &painter );
|
---|
| 49 | painter.end();
|
---|
| 50 |
|
---|
| 51 | if ( qAlpha( image.pixel( 0, 0 ) ) != 0 )
|
---|
| 52 | return w;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | return qwtBackgroundWidget( w->parentWidget() );
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | class QwtPolarCanvas::PrivateData
|
---|
| 59 | {
|
---|
| 60 | public:
|
---|
| 61 | PrivateData():
|
---|
| 62 | paintAttributes( 0 ),
|
---|
| 63 | backingStore( NULL )
|
---|
| 64 | {
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | ~PrivateData()
|
---|
| 68 | {
|
---|
| 69 | delete backingStore;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | QwtPolarCanvas::PaintAttributes paintAttributes;
|
---|
| 73 | QPixmap *backingStore;
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | //! Constructor
|
---|
| 77 | QwtPolarCanvas::QwtPolarCanvas( QwtPolarPlot *plot ):
|
---|
| 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 |
|
---|
| 87 | setPaintAttribute( BackingStore, true );
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | //! Destructor
|
---|
| 91 | QwtPolarCanvas::~QwtPolarCanvas()
|
---|
| 92 | {
|
---|
| 93 | delete d_data;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | //! \return Parent plot widget
|
---|
| 97 | QwtPolarPlot *QwtPolarCanvas::plot()
|
---|
| 98 | {
|
---|
| 99 | return qobject_cast<QwtPolarPlot *>( parent() );
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | //! \return Parent plot widget
|
---|
| 103 | const QwtPolarPlot *QwtPolarCanvas::plot() const
|
---|
| 104 | {
|
---|
| 105 | return qobject_cast<QwtPolarPlot *>( parent() );
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /*!
|
---|
| 109 | \brief Changing the paint attributes
|
---|
| 110 |
|
---|
| 111 | \param attribute Paint attribute
|
---|
| 112 | \param on On/Off
|
---|
| 113 |
|
---|
| 114 | The default setting enables BackingStore
|
---|
| 115 |
|
---|
| 116 | \sa testPaintAttribute(), paintCache()
|
---|
| 117 | */
|
---|
| 118 | void QwtPolarCanvas::setPaintAttribute( PaintAttribute attribute, bool on )
|
---|
| 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 | *d_data->backingStore = QPixmap::grabWidget( this, cr );
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | else
|
---|
| 144 | {
|
---|
| 145 | delete d_data->backingStore;
|
---|
| 146 | d_data->backingStore = NULL;
|
---|
| 147 | }
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /*!
|
---|
| 154 | Test wether a paint attribute is enabled
|
---|
| 155 |
|
---|
| 156 | \param attribute Paint attribute
|
---|
| 157 | \return true if the attribute is enabled
|
---|
| 158 | \sa setPaintAttribute()
|
---|
| 159 | */
|
---|
| 160 | bool QwtPolarCanvas::testPaintAttribute( PaintAttribute attribute ) const
|
---|
| 161 | {
|
---|
| 162 | return ( d_data->paintAttributes & attribute ) != 0;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | //! \return Backing store, might be null
|
---|
| 166 | const QPixmap *QwtPolarCanvas::backingStore() const
|
---|
| 167 | {
|
---|
| 168 | return d_data->backingStore;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | //! Invalidate the internal backing store
|
---|
| 172 | void QwtPolarCanvas::invalidateBackingStore()
|
---|
| 173 | {
|
---|
| 174 | if ( d_data->backingStore )
|
---|
| 175 | *d_data->backingStore = QPixmap();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /*!
|
---|
| 179 | Paint event
|
---|
| 180 | \param event Paint event
|
---|
| 181 | */
|
---|
| 182 | void QwtPolarCanvas::paintEvent( QPaintEvent *event )
|
---|
| 183 | {
|
---|
| 184 | QPainter painter( this );
|
---|
| 185 | painter.setClipRegion( event->region() );
|
---|
| 186 |
|
---|
| 187 | if ( ( d_data->paintAttributes & BackingStore )
|
---|
| 188 | && d_data->backingStore != NULL )
|
---|
| 189 | {
|
---|
| 190 | QPixmap &bs = *d_data->backingStore;
|
---|
| 191 | if ( bs.size() != size() )
|
---|
| 192 | {
|
---|
| 193 | bs = QPixmap( size() );
|
---|
| 194 | #ifdef Q_WS_X11
|
---|
| 195 | if ( bs.x11Info().screen() != x11Info().screen() )
|
---|
| 196 | bs.x11SetScreen( x11Info().screen() );
|
---|
| 197 | #endif
|
---|
| 198 |
|
---|
| 199 | QPainter p;
|
---|
| 200 |
|
---|
| 201 | if ( testAttribute( Qt::WA_StyledBackground ) )
|
---|
| 202 | {
|
---|
| 203 | p.begin( &bs );
|
---|
| 204 | qwtDrawStyledBackground( this, &p );
|
---|
| 205 | }
|
---|
| 206 | else
|
---|
| 207 | {
|
---|
| 208 | if ( autoFillBackground() )
|
---|
| 209 | {
|
---|
| 210 | p.begin( &bs );
|
---|
| 211 | p.fillRect( rect(), palette().brush( backgroundRole() ) );
|
---|
| 212 | }
|
---|
| 213 | else
|
---|
| 214 | {
|
---|
| 215 | QWidget *bgWidget = qwtBackgroundWidget( plot() );
|
---|
[8127] | 216 |
|
---|
| 217 | QwtPainter::fillPixmap( bgWidget, bs,
|
---|
| 218 | mapTo( bgWidget, rect().topLeft() ) );
|
---|
| 219 |
|
---|
[4272] | 220 | p.begin( &bs );
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | plot()->drawCanvas( &p, contentsRect() );
|
---|
| 225 |
|
---|
| 226 | if ( frameWidth() > 0 )
|
---|
| 227 | drawFrame( &p );
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | painter.drawPixmap( 0, 0, *d_data->backingStore );
|
---|
| 231 | }
|
---|
| 232 | else
|
---|
| 233 | {
|
---|
| 234 | qwtDrawStyledBackground( this, &painter );
|
---|
| 235 |
|
---|
| 236 | plot()->drawCanvas( &painter, contentsRect() );
|
---|
| 237 |
|
---|
| 238 | if ( frameWidth() > 0 )
|
---|
| 239 | drawFrame( &painter );
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /*!
|
---|
| 244 | Resize event
|
---|
| 245 | \param event Resize event
|
---|
| 246 | */
|
---|
| 247 | void QwtPolarCanvas::resizeEvent( QResizeEvent *event )
|
---|
| 248 | {
|
---|
| 249 | QFrame::resizeEvent( event );
|
---|
| 250 |
|
---|
| 251 | for ( int scaleId = 0; scaleId < QwtPolar::ScaleCount; scaleId++ )
|
---|
| 252 | plot()->updateScale( scaleId );
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /*!
|
---|
| 256 | Translate a point from widget into plot coordinates
|
---|
| 257 |
|
---|
| 258 | \param pos Point in widget coordinates of the plot canvas
|
---|
| 259 | \return Point in plot coordinates
|
---|
| 260 |
|
---|
| 261 | \sa transform()
|
---|
| 262 | */
|
---|
| 263 | QwtPointPolar QwtPolarCanvas::invTransform( const QPoint &pos ) const
|
---|
| 264 | {
|
---|
| 265 | const QwtPolarPlot *pl = plot();
|
---|
| 266 |
|
---|
| 267 | const QwtScaleMap azimuthMap = pl->scaleMap( QwtPolar::Azimuth );
|
---|
| 268 | const QwtScaleMap radialMap = pl->scaleMap( QwtPolar::Radius );
|
---|
| 269 |
|
---|
| 270 | const QPointF center = pl->plotRect().center();
|
---|
| 271 |
|
---|
| 272 | double dx = pos.x() - center.x();
|
---|
| 273 | double dy = -( pos.y() - center.y() );
|
---|
| 274 |
|
---|
| 275 | const QwtPointPolar polarPos = QwtPointPolar( QPoint( dx, dy ) ).normalized();
|
---|
| 276 |
|
---|
| 277 | double azimuth = azimuthMap.invTransform( polarPos.azimuth() );
|
---|
| 278 |
|
---|
| 279 | // normalize the azimuth
|
---|
| 280 | double min = azimuthMap.s1();
|
---|
| 281 | double max = azimuthMap.s2();
|
---|
| 282 | if ( max < min )
|
---|
| 283 | qSwap( min, max );
|
---|
| 284 |
|
---|
| 285 | if ( azimuth < min )
|
---|
| 286 | {
|
---|
| 287 | azimuth += max - min;
|
---|
| 288 | }
|
---|
| 289 | else if ( azimuth > max )
|
---|
| 290 | {
|
---|
| 291 | azimuth -= max - min;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | const double radius = radialMap.invTransform( polarPos.radius() );
|
---|
| 295 |
|
---|
| 296 | return QwtPointPolar( azimuth, radius );
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /*!
|
---|
| 300 | Translate a point from plot into widget coordinates
|
---|
| 301 |
|
---|
| 302 | \param polarPos Point in plot coordinates
|
---|
| 303 | \return Point in widget coordinates
|
---|
| 304 | \sa transform()
|
---|
| 305 | */
|
---|
| 306 | QPoint QwtPolarCanvas::transform( const QwtPointPolar &polarPos ) const
|
---|
| 307 | {
|
---|
| 308 | const QwtPolarPlot *pl = plot();
|
---|
| 309 |
|
---|
| 310 | const QwtScaleMap azimuthMap = pl->scaleMap( QwtPolar::Azimuth );
|
---|
| 311 | const QwtScaleMap radialMap = pl->scaleMap( QwtPolar::Radius );
|
---|
| 312 |
|
---|
| 313 | const double radius = radialMap.transform( polarPos.radius() );
|
---|
| 314 | const double azimuth = azimuthMap.transform( polarPos.azimuth() );
|
---|
| 315 |
|
---|
| 316 | const QPointF pos = qwtPolar2Pos(
|
---|
| 317 | pl->plotRect().center(), radius, azimuth );
|
---|
| 318 |
|
---|
| 319 | return pos.toPoint();
|
---|
| 320 | }
|
---|