Loading...
Searching...
No Matches
qwt_plot_scaleitem.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_scaleitem.h"
#include "qwt_plot.h"
#include "qwt_scale_map.h"
#include "qwt_interval.h"
#include "qwt_text.h"
#include <qpalette.h>
#include <qpainter.h>
class QwtPlotScaleItem::PrivateData
{
public:
PrivateData()
: position( 0.0 )
, borderDistance( -1 )
, scaleDivFromAxis( true )
, scaleDraw( new QwtScaleDraw() )
{
}
~PrivateData()
{
delete scaleDraw;
}
QwtInterval scaleInterval( const QRectF&,
const QwtScaleMap&, const QwtScaleMap& ) const;
QPalette palette;
QFont font;
double position;
int borderDistance;
bool scaleDivFromAxis;
QwtScaleDraw* scaleDraw;
};
QwtInterval QwtPlotScaleItem::PrivateData::scaleInterval( const QRectF& canvasRect,
const QwtScaleMap& xMap, const QwtScaleMap& yMap ) const
{
QwtInterval interval;
if ( scaleDraw->orientation() == Qt::Horizontal )
{
interval.setMinValue( xMap.invTransform( canvasRect.left() ) );
interval.setMaxValue( xMap.invTransform( canvasRect.right() - 1 ) );
}
else
{
interval.setMinValue( yMap.invTransform( canvasRect.bottom() - 1 ) );
interval.setMaxValue( yMap.invTransform( canvasRect.top() ) );
}
return interval;
}
QwtPlotScaleItem::QwtPlotScaleItem(
QwtScaleDraw::Alignment alignment, const double pos )
: QwtPlotItem( QwtText( "Scale" ) )
{
m_data = new PrivateData;
m_data->position = pos;
m_data->scaleDraw->setAlignment( alignment );
setItemInterest( QwtPlotItem::ScaleInterest, true );
setZ( 11.0 );
}
QwtPlotScaleItem::~QwtPlotScaleItem()
{
delete m_data;
}
int QwtPlotScaleItem::rtti() const
{
return QwtPlotItem::Rtti_PlotScale;
}
void QwtPlotScaleItem::setScaleDiv( const QwtScaleDiv& scaleDiv )
{
m_data->scaleDivFromAxis = false;
m_data->scaleDraw->setScaleDiv( scaleDiv );
}
const QwtScaleDiv& QwtPlotScaleItem::scaleDiv() const
{
return m_data->scaleDraw->scaleDiv();
}
void QwtPlotScaleItem::setScaleDivFromAxis( bool on )
{
if ( on != m_data->scaleDivFromAxis )
{
m_data->scaleDivFromAxis = on;
if ( on )
{
const QwtPlot* plt = plot();
if ( plt )
{
updateScaleDiv( plt->axisScaleDiv( xAxis() ),
plt->axisScaleDiv( yAxis() ) );
itemChanged();
}
}
}
}
bool QwtPlotScaleItem::isScaleDivFromAxis() const
{
return m_data->scaleDivFromAxis;
}
void QwtPlotScaleItem::setPalette( const QPalette& palette )
{
if ( palette != m_data->palette )
{
m_data->palette = palette;
legendChanged();
itemChanged();
}
}
QPalette QwtPlotScaleItem::palette() const
{
return m_data->palette;
}
void QwtPlotScaleItem::setFont( const QFont& font )
{
if ( font != m_data->font )
{
m_data->font = font;
itemChanged();
}
}
QFont QwtPlotScaleItem::font() const
{
return m_data->font;
}
void QwtPlotScaleItem::setScaleDraw( QwtScaleDraw* scaleDraw )
{
if ( scaleDraw == NULL )
return;
if ( scaleDraw != m_data->scaleDraw )
delete m_data->scaleDraw;
m_data->scaleDraw = scaleDraw;
const QwtPlot* plt = plot();
if ( plt )
{
updateScaleDiv( plt->axisScaleDiv( xAxis() ),
plt->axisScaleDiv( yAxis() ) );
}
itemChanged();
}
const QwtScaleDraw* QwtPlotScaleItem::scaleDraw() const
{
return m_data->scaleDraw;
}
QwtScaleDraw* QwtPlotScaleItem::scaleDraw()
{
return m_data->scaleDraw;
}
void QwtPlotScaleItem::setPosition( double pos )
{
if ( m_data->position != pos )
{
m_data->position = pos;
m_data->borderDistance = -1;
itemChanged();
}
}
double QwtPlotScaleItem::position() const
{
return m_data->position;
}
void QwtPlotScaleItem::setBorderDistance( int distance )
{
if ( distance < 0 )
distance = -1;
if ( distance != m_data->borderDistance )
{
m_data->borderDistance = distance;
itemChanged();
}
}
int QwtPlotScaleItem::borderDistance() const
{
return m_data->borderDistance;
}
void QwtPlotScaleItem::setAlignment( QwtScaleDraw::Alignment alignment )
{
QwtScaleDraw* sd = m_data->scaleDraw;
if ( sd->alignment() != alignment )
{
sd->setAlignment( alignment );
itemChanged();
}
}
void QwtPlotScaleItem::draw( QPainter* painter,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QRectF& canvasRect ) const
{
QwtScaleDraw* sd = m_data->scaleDraw;
if ( m_data->scaleDivFromAxis )
{
const QwtInterval interval =
m_data->scaleInterval( canvasRect, xMap, yMap );
if ( interval != sd->scaleDiv().interval() )
{
QwtScaleDiv scaleDiv = sd->scaleDiv();
scaleDiv.setInterval( interval );
sd->setScaleDiv( scaleDiv );
}
}
QPen pen = painter->pen();
pen.setStyle( Qt::SolidLine );
painter->setPen( pen );
if ( sd->orientation() == Qt::Horizontal )
{
double y;
if ( m_data->borderDistance >= 0 )
{
if ( sd->alignment() == QwtScaleDraw::BottomScale )
y = canvasRect.top() + m_data->borderDistance;
else
{
y = canvasRect.bottom() - m_data->borderDistance;
}
}
else
{
y = yMap.transform( m_data->position );
}
if ( y < canvasRect.top() || y > canvasRect.bottom() )
return;
sd->move( canvasRect.left(), y );
sd->setLength( canvasRect.width() - 1 );
QwtTransform* transform = NULL;
if ( xMap.transformation() )
transform = xMap.transformation()->copy();
sd->setTransformation( transform );
}
else // == Qt::Vertical
{
double x;
if ( m_data->borderDistance >= 0 )
{
if ( sd->alignment() == QwtScaleDraw::RightScale )
x = canvasRect.left() + m_data->borderDistance;
else
{
x = canvasRect.right() - m_data->borderDistance;
}
}
else
{
x = xMap.transform( m_data->position );
}
if ( x < canvasRect.left() || x > canvasRect.right() )
return;
sd->move( x, canvasRect.top() );
sd->setLength( canvasRect.height() - 1 );
QwtTransform* transform = NULL;
if ( yMap.transformation() )
transform = yMap.transformation()->copy();
sd->setTransformation( transform );
}
painter->setFont( m_data->font );
sd->draw( painter, m_data->palette );
}
void QwtPlotScaleItem::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
const QwtScaleDiv& yScaleDiv )
{
QwtScaleDraw* scaleDraw = m_data->scaleDraw;
if ( m_data->scaleDivFromAxis && scaleDraw )
{
const QwtScaleDiv& scaleDiv =
scaleDraw->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv;
const QwtPlot* plt = plot();
if ( plt != NULL )
{
const QRectF canvasRect = plt->canvas()->contentsRect();
const QwtInterval interval = m_data->scaleInterval(
canvasRect, plt->canvasMap( xAxis() ), plt->canvasMap( yAxis() ) );
QwtScaleDiv sd = scaleDiv;
sd.setInterval( interval );
if ( sd != scaleDraw->scaleDiv() )
{
// the internal label cache of QwtScaleDraw
// is cleared here, so better avoid pointless
// assignments.
scaleDraw->setScaleDiv( sd );
}
}
else
{
scaleDraw->setScaleDiv( scaleDiv );
}
}
}
QwtAbstractScaleDraw::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Definition qwt_abstract_scale_draw.cpp:104
QwtAbstractScaleDraw::setTransformation
void setTransformation(QwtTransform *)
Definition qwt_abstract_scale_draw.cpp:115
QwtAbstractScaleDraw::draw
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
Definition qwt_abstract_scale_draw.cpp:169
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition qwt_abstract_scale_draw.cpp:133
QwtInterval
A class representing an interval.
Definition qwt_interval.h:23
QwtInterval::setMaxValue
void setMaxValue(double)
Definition qwt_interval.h:186
QwtInterval::setMinValue
void setMinValue(double)
Definition qwt_interval.h:176
QwtPlot
A 2-D plotting widget.
Definition qwt_plot.h:79
QwtPlot::canvas
QWidget * canvas()
Definition qwt_plot.cpp:463
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition qwt_plot_axis.cpp:283
QwtPlotItem
Base class for items on the plot canvas.
Definition qwt_plot_item.h:67
QwtPlotItem::yAxis
QwtAxisId yAxis() const
Return yAxis.
Definition qwt_plot_item.cpp:559
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition qwt_plot_item.cpp:491
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition qwt_plot_item.cpp:165
QwtPlotItem::plot
QwtPlot * plot() const
Return attached plot.
Definition qwt_plot_item.cpp:142
QwtPlotItem::xAxis
QwtAxisId xAxis() const
Return xAxis.
Definition qwt_plot_item.cpp:553
QwtPlotItem::Rtti_PlotScale
@ Rtti_PlotScale
For QwtPlotScaleItem.
Definition qwt_plot_item.h:84
QwtPlotItem::setItemInterest
void setItemInterest(ItemInterest, bool on=true)
Definition qwt_plot_item.cpp:279
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition qwt_plot_item.cpp:481
QwtPlotItem::ScaleInterest
@ ScaleInterest
Definition qwt_plot_item.h:185
QwtPlotScaleItem::setScaleDraw
void setScaleDraw(QwtScaleDraw *)
Set a scale draw.
Definition qwt_plot_scaleitem.cpp:212
QwtPlotScaleItem::setScaleDivFromAxis
void setScaleDivFromAxis(bool on)
Definition qwt_plot_scaleitem.cpp:127
QwtPlotScaleItem::borderDistance
int borderDistance() const
Definition qwt_plot_scaleitem.cpp:312
QwtPlotScaleItem::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition qwt_plot_scaleitem.cpp:115
QwtPlotScaleItem::updateScaleDiv
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &) override
Update the item to changes of the axes scale division.
Definition qwt_plot_scaleitem.cpp:445
QwtPlotScaleItem::position
double position() const
Definition qwt_plot_scaleitem.cpp:275
QwtPlotScaleItem::isScaleDivFromAxis
bool isScaleDivFromAxis() const
Definition qwt_plot_scaleitem.cpp:150
QwtPlotScaleItem::scaleDraw
const QwtScaleDraw * scaleDraw() const
Definition qwt_plot_scaleitem.cpp:236
QwtPlotScaleItem::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const override
Draw the scale.
Definition qwt_plot_scaleitem.cpp:346
QwtPlotScaleItem::rtti
virtual int rtti() const override
Definition qwt_plot_scaleitem.cpp:94
QwtPlotScaleItem::~QwtPlotScaleItem
virtual ~QwtPlotScaleItem()
Destructor.
Definition qwt_plot_scaleitem.cpp:88
QwtPlotScaleItem::setFont
void setFont(const QFont &)
Definition qwt_plot_scaleitem.cpp:183
QwtPlotScaleItem::setPosition
void setPosition(double pos)
Definition qwt_plot_scaleitem.cpp:261
QwtPlotScaleItem::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Assign a scale division.
Definition qwt_plot_scaleitem.cpp:108
QwtPlotScaleItem::QwtPlotScaleItem
QwtPlotScaleItem(QwtScaleDraw::Alignment=QwtScaleDraw::BottomScale, const double pos=0.0)
Constructor for scale item at the position pos.
Definition qwt_plot_scaleitem.cpp:75
QwtPlotScaleItem::font
QFont font() const
Definition qwt_plot_scaleitem.cpp:196
QwtPlotScaleItem::palette
QPalette palette() const
Definition qwt_plot_scaleitem.cpp:174
QwtPlotScaleItem::setBorderDistance
void setBorderDistance(int)
Align the scale to the canvas.
Definition qwt_plot_scaleitem.cpp:296
QwtPlotScaleItem::setAlignment
void setAlignment(QwtScaleDraw::Alignment)
Definition qwt_plot_scaleitem.cpp:333
QwtPlotScaleItem::setPalette
void setPalette(const QPalette &)
Definition qwt_plot_scaleitem.cpp:159
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleDiv::interval
QwtInterval interval() const
Definition qwt_scale_div.cpp:111
QwtScaleDiv::setInterval
void setInterval(double lowerBound, double upperBound)
Definition qwt_scale_div.cpp:91
QwtScaleDraw
A class for drawing scales.
Definition qwt_scale_draw.h:36
QwtScaleDraw::setLength
void setLength(double length)
Definition qwt_scale_draw.cpp:732
QwtScaleDraw::move
void move(double x, double y)
Definition qwt_scale_draw.h:118
QwtScaleDraw::setAlignment
void setAlignment(Alignment)
Definition qwt_scale_draw.cpp:322
QwtScaleDraw::alignment
Alignment alignment() const
Definition qwt_scale_draw.cpp:309
QwtScaleDraw::orientation
Qt::Orientation orientation() const
Definition qwt_scale_draw.cpp:337
QwtScaleDraw::Alignment
Alignment
Definition qwt_scale_draw.h:43
QwtScaleDraw::BottomScale
@ BottomScale
The scale is below.
Definition qwt_scale_draw.h:45
QwtScaleDraw::RightScale
@ RightScale
The scale is right.
Definition qwt_scale_draw.h:54
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtScaleMap::transformation
const QwtTransform * transformation() const
Get the transformation.
Definition qwt_scale_map.cpp:88
QwtScaleMap::invTransform
double invTransform(double p) const
Definition qwt_scale_map.h:154
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtTransform
A transformation between coordinate systems.
Definition qwt_transform.h:36
QwtTransform::copy
virtual QwtTransform * copy() const =0
Virtualized copy operation.