Classes | Public Types | Public Slots | Signals | Public Member Functions | Protected Member Functions

QwtLegendItem Class Reference

A legend label. More...

#include <qwt_legend_item.h>

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

List of all members.

Classes

class  PrivateData

Public Types

enum  IdentifierMode { NoIdentifier = 0, ShowLine = 1, ShowSymbol = 2, ShowText = 4 }
 

Identifier mode.

More...

Public Slots

void setChecked (bool on)

Signals

void clicked ()
 Signal, when the legend item has been clicked.
void pressed ()
 Signal, when the legend item has been pressed.
void released ()
 Signal, when the legend item has been relased.
void checked (bool)
 Signal, when the legend item has been toggled.

Public Member Functions

 QwtLegendItem (QWidget *parent=0)
 QwtLegendItem (const QwtSymbol &, const QPen &, const QwtText &, QWidget *parent=0)
virtual ~QwtLegendItem ()
 Destructor.
virtual void setText (const QwtText &)
void setItemMode (QwtLegend::LegendItemMode)
QwtLegend::LegendItemMode itemMode () const
void setIdentifierMode (int)
int identifierMode () const
void setIdentifierWidth (int width)
int identifierWidth () const
void setSpacing (int spacing)
int spacing () const
void setSymbol (const QwtSymbol &)
const QwtSymbolsymbol () const
void setCurvePen (const QPen &)
const QPen & curvePen () const
virtual void drawIdentifier (QPainter *, const QRect &) const
virtual void drawItem (QPainter *p, const QRect &) const
virtual QSize sizeHint () const
 Return a size hint.
bool isChecked () const
 Return true, if the item is checked.

Protected Member Functions

void setDown (bool)
 Set the item being down.
bool isDown () const
 Return true, if the item is down.
virtual void paintEvent (QPaintEvent *)
 Paint event.
virtual void mousePressEvent (QMouseEvent *)
 Handle mouse press events.
virtual void mouseReleaseEvent (QMouseEvent *)
 Handle mouse release events.
virtual void keyPressEvent (QKeyEvent *)
 Handle key press events.
virtual void keyReleaseEvent (QKeyEvent *)
 Handle key release events.
virtual void drawText (QPainter *, const QRect &)
 Redraw the text.

Detailed Description

A legend label.

QwtLegendItem represents a curve on a legend. It displays an curve identifier with an explaining text. The identifier might be a combination of curve symbol and line. In readonly mode it behaves like a label, otherwise like an unstylish push button.

See also:
QwtLegend, QwtPlotCurve

Definition at line 35 of file qwt_legend_item.h.


Member Enumeration Documentation

Identifier mode.

Default is ShowLine | ShowText

See also:
identifierMode(), setIdentifierMode()
Enumerator:
NoIdentifier 
ShowLine 
ShowSymbol 
ShowText 

Definition at line 47 of file qwt_legend_item.h.

    {
        NoIdentifier = 0,
        ShowLine = 1,
        ShowSymbol = 2,
        ShowText = 4
    };

Constructor & Destructor Documentation

QwtLegendItem::QwtLegendItem ( QWidget *  parent = 0 ) [explicit]
Parameters:
parentParent widget

Definition at line 80 of file qwt_legend_item.cpp.

                                           :
    QwtTextLabel(parent)
{
    d_data = new PrivateData;
    init(QwtText());
}
QwtLegendItem::QwtLegendItem ( const QwtSymbol symbol,
const QPen &  curvePen,
const QwtText text,
QWidget *  parent = 0 
) [explicit]
Parameters:
symbolCurve symbol
curvePenCurve pen
textLabel text
parentParent widget

Definition at line 93 of file qwt_legend_item.cpp.

References QwtSymbol::clone(), curvePen(), QwtLegendItem::PrivateData::curvePen, and QwtLegendItem::PrivateData::symbol.

                        :
    QwtTextLabel(parent)
{
    d_data = new PrivateData;

    delete d_data->symbol;
    d_data->symbol = symbol.clone();

    d_data->curvePen = curvePen;

    init(text);
}
QwtLegendItem::~QwtLegendItem (  ) [virtual]

Destructor.

Definition at line 116 of file qwt_legend_item.cpp.

{
    delete d_data;
    d_data = NULL;
}

Member Function Documentation

void QwtLegendItem::checked ( bool   ) [signal]

