| [11158] | 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 |
|
|---|
| 76 | //! Constructor
|
|---|
| 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 |
|
|---|
| 90 | //! Destructor
|
|---|
| 91 | QwtPolarCanvas::~QwtPolarCanvas()
|
|---|
| 92 | {
|
|---|
| 93 | delete m_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( 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 |
|
|---|
| 157 | /*!
|
|---|
| 158 | Test whether a paint attribute is enabled
|
|---|
| 159 |
|
|---|
| 160 | \param attribute Paint attribute
|
|---|
| 161 | \return true if the attribute is enabled
|
|---|
| 162 | \sa setPaintAttribute()
|
|---|
| 163 | */
|
|---|
| 164 | bool QwtPolarCanvas::testPaintAttribute( PaintAttribute attribute ) const
|
|---|
| 165 | {
|
|---|
| 166 | return ( m_data->paintAttributes & attribute ) != 0;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | //! \return Backing store, might be null
|
|---|
| 170 | const QPixmap* QwtPolarCanvas::backingStore() const
|
|---|
| 171 | {
|
|---|
| 172 | return m_data->backingStore;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | //! Invalidate the internal backing store
|
|---|
| 176 | void QwtPolarCanvas::invalidateBackingStore()
|
|---|
| 177 | {
|
|---|
| 178 | if ( m_data->backingStore )
|
|---|
| 179 | *m_data->backingStore = QPixmap();
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /*!
|
|---|
| 183 | Paint event
|
|---|
| 184 | \param event Paint event
|
|---|
| 185 | */
|
|---|
| 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 |
|
|---|
| 247 | /*!
|
|---|
| 248 | Resize event
|
|---|
| 249 | \param event Resize event
|
|---|
| 250 | */
|
|---|
| 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 |
|
|---|
| 259 | /*!
|
|---|
| 260 | Translate a point from widget into plot coordinates
|
|---|
| 261 |
|
|---|
| 262 | \param pos Point in widget coordinates of the plot canvas
|
|---|
| 263 | \return Point in plot coordinates
|
|---|
| 264 |
|
|---|
| 265 | \sa transform()
|
|---|
| 266 | */
|
|---|
| 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 |
|
|---|
| 303 | /*!
|
|---|
| 304 | Translate a point from plot into widget coordinates
|
|---|
| 305 |
|
|---|
| 306 | \param polarPos Point in plot coordinates
|
|---|
| 307 | \return Point in widget coordinates
|
|---|
| 308 | \sa transform()
|
|---|
| 309 | */
|
|---|
| 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 | #include "moc_qwt_polar_canvas.cpp"
|
|---|