| 1 | /******************************************************************************
|
|---|
| 2 | * Qwt Widget Library
|
|---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
|---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
|---|
| 5 | *
|
|---|
| 6 | * This library is free software; you can redistribute it and/or
|
|---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
|---|
| 8 | *****************************************************************************/
|
|---|
| 9 |
|
|---|
| 10 | #include "qwt_plot_canvas.h"
|
|---|
| 11 | #include "qwt_painter.h"
|
|---|
| 12 | #include "qwt_plot.h"
|
|---|
| 13 |
|
|---|
| 14 | #include <qpainter.h>
|
|---|
| 15 | #include <qpainterpath.h>
|
|---|
| 16 | #include <qevent.h>
|
|---|
| 17 |
|
|---|
| 18 | class QwtPlotCanvas::PrivateData
|
|---|
| 19 | {
|
|---|
| 20 | public:
|
|---|
| 21 | PrivateData()
|
|---|
| 22 | : backingStore( NULL )
|
|---|
| 23 | {
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | ~PrivateData()
|
|---|
| 27 | {
|
|---|
| 28 | delete backingStore;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | QwtPlotCanvas::PaintAttributes paintAttributes;
|
|---|
| 32 | QPixmap* backingStore;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | /*!
|
|---|
| 36 | \brief Constructor
|
|---|
| 37 |
|
|---|
| 38 | \param plot Parent plot widget
|
|---|
| 39 | \sa QwtPlot::setCanvas()
|
|---|
| 40 | */
|
|---|
| 41 | QwtPlotCanvas::QwtPlotCanvas( QwtPlot* plot )
|
|---|
| 42 | : QFrame( plot )
|
|---|
| 43 | , QwtPlotAbstractCanvas( this )
|
|---|
| 44 | {
|
|---|
| 45 | m_data = new PrivateData;
|
|---|
| 46 |
|
|---|
| 47 | setPaintAttribute( QwtPlotCanvas::BackingStore, true );
|
|---|
| 48 | setPaintAttribute( QwtPlotCanvas::Opaque, true );
|
|---|
| 49 | setPaintAttribute( QwtPlotCanvas::HackStyledBackground, true );
|
|---|
| 50 |
|
|---|
| 51 | setLineWidth( 2 );
|
|---|
| 52 | setFrameShadow( QFrame::Sunken );
|
|---|
| 53 | setFrameShape( QFrame::Panel );
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | //! Destructor
|
|---|
| 57 | QwtPlotCanvas::~QwtPlotCanvas()
|
|---|
| 58 | {
|
|---|
| 59 | delete m_data;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /*!
|
|---|
| 63 | \brief Changing the paint attributes
|
|---|
| 64 |
|
|---|
| 65 | \param attribute Paint attribute
|
|---|
| 66 | \param on On/Off
|
|---|
| 67 |
|
|---|
| 68 | \sa testPaintAttribute(), backingStore()
|
|---|
| 69 | */
|
|---|
| 70 | void QwtPlotCanvas::setPaintAttribute( PaintAttribute attribute, bool on )
|
|---|
| 71 | {
|
|---|
| 72 | if ( bool( m_data->paintAttributes & attribute ) == on )
|
|---|
| 73 | return;
|
|---|
| 74 |
|
|---|
| 75 | if ( on )
|
|---|
| 76 | m_data->paintAttributes |= attribute;
|
|---|
| 77 | else
|
|---|
| 78 | m_data->paintAttributes &= ~attribute;
|
|---|
| 79 |
|
|---|
| 80 | switch ( attribute )
|
|---|
| 81 | {
|
|---|
| 82 | case BackingStore:
|
|---|
| 83 | {
|
|---|
| 84 | if ( on )
|
|---|
| 85 | {
|
|---|
| 86 | if ( m_data->backingStore == NULL )
|
|---|
| 87 | m_data->backingStore = new QPixmap();
|
|---|
| 88 |
|
|---|
| 89 | if ( isVisible() )
|
|---|
| 90 | {
|
|---|
| 91 | #if QT_VERSION >= 0x050000
|
|---|
| 92 | *m_data->backingStore = grab( rect() );
|
|---|
| 93 | #else
|
|---|
| 94 | *m_data->backingStore =
|
|---|
| 95 | QPixmap::grabWidget( this, rect() );
|
|---|
| 96 | #endif
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | else
|
|---|
| 100 | {
|
|---|
| 101 | delete m_data->backingStore;
|
|---|
| 102 | m_data->backingStore = NULL;
|
|---|
| 103 | }
|
|---|
| 104 | break;
|
|---|
| 105 | }
|
|---|
| 106 | case Opaque:
|
|---|
| 107 | {
|
|---|
| 108 | if ( on )
|
|---|
| 109 | setAttribute( Qt::WA_OpaquePaintEvent, true );
|
|---|
| 110 |
|
|---|
| 111 | break;
|
|---|
| 112 | }
|
|---|
| 113 | default:
|
|---|
| 114 | {
|
|---|
| 115 | break;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /*!
|
|---|
| 121 | Test whether a paint attribute is enabled
|
|---|
| 122 |
|
|---|
| 123 | \param attribute Paint attribute
|
|---|
| 124 | \return true, when attribute is enabled
|
|---|
| 125 | \sa setPaintAttribute()
|
|---|
| 126 | */
|
|---|
| 127 | bool QwtPlotCanvas::testPaintAttribute( PaintAttribute attribute ) const
|
|---|
| 128 | {
|
|---|
| 129 | return m_data->paintAttributes & attribute;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | //! \return Backing store, might be null
|
|---|
| 133 | const QPixmap* QwtPlotCanvas::backingStore() const
|
|---|
| 134 | {
|
|---|
| 135 | return m_data->backingStore;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | //! Invalidate the internal backing store
|
|---|
| 139 | void QwtPlotCanvas::invalidateBackingStore()
|
|---|
| 140 | {
|
|---|
| 141 | if ( m_data->backingStore )
|
|---|
| 142 | *m_data->backingStore = QPixmap();
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /*!
|
|---|
| 146 | Qt event handler for QEvent::PolishRequest and QEvent::StyleChange
|
|---|
| 147 |
|
|---|
| 148 | \param event Qt Event
|
|---|
| 149 | \return See QFrame::event()
|
|---|
| 150 | */
|
|---|
| 151 | bool QwtPlotCanvas::event( QEvent* event )
|
|---|
| 152 | {
|
|---|
| 153 | if ( event->type() == QEvent::PolishRequest )
|
|---|
| 154 | {
|
|---|
| 155 | if ( testPaintAttribute( QwtPlotCanvas::Opaque ) )
|
|---|
| 156 | {
|
|---|
| 157 | // Setting a style sheet changes the
|
|---|
| 158 | // Qt::WA_OpaquePaintEvent attribute, but we insist
|
|---|
| 159 | // on painting the background.
|
|---|
| 160 |
|
|---|
| 161 | setAttribute( Qt::WA_OpaquePaintEvent, true );
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if ( event->type() == QEvent::PolishRequest ||
|
|---|
| 166 | event->type() == QEvent::StyleChange )
|
|---|
| 167 | {
|
|---|
| 168 | updateStyleSheetInfo();
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | return QFrame::event( event );
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /*!
|
|---|
| 175 | Paint event
|
|---|
| 176 | \param event Paint event
|
|---|
| 177 | */
|
|---|
| 178 | void QwtPlotCanvas::paintEvent( QPaintEvent* event )
|
|---|
| 179 | {
|
|---|
| 180 | QPainter painter( this );
|
|---|
| 181 | painter.setClipRegion( event->region() );
|
|---|
| 182 |
|
|---|
| 183 | if ( testPaintAttribute( QwtPlotCanvas::BackingStore ) &&
|
|---|
| 184 | m_data->backingStore != NULL )
|
|---|
| 185 | {
|
|---|
| 186 | QPixmap& bs = *m_data->backingStore;
|
|---|
| 187 | if ( bs.size() != size() * QwtPainter::devicePixelRatio( &bs ) )
|
|---|
| 188 | {
|
|---|
| 189 | bs = QwtPainter::backingStore( this, size() );
|
|---|
| 190 |
|
|---|
| 191 | if ( testAttribute(Qt::WA_StyledBackground) )
|
|---|
| 192 | {
|
|---|
| 193 | QPainter p( &bs );
|
|---|
| 194 | drawStyled( &p, testPaintAttribute( HackStyledBackground ) );
|
|---|
| 195 | }
|
|---|
| 196 | else
|
|---|
| 197 | {
|
|---|
| 198 | QPainter p;
|
|---|
| 199 | if ( borderRadius() <= 0.0 )
|
|---|
| 200 | {
|
|---|
| 201 | QwtPainter::fillPixmap( this, bs );
|
|---|
| 202 | p.begin( &bs );
|
|---|
| 203 | drawCanvas( &p );
|
|---|
| 204 | }
|
|---|
| 205 | else
|
|---|
| 206 | {
|
|---|
| 207 | p.begin( &bs );
|
|---|
| 208 | drawUnstyled( &p );
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | if ( frameWidth() > 0 )
|
|---|
| 212 | drawBorder( &p );
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | painter.drawPixmap( 0, 0, *m_data->backingStore );
|
|---|
| 217 | }
|
|---|
| 218 | else
|
|---|
| 219 | {
|
|---|
| 220 | if ( testAttribute(Qt::WA_StyledBackground ) )
|
|---|
| 221 | {
|
|---|
| 222 | if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
|
|---|
| 223 | {
|
|---|
| 224 | drawStyled( &painter, testPaintAttribute( HackStyledBackground ) );
|
|---|
| 225 | }
|
|---|
| 226 | else
|
|---|
| 227 | {
|
|---|
| 228 | drawCanvas( &painter );
|
|---|
| 229 | }
|
|---|
| 230 | }
|
|---|
| 231 | else
|
|---|
| 232 | {
|
|---|
| 233 | if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
|
|---|
| 234 | {
|
|---|
| 235 | if ( autoFillBackground() )
|
|---|
| 236 | {
|
|---|
| 237 | fillBackground( &painter );
|
|---|
| 238 | drawBackground( &painter );
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 | else
|
|---|
| 242 | {
|
|---|
| 243 | if ( borderRadius() > 0.0 )
|
|---|
| 244 | {
|
|---|
| 245 | QPainterPath clipPath;
|
|---|
| 246 | clipPath.addRect( rect() );
|
|---|
| 247 | clipPath = clipPath.subtracted( borderPath( rect() ) );
|
|---|
| 248 |
|
|---|
| 249 | painter.save();
|
|---|
| 250 |
|
|---|
| 251 | painter.setClipPath( clipPath, Qt::IntersectClip );
|
|---|
| 252 | fillBackground( &painter );
|
|---|
| 253 | drawBackground( &painter );
|
|---|
| 254 |
|
|---|
| 255 | painter.restore();
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | drawCanvas( &painter );
|
|---|
| 260 |
|
|---|
| 261 | if ( frameWidth() > 0 )
|
|---|
| 262 | drawBorder( &painter );
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
|
|---|
| 267 | drawFocusIndicator( &painter );
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /*!
|
|---|
| 271 | Draw the border of the plot canvas
|
|---|
| 272 |
|
|---|
| 273 | \param painter Painter
|
|---|
| 274 | \sa setBorderRadius()
|
|---|
| 275 | */
|
|---|
| 276 | void QwtPlotCanvas::drawBorder( QPainter* painter )
|
|---|
| 277 | {
|
|---|
| 278 | if ( borderRadius() <= 0 )
|
|---|
| 279 | {
|
|---|
| 280 | drawFrame( painter );
|
|---|
| 281 | return;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | QwtPlotAbstractCanvas::drawBorder( painter );
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /*!
|
|---|
| 288 | Resize event
|
|---|
| 289 | \param event Resize event
|
|---|
| 290 | */
|
|---|
| 291 | void QwtPlotCanvas::resizeEvent( QResizeEvent* event )
|
|---|
| 292 | {
|
|---|
| 293 | QFrame::resizeEvent( event );
|
|---|
| 294 | updateStyleSheetInfo();
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /*!
|
|---|
| 298 | Invalidate the paint cache and repaint the canvas
|
|---|
| 299 | \sa invalidatePaintCache()
|
|---|
| 300 | */
|
|---|
| 301 | void QwtPlotCanvas::replot()
|
|---|
| 302 | {
|
|---|
| 303 | invalidateBackingStore();
|
|---|
| 304 |
|
|---|
| 305 | if ( testPaintAttribute( QwtPlotCanvas::ImmediatePaint ) )
|
|---|
| 306 | repaint( contentsRect() );
|
|---|
| 307 | else
|
|---|
| 308 | update( contentsRect() );
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /*!
|
|---|
| 312 | Calculate the painter path for a styled or rounded border
|
|---|
| 313 |
|
|---|
| 314 | When the canvas has no styled background or rounded borders
|
|---|
| 315 | the painter path is empty.
|
|---|
| 316 |
|
|---|
| 317 | \param rect Bounding rectangle of the canvas
|
|---|
| 318 | \return Painter path, that can be used for clipping
|
|---|
| 319 | */
|
|---|
| 320 | QPainterPath QwtPlotCanvas::borderPath( const QRect& rect ) const
|
|---|
| 321 | {
|
|---|
| 322 | return canvasBorderPath( rect );
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | #include "moc_qwt_plot_canvas.cpp"
|
|---|