Signal, when the legend item has been toggled.

Referenced by setDown().

void QwtLegendItem::clicked (  ) [signal]

Signal, when the legend item has been clicked.

Referenced by setDown().

const QPen & QwtLegendItem::curvePen (  ) const
Returns:
The curve pen.
See also:
setCurvePen()

Definition at line 294 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::curvePen.

Referenced by QwtPlotPrintFilter::apply(), QwtLegendItem(), and QwtPlotPrintFilter::reset().

{ 
    return d_data->curvePen; 
}
void QwtLegendItem::drawIdentifier ( QPainter *  painter,
const QRect &  rect 
) const [virtual]

Paint the identifier to a given rect.

Parameters:
painterPainter
rectRect where to paint

Definition at line 304 of file qwt_legend_item.cpp.

References QwtSymbol::brush(), QwtLegendItem::PrivateData::curvePen, QwtSymbol::draw(), QwtPainter::drawLine(), QwtLegendItem::PrivateData::identifierMode, QwtPainter::metricsMap(), QwtSymbol::NoSymbol, QwtSymbol::pen(), QwtPainter::scaledPen(), QwtMetricsMap::screenToLayout(), ShowLine, ShowSymbol, QwtSymbol::size(), QwtSymbol::style(), and QwtLegendItem::PrivateData::symbol.

Referenced by drawItem(), and paintEvent().

{
    if ( rect.isEmpty() )
        return;

    if ( (d_data->identifierMode & ShowLine ) && (d_data->curvePen.style() != Qt::NoPen) )
    {
        painter->save();
        painter->setPen(QwtPainter::scaledPen(d_data->curvePen));
        QwtPainter::drawLine(painter, rect.left(), rect.center().y(), 
            rect.right(), rect.center().y());
        painter->restore();
    }

    if ( (d_data->identifierMode & ShowSymbol) 
        && (d_data->symbol->style() != QwtSymbol::NoSymbol) )
    {
        QSize symbolSize = 
            QwtPainter::metricsMap().screenToLayout(d_data->symbol->size());

        // scale the symbol size down if it doesn't fit into rect.

        if ( rect.width() < symbolSize.width() )
        {
            const double ratio = 
                double(symbolSize.width()) / double(rect.width());
            symbolSize.setWidth(rect.width());
            symbolSize.setHeight(qRound(symbolSize.height() / ratio));
        }
        if ( rect.height() < symbolSize.height() )
        {
            const double ratio = 
                double(symbolSize.width()) / double(rect.width());
            symbolSize.setHeight(rect.height());
            symbolSize.setWidth(qRound(symbolSize.width() / ratio));
        }

        QRect symbolRect;
        symbolRect.setSize(symbolSize);
        symbolRect.moveCenter(rect.center());

        painter->save();
        painter->setBrush(d_data->symbol->brush());
        painter->setPen(QwtPainter::scaledPen(d_data->symbol->pen()));
        d_data->symbol->draw(painter, symbolRect);
        painter->restore();
    }
}
void QwtLegendItem::drawItem ( QPainter *  painter,
const QRect &  rect 
) const [virtual]

Draw the legend item to a given rect.

Parameters:
painterPainter
rectRect where to paint the button

Definition at line 360 of file qwt_legend_item.cpp.

References QwtText::draw(), drawIdentifier(), QwtLegendItem::PrivateData::identifierWidth, identifierWidth(), QwtTextLabel::margin(), QwtPainter::metricsMap(), QwtMetricsMap::screenToLayoutX(), QwtLegendItem::PrivateData::spacing, spacing(), and QwtTextLabel::text().

Referenced by QwtPlot::printLegendItem().

{
    painter->save();

    const QwtMetricsMap &map = QwtPainter::metricsMap();

    const int m = map.screenToLayoutX(margin());
    const int spacing = map.screenToLayoutX(d_data->spacing);
    const int identifierWidth = map.screenToLayoutX(d_data->identifierWidth);

    const QRect identifierRect(rect.x() + m, rect.y(), 
        identifierWidth, rect.height());
    drawIdentifier(painter, identifierRect);

    // Label

    QRect titleRect = rect;
    titleRect.setX(identifierRect.right() + 2 * spacing);
     
    text().draw(painter, titleRect);

    painter->restore();
}
void QwtLegendItem::drawText ( QPainter *  painter,
const QRect &  textRect 
) [protected, virtual]

