| 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 |
|
|---|
| 50 | /*!
|
|---|
| 51 | \brief Constructor
|
|---|
| 52 |
|
|---|
| 53 | \param plot Parent plot widget
|
|---|
| 54 | \sa QwtPlot::setCanvas()
|
|---|
| 55 | */
|
|---|
| 56 | QwtPlotGLCanvas::QwtPlotGLCanvas( QwtPlot* plot )
|
|---|
| 57 | : QGLWidget( QwtPlotGLCanvasFormat(), plot )
|
|---|
| 58 | , QwtPlotAbstractGLCanvas( this )
|
|---|
| 59 | {
|
|---|
| 60 | init();
|
|---|
| 61 | }
|
|---|
| 62 | /*!
|
|---|
| 63 | \brief Constructor
|
|---|
| 64 |
|
|---|
| 65 | \param format OpenGL rendering options
|
|---|
| 66 | \param plot Parent plot widget
|
|---|
| 67 | \sa QwtPlot::setCanvas()
|
|---|
| 68 | */
|
|---|
| 69 | QwtPlotGLCanvas::QwtPlotGLCanvas( const QGLFormat& format, QwtPlot* plot )
|
|---|
| 70 | : QGLWidget( format, plot )
|
|---|
| 71 | , QwtPlotAbstractGLCanvas( this )
|
|---|
| 72 | {
|
|---|
| 73 | init();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | //! Destructor
|
|---|
| 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 |
|
|---|
| 94 | /*!
|
|---|
| 95 | Paint event
|
|---|
| 96 |
|
|---|
| 97 | \param event Paint event
|
|---|
| 98 | \sa QwtPlot::drawCanvas()
|
|---|
| 99 | */
|
|---|
| 100 | void QwtPlotGLCanvas::paintEvent( QPaintEvent* event )
|
|---|
| 101 | {
|
|---|
| 102 | QGLWidget::paintEvent( event );
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /*!
|
|---|
| 106 | Qt event handler for QEvent::PolishRequest and QEvent::StyleChange
|
|---|
| 107 | \param event Qt Event
|
|---|
| 108 | \return See QGLWidget::event()
|
|---|
| 109 | */
|
|---|
| 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 |
|
|---|
| 127 | /*!
|
|---|
| 128 | Invalidate the paint cache and repaint the canvas
|
|---|
| 129 | \sa invalidatePaintCache()
|
|---|
| 130 | */
|
|---|
| 131 | void QwtPlotGLCanvas::replot()
|
|---|
| 132 | {
|
|---|
| 133 | QwtPlotAbstractGLCanvas::replot();
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | //! Invalidate the internal backing store
|
|---|
| 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 |
|
|---|
| 148 | /*!
|
|---|
| 149 | Calculate the painter path for a styled or rounded border
|
|---|
| 150 |
|
|---|
| 151 | When the canvas has no styled background or rounded borders
|
|---|
| 152 | the painter path is empty.
|
|---|
| 153 |
|
|---|
| 154 | \param rect Bounding rectangle of the canvas
|
|---|
| 155 | \return Painter path, that can be used for clipping
|
|---|
| 156 | */
|
|---|
| 157 | QPainterPath QwtPlotGLCanvas::borderPath( const QRect& rect ) const
|
|---|
| 158 | {
|
|---|
| 159 | return canvasBorderPath( rect );
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | //! No operation - reserved for some potential use in the future
|
|---|
| 163 | void QwtPlotGLCanvas::initializeGL()
|
|---|
| 164 | {
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | //! Paint the plot
|
|---|
| 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( 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 |
|
|---|
| 232 | //! No operation - reserved for some potential use in the future
|
|---|
| 233 | void QwtPlotGLCanvas::resizeGL( int, int )
|
|---|
| 234 | {
|
|---|
| 235 | // nothing to do
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | #include "moc_qwt_plot_glcanvas.cpp"
|
|---|