Loading...
Searching...
No Matches
qwt_plot_canvas.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_canvas.h"
#include "qwt_painter.h"
#include "qwt_plot.h"
#include <qpainter.h>
#include <qpainterpath.h>
#include <qevent.h>
class QwtPlotCanvas::PrivateData
{
public:
PrivateData()
: backingStore( NULL )
{
}
~PrivateData()
{
delete backingStore;
}
QwtPlotCanvas::PaintAttributes paintAttributes;
QPixmap* backingStore;
};
QwtPlotCanvas::QwtPlotCanvas( QwtPlot* plot )
: QFrame( plot )
, QwtPlotAbstractCanvas( this )
{
m_data = new PrivateData;
setPaintAttribute( QwtPlotCanvas::BackingStore, true );
setPaintAttribute( QwtPlotCanvas::Opaque, true );
setPaintAttribute( QwtPlotCanvas::HackStyledBackground, true );
setLineWidth( 2 );
setFrameShadow( QFrame::Sunken );
setFrameShape( QFrame::Panel );
}
QwtPlotCanvas::~QwtPlotCanvas()
{
delete m_data;
}
void QwtPlotCanvas::setPaintAttribute( PaintAttribute attribute, bool on )
{
if ( bool( m_data->paintAttributes & attribute ) == on )
return;
if ( on )
m_data->paintAttributes |= attribute;
else
m_data->paintAttributes &= ~attribute;
switch ( attribute )
{
case BackingStore:
{
if ( on )
{
if ( m_data->backingStore == NULL )
m_data->backingStore = new QPixmap();
if ( isVisible() )
{
#if QT_VERSION >= 0x050000
*m_data->backingStore = grab( rect() );
#else
*m_data->backingStore =
QPixmap::grabWidget( this, rect() );
#endif
}
}
else
{
delete m_data->backingStore;
m_data->backingStore = NULL;
}
break;
}
case Opaque:
{
if ( on )
setAttribute( Qt::WA_OpaquePaintEvent, true );
break;
}
default:
{
break;
}
}
}
bool QwtPlotCanvas::testPaintAttribute( PaintAttribute attribute ) const
{
return m_data->paintAttributes & attribute;
}
const QPixmap* QwtPlotCanvas::backingStore() const
{
return m_data->backingStore;
}
void QwtPlotCanvas::invalidateBackingStore()
{
if ( m_data->backingStore )
*m_data->backingStore = QPixmap();
}
bool QwtPlotCanvas::event( QEvent* event )
{
if ( event->type() == QEvent::PolishRequest )
{
if ( testPaintAttribute( QwtPlotCanvas::Opaque ) )
{
// Setting a style sheet changes the
// Qt::WA_OpaquePaintEvent attribute, but we insist
// on painting the background.
setAttribute( Qt::WA_OpaquePaintEvent, true );
}
}
if ( event->type() == QEvent::PolishRequest ||
event->type() == QEvent::StyleChange )
{
updateStyleSheetInfo();
}
return QFrame::event( event );
}
void QwtPlotCanvas::paintEvent( QPaintEvent* event )
{
QPainter painter( this );
painter.setClipRegion( event->region() );
if ( testPaintAttribute( QwtPlotCanvas::BackingStore ) &&
m_data->backingStore != NULL )
{
QPixmap& bs = *m_data->backingStore;
if ( bs.size() != size() * QwtPainter::devicePixelRatio( &bs ) )
{
bs = QwtPainter::backingStore( this, size() );
if ( testAttribute(Qt::WA_StyledBackground) )
{
QPainter p( &bs );
drawStyled( &p, testPaintAttribute( HackStyledBackground ) );
}
else
{
QPainter p;
if ( borderRadius() <= 0.0 )
{
QwtPainter::fillPixmap( this, bs );
p.begin( &bs );
drawCanvas( &p );
}
else
{
p.begin( &bs );
drawUnstyled( &p );
}
if ( frameWidth() > 0 )
drawBorder( &p );
}
}
painter.drawPixmap( 0, 0, *m_data->backingStore );
}
else
{
if ( testAttribute(Qt::WA_StyledBackground ) )
{
if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
{
drawStyled( &painter, testPaintAttribute( HackStyledBackground ) );
}
else
{
drawCanvas( &painter );
}
}
else
{
if ( testAttribute( Qt::WA_OpaquePaintEvent ) )
{
if ( autoFillBackground() )
{
fillBackground( &painter );
drawBackground( &painter );
}
}
else
{
if ( borderRadius() > 0.0 )
{
QPainterPath clipPath;
clipPath.addRect( rect() );
clipPath = clipPath.subtracted( borderPath( rect() ) );
painter.save();
painter.setClipPath( clipPath, Qt::IntersectClip );
fillBackground( &painter );
drawBackground( &painter );
painter.restore();
}
}
drawCanvas( &painter );
if ( frameWidth() > 0 )
drawBorder( &painter );
}
}
if ( hasFocus() && focusIndicator() == CanvasFocusIndicator )
drawFocusIndicator( &painter );
}
void QwtPlotCanvas::drawBorder( QPainter* painter )
{
if ( borderRadius() <= 0 )
{
drawFrame( painter );
return;
}
QwtPlotAbstractCanvas::drawBorder( painter );
}
void QwtPlotCanvas::resizeEvent( QResizeEvent* event )
{
QFrame::resizeEvent( event );
updateStyleSheetInfo();
}
void QwtPlotCanvas::replot()
{
invalidateBackingStore();
if ( testPaintAttribute( QwtPlotCanvas::ImmediatePaint ) )
repaint( contentsRect() );
else
update( contentsRect() );
}
QPainterPath QwtPlotCanvas::borderPath( const QRect& rect ) const
{
return canvasBorderPath( rect );
}
#include "moc_qwt_plot_canvas.cpp"
QwtPainter::fillPixmap
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
Definition qwt_painter.cpp:1301
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition qwt_painter.cpp:1481
QwtPainter::backingStore
static QPixmap backingStore(QWidget *, const QSize &)
Definition qwt_painter.cpp:1515
QwtPlotAbstractCanvas
Base class for all type of plot canvases.
Definition qwt_plot_abstract_canvas.h:22
QwtPlotAbstractCanvas::fillBackground
void fillBackground(QPainter *)
Helper function for the derived plot canvas.
Definition qwt_plot_abstract_canvas.cpp:696
QwtPlotAbstractCanvas::drawBackground
virtual void drawBackground(QPainter *)
Helper function for the derived plot canvas.
Definition qwt_plot_abstract_canvas.cpp:690
QwtPlotAbstractCanvas::drawUnstyled
void drawUnstyled(QPainter *)
Helper function for the derived plot canvas.
Definition qwt_plot_abstract_canvas.cpp:702
QwtPlotAbstractCanvas::focusIndicator
FocusIndicator focusIndicator() const
Definition qwt_plot_abstract_canvas.cpp:578
QwtPlotAbstractCanvas::updateStyleSheetInfo
void updateStyleSheetInfo()
Update the cached information about the current style sheet.
Definition qwt_plot_abstract_canvas.cpp:835
QwtPlotAbstractCanvas::borderRadius
double borderRadius() const
Definition qwt_plot_abstract_canvas.cpp:613
QwtPlotAbstractCanvas::canvasBorderPath
QPainterPath canvasBorderPath(const QRect &rect) const
Definition qwt_plot_abstract_canvas.cpp:619
QwtPlotAbstractCanvas::drawBorder
virtual void drawBorder(QPainter *)
Definition qwt_plot_abstract_canvas.cpp:628
QwtPlotAbstractCanvas::drawCanvas
void drawCanvas(QPainter *)
Draw the plot to the canvas.
Definition qwt_plot_abstract_canvas.cpp:803
QwtPlotAbstractCanvas::drawStyled
void drawStyled(QPainter *, bool)
Helper function for the derived plot canvas.
Definition qwt_plot_abstract_canvas.cpp:744
QwtPlotAbstractCanvas::drawFocusIndicator
virtual void drawFocusIndicator(QPainter *)
Definition qwt_plot_abstract_canvas.cpp:587
QwtPlotAbstractCanvas::CanvasFocusIndicator
@ CanvasFocusIndicator
Definition qwt_plot_abstract_canvas.h:39
QwtPlotCanvas::PaintAttributes
QFlags< PaintAttribute > PaintAttributes
Definition qwt_plot_canvas.h:102
QwtPlotCanvas::paintEvent
virtual void paintEvent(QPaintEvent *) override
Definition qwt_plot_canvas.cpp:178
QwtPlotCanvas::replot
void replot()
Definition qwt_plot_canvas.cpp:301
QwtPlotCanvas::~QwtPlotCanvas
virtual ~QwtPlotCanvas()
Destructor.
Definition qwt_plot_canvas.cpp:57
QwtPlotCanvas::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition qwt_plot_canvas.cpp:127
QwtPlotCanvas::PaintAttribute
PaintAttribute
Paint attributes.
Definition qwt_plot_canvas.h:45
QwtPlotCanvas::Opaque
@ Opaque
Try to fill the complete contents rectangle of the plot canvas.
Definition qwt_plot_canvas.h:74
QwtPlotCanvas::HackStyledBackground
@ HackStyledBackground
Try to improve painting of styled backgrounds.
Definition qwt_plot_canvas.h:91
QwtPlotCanvas::BackingStore
@ BackingStore
Paint double buffered reusing the content of the pixmap buffer when possible.
Definition qwt_plot_canvas.h:57
QwtPlotCanvas::ImmediatePaint
@ ImmediatePaint
Definition qwt_plot_canvas.h:99
QwtPlotCanvas::setPaintAttribute
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
Definition qwt_plot_canvas.cpp:70
QwtPlotCanvas::resizeEvent
virtual void resizeEvent(QResizeEvent *) override
Definition qwt_plot_canvas.cpp:291
QwtPlotCanvas::QwtPlotCanvas
QwtPlotCanvas(QwtPlot *=NULL)
Constructor.
Definition qwt_plot_canvas.cpp:41
QwtPlotCanvas::event
virtual bool event(QEvent *) override
Definition qwt_plot_canvas.cpp:151
QwtPlotCanvas::backingStore
const QPixmap * backingStore() const
Definition qwt_plot_canvas.cpp:133
QwtPlotCanvas::invalidateBackingStore
Q_INVOKABLE void invalidateBackingStore()
Invalidate the internal backing store.
Definition qwt_plot_canvas.cpp:139
QwtPlotCanvas::drawBorder
virtual void drawBorder(QPainter *) override
Definition qwt_plot_canvas.cpp:276
QwtPlotCanvas::borderPath
Q_INVOKABLE QPainterPath borderPath(const QRect &) const
Definition qwt_plot_canvas.cpp:320
QwtPlot
A 2-D plotting widget.
Definition qwt_plot.h:79