Classes | Public Member Functions

QwtPlotScaleItem Class Reference

A class which draws a scale inside the plot canvas. More...

#include <qwt_plot_scaleitem.h>

Inheritance diagram for QwtPlotScaleItem:
Inheritance graph
[legend]
Collaboration diagram for QwtPlotScaleItem:
Collaboration graph
[legend]

List of all members.

Classes

class  PrivateData

Public Member Functions

 QwtPlotScaleItem (QwtScaleDraw::Alignment=QwtScaleDraw::BottomScale, const double pos=0.0)
 Constructor for scale item at the position pos.
virtual ~QwtPlotScaleItem ()
 Destructor.
virtual int rtti () const
void setScaleDiv (const QwtScaleDiv &)
 Assign a scale division.
const QwtScaleDivscaleDiv () const
void setScaleDivFromAxis (bool on)
bool isScaleDivFromAxis () const
void setColorGroup (const QColorGroup &)
QColorGroup colorGroup () const
void setFont (const QFont &)
QFont font () const
void setScaleDraw (QwtScaleDraw *)
 Set a scale draw.
const QwtScaleDrawscaleDraw () const
QwtScaleDrawscaleDraw ()
void setPosition (double pos)
double position () const
void setBorderDistance (int numPixels)
 Align the scale to the canvas.
int borderDistance () const
void setAlignment (QwtScaleDraw::Alignment)
virtual void draw (QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRect &rect) const
 Draw the scale.
virtual void updateScaleDiv (const QwtScaleDiv &, const QwtScaleDiv &)
 Update the item to changes of the axes scale division.

Detailed Description

A class which draws a scale inside the plot canvas.

QwtPlotScaleItem can be used to draw an axis inside the plot canvas. It might by synchronized to one of the axis of the plot, but can also display its own ticks and labels.

It is allowed to synchronize the scale item with a disabled axis. In plots with vertical and horizontal scale items, it might be necessary to remove ticks at the intersections, by overloading updateScaleDiv().

The scale might be at a specific position (f.e 0.0) or it might be aligned to a canvas border.

Example
The following example shows how to replace the left axis, by a scale item at the x position 0.0.
QwtPlotScaleItem *scaleItem = 
    new QwtPlotScaleItem(QwtScaleDraw::RightScale, 0.0);
scaleItem->setFont(plot->axisWidget(QwtPlot::yLeft)->font());
scaleItem->attach(plot);

plot->enableAxis(QwtPlot::yLeft, false);

Definition at line 51 of file qwt_plot_scaleitem.h.


Constructor & Destructor Documentation

QwtPlotScaleItem::QwtPlotScaleItem ( QwtScaleDraw::Alignment  alignment = QwtScaleDraw::BottomScale,
const double  pos = 0.0 
) [explicit]

Constructor for scale item at the position pos.

Parameters:
alignmentIn case of QwtScaleDraw::BottomScale/QwtScaleDrawTopScale the scale item is corresponding to the xAxis(), otherwise it corresponds to the yAxis().
posx or y position, depending on the corresponding axis.
See also:
setPosition(), setAlignment()

Definition at line 58 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::position, QwtPlotScaleItem::PrivateData::scaleDraw, QwtScaleDraw::setAlignment(), and QwtPlotItem::setZ().

                                                          :
    QwtPlotItem(QwtText("Scale"))
{
    d_data = new PrivateData;
    d_data->position = pos;
    d_data->scaleDraw->setAlignment(alignment);

    setZ(11.0);
}
QwtPlotScaleItem::~QwtPlotScaleItem (  ) [virtual]

Destructor.

Definition at line 70 of file qwt_plot_scaleitem.cpp.

{
    delete d_data;
}

Member Function Documentation

int QwtPlotScaleItem::borderDistance (  ) const
Returns:
Distance from a canvas border
See also:
setBorderDistance(), setPosition()

Definition at line 320 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::borderDistance.

