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_math.h"
13 #include "qwt_plot.h"
14 
15 #ifndef QWT_NO_OPENGL
16 
17 #if QT_VERSION < 0x050000
18 #define FBO_OPENGL 0
19 #else
20 #define FBO_OPENGL 1
21 #endif
22 
23 #if FBO_OPENGL
24 #include <qopenglcontext.h>
25 #include <qopenglframebufferobject.h>
26 #include <qopenglpaintdevice.h>
27 
28 #if QT_VERSION >= 0x050100
29 #include <qoffscreensurface.h>
30 typedef QOffscreenSurface QwtPlotCanvasSurfaceGL;
31 
32 #else
33 #include <qwindow.h>
34 class QwtPlotCanvasSurfaceGL: public QWindow
35 {
36 public:
37  QwtPlotCanvasSurfaceGL() { setSurfaceType( QWindow::OpenGLSurface ); }
38 };
39 #endif
40 
41 #else
42 #include <qglframebufferobject.h>
43 typedef QGLWidget QwtPlotCanvasSurfaceGL;
44 #endif
45 
46 #endif // !QWT_NO_OPENGL
47 
48 #include <qpainter.h>
49 #include <qstyle.h>
50 #include <qstyleoption.h>
51 #include <qpaintengine.h>
52 #include <qevent.h>
53 
55 {
56 public:
58  paintAttributes( 0 ),
59 #ifndef QWT_NO_OPENGL
60  surfaceGL( NULL ),
61 #endif
62  backingStore( NULL )
63  {
64  }
65 
67  {
68  delete backingStore;
69 
70 #ifndef QWT_NO_OPENGL
71  delete surfaceGL;
72 #endif
73  }
74 
76 
77 #ifndef QWT_NO_OPENGL
79 #endif
80 
81  QPixmap *backingStore;
82 };
83 
91  QFrame( plot ),
92  QwtPlotAbstractCanvas( this )
93 {
94  d_data = new PrivateData;
95 
99 }
100 
103 {
104  delete d_data;
105 }
106 
116 {
117  if ( bool( d_data->paintAttributes & attribute ) == on )
118  return;
119 
120  if ( on )
121  d_data->paintAttributes |= attribute;
122  else
123  d_data->paintAttributes &= ~attribute;
124 
125  switch ( attribute )
126  {
127  case BackingStore:
128  {
129  if ( on )
130  {
131  if ( d_data->backingStore == NULL )
132  d_data->backingStore = new QPixmap();
133 
134  if ( isVisible() )
135  {
136 #if QT_VERSION >= 0x050000
137  *d_data->backingStore = grab( rect() );
138 #else
139  *d_data->backingStore =
140  QPixmap::grabWidget( this, rect() );
141 #endif
142  }
143  }
144  else
145  {
146  delete d_data->backingStore;
147  d_data->backingStore = NULL;
148  }
149  break;
150  }
151  case Opaque:
152  {
153  if ( on )
154  setAttribute( Qt::WA_OpaquePaintEvent, true );
155 
156  break;
157  }
158  default:
159  {
160  break;
161  }
162  }
163 }
164 
173 {
174  return d_data->paintAttributes & attribute;
175 }
176 
178 const QPixmap *QwtPlotCanvas::backingStore() const
179 {
180  return d_data->backingStore;
181 }
182 
185 {
186  if ( d_data->backingStore )
187  *d_data->backingStore = QPixmap();
188 }
189 
197 {
198  if ( event->type() == QEvent::PolishRequest )
199  {
201  {
202  // Setting a style sheet changes the
203  // Qt::WA_OpaquePaintEvent attribute, but we insist
204  // on painting the background.
205 
206  setAttribute( Qt::WA_OpaquePaintEvent, true );
207  }
208  }
209 
210  if ( event->type() == QEvent::PolishRequest ||
211  event->type() == QEvent::StyleChange )
212  {
214  }
215 
216  return QFrame::event( event );
217 }
218 
223 void QwtPlotCanvas::paintEvent( QPaintEvent *event )
224 {
225  QPainter painter( this );
226  painter.setClipRegion( event->region() );
227 
229  d_data->backingStore != NULL )
230  {
231  QPixmap &bs = *d_data->backingStore;
232  if ( bs.size() != size() )
233  {
234  bs = QwtPainter::backingStore( this, size() );
235 
236 #ifndef QWT_NO_OPENGL
238  {
239  QPainter p( &bs );
240  p.drawImage( 0, 0, toImageFBO( size() ) );
241  p.end();
242  }
243  else
244 #endif
245  if ( testAttribute(Qt::WA_StyledBackground) )
246  {
247  QPainter p( &bs );
249  }
250  else
251  {
252  QPainter p;
253  if ( borderRadius() <= 0.0 )
254  {
255  QwtPainter::fillPixmap( this, bs );
256  p.begin( &bs );
257  drawCanvas( &p );
258  }
259  else
260  {
261  p.begin( &bs );
262  drawUnstyled( &p );
263  }
264 
265  if ( frameWidth() > 0 )
266  drawBorder( &p );
267  }
268  }
269 
270  painter.drawPixmap( 0, 0, *d_data->backingStore );
271  }
272  else
273  {
274 #ifndef QWT_NO_OPENGL
276  {
277  painter.drawImage( 0, 0, toImageFBO( size() ) );
278  }
279  else
280 #endif
281  if ( testAttribute(Qt::WA_StyledBackground ) )
282  {
283  if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
284  {
286  }
287  else
288  {
289  drawCanvas( &painter );
290  }
291  }
292  else
293  {
294  if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
295  {
296  if ( autoFillBackground() )
297  {
298  fillBackground( &painter );
299  drawBackground( &painter );
300  }
301  }
302  else
303  {
304  if ( borderRadius() > 0.0 )
305  {
306  QPainterPath clipPath;
307  clipPath.addRect( rect() );
308  clipPath = clipPath.subtracted( borderPath( rect() ) );
309 
310  painter.save();
311 
312  painter.setClipPath( clipPath, Qt::IntersectClip );
313  fillBackground( &painter );
314  drawBackground( &painter );
315 
316  painter.restore();
317  }
318  }
319 
320  drawCanvas( &painter );
321 
322  if ( frameWidth() > 0 )
323  drawBorder( &painter );
324  }
325  }
326 
327  if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
328  drawFocusIndicator( &painter );
329 }
330 
337 void QwtPlotCanvas::drawBorder( QPainter *painter )
338 {
339 #if QT_VERSION >= 0x040500
340  if ( borderRadius() <= 0 )
341  {
342  drawFrame( painter );
343  return;
344  }
345 #endif
346 
348 }
349 
354 void QwtPlotCanvas::resizeEvent( QResizeEvent *event )
355 {
356  QFrame::resizeEvent( event );
358 }
359 
365 {
367 
369  repaint( contentsRect() );
370  else
371  update( contentsRect() );
372 }
373 
383 QPainterPath QwtPlotCanvas::borderPath( const QRect &rect ) const
384 {
385  return borderPath2( rect );
386 }
387 
388 #ifndef QWT_NO_OPENGL
389 
390 #define FIX_GL_TRANSLATION 0
391 
392 QImage QwtPlotCanvas::toImageFBO( const QSize &size )
393 {
394  const int numSamples = 4;
395 
396 #if FBO_OPENGL
397 
398  if ( d_data->surfaceGL == NULL )
399  {
401  d_data->surfaceGL->create();
402  }
403 
404  QOpenGLContext context;
405  context.create();
406 
407  context.makeCurrent( d_data->surfaceGL );
408 
409 
410  QOpenGLFramebufferObjectFormat fboFormat;
411  fboFormat.setSamples(numSamples);
412  QOpenGLFramebufferObject fbo( size, fboFormat );
413 
414  QOpenGLPaintDevice pd( size );
415 
416 #else
417 
418  if ( d_data->surfaceGL == NULL )
419  {
420  QGLFormat format = QGLFormat::defaultFormat();
421  format.setSampleBuffers( true );
422  format.setSamples( numSamples );
423 
424  d_data->surfaceGL = new QwtPlotCanvasSurfaceGL( format );
425  }
426 
427  d_data->surfaceGL->makeCurrent();
428 
429 #if QT_VERSION >= 0x040600
430  QGLFramebufferObjectFormat fboFormat;
431  fboFormat.setSamples(numSamples);
432 
433  QGLFramebufferObject fbo( size, fboFormat );
434 #else
435  QGLFramebufferObject fbo( size );
436 #endif
437  QGLFramebufferObject &pd = fbo;
438 
439 #endif
440 
441  QPainter painter( &pd );
442 
443  if ( testAttribute( Qt::WA_StyledBackground ) )
445  else
446  drawUnstyled( &painter );
447 
448  if ( frameWidth() > 0 )
449  drawBorder( &painter );
450 
451  painter.end();
452 
453  QImage image = fbo.toImage();
454 
455 #if QT_VERSION >= 0x050000
456  image.setDevicePixelRatio( QwtPainter::devicePixelRatio( this ) );
457 #endif
458  return image;
459 }
460 
461 #else
462 
463 QImage QwtPlotCanvas::toImageFBO( const QSize &)
464 {
465  // will never be called
466  return QImage();
467 }
468 
469 #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())
QFlags< PaintAttribute > PaintAttributes
Paint attributes.
QGLWidget QwtPlotCanvasSurfaceGL
virtual void paintEvent(QPaintEvent *)
QwtPlotCanvas(QwtPlot *=NULL)
Constructor.
virtual ~QwtPlotCanvas()
Destructor.
FocusIndicator focusIndicator() const
A 2-D plotting widget.
Definition: qwt_plot.h:74
bool testPaintAttribute(PaintAttribute) const
void drawStyled(QPainter *, bool)
Q_INVOKABLE QPainterPath borderPath(const QRect &) const
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
QwtPlotCanvas::PaintAttributes paintAttributes
const QPixmap * backingStore() const
Render the canvas via an OpenGL buffer.
static qreal devicePixelRatio(const QPaintDevice *)
QImage toImageFBO(const QSize &size)
std::string format(const std::string &, const time_point< seconds > &, const femtoseconds &, const time_zone &)
PaintAttribute
Paint attributes.
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
uintptr_t size
QPainterPath borderPath2(const QRect &rect) const
QwtPlotCanvasSurfaceGL * surfaceGL
virtual void drawBorder(QPainter *)
virtual void resizeEvent(QResizeEvent *)
virtual void drawBorder(QPainter *)
Q_INVOKABLE void invalidateBackingStore()
Invalidate the internal backing store.
virtual void drawFocusIndicator(QPainter *)
void updateStyleSheetInfo()
Update the cached information about the current style sheet.
virtual bool event(QEvent *)
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 Sat Jul 6 2019 03:44:17