Redraw the text.

Reimplemented from QwtTextLabel.

Definition at line 587 of file qwt_legend_item.cpp.

{
    QwtTextLabel::drawText(painter, rect);
}
int QwtLegendItem::identifierMode (  ) const

Or'd values of IdentifierMode.

See also:
setIdentifierMode(), IdentifierMode

Definition at line 194 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::identifierMode.

{ 
    return d_data->identifierMode; 
}
int QwtLegendItem::identifierWidth (  ) const

Return the width of the identifier

See also:
setIdentifierWidth()

Definition at line 222 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::identifierWidth.

Referenced by drawItem().

{
    return d_data->identifierWidth;
}
bool QwtLegendItem::isChecked (  ) const

Return true, if the item is checked.

Definition at line 542 of file qwt_legend_item.cpp.

References QwtLegend::CheckableItem, isDown(), and QwtLegendItem::PrivateData::itemMode.

{
    return d_data->itemMode == QwtLegend::CheckableItem && isDown();
}
bool QwtLegendItem::isDown (  ) const [protected]

Return true, if the item is down.

Definition at line 572 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::isDown.

Referenced by isChecked(), keyPressEvent(), and mousePressEvent().

{
    return d_data->isDown;
}
QwtLegend::LegendItemMode QwtLegendItem::itemMode (  ) const

Return the item mode

See also:
setItemMode()

Definition at line 169 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::itemMode.

{ 
    return d_data->itemMode; 
}
void QwtLegendItem::keyPressEvent ( QKeyEvent *  e ) [protected, virtual]

Handle key press events.

Definition at line 473 of file qwt_legend_item.cpp.

References QwtLegend::CheckableItem, QwtLegend::ClickableItem, isDown(), QwtLegendItem::PrivateData::itemMode, mitk::Key_Space, and setDown().

{
    if ( e->key() == Qt::Key_Space )
    {
        switch(d_data->itemMode)
        {
            case QwtLegend::ClickableItem:
            {
                if ( !e->isAutoRepeat() )
                    setDown(true);
                return;
            }
            case QwtLegend::CheckableItem:
            {
                if ( !e->isAutoRepeat() )
                    setDown(!isDown());
                return;
            }
            default:;
        }
    }

    QwtTextLabel::keyPressEvent(e);
}
void QwtLegendItem::keyReleaseEvent ( QKeyEvent *  e ) [protected, virtual]

Handle key release events.

Definition at line 499 of file qwt_legend_item.cpp.

References QwtLegend::CheckableItem, QwtLegend::ClickableItem, QwtLegendItem::PrivateData::itemMode, mitk::Key_Space, and setDown().

{
    if ( e->key() == Qt::Key_Space )
    {
        switch(d_data->itemMode)
        {
            case QwtLegend::ClickableItem:
            {
                if ( !e->isAutoRepeat() )
                    setDown(false);
                return;
            }
            case QwtLegend::CheckableItem:
            {
                return; // do nothing, but accept
            }
            default:;
        }
    }

    QwtTextLabel::keyReleaseEvent(e);
}
void QwtLegendItem::mousePressEvent ( QMouseEvent *  e ) [protected, virtual]

Handle mouse press events.

Definition at line 428 of file qwt_legend_item.cpp.

References QwtLegend::CheckableItem, QwtLegend::ClickableItem, isDown(), QwtLegendItem::PrivateData::itemMode, and setDown().

{
    if ( e->button() == Qt::LeftButton )
    {
        switch(d_data->itemMode)
        {
            case QwtLegend::ClickableItem:
            {
                setDown(true);
                return;
            }
            case QwtLegend::CheckableItem:
            {
                setDown(!isDown());
                return;
            }
            default:;
        }
    }
    QwtTextLabel::mousePressEvent(e);
}
void QwtLegendItem::mouseReleaseEvent ( QMouseEvent *  e ) [protected, virtual]

Handle mouse release events.

Definition at line 451 of file qwt_legend_item.cpp.

References QwtLegend::CheckableItem, QwtLegend::ClickableItem, QwtLegendItem::PrivateData::itemMode, and setDown().

