Loading...
Searching...
No Matches
qwt_polar_canvas.cpp
/******************************************************************************
* QwtPolar Widget Library
* Copyright (C) 2008 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_polar_canvas.h"
#include "qwt_polar_plot.h"
#include "qwt_painter.h"
#include <qpainter.h>
#include <qevent.h>
#include <qpixmap.h>
#include <qstyle.h>
#include <qstyleoption.h>
#ifdef Q_WS_X11
#include <qx11info_x11.h>
#endif
static inline void qwtDrawStyledBackground(
QWidget* widget, QPainter* painter )
{
QStyleOption opt;
opt.initFrom( widget );
widget->style()->drawPrimitive( QStyle::PE_Widget, &opt, painter, widget );
}
static QWidget* qwtBackgroundWidget( QWidget* w )
{
if ( w->parentWidget() == NULL )
return w;
if ( w->autoFillBackground() )
{
const QBrush brush = w->palette().brush( w->backgroundRole() );
if ( brush.color().alpha() > 0 )
return w;
}
if ( w->testAttribute( Qt::WA_StyledBackground ) )
{
QImage image( 1, 1, QImage::Format_ARGB32 );
image.fill( Qt::transparent );
QPainter painter( &image );
painter.translate( -w->rect().center() );
qwtDrawStyledBackground( w, &painter );
painter.end();
if ( qAlpha( image.pixel( 0, 0 ) ) != 0 )
return w;
}
return qwtBackgroundWidget( w->parentWidget() );
}
class QwtPolarCanvas::PrivateData
{
public:
PrivateData()
: backingStore( NULL )
{
}
~PrivateData()
{
delete backingStore;
}
QwtPolarCanvas::PaintAttributes paintAttributes;
QPixmap* backingStore;
};
QwtPolarCanvas::QwtPolarCanvas( QwtPolarPlot* plot )
: QFrame( plot )
{
m_data = new PrivateData;
#ifndef QT_NO_CURSOR
setCursor( Qt::CrossCursor );
#endif
setFocusPolicy( Qt::WheelFocus );
setPaintAttribute( BackingStore, true );
}
QwtPolarCanvas::~QwtPolarCanvas()
{
delete m_data;
}
QwtPolarPlot* QwtPolarCanvas::plot()
{
return qobject_cast< QwtPolarPlot* >( parent() );
}
const QwtPolarPlot* QwtPolarCanvas::plot() const
{
return qobject_cast< QwtPolarPlot* >( parent() );
}
void QwtPolarCanvas::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() )
{
const QRect cr = contentsRect();
#if QT_VERSION >= 0x050000
*m_data->backingStore = grab( cr );
#else
*m_data->backingStore = QPixmap::grabWidget( this, cr );
#endif
}
}
else
{
delete m_data->backingStore;
m_data->backingStore = NULL;
}
break;
}
}
}
bool QwtPolarCanvas::testPaintAttribute( PaintAttribute attribute ) const
{
return ( m_data->paintAttributes & attribute ) != 0;
}
const QPixmap* QwtPolarCanvas::backingStore() const
{
return m_data->backingStore;
}
void QwtPolarCanvas::invalidateBackingStore()
{
if ( m_data->backingStore )
*m_data->backingStore = QPixmap();
}
void QwtPolarCanvas::paintEvent( QPaintEvent* event )
{
QPainter painter( this );
painter.setClipRegion( event->region() );
if ( ( m_data->paintAttributes & BackingStore )
&& m_data->backingStore != NULL )
{
QPixmap& bs = *m_data->backingStore;
if ( bs.size() != size() )
{
bs = QPixmap( size() );
#ifdef Q_WS_X11
if ( bs.x11Info().screen() != x11Info().screen() )
bs.x11SetScreen( x11Info().screen() );
#endif
QPainter p;
if ( testAttribute( Qt::WA_StyledBackground ) )
{
p.begin( &bs );
qwtDrawStyledBackground( this, &p );
}
else
{
if ( autoFillBackground() )
{
p.begin( &bs );
p.fillRect( rect(), palette().brush( backgroundRole() ) );
}
else
{
QWidget* bgWidget = qwtBackgroundWidget( plot() );
QwtPainter::fillPixmap( bgWidget, bs,
mapTo( bgWidget, rect().topLeft() ) );
p.begin( &bs );
}
}
plot()->drawCanvas( &p, contentsRect() );
if ( frameWidth() > 0 )
drawFrame( &p );
}
painter.drawPixmap( 0, 0, *m_data->backingStore );
}
else
{
qwtDrawStyledBackground( this, &painter );
plot()->drawCanvas( &painter, contentsRect() );
if ( frameWidth() > 0 )
drawFrame( &painter );
}
}
void QwtPolarCanvas::resizeEvent( QResizeEvent* event )
{
QFrame::resizeEvent( event );
for ( int scaleId = 0; scaleId < QwtPolar::ScaleCount; scaleId++ )
plot()->updateScale( scaleId );
}
QwtPointPolar QwtPolarCanvas::invTransform( const QPoint& pos ) const
{
const QwtPolarPlot* pl = plot();
const QwtScaleMap azimuthMap = pl->scaleMap( QwtPolar::Azimuth );
const QwtScaleMap radialMap = pl->scaleMap( QwtPolar::Radius );
const QPointF center = pl->plotRect().center();
double dx = pos.x() - center.x();
double dy = -( pos.y() - center.y() );
const QwtPointPolar polarPos = QwtPointPolar( QPoint( dx, dy ) ).normalized();
double azimuth = azimuthMap.invTransform( polarPos.azimuth() );
// normalize the azimuth
double min = azimuthMap.s1();
double max = azimuthMap.s2();
if ( max < min )
qSwap( min, max );
if ( azimuth < min )
{
azimuth += max - min;
}
else if ( azimuth > max )
{
azimuth -= max - min;
}
const double radius = radialMap.invTransform( polarPos.radius() );
return QwtPointPolar( azimuth, radius );
}
QPoint QwtPolarCanvas::transform( const QwtPointPolar& polarPos ) const
{
const QwtPolarPlot* pl = plot();
const QwtScaleMap azimuthMap = pl->scaleMap( QwtPolar::Azimuth );
const QwtScaleMap radialMap = pl->scaleMap( QwtPolar::Radius );
const double radius = radialMap.transform( polarPos.radius() );
const double azimuth = azimuthMap.transform( polarPos.azimuth() );
const QPointF pos = qwtPolar2Pos(
pl->plotRect().center(), radius, azimuth );
return pos.toPoint();
}
#include "moc_qwt_polar_canvas.cpp"
QwtPainter::fillPixmap
static void fillPixmap(const QWidget *, QPixmap &, const QPoint &offset=QPoint())
Definition qwt_painter.cpp:1301
QwtPointPolar
A point in polar coordinates.
Definition qwt_point_polar.h:29
QwtPointPolar::radius
double radius() const
Returns the radius.
Definition qwt_point_polar.h:102
QwtPointPolar::azimuth
double azimuth() const
Returns the azimuth.
Definition qwt_point_polar.h:108
QwtPointPolar::normalized
QwtPointPolar normalized() const
Definition qwt_point_polar.cpp:118
QwtPolarCanvas::PaintAttributes
QFlags< PaintAttribute > PaintAttributes
Definition qwt_polar_canvas.h:50
QwtPolarCanvas::plot
QwtPolarPlot * plot()
Definition qwt_polar_canvas.cpp:97
QwtPolarCanvas::PaintAttribute
PaintAttribute
Paint attributes.
Definition qwt_polar_canvas.h:41
QwtPolarCanvas::BackingStore
@ BackingStore
Definition qwt_polar_canvas.h:47
QwtPolarCanvas::invalidateBackingStore
void invalidateBackingStore()
Invalidate the internal backing store.
Definition qwt_polar_canvas.cpp:176
QwtPolarCanvas::invTransform
QwtPointPolar invTransform(const QPoint &) const
Definition qwt_polar_canvas.cpp:267
QwtPolarCanvas::transform
QPoint transform(const QwtPointPolar &) const
Definition qwt_polar_canvas.cpp:310
QwtPolarCanvas::paintEvent
virtual void paintEvent(QPaintEvent *) override
Definition qwt_polar_canvas.cpp:186
QwtPolarCanvas::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition qwt_polar_canvas.cpp:164
QwtPolarCanvas::QwtPolarCanvas
QwtPolarCanvas(QwtPolarPlot *)
Constructor.
Definition qwt_polar_canvas.cpp:77
QwtPolarCanvas::~QwtPolarCanvas
virtual ~QwtPolarCanvas()
Destructor.
Definition qwt_polar_canvas.cpp:91
QwtPolarCanvas::resizeEvent
virtual void resizeEvent(QResizeEvent *) override
Definition qwt_polar_canvas.cpp:251
QwtPolarCanvas::backingStore
const QPixmap * backingStore() const
Definition qwt_polar_canvas.cpp:170
QwtPolarCanvas::setPaintAttribute
void setPaintAttribute(PaintAttribute, bool on=true)
Changing the paint attributes.
Definition qwt_polar_canvas.cpp:118
QwtPolarPlot
A plotting widget, displaying a polar coordinate system.
Definition qwt_polar_plot.h:47
QwtPolarPlot::scaleMap
QwtScaleMap scaleMap(int scaleId, double radius) const
Definition qwt_polar_plot.cpp:730
QwtPolarPlot::drawCanvas
virtual void drawCanvas(QPainter *, const QRectF &) const
Definition qwt_polar_plot.cpp:930
QwtPolarPlot::updateScale
void updateScale(int scaleId)
Definition qwt_polar_plot.cpp:1040
QwtPolarPlot::plotRect
QRectF plotRect() const
Definition qwt_polar_plot.cpp:1120
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtScaleMap::s1
double s1() const
Definition qwt_scale_map.h:83
QwtScaleMap::s2
double s2() const
Definition qwt_scale_map.h:91
QwtScaleMap::invTransform
double invTransform(double p) const
Definition qwt_scale_map.h:154