| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- ***************************** * 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_legend_item.h"#include "qwt_math.h"#include "qwt_painter.h"#include "qwt_symbol.h"#include <qpainter.h>#include <qdrawutil.h>#include <qstyle.h>#include <qpen.h>#include <qevent.h>#include <qstyleoption.h>static const int ButtonFrame = 2;static const int Margin = 2;static QSize buttonShift( const QwtLegendItem *w ){    QStyleOption option;    option.init( w );    const int ph = w->style()->pixelMetric(        QStyle::PM_ButtonShiftHorizontal, &option, w );    const int pv = w->style()->pixelMetric(        QStyle::PM_ButtonShiftVertical, &option, w );    return QSize( ph, pv );}class QwtLegendItem::PrivateData{public:    PrivateData():        itemMode( QwtLegend::ReadOnlyItem ),        isDown( false ),        identifierSize( 8, 8 ),        spacing( Margin )    {    }    QwtLegend::LegendItemMode itemMode;    bool isDown;    QSize identifierSize;    QPixmap identifier;    int spacing;};/*!  \param parent Parent widget*/QwtLegendItem::QwtLegendItem( QWidget *parent ):    QwtTextLabel( parent ){    d_data = new PrivateData;    setMargin( Margin );    setIndent( Margin + d_data->identifierSize.width() + 2 * d_data->spacing );}//! DestructorQwtLegendItem::~QwtLegendItem(){    delete d_data;    d_data = NULL;}/*!   Set the text to the legend item   \param text Text label    \sa QwtTextLabel::text()*/void QwtLegendItem::setText( const QwtText &text ){    const int flags = Qt::AlignLeft | Qt::AlignVCenter        | Qt::TextExpandTabs | Qt::TextWordWrap;    QwtText txt = text;    txt.setRenderFlags( flags );    QwtTextLabel::setText( txt );}/*!   Set the item mode   The default is QwtLegend::ReadOnlyItem   \param mode Item mode   \sa itemMode()*/void QwtLegendItem::setItemMode( QwtLegend::LegendItemMode mode ){    if ( mode != d_data->itemMode )    {        d_data->itemMode = mode;        d_data->isDown = false;        setFocusPolicy( mode != QwtLegend::ReadOnlyItem ? Qt::TabFocus : Qt::NoFocus );        setMargin( ButtonFrame + Margin );        updateGeometry();    }}/*!   Return the item mode   \sa setItemMode()*/QwtLegend::LegendItemMode QwtLegendItem::itemMode() const{    return d_data->itemMode;}/*!  Assign the identifier  The identifier needs to be created according to the identifierWidth()  \param identifier Pixmap representing a plot item  \sa identifier(), identifierWidth()*/void QwtLegendItem::setIdentifier( const QPixmap &identifier ){    d_data->identifier = identifier;    update();}/*!  \return pixmap representing a plot item  \sa setIdentifier()*/QPixmap QwtLegendItem::identifier() const{    return d_data->identifier;}/*!  Set the size for the identifier  Default is 8x8 pixels  \param size New size  \sa identifierSize()*/void QwtLegendItem::setIdentifierSize( const QSize &size ){    QSize sz = size.expandedTo( QSize( 0, 0 ) );    if ( sz != d_data->identifierSize )    {        d_data->identifierSize = sz;        setIndent( margin() + d_data->identifierSize.width()            + 2 * d_data->spacing );        updateGeometry();    }}/*!   Return the width of the identifier   \sa setIdentifierSize()*/QSize QwtLegendItem::identifierSize() const{    return d_data->identifierSize;}/*!   Change the spacing   \param spacing Spacing   \sa spacing(), identifierWidth(), QwtTextLabel::margin()*/void QwtLegendItem::setSpacing( int spacing ){    spacing = qMax( spacing, 0 );    if ( spacing != d_data->spacing )    {        d_data->spacing = spacing;        setIndent( margin() + d_data->identifierSize.width()            + 2 * d_data->spacing );    }}/*!   Return the spacing   \sa setSpacing(), identifierWidth(), QwtTextLabel::margin()*/int QwtLegendItem::spacing() const{    return d_data->spacing;}/*!    Check/Uncheck a the item    \param on check/uncheck    \sa setItemMode()*/void QwtLegendItem::setChecked( bool on ){    if ( d_data->itemMode == QwtLegend::CheckableItem )    {        const bool isBlocked = signalsBlocked();        blockSignals( true );        setDown( on );        blockSignals( isBlocked );    }}//! Return true, if the item is checkedbool QwtLegendItem::isChecked() const{    return d_data->itemMode == QwtLegend::CheckableItem && isDown();}//! Set the item being downvoid QwtLegendItem::setDown( bool down ){    if ( down == d_data->isDown )        return;    d_data->isDown = down;    update();    if ( d_data->itemMode == QwtLegend::ClickableItem )    {        if ( d_data->isDown )            Q_EMIT pressed();        else        {            Q_EMIT released();            Q_EMIT clicked();        }    }    if ( d_data->itemMode == QwtLegend::CheckableItem )        Q_EMIT checked( d_data->isDown );}//! Return true, if the item is downbool QwtLegendItem::isDown() const{    return d_data->isDown;}//! Return a size hintQSize QwtLegendItem::sizeHint() const{    QSize sz = QwtTextLabel::sizeHint();    sz.setHeight( qMax( sz.height(), d_data->identifier.height() + 4 ) );    if ( d_data->itemMode != QwtLegend::ReadOnlyItem )        sz += buttonShift( this );    return sz;}//! Paint eventvoid QwtLegendItem::paintEvent( QPaintEvent *e ){    const QRect cr = contentsRect();    QPainter painter( this );    painter.setClipRegion( e->region() );    if ( d_data->isDown )    {        qDrawWinButton( &painter, 0, 0, width(), height(),            palette(), true );    }    painter.save();    if ( d_data->isDown )    {        const QSize shiftSize = buttonShift( this );        painter.translate( shiftSize.width(), shiftSize.height() );    }    painter.setClipRect( cr );    drawContents( &painter );    if ( !d_data->identifier.isNull() )    {        QRect identRect = cr;        identRect.setX( identRect.x() + margin() );        if ( d_data->itemMode != QwtLegend::ReadOnlyItem )            identRect.setX( identRect.x() + ButtonFrame );        identRect.setSize( d_data->identifier.size() );        identRect.moveCenter( QPoint( identRect.center().x(), cr.center().y() ) );        painter.drawPixmap( identRect, d_data->identifier );    }    painter.restore();}//! Handle mouse press eventsvoid QwtLegendItem::mousePressEvent( QMouseEvent *e ){    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 );}//! Handle mouse release eventsvoid QwtLegendItem::mouseReleaseEvent( QMouseEvent *e ){    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 );}//! Handle key press eventsvoid QwtLegendItem::keyPressEvent( QKeyEvent *e ){    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 );}//! Handle key release eventsvoid QwtLegendItem::keyReleaseEvent( QKeyEvent *e ){    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 );}
 |