Loading...
Searching...
No Matches
qwt_plot_glcanvas.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot_glcanvas.h"
#include "qwt_plot.h"
#include "qwt_painter.h"
#include <qcoreevent.h>
#include <qpainter.h>
#include <qpainterpath.h>
#include <qglframebufferobject.h>
namespace
{
class QwtPlotGLCanvasFormat : public QGLFormat
{
public:
QwtPlotGLCanvasFormat()
: QGLFormat( QGLFormat::defaultFormat() )
{
setSampleBuffers( true );
}
};
}
class QwtPlotGLCanvas::PrivateData
{
public:
PrivateData()
: fboDirty( true )
, fbo( NULL )
{
}
~PrivateData()
{
delete fbo;
}
bool fboDirty;
QGLFramebufferObject* fbo;
};
QwtPlotGLCanvas::QwtPlotGLCanvas( QwtPlot* plot )
: QGLWidget( QwtPlotGLCanvasFormat(), plot )
, QwtPlotAbstractGLCanvas( this )
{
init();
}
QwtPlotGLCanvas::QwtPlotGLCanvas( const QGLFormat& format, QwtPlot* plot )
: QGLWidget( format, plot )
, QwtPlotAbstractGLCanvas( this )
{
init();
}
QwtPlotGLCanvas::~QwtPlotGLCanvas()
{
delete m_data;
}
void QwtPlotGLCanvas::init()
{
m_data = new PrivateData;
#if 1
setAttribute( Qt::WA_OpaquePaintEvent, true );
#endif
setLineWidth( 2 );
setFrameShadow( QFrame::Sunken );
setFrameShape( QFrame::Panel );
}
void QwtPlotGLCanvas::paintEvent( QPaintEvent* event )
{
QGLWidget::paintEvent( event );
}
bool QwtPlotGLCanvas::event( QEvent* event )
{
const bool ok = QGLWidget::event( event );
if ( event->type() == QEvent::PolishRequest ||
event->type() == QEvent::StyleChange )
{
// assuming, that we always have a styled background
// when we have a style sheet
setAttribute( Qt::WA_StyledBackground,
testAttribute( Qt::WA_StyleSheet ) );
}
return ok;
}
void QwtPlotGLCanvas::replot()
{
QwtPlotAbstractGLCanvas::replot();
}
void QwtPlotGLCanvas::invalidateBackingStore()
{
m_data->fboDirty = true;
}
void QwtPlotGLCanvas::clearBackingStore()
{
delete m_data->fbo;
m_data->fbo = NULL;
}
QPainterPath QwtPlotGLCanvas::borderPath( const QRect& rect ) const
{
return canvasBorderPath( rect );
}
void QwtPlotGLCanvas::initializeGL()
{
}
void QwtPlotGLCanvas::paintGL()
{
const bool hasFocusIndicator =
hasFocus() && focusIndicator() == CanvasFocusIndicator;
QPainter painter;
if ( testPaintAttribute( QwtPlotGLCanvas::BackingStore ) )
{
const qreal pixelRatio = QwtPainter::devicePixelRatio( this );
const QRect rect( 0, 0, width() * pixelRatio, height() * pixelRatio );
if ( hasFocusIndicator )
painter.begin( this );
if ( m_data->fbo )
{
if ( m_data->fbo->size() != rect.size() )
{
delete m_data->fbo;
m_data->fbo = NULL;
}
}
if ( m_data->fbo == NULL )
{
QGLFramebufferObjectFormat format;
format.setSamples( 4 );
format.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
m_data->fbo = new QGLFramebufferObject( rect.size(), format );
m_data->fboDirty = true;
}
if ( m_data->fboDirty )
{
QPainter fboPainter( m_data->fbo );
fboPainter.scale( pixelRatio, pixelRatio );
draw( &fboPainter );
fboPainter.end();
m_data->fboDirty = false;
}
/*
Why do we have this strange translation - but, anyway
QwtPlotGLCanvas in combination with scaling factor
is not very likely to happen as using QwtPlotOpenGLCanvas
usually makes more sense then.
*/
QGLFramebufferObject::blitFramebuffer( NULL,
rect.translated( 0, height() - rect.height() ), m_data->fbo, rect );
}
else
{
painter.begin( this );
draw( &painter );
}
if ( hasFocusIndicator )
drawFocusIndicator( &painter );
}
void QwtPlotGLCanvas::resizeGL( int, int )
{
// nothing to do
}
#include "moc_qwt_plot_glcanvas.cpp"
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition qwt_painter.cpp:1481
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