{
    return d_data->borderDistance;
}
QColorGroup QwtPlotScaleItem::colorGroup (  ) const
Returns:
color group
See also:
setColorGroup()

Definition at line 156 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::colorGroup.

Referenced by setColorGroup().

{
    return d_data->colorGroup;
}
void QwtPlotScaleItem::draw ( QPainter *  p,
const QwtScaleMap xMap,
const QwtScaleMap yMap,
const QRect &  rect 
) const [virtual]

Draw the scale.

Implements QwtPlotItem.

Definition at line 354 of file qwt_plot_scaleitem.cpp.

References QwtScaleDraw::alignment(), QwtPlotScaleItem::PrivateData::borderDistance, QwtScaleDraw::BottomScale, QwtPlotScaleItem::PrivateData::canvasRectCache, QwtPlotScaleItem::PrivateData::colorGroup, QwtScaleTransformation::copy(), QwtAbstractScaleDraw::draw(), QwtPlotScaleItem::PrivateData::font, QwtScaleDraw::move(), QwtScaleDraw::orientation(), QwtPlotScaleItem::PrivateData::position, QwtScaleDraw::RightScale, QwtPlotScaleItem::PrivateData::scaleDraw, QwtScaleDraw::setLength(), QwtAbstractScaleDraw::setTransformation(), QwtScaleMap::transform(), and QwtScaleMap::transformation().

{
    if ( canvasRect != d_data->canvasRectCache )
    {
        QwtPlotScaleItem* that = (QwtPlotScaleItem*)this;
        that->updateBorders();
    }

    QPen pen = painter->pen();
    pen.setStyle(Qt::SolidLine);
    painter->setPen(pen);

    int pw = painter->pen().width();
    if ( pw == 0 )
        pw = 1;

    QwtScaleDraw *sd = d_data->scaleDraw;
    if ( sd->orientation() == Qt::Horizontal )
    {
        int y;
        if ( d_data->borderDistance >= 0 )
        {
            if ( sd->alignment() == QwtScaleDraw::BottomScale )
                y = canvasRect.top() + d_data->borderDistance;
            else
            {
                y = canvasRect.bottom() - d_data->borderDistance - pw + 1;
            }

        }
        else
        {
            y = yMap.transform(d_data->position);
        }

        if ( y < canvasRect.top() || y > canvasRect.bottom() )
            return;

        sd->move(canvasRect.left(), y);
        sd->setLength(canvasRect.width() - 1);
        sd->setTransformation(xMap.transformation()->copy());
    }
    else // == Qt::Vertical
    {
        int x;
        if ( d_data->borderDistance >= 0 )
        {
            if ( sd->alignment() == QwtScaleDraw::RightScale )
                x = canvasRect.left() + d_data->borderDistance;
            else
            {
                x = canvasRect.right() - d_data->borderDistance - pw + 1;
            }
        }
        else
        {
            x = xMap.transform(d_data->position);
        }
        if ( x < canvasRect.left() || x > canvasRect.right() )
            return;

        sd->move(x, canvasRect.top());
        sd->setLength(canvasRect.height() - 1);
        sd->setTransformation(yMap.transformation()->copy());
    }

    painter->setFont(d_data->font);

#if QT_VERSION < 0x040000
    sd->draw(painter, d_data->colorGroup);
#else
    sd->draw(painter, d_data->palette);
#endif
    
}
QFont QwtPlotScaleItem::font (  ) const
Returns:
tick label font
See also:
setFont()

Definition at line 204 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::font.

Referenced by setFont().

{
    return d_data->font;
}
bool QwtPlotScaleItem::isScaleDivFromAxis (  ) const
Returns:
True, if the synchronization of the scale division with the corresponding axis is enabled.
See also:
setScaleDiv(), setScaleDivFromAxis()

Definition at line 132 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::scaleDivFromAxis.

{
    return d_data->scaleDivFromAxis;
}
double QwtPlotScaleItem::position (  ) const
Returns:
Position of the scale
See also:
setPosition(), setAlignment()