{
    if ( e->button() == Qt::LeftButton )
    {
        switch(d_data->itemMode)
        {
            case QwtLegend::ClickableItem:
            {
                setDown(false);
                return;
            }
            case QwtLegend::CheckableItem:
            {
                return; // do nothing, but accept
            }
            default:;
        }
    }
    QwtTextLabel::mouseReleaseEvent(e);
}
void QwtLegendItem::paintEvent ( QPaintEvent *  e ) [protected, virtual]

Paint event.

Reimplemented from QwtTextLabel.

Definition at line 385 of file qwt_legend_item.cpp.

References ButtonFrame, buttonShift(), QwtTextLabel::drawContents(), drawIdentifier(), QwtLegendItem::PrivateData::identifierWidth, QwtLegendItem::PrivateData::isDown, QwtLegendItem::PrivateData::itemMode, QwtTextLabel::margin(), and QwtLegend::ReadOnlyItem.

{
    const QRect cr = contentsRect();

    QPainter painter(this);
    painter.setClipRegion(e->region());

    if ( d_data->isDown )
    {
        qDrawWinButton(&painter, 0, 0, width(), height(), 
#if QT_VERSION < 0x040000
            colorGroup(), 
#else
            palette(),
#endif
            true);
    }

    painter.save();

    if ( d_data->isDown )
    {
        const QSize shiftSize = buttonShift(this);
        painter.translate(shiftSize.width(), shiftSize.height());
    }

    painter.setClipRect(cr);

    drawContents(&painter);

    QRect rect = cr;
    rect.setX(rect.x() + margin());
    if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
        rect.setX(rect.x() + ButtonFrame);

    rect.setWidth(d_data->identifierWidth);

    drawIdentifier(&painter, rect);

    painter.restore();
}
void QwtLegendItem::pressed (  ) [signal]

Signal, when the legend item has been pressed.

Referenced by setDown().

void QwtLegendItem::released (  ) [signal]

Signal, when the legend item has been relased.

Referenced by setDown().

void QwtLegendItem::setChecked ( bool  on ) [slot]

Check/Uncheck a the item

Parameters:
oncheck/uncheck
See also:
setItemMode()

Definition at line 528 of file qwt_legend_item.cpp.

References QwtLegend::CheckableItem, QwtLegendItem::PrivateData::itemMode, and setDown().

{
    if ( d_data->itemMode == QwtLegend::CheckableItem )
    {
        const bool isBlocked = signalsBlocked();
        blockSignals(true);

        setDown(on);

        blockSignals(isBlocked);
    }
}
void QwtLegendItem::setCurvePen ( const QPen &  pen )

Set curve pen.

Parameters:
penCurve pen
See also:
curvePen()

Definition at line 281 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::curvePen.

Referenced by QwtPlotPrintFilter::apply(), QwtPlotPrintFilter::reset(), and QwtPlotCurve::updateLegend().

{
    if ( pen != d_data->curvePen )
    {
        d_data->curvePen = pen;
        update();
    }
}
void QwtLegendItem::setDown ( bool  down ) [protected]

Set the item being down.

Definition at line 548 of file qwt_legend_item.cpp.

References QwtLegend::CheckableItem, checked(), QwtLegend::ClickableItem, clicked(), QwtLegendItem::PrivateData::isDown, QwtLegendItem::PrivateData::itemMode, pressed(), and released().

Referenced by keyPressEvent(), keyReleaseEvent(), mousePressEvent(), mouseReleaseEvent(), and setChecked().

{
    if ( down == d_data->isDown )
        return;

    d_data->isDown = down;
    update();

    if ( d_data->itemMode == QwtLegend::ClickableItem )
    {
        if ( d_data->isDown )
            emit pressed();
        else
        {
            emit released();
            emit clicked();
        }
    }

    if ( d_data->itemMode == QwtLegend::CheckableItem )
        emit checked(d_data->isDown);
}
void QwtLegendItem::setIdentifierMode ( int  mode )

Set identifier mode. Default is ShowLine | ShowText.

Parameters:
modeOr'd values of IdentifierMode
See also:
identifierMode()

Definition at line 181 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::identifierMode.

Referenced by QwtPlotCurve::updateLegend().

{
    if ( mode != d_data->identifierMode )
    {
        d_data->identifierMode = mode;
        update();
    }
}
void QwtLegendItem::setIdentifierWidth ( int  width )

Set the width for the identifier Default is 8 pixels

Parameters:
widthNew width
See also:
identifierMode(), identifierWidth()

