Loading...
Searching...
No Matches
qwt_legend_label.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_legend_label.h"
#include "qwt_legend_data.h"
#include "qwt_graphic.h"
#include "qwt_painter.h"
#include "qwt.h"
#include <qpainter.h>
#include <qdrawutil.h>
#include <qstyle.h>
#include <qevent.h>
#include <qstyleoption.h>
static const int ButtonFrame = 2;
static const int Margin = 2;
static QSize buttonShift( const QwtLegendLabel* w )
{
QStyleOption option;
option.initFrom( 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 QwtLegendLabel::PrivateData
{
public:
PrivateData()
: itemMode( QwtLegendData::ReadOnly )
, isDown( false )
, spacing( Margin )
{
}
QwtLegendData::Mode itemMode;
QwtLegendData legendData;
bool isDown;
QPixmap icon;
int spacing;
};
void QwtLegendLabel::setData( const QwtLegendData& legendData )
{
m_data->legendData = legendData;
const bool doUpdate = updatesEnabled();
if ( doUpdate )
setUpdatesEnabled( false );
setText( legendData.title() );
setIcon( legendData.icon().toPixmap() );
if ( legendData.hasRole( QwtLegendData::ModeRole ) )
setItemMode( legendData.mode() );
if ( doUpdate )
setUpdatesEnabled( true );
}
const QwtLegendData& QwtLegendLabel::data() const
{
return m_data->legendData;
}
QwtLegendLabel::QwtLegendLabel( QWidget* parent )
: QwtTextLabel( parent )
{
m_data = new PrivateData;
setMargin( Margin );
setIndent( Margin );
}
QwtLegendLabel::~QwtLegendLabel()
{
delete m_data;
m_data = NULL;
}
void QwtLegendLabel::setText( const QwtText& text )
{
const int flags = Qt::AlignLeft | Qt::AlignVCenter
| Qt::TextExpandTabs | Qt::TextWordWrap;
QwtText txt = text;
txt.setRenderFlags( flags );
QwtTextLabel::setText( txt );
}
void QwtLegendLabel::setItemMode( QwtLegendData::Mode mode )
{
if ( mode != m_data->itemMode )
{
m_data->itemMode = mode;
m_data->isDown = false;
setFocusPolicy( ( mode != QwtLegendData::ReadOnly )
? Qt::TabFocus : Qt::NoFocus );
setMargin( ButtonFrame + Margin );
updateGeometry();
}
}
QwtLegendData::Mode QwtLegendLabel::itemMode() const
{
return m_data->itemMode;
}
void QwtLegendLabel::setIcon( const QPixmap& icon )
{
m_data->icon = icon;
int indent = margin() + m_data->spacing;
if ( icon.width() > 0 )
{
const qreal devicePixelRatio = QwtPainter::devicePixelRatio( &icon );
indent += icon.width() / devicePixelRatio + m_data->spacing;
}
setIndent( indent );
}
QPixmap QwtLegendLabel::icon() const
{
return m_data->icon;
}
void QwtLegendLabel::setSpacing( int spacing )
{
spacing = qMax( spacing, 0 );
if ( spacing != m_data->spacing )
{
m_data->spacing = spacing;
int indent = margin() + m_data->spacing;
if ( m_data->icon.width() > 0 )
indent += m_data->icon.width() + m_data->spacing;
setIndent( indent );
}
}
int QwtLegendLabel::spacing() const
{
return m_data->spacing;
}
void QwtLegendLabel::setChecked( bool on )
{
if ( m_data->itemMode == QwtLegendData::Checkable )
{
const bool isBlocked = signalsBlocked();
blockSignals( true );
setDown( on );
blockSignals( isBlocked );
}
}
bool QwtLegendLabel::isChecked() const
{
return m_data->itemMode == QwtLegendData::Checkable && isDown();
}
void QwtLegendLabel::setDown( bool down )
{
if ( down == m_data->isDown )
return;
m_data->isDown = down;
update();
if ( m_data->itemMode == QwtLegendData::Clickable )
{
if ( m_data->isDown )
Q_EMIT pressed();
else
{
Q_EMIT released();
Q_EMIT clicked();
}
}
if ( m_data->itemMode == QwtLegendData::Checkable )
Q_EMIT checked( m_data->isDown );
}
bool QwtLegendLabel::isDown() const
{
return m_data->isDown;
}
QSize QwtLegendLabel::sizeHint() const
{
QSize sz = QwtTextLabel::sizeHint();
sz.setHeight( qMax( sz.height(), m_data->icon.height() + 4 ) );
if ( m_data->itemMode != QwtLegendData::ReadOnly )
{
sz += buttonShift( this );
sz = qwtExpandedToGlobalStrut( sz );
}
return sz;
}
void QwtLegendLabel::paintEvent( QPaintEvent* e )
{
const QRect cr = contentsRect();
QPainter painter( this );
painter.setClipRegion( e->region() );
if ( m_data->isDown )
{
qDrawWinButton( &painter, 0, 0, width(), height(),
palette(), true );
}
painter.save();
if ( m_data->isDown )
{
const QSize shiftSize = buttonShift( this );
painter.translate( shiftSize.width(), shiftSize.height() );
}
painter.setClipRect( cr );
drawContents( &painter );
if ( !m_data->icon.isNull() )
{
QRect iconRect = cr;
iconRect.setX( iconRect.x() + margin() );
if ( m_data->itemMode != QwtLegendData::ReadOnly )
iconRect.setX( iconRect.x() + ButtonFrame );
const qreal devicePixelRatio = QwtPainter::devicePixelRatio( &m_data->icon );
iconRect.setSize( m_data->icon.size() / devicePixelRatio );
iconRect.moveCenter( QPoint( iconRect.center().x(), cr.center().y() ) );
painter.drawPixmap( iconRect, m_data->icon );
}
painter.restore();
}
void QwtLegendLabel::mousePressEvent( QMouseEvent* e )
{
if ( e->button() == Qt::LeftButton )
{
switch ( m_data->itemMode )
{
case QwtLegendData::Clickable:
{
setDown( true );
return;
}
case QwtLegendData::Checkable:
{
setDown( !isDown() );
return;
}
default:;
}
}
QwtTextLabel::mousePressEvent( e );
}
void QwtLegendLabel::mouseReleaseEvent( QMouseEvent* e )
{
if ( e->button() == Qt::LeftButton )
{
switch ( m_data->itemMode )
{
case QwtLegendData::Clickable:
{
setDown( false );
return;
}
case QwtLegendData::Checkable:
{
return; // do nothing, but accept
}
default:;
}
}
QwtTextLabel::mouseReleaseEvent( e );
}
void QwtLegendLabel::keyPressEvent( QKeyEvent* e )
{
if ( e->key() == Qt::Key_Space )
{
switch ( m_data->itemMode )
{
case QwtLegendData::Clickable:
{
if ( !e->isAutoRepeat() )
setDown( true );
return;
}
case QwtLegendData::Checkable:
{
if ( !e->isAutoRepeat() )
setDown( !isDown() );
return;
}
default:;
}
}
QwtTextLabel::keyPressEvent( e );
}
void QwtLegendLabel::keyReleaseEvent( QKeyEvent* e )
{
if ( e->key() == Qt::Key_Space )
{
switch ( m_data->itemMode )
{
case QwtLegendData::Clickable:
{
if ( !e->isAutoRepeat() )
setDown( false );
return;
}
case QwtLegendData::Checkable:
{
return; // do nothing, but accept
}
default:;
}
}
QwtTextLabel::keyReleaseEvent( e );
}
#include "moc_qwt_legend_label.cpp"
QwtGraphic::toPixmap
QPixmap toPixmap(qreal devicePixelRatio=0.0) const
Convert the graphic to a QPixmap.
Definition qwt_graphic.cpp:812
QwtLegendData
Attributes of an entry on a legend.
Definition qwt_legend_data.h:37
QwtLegendData::mode
Mode mode() const
Definition qwt_legend_data.cpp:120
QwtLegendData::Mode
Mode
Mode defining how a legend entry interacts.
Definition qwt_legend_data.h:41
QwtLegendData::Checkable
@ Checkable
The legend item is checkable, like a checkable button.
Definition qwt_legend_data.h:49
QwtLegendData::Clickable
@ Clickable
The legend item is clickable, like a push button.
Definition qwt_legend_data.h:46
QwtLegendData::ReadOnly
@ ReadOnly
The legend item is not interactive, like a label.
Definition qwt_legend_data.h:43
QwtLegendData::hasRole
bool hasRole(int role) const
Definition qwt_legend_data.cpp:51
QwtLegendData::icon
QwtGraphic icon() const
Definition qwt_legend_data.cpp:106
QwtLegendData::title
QwtText title() const
Definition qwt_legend_data.cpp:88
QwtLegendLabel
A widget representing something on a QwtLegend.
Definition qwt_legend_label.h:23
QwtLegendLabel::sizeHint
virtual QSize sizeHint() const override
Return a size hint.
Definition qwt_legend_label.cpp:271
QwtLegendLabel::checked
void checked(bool)
Signal, when the legend item has been toggled.
QwtLegendLabel::keyPressEvent
virtual void keyPressEvent(QKeyEvent *) override
Handle key press events.
Definition qwt_legend_label.cpp:374
QwtLegendLabel::setItemMode
void setItemMode(QwtLegendData::Mode)
Definition qwt_legend_label.cpp:131
QwtLegendLabel::spacing
int spacing() const
Definition qwt_legend_label.cpp:210
QwtLegendLabel::QwtLegendLabel
QwtLegendLabel(QWidget *parent=0)
Definition qwt_legend_label.cpp:92
QwtLegendLabel::isDown
bool isDown() const
Return true, if the item is down.
Definition qwt_legend_label.cpp:265
QwtLegendLabel::released
void released()
Signal, when the legend item has been released.
QwtLegendLabel::setText
virtual void setText(const QwtText &) override
Definition qwt_legend_label.cpp:113
QwtLegendLabel::itemMode
QwtLegendData::Mode itemMode() const
Definition qwt_legend_label.cpp:150
QwtLegendLabel::data
const QwtLegendData & data() const
Definition qwt_legend_label.cpp:84
QwtLegendLabel::icon
QPixmap icon() const
Definition qwt_legend_label.cpp:180
QwtLegendLabel::paintEvent
virtual void paintEvent(QPaintEvent *) override
Paint event.
Definition qwt_legend_label.cpp:286
QwtLegendLabel::setData
void setData(const QwtLegendData &)
Definition qwt_legend_label.cpp:62
QwtLegendLabel::setSpacing
void setSpacing(int spacing)
Change the spacing between icon and text.
Definition qwt_legend_label.cpp:191
QwtLegendLabel::setDown
void setDown(bool)
Set the item being down.
Definition qwt_legend_label.cpp:241
QwtLegendLabel::~QwtLegendLabel
virtual ~QwtLegendLabel()
Destructor.
Definition qwt_legend_label.cpp:101
QwtLegendLabel::mousePressEvent
virtual void mousePressEvent(QMouseEvent *) override
Handle mouse press events.
Definition qwt_legend_label.cpp:329
QwtLegendLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *) override
Handle mouse release events.
Definition qwt_legend_label.cpp:352
QwtLegendLabel::clicked
void clicked()
Signal, when the legend item has been clicked.
QwtLegendLabel::pressed
void pressed()
Signal, when the legend item has been pressed.
QwtLegendLabel::setIcon
void setIcon(const QPixmap &)
Definition qwt_legend_label.cpp:162
QwtLegendLabel::setChecked
void setChecked(bool on)
Definition qwt_legend_label.cpp:221
QwtLegendLabel::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *) override
Handle key release events.
Definition qwt_legend_label.cpp:400
QwtLegendLabel::isChecked
bool isChecked() const
Return true, if the item is checked.
Definition qwt_legend_label.cpp:235
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition qwt_painter.cpp:1481
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtText::setRenderFlags
void setRenderFlags(int)
Change the render flags.
Definition qwt_text.cpp:304
QwtTextLabel
A Widget which displays a QwtText.
Definition qwt_text_label.h:27
QwtTextLabel::indent
int indent() const
Return label's text indent in pixels.
Definition qwt_text_label.cpp:133
QwtTextLabel::margin
int margin() const
Return label's text margin in pixels.
Definition qwt_text_label.cpp:154
QwtTextLabel::text
const QwtText & text() const
Return the text.
Definition qwt_text_label.cpp:118
QwtTextLabel::sizeHint
virtual QSize sizeHint() const override
Return a size hint.
Definition qwt_text_label.cpp:172
QwtTextLabel::setMargin
void setMargin(int)
Definition qwt_text_label.cpp:163
QwtTextLabel::setIndent
void setIndent(int)
Definition qwt_text_label.cpp:142
QwtTextLabel::drawContents
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
Definition qwt_text_label.cpp:257
QwtTextLabel::setText
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)
Definition qwt_text_label.cpp:96