Definition at line 283 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::position.

{
    return d_data->position;
}
int QwtPlotScaleItem::rtti (  ) const [virtual]
Returns:
QwtPlotItem::Rtti_PlotScale

Reimplemented from QwtPlotItem.

Definition at line 76 of file qwt_plot_scaleitem.cpp.

References QwtPlotItem::Rtti_PlotScale.

const QwtScaleDiv & QwtPlotScaleItem::scaleDiv (  ) const
Returns:
Scale division

Definition at line 97 of file qwt_plot_scaleitem.cpp.

References QwtAbstractScaleDraw::scaleDiv(), and QwtPlotScaleItem::PrivateData::scaleDraw.

{
    return d_data->scaleDraw->scaleDiv();
}
const QwtScaleDraw * QwtPlotScaleItem::scaleDraw (  ) const
Returns:
Scale draw
See also:
setScaleDraw()

Definition at line 244 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::scaleDraw.

Referenced by setScaleDraw().

{
    return d_data->scaleDraw;
}
QwtScaleDraw * QwtPlotScaleItem::scaleDraw (  )
Returns:
Scale draw
See also:
setScaleDraw()

Definition at line 253 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::scaleDraw.

{
    return d_data->scaleDraw;
}
void QwtPlotScaleItem::setAlignment ( QwtScaleDraw::Alignment  alignment )

Change the alignment of the scale

The alignment sets the orientation of the scale and the position of the ticks:

For horizontal scales the position corresponds to QwtPlotItem::yAxis(), otherwise to QwtPlotItem::xAxis().

See also:
scaleDraw(), QwtScaleDraw::alignment(), setPosition()

Definition at line 341 of file qwt_plot_scaleitem.cpp.

References QwtScaleDraw::alignment(), QwtPlotItem::itemChanged(), QwtPlotScaleItem::PrivateData::scaleDraw, and QwtScaleDraw::setAlignment().

{
    QwtScaleDraw *sd = d_data->scaleDraw;
    if ( sd->alignment() != alignment )
    {
        sd->setAlignment(alignment);
        itemChanged();
    }
}
void QwtPlotScaleItem::setBorderDistance ( int  distance )

Align the scale to the canvas.

If distance is >= 0 the scale will be aligned to a border of the contents rect of the canvas. If alignment() is QwtScaleDraw::LeftScale, the scale will be aligned to the right border, if it is QwtScaleDraw::TopScale it will be aligned to the bottom (and vice versa),

If distance is < 0 the scale will be at the position().

Parameters:
distanceNumber of pixels between the canvas border and the backbone of the scale.
See also:
setPosition(), borderDistance()

Definition at line 304 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::borderDistance, QuadProgPP::distance(), and QwtPlotItem::itemChanged().

{
    if ( distance < 0 )
        distance = -1;

    if ( distance != d_data->borderDistance )
    {
        d_data->borderDistance = distance;
        itemChanged();
    }
}
void QwtPlotScaleItem::setColorGroup ( const QColorGroup &  colorGroup )
void QwtPlotScaleItem::setFont ( const QFont &  font )

Change the tick label font

See also:
font()

Definition at line 191 of file qwt_plot_scaleitem.cpp.

References font(), QwtPlotScaleItem::PrivateData::font, and QwtPlotItem::itemChanged().

{
    if ( font != d_data->font )
    {
        d_data->font = font;
        itemChanged();
    }
}
void QwtPlotScaleItem::setPosition ( double  pos )

Change the position of the scale

The position is interpreted as y value for horizontal axes and as x value for vertical axes.

The border distance is set to -1.

Parameters:
posNew position
See also:
position(), setAlignment()

Definition at line 269 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::borderDistance, QwtPlotItem::itemChanged(), and QwtPlotScaleItem::PrivateData::position.

{
    if ( d_data->position != pos )
    {
        d_data->position = pos;
        d_data->borderDistance = -1;
        itemChanged();
    }
}
void QwtPlotScaleItem::setScaleDiv ( const QwtScaleDiv scaleDiv )

