Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
19namespace
20{
21 class QwtPlotGLCanvasFormat : public QGLFormat
22 {
23 public:
24 QwtPlotGLCanvasFormat()
25 : QGLFormat( QGLFormat::defaultFormat() )
26 {
27 setSampleBuffers( true );
28 }
29 };
30}
31
32class 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
57 : QGLWidget( QwtPlotGLCanvasFormat(), plot )
59{
60 init();
61}
69QwtPlotGLCanvas::QwtPlotGLCanvas( const QGLFormat& format, QwtPlot* plot )
70 : QGLWidget( format, plot )
72{
73 init();
74}
75
78{
79 delete m_data;
80}
81
82void 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
100void QwtPlotGLCanvas::paintEvent( QPaintEvent* event )
101{
102 QGLWidget::paintEvent( event );
103}
104
110bool 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
135
138{
139 m_data->fboDirty = true;
140}
141
142void QwtPlotGLCanvas::clearBackingStore()
143{
144 delete m_data->fbo;
145 m_data->fbo = NULL;
146}
147
157QPainterPath QwtPlotGLCanvas::borderPath( const QRect& rect ) const
158{
159 return canvasBorderPath( rect );
160}
161
166
169{
170 const bool hasFocusIndicator =
171 hasFocus() && focusIndicator() == CanvasFocusIndicator;
172
173 QPainter painter;
174
176 {
177 const qreal pixelRatio = QwtPainter::devicePixelRatio( this );
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
234{
235 // nothing to do
236}
237
238#include "moc_qwt_plot_glcanvas.cpp"
static qreal devicePixelRatio(const QPaintDevice *)
FocusIndicator focusIndicator() const
QPainterPath canvasBorderPath(const QRect &rect) const
virtual void drawFocusIndicator(QPainter *)
Base class of QwtPlotOpenGLCanvas and QwtPlotGLCanvas.
bool testPaintAttribute(PaintAttribute) const
@ BackingStore
Paint double buffered reusing the content of the pixmap buffer when possible.
void draw(QPainter *)
Helper function for the derived plot canvas.
virtual ~QwtPlotGLCanvas()
Destructor.
Q_INVOKABLE QPainterPath borderPath(const QRect &) const
virtual void initializeGL() override
No operation - reserved for some potential use in the future.
virtual Q_INVOKABLE void invalidateBackingStore() override
Invalidate the internal backing store.
virtual bool event(QEvent *) override
virtual void paintGL() override
Paint the plot.
virtual void paintEvent(QPaintEvent *) override
virtual void resizeGL(int width, int height) override
No operation - reserved for some potential use in the future.
QwtPlotGLCanvas(QwtPlot *=NULL)
Constructor.
A 2-D plotting widget.
Definition qwt_plot.h:79