Definition at line 207 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::identifierWidth, QwtTextLabel::margin(), qwtMax, QwtTextLabel::setIndent(), and QwtLegendItem::PrivateData::spacing.

{
    width = qwtMax(width, 0);
    if ( width != d_data->identifierWidth )
    {
        d_data->identifierWidth = width;
        setIndent(margin() + d_data->identifierWidth 
            + 2 * d_data->spacing);
    }
}
void QwtLegendItem::setItemMode ( QwtLegend::LegendItemMode  mode )

Set the item mode The default is QwtLegend::ReadOnlyItem

Parameters:
modeItem mode
See also:
itemMode()

Definition at line 150 of file qwt_legend_item.cpp.

References ButtonFrame, QwtLegendItem::PrivateData::isDown, QwtLegendItem::PrivateData::itemMode, Margin, QwtLegend::ReadOnlyItem, and QwtTextLabel::setMargin().

Referenced by QwtPlotItem::updateLegend().

{ 
    d_data->itemMode = mode; 
    d_data->isDown = false; 

#if QT_VERSION >= 0x040000
    using namespace Qt;
#endif
    setFocusPolicy(mode != QwtLegend::ReadOnlyItem ? TabFocus : NoFocus);
    setMargin(ButtonFrame + Margin);

    updateGeometry();
}
void QwtLegendItem::setSpacing ( int  spacing )

Change the spacing

Parameters:
spacingSpacing
See also:
spacing(), identifierWidth(), QwtTextLabel::margin()

Definition at line 232 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::identifierWidth, QwtTextLabel::margin(), qwtMax, QwtTextLabel::setIndent(), spacing(), and QwtLegendItem::PrivateData::spacing.

{
    spacing = qwtMax(spacing, 0);
    if ( spacing != d_data->spacing )
    {
        d_data->spacing = spacing;
        setIndent(margin() + d_data->identifierWidth 
            + 2 * d_data->spacing);
    }
}
void QwtLegendItem::setSymbol ( const QwtSymbol symbol )

Set curve symbol.

Parameters:
symbolSymbol
See also:
symbol()

Definition at line 258 of file qwt_legend_item.cpp.

References QwtSymbol::clone(), and QwtLegendItem::PrivateData::symbol.

Referenced by QwtPlotPrintFilter::apply(), QwtPlotPrintFilter::reset(), and QwtPlotCurve::updateLegend().

{
    delete d_data->symbol;
    d_data->symbol = symbol.clone();
    update();
}
void QwtLegendItem::setText ( const QwtText text ) [virtual]

Set the text to the legend item

Parameters:
textText label
See also:
QwtTextLabel::text()

Reimplemented from QwtTextLabel.

Definition at line 128 of file qwt_legend_item.cpp.

References QwtText::setRenderFlags(), and QwtTextLabel::text().

Referenced by QwtPlotItem::updateLegend(), and QwtPlotCurve::updateLegend().

{
    const int flags = Qt::AlignLeft | Qt::AlignVCenter
#if QT_VERSION < 0x040000
        | Qt::WordBreak | Qt::ExpandTabs;
#else
        | Qt::TextExpandTabs | Qt::TextWordWrap;
#endif

    QwtText txt = text;
    txt.setRenderFlags(flags);

    QwtTextLabel::setText(txt);
}
QSize QwtLegendItem::sizeHint (  ) const [virtual]

Return a size hint.

Reimplemented from QwtTextLabel.

Definition at line 578 of file qwt_legend_item.cpp.

References buttonShift(), QwtLegendItem::PrivateData::itemMode, and QwtLegend::ReadOnlyItem.

{
    QSize sz = QwtTextLabel::sizeHint();
    if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
        sz += buttonShift(this);

    return sz;
}
int QwtLegendItem::spacing (  ) const

Return the spacing

See also:
setSpacing(), identifierWidth(), QwtTextLabel::margin()

Definition at line 247 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::spacing.

Referenced by drawItem(), and setSpacing().

{
    return d_data->spacing;
}
const QwtSymbol & QwtLegendItem::symbol (  ) const
Returns:
The curve symbol.
See also:
setSymbol()

Definition at line 269 of file qwt_legend_item.cpp.

References QwtLegendItem::PrivateData::symbol.

Referenced by QwtPlotPrintFilter::apply(), and QwtPlotPrintFilter::reset().

{ 
    return *d_data->symbol; 
}

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