Assign a scale division.

When assigning a scaleDiv the scale division won't be synchronized with the corresponding axis anymore.

Parameters:
scaleDivScale division
See also:
scaleDiv(), setScaleDivFromAxis(), isScaleDivFromAxis()

Definition at line 90 of file qwt_plot_scaleitem.cpp.

References QwtPlotScaleItem::PrivateData::scaleDivFromAxis, QwtPlotScaleItem::PrivateData::scaleDraw, and QwtAbstractScaleDraw::setScaleDiv().

{
    d_data->scaleDivFromAxis = false;
    d_data->scaleDraw->setScaleDiv(scaleDiv);
}
void QwtPlotScaleItem::setScaleDivFromAxis ( bool  on )

Enable/Disable the synchronization of the scale division with the corresponding axis.

Parameters:
ontrue/false
See also:
isScaleDivFromAxis()

Definition at line 109 of file qwt_plot_scaleitem.cpp.

References QwtPlot::axisScaleDiv(), QwtPlotItem::itemChanged(), QwtPlotItem::plot(), QwtPlotScaleItem::PrivateData::scaleDivFromAxis, updateScaleDiv(), QwtPlotItem::xAxis(), and QwtPlotItem::yAxis().

{
    if ( on != d_data->scaleDivFromAxis )
    {
        d_data->scaleDivFromAxis = on;
        if ( on )
        {
            const QwtPlot *plt = plot();
            if ( plt )
            {
                updateScaleDiv( *plt->axisScaleDiv(xAxis()),
                    *plt->axisScaleDiv(yAxis()) );
                itemChanged();
            }
        }
    }
}
void QwtPlotScaleItem::setScaleDraw ( QwtScaleDraw scaleDraw )

Set a scale draw.

Parameters:
scaleDrawobject responsible for drawing scales.

The main use case for replacing the default QwtScaleDraw is to overload QwtAbstractScaleDraw::label, to replace or swallow tick labels.

See also:
scaleDraw()

Definition at line 220 of file qwt_plot_scaleitem.cpp.

References QwtPlot::axisScaleDiv(), QwtPlotItem::itemChanged(), QwtPlotItem::plot(), scaleDraw(), QwtPlotScaleItem::PrivateData::scaleDraw, updateScaleDiv(), QwtPlotItem::xAxis(), and QwtPlotItem::yAxis().

{
    if ( scaleDraw == NULL )
        return;

    if ( scaleDraw != d_data->scaleDraw )
        delete d_data->scaleDraw;

    d_data->scaleDraw = scaleDraw;

    const QwtPlot *plt = plot();
    if ( plt )
    {
        updateScaleDiv( *plt->axisScaleDiv(xAxis()),
            *plt->axisScaleDiv(yAxis()) );
    }

    itemChanged();
}
void QwtPlotScaleItem::updateScaleDiv ( const QwtScaleDiv xScaleDiv,
const QwtScaleDiv yScaleDiv 
) [virtual]

Update the item to changes of the axes scale division.

In case of isScaleDivFromAxis(), the scale draw is synchronized to the correspond axis.

Parameters:
xScaleDivScale division of the x-axis
yScaleDivScale division of the y-axis
See also:
QwtPlot::updateAxes()

Reimplemented from QwtPlotItem.

Definition at line 444 of file qwt_plot_scaleitem.cpp.

References QwtScaleDraw::orientation(), QwtPlotScaleItem::PrivateData::scaleDivFromAxis, QwtPlotScaleItem::PrivateData::scaleDraw, and QwtAbstractScaleDraw::setScaleDiv().

Referenced by setScaleDivFromAxis(), and setScaleDraw().

{
    QwtScaleDraw *sd = d_data->scaleDraw;
    if ( d_data->scaleDivFromAxis && sd )
    {
        sd->setScaleDiv(
            sd->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv);
        updateBorders();
    }
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines