Loading...
Searching...
No Matches
qwt_plot_item.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_item.h"
#include "qwt_text.h"
#include "qwt_plot.h"
#include "qwt_legend_data.h"
#include "qwt_scale_map.h"
#include "qwt_graphic.h"
#include <qpainter.h>
class QwtPlotItem::PrivateData
{
public:
PrivateData()
: plot( NULL )
, isVisible( true )
, renderThreadCount( 1 )
, z( 0.0 )
, xAxisId( QwtAxis::XBottom )
, yAxisId( QwtAxis::YLeft )
, legendIconSize( 8, 8 )
{
}
mutable QwtPlot* plot;
bool isVisible;
QwtPlotItem::ItemAttributes attributes;
QwtPlotItem::ItemInterests interests;
QwtPlotItem::RenderHints renderHints;
uint renderThreadCount;
double z;
QwtAxisId xAxisId;
QwtAxisId yAxisId;
QwtText title;
QSize legendIconSize;
};
QwtPlotItem::QwtPlotItem()
{
m_data = new PrivateData;
}
QwtPlotItem::QwtPlotItem( const QString& title )
{
m_data = new PrivateData;
m_data->title = title;
}
QwtPlotItem::QwtPlotItem( const QwtText& title )
{
m_data = new PrivateData;
m_data->title = title;
}
QwtPlotItem::~QwtPlotItem()
{
attach( NULL );
delete m_data;
}
void QwtPlotItem::attach( QwtPlot* plot )
{
if ( plot == m_data->plot )
return;
if ( m_data->plot )
m_data->plot->attachItem( this, false );
m_data->plot = plot;
if ( m_data->plot )
m_data->plot->attachItem( this, true );
}
void QwtPlotItem::detach()
{
attach( NULL );
}
int QwtPlotItem::rtti() const
{
return Rtti_PlotItem;
}
QwtPlot* QwtPlotItem::plot() const
{
return m_data->plot;
}
double QwtPlotItem::z() const
{
return m_data->z;
}
void QwtPlotItem::setZ( double z )
{
if ( m_data->z != z )
{
if ( m_data->plot ) // update the z order
m_data->plot->attachItem( this, false );
m_data->z = z;
if ( m_data->plot )
m_data->plot->attachItem( this, true );
itemChanged();
}
}
void QwtPlotItem::setTitle( const QString& title )
{
setTitle( QwtText( title ) );
}
void QwtPlotItem::setTitle( const QwtText& title )
{
if ( m_data->title != title )
{
m_data->title = title;
legendChanged();
#if 0
itemChanged();
#endif
}
}
const QwtText& QwtPlotItem::title() const
{
return m_data->title;
}
void QwtPlotItem::setItemAttribute( ItemAttribute attribute, bool on )
{
if ( m_data->attributes.testFlag( attribute ) != on )
{
if ( on )
m_data->attributes |= attribute;
else
m_data->attributes &= ~attribute;
if ( attribute == QwtPlotItem::Legend )
{
if ( on )
{
legendChanged();
}
else
{
/*
In the special case of taking an item from
the legend we can't use legendChanged() as
it depends on QwtPlotItem::Legend being enabled
*/
if ( m_data->plot )
m_data->plot->updateLegend( this );
}
}
itemChanged();
}
}
bool QwtPlotItem::testItemAttribute( ItemAttribute attribute ) const
{
return m_data->attributes.testFlag( attribute );
}
void QwtPlotItem::setItemInterest( ItemInterest interest, bool on )
{
if ( m_data->interests.testFlag( interest ) != on )
{
if ( on )
m_data->interests |= interest;
else
m_data->interests &= ~interest;
itemChanged();
}
}
bool QwtPlotItem::testItemInterest( ItemInterest interest ) const
{
return m_data->interests.testFlag( interest );
}
void QwtPlotItem::setRenderHint( RenderHint hint, bool on )
{
if ( m_data->renderHints.testFlag( hint ) != on )
{
if ( on )
m_data->renderHints |= hint;
else
m_data->renderHints &= ~hint;
itemChanged();
}
}
bool QwtPlotItem::testRenderHint( RenderHint hint ) const
{
return m_data->renderHints.testFlag( hint );
}
void QwtPlotItem::setRenderThreadCount( uint numThreads )
{
m_data->renderThreadCount = numThreads;
}
uint QwtPlotItem::renderThreadCount() const
{
return m_data->renderThreadCount;
}
void QwtPlotItem::setLegendIconSize( const QSize& size )
{
if ( m_data->legendIconSize != size )
{
m_data->legendIconSize = size;
legendChanged();
}
}
QSize QwtPlotItem::legendIconSize() const
{
return m_data->legendIconSize;
}
QwtGraphic QwtPlotItem::legendIcon(
int index, const QSizeF& size ) const
{
Q_UNUSED( index )
Q_UNUSED( size )
return QwtGraphic();
}
QwtGraphic QwtPlotItem::defaultIcon(
const QBrush& brush, const QSizeF& size ) const
{
QwtGraphic icon;
if ( !size.isEmpty() )
{
icon.setDefaultSize( size );
QRectF r( 0, 0, size.width(), size.height() );
QPainter painter( &icon );
painter.fillRect( r, brush );
}
return icon;
}
void QwtPlotItem::show()
{
setVisible( true );
}
void QwtPlotItem::hide()
{
setVisible( false );
}
void QwtPlotItem::setVisible( bool on )
{
if ( on != m_data->isVisible )
{
m_data->isVisible = on;
itemChanged();
}
}
bool QwtPlotItem::isVisible() const
{
return m_data->isVisible;
}
void QwtPlotItem::itemChanged()
{
if ( m_data->plot )
m_data->plot->autoRefresh();
}
void QwtPlotItem::legendChanged()
{
if ( testItemAttribute( QwtPlotItem::Legend ) && m_data->plot )
m_data->plot->updateLegend( this );
}
void QwtPlotItem::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
{
if ( QwtAxis::isXAxis( xAxisId ) )
m_data->xAxisId = xAxisId;
if ( QwtAxis::isYAxis( yAxisId ) )
m_data->yAxisId = yAxisId;
itemChanged();
}
void QwtPlotItem::setXAxis( QwtAxisId axisId )
{
if ( QwtAxis::isXAxis( axisId ) )
{
m_data->xAxisId = axisId;
itemChanged();
}
}
void QwtPlotItem::setYAxis( QwtAxisId axisId )
{
if ( QwtAxis::isYAxis( axisId ) )
{
m_data->yAxisId = axisId;
itemChanged();
}
}
QwtAxisId QwtPlotItem::xAxis() const
{
return m_data->xAxisId;
}
QwtAxisId QwtPlotItem::yAxis() const
{
return m_data->yAxisId;
}
QRectF QwtPlotItem::boundingRect() const
{
return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
}
void QwtPlotItem::getCanvasMarginHint( const QwtScaleMap& xMap,
const QwtScaleMap& yMap, const QRectF& canvasRect,
double& left, double& top, double& right, double& bottom ) const
{
Q_UNUSED( xMap );
Q_UNUSED( yMap );
Q_UNUSED( canvasRect );
// use QMargins, when we don't need to support Qt < 4.6 anymore
left = top = right = bottom = 0.0;
}
QList< QwtLegendData > QwtPlotItem::legendData() const
{
QwtLegendData data;
QwtText label = title();
label.setRenderFlags( label.renderFlags() & Qt::AlignLeft );
data.setValue( QwtLegendData::TitleRole,
QVariant::fromValue( label ) );
const QwtGraphic graphic = legendIcon( 0, legendIconSize() );
if ( !graphic.isNull() )
{
data.setValue( QwtLegendData::IconRole,
QVariant::fromValue( graphic ) );
}
QList< QwtLegendData > list;
list += data;
return list;
}
void QwtPlotItem::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
const QwtScaleDiv& yScaleDiv )
{
Q_UNUSED( xScaleDiv );
Q_UNUSED( yScaleDiv );
}
void QwtPlotItem::updateLegend( const QwtPlotItem* item,
const QList< QwtLegendData >& data )
{
Q_UNUSED( item );
Q_UNUSED( data );
}
QRectF QwtPlotItem::scaleRect( const QwtScaleMap& xMap,
const QwtScaleMap& yMap ) const
{
return QRectF( xMap.s1(), yMap.s1(),
xMap.sDist(), yMap.sDist() );
}
QRectF QwtPlotItem::paintRect( const QwtScaleMap& xMap,
const QwtScaleMap& yMap ) const
{
const QRectF rect( xMap.p1(), yMap.p1(),
xMap.pDist(), yMap.pDist() );
return rect;
}
QList
Definition qwt_abstract_legend.h:17
QwtGraphic
A paint device for scalable graphics.
Definition qwt_graphic.h:76
QwtGraphic::isNull
bool isNull() const
Definition qwt_graphic.cpp:413
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition qwt_graphic.cpp:553
QwtLegendData
Attributes of an entry on a legend.
Definition qwt_legend_data.h:37
QwtLegendData::setValue
void setValue(int role, const QVariant &)
Definition qwt_legend_data.cpp:64
QwtPlot
A 2-D plotting widget.
Definition qwt_plot.h:79
QwtPlot::updateLegend
void updateLegend()
Definition qwt_plot.cpp:1016
QwtPlot::autoRefresh
void autoRefresh()
Replots the plot if autoReplot() is true.
Definition qwt_plot.cpp:289
QwtPlotItem
Base class for items on the plot canvas.
Definition qwt_plot_item.h:67
QwtPlotItem::QwtPlotItem
QwtPlotItem()
Definition qwt_plot_item.cpp:55
QwtPlotItem::yAxis
QwtAxisId yAxis() const
Return yAxis.
Definition qwt_plot_item.cpp:559
QwtPlotItem::setLegendIconSize
void setLegendIconSize(const QSize &)
Definition qwt_plot_item.cpp:373
QwtPlotItem::paintRect
QRectF paintRect(const QwtScaleMap &, const QwtScaleMap &) const
Calculate the bounding paint rectangle of 2 maps.
Definition qwt_plot_item.cpp:720
QwtPlotItem::ItemInterests
QFlags< ItemInterest > ItemInterests
Definition qwt_plot_item.h:200
QwtPlotItem::setRenderThreadCount
void setRenderThreadCount(uint numThreads)
Definition qwt_plot_item.cpp:350
QwtPlotItem::legendIcon
virtual QwtGraphic legendIcon(int index, const QSizeF &) const
Definition qwt_plot_item.cpp:402
QwtPlotItem::setTitle
void setTitle(const QString &title)
Definition qwt_plot_item.cpp:187
QwtPlotItem::isVisible
bool isVisible() const
Definition qwt_plot_item.cpp:470
QwtPlotItem::hide
void hide()
Hide the item.
Definition qwt_plot_item.cpp:446
QwtPlotItem::setYAxis
void setYAxis(QwtAxisId)
Definition qwt_plot_item.cpp:543
QwtPlotItem::~QwtPlotItem
virtual ~QwtPlotItem()
Destroy the QwtPlotItem.
Definition qwt_plot_item.cpp:81
QwtPlotItem::legendData
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
Definition qwt_plot_item.cpp:626
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition qwt_plot_item.cpp:491
QwtPlotItem::title
const QwtText & title() const
Definition qwt_plot_item.cpp:215
QwtPlotItem::setXAxis
void setXAxis(QwtAxisId)
Definition qwt_plot_item.cpp:526
QwtPlotItem::scaleRect
QRectF scaleRect(const QwtScaleMap &, const QwtScaleMap &) const
Calculate the bounding scale rectangle of 2 maps.
Definition qwt_plot_item.cpp:705
QwtPlotItem::RenderHints
QFlags< RenderHint > RenderHints
Definition qwt_plot_item.h:209
QwtPlotItem::ItemAttributes
QFlags< ItemAttribute > ItemAttributes
Definition qwt_plot_item.h:167
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition qwt_plot_item.cpp:228
QwtPlotItem::setVisible
virtual void setVisible(bool)
Definition qwt_plot_item.cpp:457
QwtPlotItem::plot
QwtPlot * plot() const
Return attached plot.
Definition qwt_plot_item.cpp:142
QwtPlotItem::z
double z() const
Definition qwt_plot_item.cpp:152
QwtPlotItem::rtti
virtual int rtti() const
Definition qwt_plot_item.cpp:136
QwtPlotItem::xAxis
QwtAxisId xAxis() const
Return xAxis.
Definition qwt_plot_item.cpp:553
QwtPlotItem::testItemInterest
bool testItemInterest(ItemInterest) const
Definition qwt_plot_item.cpp:299
QwtPlotItem::show
void show()
Show the item.
Definition qwt_plot_item.cpp:440
QwtPlotItem::defaultIcon
QwtGraphic defaultIcon(const QBrush &, const QSizeF &) const
Return a default icon from a brush.
Definition qwt_plot_item.cpp:422
QwtPlotItem::Rtti_PlotItem
@ Rtti_PlotItem
Unspecific value, that can be used, when it doesn't matter.
Definition qwt_plot_item.h:78
QwtPlotItem::detach
void detach()
This method detaches a QwtPlotItem from any QwtPlot it has been associated with.
Definition qwt_plot_item.cpp:119
QwtPlotItem::setItemInterest
void setItemInterest(ItemInterest, bool on=true)
Definition qwt_plot_item.cpp:279
QwtPlotItem::RenderHint
RenderHint
Render hints.
Definition qwt_plot_item.h:204
QwtPlotItem::updateScaleDiv
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &)
Update the item to changes of the axes scale division.
Definition qwt_plot_item.cpp:665
QwtPlotItem::testItemAttribute
bool testItemAttribute(ItemAttribute) const
Definition qwt_plot_item.cpp:266
QwtPlotItem::setAxes
void setAxes(QwtAxisId xAxis, QwtAxisId yAxis)
Definition qwt_plot_item.cpp:507
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition qwt_plot_item.cpp:332
QwtPlotItem::setRenderHint
void setRenderHint(RenderHint, bool on=true)
Definition qwt_plot_item.cpp:312
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition qwt_plot_item.cpp:481
QwtPlotItem::ItemAttribute
ItemAttribute
Plot Item Attributes.
Definition qwt_plot_item.h:148
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition qwt_plot_item.h:150
QwtPlotItem::renderThreadCount
uint renderThreadCount() const
Definition qwt_plot_item.cpp:360
QwtPlotItem::getCanvasMarginHint
virtual void getCanvasMarginHint(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, double &left, double &top, double &right, double &bottom) const
Calculate a hint for the canvas margin.
Definition qwt_plot_item.cpp:595
QwtPlotItem::attach
void attach(QwtPlot *plot)
Attach the item to a plot.
Definition qwt_plot_item.cpp:98
QwtPlotItem::boundingRect
virtual QRectF boundingRect() const
Definition qwt_plot_item.cpp:568
QwtPlotItem::updateLegend
virtual void updateLegend(const QwtPlotItem *, const QList< QwtLegendData > &)
Update the item to changes of the legend info.
Definition qwt_plot_item.cpp:690
QwtPlotItem::legendIconSize
QSize legendIconSize() const
Definition qwt_plot_item.cpp:386
QwtPlotItem::ItemInterest
ItemInterest
Plot Item Interests.
Definition qwt_plot_item.h:180
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::pDist
double pDist() const
Definition qwt_scale_map.h:115
QwtScaleMap::p1
double p1() const
Definition qwt_scale_map.h:99
QwtScaleMap::s1
double s1() const
Definition qwt_scale_map.h:83
QwtScaleMap::sDist
double sDist() const
Definition qwt_scale_map.h:123
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtText::renderFlags
int renderFlags() const
Definition qwt_text.cpp:317
QwtText::setRenderFlags
void setRenderFlags(int)
Change the render flags.
Definition qwt_text.cpp:304
QwtAxis
Definition qwt_axis.h:19
QwtAxis::isYAxis
bool isYAxis(int axisPos)
Definition qwt_axis.h:57
QwtAxis::XBottom
@ XBottom
X axis below the canvas.
Definition qwt_axis.h:30
QwtAxis::YLeft
@ YLeft
Y axis left of the canvas.
Definition qwt_axis.h:24
QwtAxis::isXAxis
bool isXAxis(int axisPos)
Definition qwt_axis.h:51