Classes | Public Types | Public Member Functions | Protected Member Functions

QwtLegend Class Reference

The legend widget. More...

#include <qwt_legend.h>

Collaboration diagram for QwtLegend:
Collaboration graph
[legend]

List of all members.

Classes

class  PrivateData

Public Types

enum  LegendDisplayPolicy { NoIdentifier = 0, FixedIdentifier = 1, AutoIdentifier = 2 }
 

Display policy.

More...
enum  LegendItemMode { ReadOnlyItem, ClickableItem, CheckableItem }
 

Interaction mode for the legend items.

More...

Public Member Functions

 QwtLegend (QWidget *parent=NULL)
virtual ~QwtLegend ()
 Destructor.
void setDisplayPolicy (LegendDisplayPolicy policy, int mode)
LegendDisplayPolicy displayPolicy () const
void setItemMode (LegendItemMode)
LegendItemMode itemMode () const
int identifierMode () const
QWidget * contentsWidget ()
const QWidget * contentsWidget () const
void insert (const QwtLegendItemManager *, QWidget *)
void remove (const QwtLegendItemManager *)
QWidget * find (const QwtLegendItemManager *) const
QwtLegendItemManagerfind (const QWidget *) const
virtual QValueList< QWidget * > legendItems () const
 Return a list of all legend items.
void clear ()
 Remove all items.
bool isEmpty () const
 Return true, if there are no legend items.
uint itemCount () const
 Return the number of legend items.
virtual bool eventFilter (QObject *, QEvent *)
virtual QSize sizeHint () const
 Return a size hint.
virtual int heightForWidth (int w) const
QScrollBar * horizontalScrollBar () const
QScrollBar * verticalScrollBar () const

Protected Member Functions

virtual void resizeEvent (QResizeEvent *)
virtual void layoutContents ()

Detailed Description

The legend widget.

The QwtLegend widget is a tabular arrangement of legend items. Legend items might be any type of widget, but in general they will be a QwtLegendItem.

See also:
QwtLegendItem, QwtLegendItemManager QwtPlot

Definition at line 36 of file qwt_legend.h.


Member Enumeration Documentation

Display policy.

  • NoIdentifier
    The client code is responsible how to display of each legend item. The Qwt library will not interfere.

Default is AutoIdentifier.

See also:
setDisplayPolicy(), displayPolicy(), QwtLegendItem::IdentifierMode
Enumerator:
NoIdentifier 
FixedIdentifier 
AutoIdentifier 

Definition at line 62 of file qwt_legend.h.

Interaction mode for the legend items.

  • ReadOnlyItem
    The legend item is not interactive, like a label
  • ClickableItem
    The legend item is clickable, like a push button
  • CheckableItem
    The legend item is checkable, like a checkable button

Default is ReadOnlyItem.

See also:
setItemMode(), itemMode(), QwtLegendItem::IdentifierMode QwtLegendItem::clicked(), QwtLegendItem::checked(), QwtPlot::legendClicked(), QwtPlot::legendChecked()
Enumerator:
ReadOnlyItem 
ClickableItem 
CheckableItem 

Definition at line 87 of file qwt_legend.h.


Constructor & Destructor Documentation

QwtLegend::QwtLegend ( QWidget *  parent = NULL ) [explicit]

Constructor

Parameters:
parentParent widget

Definition at line 260 of file qwt_legend.cpp.

References AutoIdentifier, QwtLegend::PrivateData::displayPolicy, QwtLegend::PrivateData::identifierMode, QwtLegend::PrivateData::itemMode, ReadOnlyItem, QwtLegendItem::ShowLine, QwtLegendItem::ShowSymbol, QwtLegendItem::ShowText, and QwtLegend::PrivateData::view.

                                   : 
    QFrame(parent)
{
    setFrameStyle(NoFrame);

    d_data = new QwtLegend::PrivateData;
    d_data->itemMode = QwtLegend::ReadOnlyItem;
    d_data->displayPolicy = QwtLegend::AutoIdentifier;
    d_data->identifierMode = QwtLegendItem::ShowLine | 
        QwtLegendItem::ShowSymbol | QwtLegendItem::ShowText;

    d_data->view = new QwtLegend::PrivateData::LegendView(this);
    d_data->view->setFrameStyle(NoFrame);

    QwtDynGridLayout *layout = new QwtDynGridLayout(
        d_data->view->contentsWidget);
#if QT_VERSION < 0x040000
    layout->setAutoAdd(true);
#endif
    layout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    d_data->view->contentsWidget->installEventFilter(this);
}
QwtLegend::~QwtLegend (  ) [virtual]

