Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
18class 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
42 : QFrame( plot )
43 , QwtPlotAbstractCanvas( this )
44{
45 m_data = new PrivateData;
46
50
51 setLineWidth( 2 );
52 setFrameShadow( QFrame::Sunken );
53 setFrameShape( QFrame::Panel );
54}
55
58{
59 delete m_data;
60}
61
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
128{
129 return m_data->paintAttributes & attribute;
130}
131
133const QPixmap* QwtPlotCanvas::backingStore() const
134{
135 return m_data->backingStore;
136}
137
140{
141 if ( m_data->backingStore )
142 *m_data->backingStore = QPixmap();
143}
144
151bool QwtPlotCanvas::event( QEvent* event )
152{
153 if ( event->type() == QEvent::PolishRequest )
154 {
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 {
169 }
170
171 return QFrame::event( event );
172}
173
178void QwtPlotCanvas::paintEvent( QPaintEvent* event )
179{
180 QPainter painter( this );
181 painter.setClipRegion( event->region() );
182
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 );
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 {
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
276void QwtPlotCanvas::drawBorder( QPainter* painter )
277{
278 if ( borderRadius() <= 0 )
279 {
280 drawFrame( painter );
281 return;
282 }
283
285}
286
291void QwtPlotCanvas::resizeEvent( QResizeEvent* event )
292{
293 QFrame::resizeEvent( event );
295}
296
302{
304
306 repaint( contentsRect() );
307 else
308 update( contentsRect() );
309}
310
320QPainterPath QwtPlotCanvas::borderPath( const QRect& rect ) const
321{
322 return canvasBorderPath( rect );
323}
324
325#include "moc_qwt_plot_canvas.cpp"
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
static qreal devicePixelRatio(const QPaintDevice *)
static QPixmap backingStore(QWidget *, const QSize &)
Base class for all type of plot canvases.
void fillBackground(QPainter *)
Helper function for the derived plot canvas.
virtual void drawBackground(QPainter *)
Helper function for the derived plot canvas.
void drawUnstyled(QPainter *)
Helper function for the derived plot canvas.
FocusIndicator focusIndicator() const
void updateStyleSheetInfo()
Update the cached information about the current style sheet.
QPainterPath canvasBorderPath(const QRect &rect) const
virtual void drawBorder(QPainter *)
void drawCanvas(QPainter *)
Draw the plot to the canvas.
void drawStyled(QPainter *, bool)
Helper function for the derived plot canvas.
virtual void drawFocusIndicator(QPainter *)
QFlags< PaintAttribute > PaintAttributes
virtual void paintEvent(QPaintEvent *) override
virtual ~QwtPlotCanvas()
Destructor.
bool testPaintAttribute(PaintAttribute) const
PaintAttribute
Paint attributes.
@ Opaque
Try to fill the complete contents rectangle of the plot canvas.
@ HackStyledBackground
Try to improve painting of styled backgrounds.
@ BackingStore
Paint double buffered reusing the content of the pixmap buffer when possible.
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
virtual void resizeEvent(QResizeEvent *) override
QwtPlotCanvas(QwtPlot *=NULL)
Constructor.
virtual bool event(QEvent *) override
const QPixmap * backingStore() const
Q_INVOKABLE void invalidateBackingStore()
Invalidate the internal backing store.
virtual void drawBorder(QPainter *) override
Q_INVOKABLE QPainterPath borderPath(const QRect &) const
A 2-D plotting widget.
Definition qwt_plot.h:79