Qwt User's Guide: qwt_plot_glcanvas.cpp Source File

Qwt User's Guide 6.2.0

qwt_plot_glcanvas.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_glcanvas.h"
11 #include "qwt_plot.h"
12 #include "qwt_painter.h"
13
14 #include <qcoreevent.h>
15 #include <qpainter.h>
16 #include <qpainterpath.h>
17 #include <qglframebufferobject.h>
18
19 namespace
20 {
21 class QwtPlotGLCanvasFormat : public QGLFormat
22 {
23 public:
24 QwtPlotGLCanvasFormat()
25 : QGLFormat( QGLFormat::defaultFormat() )
26 {
27 setSampleBuffers( true );
28 }
29 };
30 }
31
32 class QwtPlotGLCanvas::PrivateData
33 {
34 public:
35 PrivateData()
36 : fboDirty( true )
37 , fbo( NULL )
38 {
39 }
40
41 ~PrivateData()
42 {
43 delete fbo;
44 }
45
46 bool fboDirty;
47 QGLFramebufferObject* fbo;
48 };
49
56 QwtPlotGLCanvas::QwtPlotGLCanvas( QwtPlot* plot )
57 : QGLWidget( QwtPlotGLCanvasFormat(), plot )
58 , QwtPlotAbstractGLCanvas( this )
59 {
60 init();
61 }
69 QwtPlotGLCanvas::QwtPlotGLCanvas( const QGLFormat& format, QwtPlot* plot )
70 : QGLWidget( format, plot )
71 , QwtPlotAbstractGLCanvas( this )
72 {
73 init();
74 }
75
77 QwtPlotGLCanvas::~QwtPlotGLCanvas()
78 {
79 delete m_data;
80 }
81
82 void QwtPlotGLCanvas::init()
83 {
84 m_data = new PrivateData;
85
86 #if 1
87 setAttribute( Qt::WA_OpaquePaintEvent, true );
88 #endif
89 setLineWidth( 2 );
90 setFrameShadow( QFrame::Sunken );
91 setFrameShape( QFrame::Panel );
92 }
93
100 void QwtPlotGLCanvas::paintEvent( QPaintEvent* event )
101 {
102 QGLWidget::paintEvent( event );
103 }
104
110 bool QwtPlotGLCanvas::event( QEvent* event )
111 {
112 const bool ok = QGLWidget::event( event );
113
114 if ( event->type() == QEvent::PolishRequest ||
115 event->type() == QEvent::StyleChange )
116 {
117 // assuming, that we always have a styled background
118 // when we have a style sheet
119
120 setAttribute( Qt::WA_StyledBackground,
121 testAttribute( Qt::WA_StyleSheet ) );
122 }
123
124 return ok;
125 }
126
131 void QwtPlotGLCanvas::replot()
132 {
133 QwtPlotAbstractGLCanvas::replot();
134 }
135
137 void QwtPlotGLCanvas::invalidateBackingStore()
138 {
139 m_data->fboDirty = true;
140 }
141
142 void QwtPlotGLCanvas::clearBackingStore()
143 {
144 delete m_data->fbo;
145 m_data->fbo = NULL;
146 }
147
157 QPainterPath QwtPlotGLCanvas::borderPath( const QRect& rect ) const
158 {
159 return canvasBorderPath( rect );
160 }
161
163 void QwtPlotGLCanvas::initializeGL()
164 {
165 }
166
168 void QwtPlotGLCanvas::paintGL()
169 {
170 const bool hasFocusIndicator =
171 hasFocus() && focusIndicator() == CanvasFocusIndicator;
172
173 QPainter painter;
174
175 if ( testPaintAttribute( QwtPlotGLCanvas::BackingStore ) )
176 {
177 const qreal pixelRatio = QwtPainter::devicePixelRatio( NULL );
178 const QRect rect( 0, 0, width() * pixelRatio, height() * pixelRatio );
179
180 if ( hasFocusIndicator )
181 painter.begin( this );
182
183 if ( m_data->fbo )
184 {
185 if ( m_data->fbo->size() != rect.size() )
186 {
187 delete m_data->fbo;
188 m_data->fbo = NULL;
189 }
190 }
191
192 if ( m_data->fbo == NULL )
193 {
194 QGLFramebufferObjectFormat format;
195 format.setSamples( 4 );
196 format.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
197
198 m_data->fbo = new QGLFramebufferObject( rect.size(), format );
199 m_data->fboDirty = true;
200 }
201
202 if ( m_data->fboDirty )
203 {
204 QPainter fboPainter( m_data->fbo );
205 fboPainter.scale( pixelRatio, pixelRatio );
206 draw( &fboPainter );
207 fboPainter.end();
208
209 m_data->fboDirty = false;
210 }
211
212 /*
213 Why do we have this strange translation - but, anyway
214 QwtPlotGLCanvas in combination with scaling factor
215 is not very likely to happen as using QwtPlotOpenGLCanvas
216 usually makes more sense then.
217 */
218
219 QGLFramebufferObject::blitFramebuffer( NULL,
220 rect.translated( 0, height() - rect.height() ), m_data->fbo, rect );
221 }
222 else
223 {
224 painter.begin( this );
225 draw( &painter );
226 }
227
228 if ( hasFocusIndicator )
229 drawFocusIndicator( &painter );
230 }
231
233 void QwtPlotGLCanvas::resizeGL( int, int )
234 {
235 // nothing to do
236 }
237
238 #if QWT_MOC_INCLUDE
239 #include "moc_qwt_plot_glcanvas.cpp"
240 #endif
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition: qwt_painter.cpp:1491
QwtPlotAbstractCanvas::focusIndicator
FocusIndicator focusIndicator() const
Definition: qwt_plot_abstract_canvas.cpp:578
QwtPlotAbstractCanvas::canvasBorderPath
QPainterPath canvasBorderPath(const QRect &rect) const
Definition: qwt_plot_abstract_canvas.cpp:619
QwtPlotAbstractCanvas::drawFocusIndicator
virtual void drawFocusIndicator(QPainter *)
Definition: qwt_plot_abstract_canvas.cpp:587
QwtPlotAbstractCanvas::CanvasFocusIndicator
@ CanvasFocusIndicator
Definition: qwt_plot_abstract_canvas.h:39
QwtPlotAbstractGLCanvas
Base class of QwtPlotOpenGLCanvas and QwtPlotGLCanvas.
Definition: qwt_plot_abstract_canvas.h:88
QwtPlotAbstractGLCanvas::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition: qwt_plot_abstract_canvas.cpp:952
QwtPlotAbstractGLCanvas::setLineWidth
void setLineWidth(int)
Definition: qwt_plot_abstract_canvas.cpp:1033
QwtPlotAbstractGLCanvas::BackingStore
@ BackingStore
Paint double buffered reusing the content of the pixmap buffer when possible.
Definition: qwt_plot_abstract_canvas.h:110
QwtPlotAbstractGLCanvas::setFrameShape
void setFrameShape(QFrame::Shape)
Definition: qwt_plot_abstract_canvas.cpp:1011
QwtPlotAbstractGLCanvas::replot
void replot()
Definition: qwt_plot_abstract_canvas.cpp:1093
QwtPlotAbstractGLCanvas::setFrameShadow
void setFrameShadow(QFrame::Shadow)
Definition: qwt_plot_abstract_canvas.cpp:991
QwtPlotAbstractGLCanvas::draw
void draw(QPainter *)
Helper function for the derived plot canvas.
Definition: qwt_plot_abstract_canvas.cpp:1112
QwtPlotGLCanvas::~QwtPlotGLCanvas
virtual ~QwtPlotGLCanvas()
Destructor.
Definition: qwt_plot_glcanvas.cpp:77
QwtPlotGLCanvas::borderPath
Q_INVOKABLE QPainterPath borderPath(const QRect &) const
Definition: qwt_plot_glcanvas.cpp:157
QwtPlotGLCanvas::initializeGL
virtual void initializeGL() override
No operation - reserved for some potential use in the future.
Definition: qwt_plot_glcanvas.cpp:163
QwtPlotGLCanvas::invalidateBackingStore
virtual Q_INVOKABLE void invalidateBackingStore() override
Invalidate the internal backing store.
Definition: qwt_plot_glcanvas.cpp:137
QwtPlotGLCanvas::event
virtual bool event(QEvent *) override
Definition: qwt_plot_glcanvas.cpp:110
QwtPlotGLCanvas::paintGL
virtual void paintGL() override
Paint the plot.
Definition: qwt_plot_glcanvas.cpp:168
QwtPlotGLCanvas::paintEvent
virtual void paintEvent(QPaintEvent *) override
Definition: qwt_plot_glcanvas.cpp:100
QwtPlotGLCanvas::resizeGL
virtual void resizeGL(int width, int height) override
No operation - reserved for some potential use in the future.
Definition: qwt_plot_glcanvas.cpp:233
QwtPlotGLCanvas::QwtPlotGLCanvas
QwtPlotGLCanvas(QwtPlot *=NULL)
Constructor.
Definition: qwt_plot_glcanvas.cpp:56
QwtPlotGLCanvas::replot
void replot()
Definition: qwt_plot_glcanvas.cpp:131
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:79