Destructor.

Definition at line 285 of file qwt_legend.cpp.

{
    delete d_data;
}

Member Function Documentation

void QwtLegend::clear (  )

Remove all items.

Definition at line 500 of file qwt_legend.cpp.

References QwtLegend::PrivateData::map.

{
#if QT_VERSION < 0x040000
    bool doUpdate = isUpdatesEnabled();
#else
    bool doUpdate = updatesEnabled();
#endif
    setUpdatesEnabled(false);

    d_data->map.clear();

    setUpdatesEnabled(doUpdate);
    update();
}
QWidget * QwtLegend::contentsWidget (  )

The contents widget is the only child of the viewport() and the parent widget of all legend items.

Definition at line 358 of file qwt_legend.cpp.

References QwtLegend::PrivateData::view.

Referenced by insert(), and QwtPlot::printLegend().

{ 
    return d_data->view->contentsWidget; 
}
const QWidget * QwtLegend::contentsWidget (  ) const

The contents widget is the only child of the viewport() and the parent widget of all legend items.

Definition at line 385 of file qwt_legend.cpp.

References QwtLegend::PrivateData::view.

{ 
    return d_data->view->contentsWidget; 
}
QwtLegend::LegendDisplayPolicy QwtLegend::displayPolicy (  ) const
Returns:
the legend display policy. Default is LegendDisplayPolicy::Auto.
See also:
setDisplayPolicy(), LegendDisplayPolicy

Definition at line 326 of file qwt_legend.cpp.

References QwtLegend::PrivateData::displayPolicy.

Referenced by QwtPlotCurve::updateLegend().

{ 
    return d_data->displayPolicy; 
}
bool QwtLegend::eventFilter ( QObject *  o,
QEvent *  e 
) [virtual]

Filter layout related events of QwtLegend::contentsWidget().

Parameters:
oObject to be filtered
eEvent

Definition at line 587 of file qwt_legend.cpp.

References layoutContents(), QwtLegend::PrivateData::map, and QwtLegend::PrivateData::view.

{
    if ( o == d_data->view->contentsWidget )
    {
        switch(e->type())
        {
            case QEvent::ChildRemoved:
            {   
                const QChildEvent *ce = (const QChildEvent *)e;
                if ( ce->child()->isWidgetType() )
                    d_data->map.remove((QWidget *)ce->child());
                break;
            }
#if QT_VERSION < 0x040000
            case QEvent::LayoutHint:
#else
            case QEvent::LayoutRequest:
#endif
            {
                layoutContents();
                break;
            }
#if QT_VERSION < 0x040000
            case QEvent::Resize:
            {
                updateGeometry();
                break;
            }
#endif
            default:
                break;
        }
    }
    
    return QFrame::eventFilter(o, e);
}
QWidget * QwtLegend::find ( const QwtLegendItemManager plotItem ) const

Find the widget that represents a plot item

Parameters:
plotItemPlot item
Returns:
Widget on the legend, or NULL

Definition at line 470 of file qwt_legend.cpp.

References QwtLegend::PrivateData::map.

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

{
    return d_data->map.find(plotItem);
}
QwtLegendItemManager * QwtLegend::find ( const QWidget *  legendItem ) const

Find the widget that represents a plot item

Parameters:
legendItemLegend item
Returns:
Widget on the legend, or NULL

Definition at line 481 of file qwt_legend.cpp.

References QwtLegend::PrivateData::map.

{
    return d_data->map.find(legendItem);
}
int QwtLegend::heightForWidth ( int  width ) const [virtual]
Returns:
The preferred height, for the width w.
Parameters:
widthWidth

Definition at line 528 of file qwt_legend.cpp.

References QwtLegend::PrivateData::view.

Referenced by QwtPlotLayout::LayoutData::init(), and QwtPlotLayout::minimumSizeHint().

{
    width -= 2 * frameWidth();

    int h = d_data->view->contentsWidget->heightForWidth(width);
#if QT_VERSION < 0x040000

    // Asking the layout is the default implementation in Qt4 

    if ( h <= 0 ) 
    {
        QLayout *l = d_data->view->contentsWidget->layout();
        if ( l && l->hasHeightForWidth() )
            h = l->heightForWidth(width);
    }
#endif
    if ( h >= 0 )
        h += 2 * frameWidth();

    return h;
}
QScrollBar * QwtLegend::horizontalScrollBar (  ) const
Returns:
Horizontal scrollbar
See also:
verticalScrollBar()

