Qwt User's Guide: qwt_plot_canvas.cpp Source File

Qwt User's Guide 6.2.0

qwt_plot_canvas.cpp
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
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
57 QwtPlotCanvas::~QwtPlotCanvas()
58 {
59 delete m_data;
60 }
61
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
127 bool QwtPlotCanvas::testPaintAttribute( PaintAttribute attribute ) const
128 {
129 return m_data->paintAttributes & attribute;
130 }
131
133 const QPixmap* QwtPlotCanvas::backingStore() const
134 {
135 return m_data->backingStore;
136 }
137
139 void QwtPlotCanvas::invalidateBackingStore()
140 {
141 if ( m_data->backingStore )
142 *m_data->backingStore = QPixmap();
143 }
144
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
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
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
291 void QwtPlotCanvas::resizeEvent( QResizeEvent* event )
292 {
293 QFrame::resizeEvent( event );
294 updateStyleSheetInfo();
295 }
296
301 void QwtPlotCanvas::replot()
302 {
303 invalidateBackingStore();
304
305 if ( testPaintAttribute( QwtPlotCanvas::ImmediatePaint ) )
306 repaint( contentsRect() );
307 else
308 update( contentsRect() );
309 }
310
320 QPainterPath QwtPlotCanvas::borderPath( const QRect& rect ) const
321 {
322 return canvasBorderPath( rect );
323 }
324
325 #if QWT_MOC_INCLUDE
326 #include "moc_qwt_plot_canvas.cpp"
327 #endif
QwtPainter::fillPixmap
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
Definition: qwt_painter.cpp:1311
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition: qwt_painter.cpp:1491
QwtPainter::backingStore
static QPixmap backingStore(QWidget *, const QSize &)
Definition: qwt_painter.cpp:1525
QwtPlotAbstractCanvas
Base class for all type of plot canvases.
Definition: qwt_plot_abstract_canvas.h:22
QwtPlotAbstractCanvas::fillBackground
void fillBackground(QPainter *)
Helper function for the derived plot canvas.
Definition: qwt_plot_abstract_canvas.cpp:696
QwtPlotAbstractCanvas::drawBackground
virtual void drawBackground(QPainter *)
Helper function for the derived plot canvas.
Definition: qwt_plot_abstract_canvas.cpp:690
QwtPlotAbstractCanvas::drawUnstyled
void drawUnstyled(QPainter *)
Helper function for the derived plot canvas.
Definition: qwt_plot_abstract_canvas.cpp:702
QwtPlotAbstractCanvas::focusIndicator
FocusIndicator focusIndicator() const
Definition: qwt_plot_abstract_canvas.cpp:578
QwtPlotAbstractCanvas::updateStyleSheetInfo
void updateStyleSheetInfo()
Update the cached information about the current style sheet.
Definition: qwt_plot_abstract_canvas.cpp:835
QwtPlotAbstractCanvas::borderRadius
double borderRadius() const
Definition: qwt_plot_abstract_canvas.cpp:613
QwtPlotAbstractCanvas::canvasBorderPath
QPainterPath canvasBorderPath(const QRect &rect) const
Definition: qwt_plot_abstract_canvas.cpp:619
QwtPlotAbstractCanvas::drawBorder
virtual void drawBorder(QPainter *)
Definition: qwt_plot_abstract_canvas.cpp:628
QwtPlotAbstractCanvas::drawCanvas
void drawCanvas(QPainter *)
Draw the plot to the canvas.
Definition: qwt_plot_abstract_canvas.cpp:803
QwtPlotAbstractCanvas::drawStyled
void drawStyled(QPainter *, bool)
Helper function for the derived plot canvas.
Definition: qwt_plot_abstract_canvas.cpp:744
QwtPlotAbstractCanvas::drawFocusIndicator
virtual void drawFocusIndicator(QPainter *)
Definition: qwt_plot_abstract_canvas.cpp:587
QwtPlotAbstractCanvas::CanvasFocusIndicator
@ CanvasFocusIndicator
Definition: qwt_plot_abstract_canvas.h:39
QwtPlotCanvas::PaintAttributes
QFlags< PaintAttribute > PaintAttributes
Definition: qwt_plot_canvas.h:102
QwtPlotCanvas::paintEvent
virtual void paintEvent(QPaintEvent *) override
Definition: qwt_plot_canvas.cpp:178
QwtPlotCanvas::replot
void replot()
Definition: qwt_plot_canvas.cpp:301
QwtPlotCanvas::~QwtPlotCanvas
virtual ~QwtPlotCanvas()
Destructor.
Definition: qwt_plot_canvas.cpp:57
QwtPlotCanvas::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition: qwt_plot_canvas.cpp:127
QwtPlotCanvas::PaintAttribute
PaintAttribute
Paint attributes.
Definition: qwt_plot_canvas.h:45
QwtPlotCanvas::Opaque
@ Opaque
Try to fill the complete contents rectangle of the plot canvas.
Definition: qwt_plot_canvas.h:74
QwtPlotCanvas::HackStyledBackground
@ HackStyledBackground
Try to improve painting of styled backgrounds.
Definition: qwt_plot_canvas.h:91
QwtPlotCanvas::BackingStore
@ BackingStore
Paint double buffered reusing the content of the pixmap buffer when possible.
Definition: qwt_plot_canvas.h:57
QwtPlotCanvas::ImmediatePaint
@ ImmediatePaint
Definition: qwt_plot_canvas.h:99
QwtPlotCanvas::setPaintAttribute
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
Definition: qwt_plot_canvas.cpp:70
QwtPlotCanvas::resizeEvent
virtual void resizeEvent(QResizeEvent *) override
Definition: qwt_plot_canvas.cpp:291
QwtPlotCanvas::QwtPlotCanvas
QwtPlotCanvas(QwtPlot *=NULL)
Constructor.
Definition: qwt_plot_canvas.cpp:41
QwtPlotCanvas::event
virtual bool event(QEvent *) override
Definition: qwt_plot_canvas.cpp:151
QwtPlotCanvas::backingStore
const QPixmap * backingStore() const
Definition: qwt_plot_canvas.cpp:133
QwtPlotCanvas::invalidateBackingStore
Q_INVOKABLE void invalidateBackingStore()
Invalidate the internal backing store.
Definition: qwt_plot_canvas.cpp:139
QwtPlotCanvas::drawBorder
virtual void drawBorder(QPainter *) override
Definition: qwt_plot_canvas.cpp:276
QwtPlotCanvas::borderPath
Q_INVOKABLE QPainterPath borderPath(const QRect &) const
Definition: qwt_plot_canvas.cpp:320
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:79