qwt_plot_canvas.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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 
19 {
20 public:
22  backingStore( NULL )
23  {
24  }
25 
27  {
28  delete backingStore;
29  }
30 
32  QPixmap *backingStore;
33 };
34 
42  QFrame( plot ),
43  QwtPlotAbstractCanvas( this )
44 {
45  d_data = new PrivateData;
46 
50 }
51 
54 {
55  delete d_data;
56 }
57 
67 {
68  if ( bool( d_data->paintAttributes & attribute ) == on )
69  return;
70 
71  if ( on )
72  d_data->paintAttributes |= attribute;
73  else
74  d_data->paintAttributes &= ~attribute;
75 
76  switch ( attribute )
77  {
78  case BackingStore:
79  {
80  if ( on )
81  {
82  if ( d_data->backingStore == NULL )
83  d_data->backingStore = new QPixmap();
84 
85  if ( isVisible() )
86  {
87 #if QT_VERSION >= 0x050000
88  *d_data->backingStore = grab( rect() );
89 #else
91  QPixmap::grabWidget( this, rect() );
92 #endif
93  }
94  }
95  else
96  {
97  delete d_data->backingStore;
98  d_data->backingStore = NULL;
99  }
100  break;
101  }
102  case Opaque:
103  {
104  if ( on )
105  setAttribute( Qt::WA_OpaquePaintEvent, true );
106 
107  break;
108  }
109  default:
110  {
111  break;
112  }
113  }
114 }
115 
124 {
125  return d_data->paintAttributes & attribute;
126 }
127 
129 const QPixmap *QwtPlotCanvas::backingStore() const
130 {
131  return d_data->backingStore;
132 }
133 
136 {
137  if ( d_data->backingStore )
138  *d_data->backingStore = QPixmap();
139 }
140 
148 {
149  if ( event->type() == QEvent::PolishRequest )
150  {
152  {
153  // Setting a style sheet changes the
154  // Qt::WA_OpaquePaintEvent attribute, but we insist
155  // on painting the background.
156 
157  setAttribute( Qt::WA_OpaquePaintEvent, true );
158  }
159  }
160 
161  if ( event->type() == QEvent::PolishRequest ||
162  event->type() == QEvent::StyleChange )
163  {
165  }
166 
167  return QFrame::event( event );
168 }
169 
174 void QwtPlotCanvas::paintEvent( QPaintEvent *event )
175 {
176  QPainter painter( this );
177  painter.setClipRegion( event->region() );
178 
180  d_data->backingStore != NULL )
181  {
182  QPixmap &bs = *d_data->backingStore;
183  if ( bs.size() != size() * QwtPainter::devicePixelRatio( &bs ) )
184  {
185  bs = QwtPainter::backingStore( this, size() );
186 
187  if ( testAttribute(Qt::WA_StyledBackground) )
188  {
189  QPainter p( &bs );
191  }
192  else
193  {
194  QPainter p;
195  if ( borderRadius() <= 0.0 )
196  {
197  QwtPainter::fillPixmap( this, bs );
198  p.begin( &bs );
199  drawCanvas( &p );
200  }
201  else
202  {
203  p.begin( &bs );
204  drawUnstyled( &p );
205  }
206 
207  if ( frameWidth() > 0 )
208  drawBorder( &p );
209  }
210  }
211 
212  painter.drawPixmap( 0, 0, *d_data->backingStore );
213  }
214  else
215  {
216  if ( testAttribute(Qt::WA_StyledBackground ) )
217  {
218  if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
219  {
221  }
222  else
223  {
224  drawCanvas( &painter );
225  }
226  }
227  else
228  {
229  if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
230  {
231  if ( autoFillBackground() )
232  {
233  fillBackground( &painter );
234  drawBackground( &painter );
235  }
236  }
237  else
238  {
239  if ( borderRadius() > 0.0 )
240  {
241  QPainterPath clipPath;
242  clipPath.addRect( rect() );
243  clipPath = clipPath.subtracted( borderPath( rect() ) );
244 
245  painter.save();
246 
247  painter.setClipPath( clipPath, Qt::IntersectClip );
248  fillBackground( &painter );
249  drawBackground( &painter );
250 
251  painter.restore();
252  }
253  }
254 
255  drawCanvas( &painter );
256 
257  if ( frameWidth() > 0 )
258  drawBorder( &painter );
259  }
260  }
261 
262  if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
263  drawFocusIndicator( &painter );
264 }
265 
272 void QwtPlotCanvas::drawBorder( QPainter *painter )
273 {
274  if ( borderRadius() <= 0 )
275  {
276  drawFrame( painter );
277  return;
278  }
279 
281 }
282 
287 void QwtPlotCanvas::resizeEvent( QResizeEvent *event )
288 {
289  QFrame::resizeEvent( event );
291 }
292 
298 {
300 
302  repaint( contentsRect() );
303  else
304  update( contentsRect() );
305 }
306 
316 QPainterPath QwtPlotCanvas::borderPath( const QRect &rect ) const
317 {
318  return borderPath2( rect );
319 }
320 
321 #if QWT_MOC_INCLUDE
322 #include "moc_qwt_plot_canvas.cpp"
323 #endif
PrivateData * d_data
Try to fill the complete contents rectangle of the plot canvas.
virtual void drawBackground(QPainter *)
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
virtual void paintEvent(QPaintEvent *) QWT_OVERRIDE
QFlags< PaintAttribute > PaintAttributes
Paint attributes.
virtual bool event(QEvent *) QWT_OVERRIDE
virtual void drawBorder(QPainter *) QWT_OVERRIDE
QwtPlotCanvas(QwtPlot *=NULL)
Constructor.
virtual ~QwtPlotCanvas()
Destructor.
FocusIndicator focusIndicator() const
A 2-D plotting widget.
Definition: qwt_plot.h:75
virtual void resizeEvent(QResizeEvent *) QWT_OVERRIDE
bool testPaintAttribute(PaintAttribute) const
void drawStyled(QPainter *, bool)
Q_INVOKABLE QPainterPath borderPath(const QRect &) const
QwtPlotCanvas::PaintAttributes paintAttributes
const QPixmap * backingStore() const
static qreal devicePixelRatio(const QPaintDevice *)
PaintAttribute
Paint attributes.
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
QPainterPath borderPath2(const QRect &rect) const
virtual void drawBorder(QPainter *)
QwtPlot * plot()
Return parent plot widget.
Q_INVOKABLE void invalidateBackingStore()
Invalidate the internal backing store.
virtual void drawFocusIndicator(QPainter *)
void updateStyleSheetInfo()
Update the cached information about the current style sheet.
static QPixmap backingStore(QWidget *, const QSize &)
Try to improve painting of styled backgrounds.
Paint double buffered reusing the content of the pixmap buffer when possible.


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10