Definition at line 367 of file qwt_legend.cpp.

References QwtLegend::PrivateData::view.

Referenced by QwtPlotLayout::LayoutData::init().

{
    return d_data->view->horizontalScrollBar();
}
int QwtLegend::identifierMode (  ) const
Returns:
the IdentifierMode to be used in combination with LegendDisplayPolicy::Fixed.

Default is ShowLine | ShowSymbol | ShowText.

Definition at line 349 of file qwt_legend.cpp.

References QwtLegend::PrivateData::identifierMode.

Referenced by QwtPlotCurve::updateLegend().

{
    return d_data->identifierMode;
}
void QwtLegend::insert ( const QwtLegendItemManager plotItem,
QWidget *  legendItem 
)

Insert a new item for a plot item

Parameters:
plotItemPlot item
legendItemNew legend item
Note:
The parent of item will be changed to QwtLegend::contentsWidget()

Definition at line 396 of file qwt_legend.cpp.

References contentsWidget(), layoutContents(), QwtLegend::PrivateData::map, and QwtLegend::PrivateData::view.

Referenced by QwtPlotItem::updateLegend().

{
    if ( legendItem == NULL || plotItem == NULL )
        return;

    QWidget *contentsWidget = d_data->view->contentsWidget;

    if ( legendItem->parent() != contentsWidget )
    {
#if QT_VERSION >= 0x040000
        legendItem->setParent(contentsWidget);
#else
        legendItem->reparent(contentsWidget, QPoint(0, 0));
#endif
    }

    legendItem->show();

    d_data->map.insert(plotItem, legendItem);

    layoutContents();

    if ( contentsWidget->layout() )
    {
#if QT_VERSION >= 0x040000
        contentsWidget->layout()->addWidget(legendItem);
#endif

        // set tab focus chain

        QWidget *w = NULL;

#if QT_VERSION < 0x040000
        QLayoutIterator layoutIterator = 
            contentsWidget->layout()->iterator();
        for ( QLayoutItem *item = layoutIterator.current();
            item != 0; item = ++layoutIterator)
        {
#else
        for (int i = 0; i < contentsWidget->layout()->count(); i++)
        {
            QLayoutItem *item = contentsWidget->layout()->itemAt(i);
#endif
            if ( w && item->widget() )
            {
                QWidget::setTabOrder(w, item->widget());
                w = item->widget();
            }
        }
    }
    if ( parentWidget() && parentWidget()->layout() == NULL )
    {
       /*
          updateGeometry() doesn't post LayoutRequest in certain
          situations, like when we are hidden. But we want the
          parent widget notified, so it can show/hide the legend
          depending on its items.
        */
#if QT_VERSION < 0x040000
        QApplication::postEvent(parentWidget(),
            new QEvent(QEvent::LayoutHint));
#else
        QApplication::postEvent(parentWidget(),
            new QEvent(QEvent::LayoutRequest));
#endif
    }
}
bool QwtLegend::isEmpty (  ) const

Return true, if there are no legend items.

Definition at line 626 of file qwt_legend.cpp.

References QwtLegend::PrivateData::map.

Referenced by QwtPlotLayout::activate(), QwtPlotLayout::minimumSizeHint(), and QwtPlot::print().

{
    return d_data->map.count() == 0;
}
uint QwtLegend::itemCount (  ) const

Return the number of legend items.

Definition at line 632 of file qwt_legend.cpp.

References QwtLegend::PrivateData::map.

{
    return d_data->map.count();
}
QwtLegend::LegendItemMode QwtLegend::itemMode (  ) const
See also:
LegendItemMode

Definition at line 338 of file qwt_legend.cpp.

References QwtLegend::PrivateData::itemMode.

Referenced by QwtPlotItem::updateLegend().

{
    return d_data->itemMode;
}
void QwtLegend::layoutContents (  ) [protected, virtual]

Adjust contents widget and item layout to the size of the viewport().

Definition at line 553 of file qwt_legend.cpp.

References QwtDynGridLayout::heightForWidth(), int(), QwtDynGridLayout::maxItemWidth(), qwtMax, and QwtLegend::PrivateData::view.

Referenced by eventFilter(), and insert().

{
    const QSize visibleSize = d_data->view->viewport()->size();

    const QLayout *l = d_data->view->contentsWidget->layout();
    if ( l && l->inherits("QwtDynGridLayout") )
    {
        const QwtDynGridLayout *tl = (const QwtDynGridLayout *)l;

        const int minW = int(tl->maxItemWidth()) + 2 * tl->margin();

        int w = qwtMax(visibleSize.width(), minW);
        int h = qwtMax(tl->heightForWidth(w), visibleSize.height());

        const int vpWidth = d_data->view->viewportSize(w, h).width();
        if ( w > vpWidth )
        {
            w = qwtMax(vpWidth, minW);
            h = qwtMax(tl->heightForWidth(w), visibleSize.height());
        }

        d_data->view->contentsWidget->resize(w, h);
#if QT_VERSION < 0x040000
        d_data->view->resizeContents(w, h);
#endif
    }
}
QValueList< QWidget * > QwtLegend::legendItems (  ) const [virtual]

Return a list of all legend items.

Definition at line 639 of file qwt_legend.cpp.

References QwtLegend::PrivateData::map.

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

{
    const QMap<QWidget *, const QwtLegendItemManager *> &map = 
        d_data->map.widgetMap();

#if QT_VERSION < 0x040000
    QValueList<QWidget *> list;
#else
    QList<QWidget *> list;
#endif

    QMap<QWidget *, const QwtLegendItemManager *>::const_iterator it;
    for ( it = map.begin(); it != map.end(); ++it ) 
        list += it.key();

    return list;
}
void QwtLegend::remove ( const QwtLegendItemManager plotItem )

Find the corresponding item for a plotItem and remove it from the item list.

Parameters:
plotItemPlot item

Definition at line 492 of file qwt_legend.cpp.

References QwtLegend::PrivateData::map.

{ 
    QWidget *legendItem = d_data->map.find(plotItem);
    d_data->map.remove(legendItem); 
    delete legendItem;
}
void QwtLegend::resizeEvent ( QResizeEvent *  e ) [protected, virtual]

Resize event

Parameters:
eResize event

Definition at line 664 of file qwt_legend.cpp.

References QwtLegend::PrivateData::view.

{
    QFrame::resizeEvent(e);
    d_data->view->setGeometry(contentsRect());
}
void QwtLegend::setDisplayPolicy ( LegendDisplayPolicy  policy,
int  mode 
)

Set the legend display policy to:

Parameters:
policyLegend display policy
modeIdentifier mode (or'd ShowLine, ShowSymbol, ShowText)
See also:
displayPolicy(), LegendDisplayPolicy

Definition at line 298 of file qwt_legend.cpp.

References QwtLegend::PrivateData::displayPolicy, QwtLegend::PrivateData::identifierMode, QwtLegend::PrivateData::map, and QwtLegendItemManager::updateLegend().

{
    d_data->displayPolicy = policy;
    if (-1 != mode)
       d_data->identifierMode = mode;

    QMap<QWidget *, const QwtLegendItemManager *> &map = 
        d_data->map.widgetMap();

    QMap<QWidget *, const QwtLegendItemManager *>::iterator it;
    for ( it = map.begin(); it != map.end(); ++it ) 
    {
#if QT_VERSION < 0x040000
        QwtLegendItemManager *item = (QwtLegendItemManager *)it.data();
#else
        QwtLegendItemManager *item = (QwtLegendItemManager *)it.value();
#endif
        if ( item )
            item->updateLegend(this);
    }
}
void QwtLegend::setItemMode ( LegendItemMode  mode )
See also:
LegendItemMode

Definition at line 332 of file qwt_legend.cpp.

References QwtLegend::PrivateData::itemMode.

{
    d_data->itemMode = mode;
}
QSize QwtLegend::sizeHint (  ) const [virtual]

Return a size hint.

Definition at line 516 of file qwt_legend.cpp.

References QwtLegend::PrivateData::view.

Referenced by QwtPlotLayout::LayoutData::init(), and QwtPlotLayout::minimumSizeHint().

{
    QSize hint = d_data->view->contentsWidget->sizeHint();
    hint += QSize(2 * frameWidth(), 2 * frameWidth());

    return hint;
}
QScrollBar * QwtLegend::verticalScrollBar (  ) const
Returns:
Vertical scrollbar
See also:
horizontalScrollBar()

Definition at line 376 of file qwt_legend.cpp.

References QwtLegend::PrivateData::view.

Referenced by QwtPlotLayout::LayoutData::init(), and QwtPlotLayout::minimumSizeHint().

{
    return d_data->view->verticalScrollBar();
}

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