qwt_legend_label.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_legend_label.h"
00011 #include "qwt_legend_data.h"
00012 #include "qwt_math.h"
00013 #include "qwt_painter.h"
00014 #include "qwt_symbol.h"
00015 #include "qwt_graphic.h"
00016 #include <qpainter.h>
00017 #include <qdrawutil.h>
00018 #include <qstyle.h>
00019 #include <qpen.h>
00020 #include <qevent.h>
00021 #include <qstyleoption.h>
00022 #include <qapplication.h>
00023 
00024 static const int ButtonFrame = 2;
00025 static const int Margin = 2;
00026 
00027 static QSize buttonShift( const QwtLegendLabel *w )
00028 {
00029     QStyleOption option;
00030     option.init( w );
00031 
00032     const int ph = w->style()->pixelMetric(
00033         QStyle::PM_ButtonShiftHorizontal, &option, w );
00034     const int pv = w->style()->pixelMetric(
00035         QStyle::PM_ButtonShiftVertical, &option, w );
00036     return QSize( ph, pv );
00037 }
00038 
00039 class QwtLegendLabel::PrivateData
00040 {
00041 public:
00042     PrivateData():
00043         itemMode( QwtLegendData::ReadOnly ),
00044         isDown( false ),
00045         spacing( Margin )
00046     {
00047     }
00048 
00049     QwtLegendData::Mode itemMode;
00050     QwtLegendData legendData;
00051     bool isDown;
00052 
00053     QPixmap icon;
00054 
00055     int spacing;
00056 };
00057 
00064 void QwtLegendLabel::setData( const QwtLegendData &legendData )
00065 {
00066     d_data->legendData = legendData;
00067 
00068     const bool doUpdate = updatesEnabled();
00069     if ( doUpdate )
00070         setUpdatesEnabled( false );
00071 
00072     setText( legendData.title() );
00073     setIcon( legendData.icon().toPixmap() );
00074 
00075     if ( legendData.hasRole( QwtLegendData::ModeRole ) )
00076         setItemMode( legendData.mode() );
00077 
00078     if ( doUpdate )
00079         setUpdatesEnabled( true );
00080 }
00081 
00086 const QwtLegendData &QwtLegendLabel::data() const
00087 {
00088     return d_data->legendData;
00089 }
00090 
00094 QwtLegendLabel::QwtLegendLabel( QWidget *parent ):
00095     QwtTextLabel( parent )
00096 {
00097     d_data = new PrivateData;
00098     setMargin( Margin );
00099     setIndent( Margin );
00100 }
00101 
00103 QwtLegendLabel::~QwtLegendLabel()
00104 {
00105     delete d_data;
00106     d_data = NULL;
00107 }
00108 
00115 void QwtLegendLabel::setText( const QwtText &text )
00116 {
00117     const int flags = Qt::AlignLeft | Qt::AlignVCenter
00118         | Qt::TextExpandTabs | Qt::TextWordWrap;
00119 
00120     QwtText txt = text;
00121     txt.setRenderFlags( flags );
00122 
00123     QwtTextLabel::setText( txt );
00124 }
00125 
00133 void QwtLegendLabel::setItemMode( QwtLegendData::Mode mode )
00134 {
00135     if ( mode != d_data->itemMode )
00136     {
00137         d_data->itemMode = mode;
00138         d_data->isDown = false;
00139 
00140         setFocusPolicy( ( mode != QwtLegendData::ReadOnly ) 
00141             ? Qt::TabFocus : Qt::NoFocus );
00142         setMargin( ButtonFrame + Margin );
00143 
00144         updateGeometry();
00145     }
00146 }
00147 
00152 QwtLegendData::Mode QwtLegendLabel::itemMode() const
00153 {
00154     return d_data->itemMode;
00155 }
00156 
00164 void QwtLegendLabel::setIcon( const QPixmap &icon )
00165 {
00166     d_data->icon = icon;
00167 
00168     int indent = margin() + d_data->spacing;
00169     if ( icon.width() > 0 )
00170         indent += icon.width() + d_data->spacing;
00171 
00172     setIndent( indent );
00173 }
00174 
00179 QPixmap QwtLegendLabel::icon() const
00180 {
00181     return d_data->icon;
00182 }
00183 
00190 void QwtLegendLabel::setSpacing( int spacing )
00191 {
00192     spacing = qMax( spacing, 0 );
00193     if ( spacing != d_data->spacing )
00194     {
00195         d_data->spacing = spacing;
00196 
00197         int indent = margin() + d_data->spacing;
00198         if ( d_data->icon.width() > 0 )
00199             indent += d_data->icon.width() + d_data->spacing;
00200 
00201         setIndent( indent );
00202     }
00203 }
00204 
00209 int QwtLegendLabel::spacing() const
00210 {
00211     return d_data->spacing;
00212 }
00213 
00220 void QwtLegendLabel::setChecked( bool on )
00221 {
00222     if ( d_data->itemMode == QwtLegendData::Checkable )
00223     {
00224         const bool isBlocked = signalsBlocked();
00225         blockSignals( true );
00226 
00227         setDown( on );
00228 
00229         blockSignals( isBlocked );
00230     }
00231 }
00232 
00234 bool QwtLegendLabel::isChecked() const
00235 {
00236     return d_data->itemMode == QwtLegendData::Checkable && isDown();
00237 }
00238 
00240 void QwtLegendLabel::setDown( bool down )
00241 {
00242     if ( down == d_data->isDown )
00243         return;
00244 
00245     d_data->isDown = down;
00246     update();
00247 
00248     if ( d_data->itemMode == QwtLegendData::Clickable )
00249     {
00250         if ( d_data->isDown )
00251             Q_EMIT pressed();
00252         else
00253         {
00254             Q_EMIT released();
00255             Q_EMIT clicked();
00256         }
00257     }
00258 
00259     if ( d_data->itemMode == QwtLegendData::Checkable )
00260         Q_EMIT checked( d_data->isDown );
00261 }
00262 
00264 bool QwtLegendLabel::isDown() const
00265 {
00266     return d_data->isDown;
00267 }
00268 
00270 QSize QwtLegendLabel::sizeHint() const
00271 {
00272     QSize sz = QwtTextLabel::sizeHint();
00273     sz.setHeight( qMax( sz.height(), d_data->icon.height() + 4 ) );
00274 
00275     if ( d_data->itemMode != QwtLegendData::ReadOnly )
00276     {
00277         sz += buttonShift( this );
00278         sz = sz.expandedTo( QApplication::globalStrut() );
00279     }
00280 
00281     return sz;
00282 }
00283 
00285 void QwtLegendLabel::paintEvent( QPaintEvent *e )
00286 {
00287     const QRect cr = contentsRect();
00288 
00289     QPainter painter( this );
00290     painter.setClipRegion( e->region() );
00291 
00292     if ( d_data->isDown )
00293     {
00294         qDrawWinButton( &painter, 0, 0, width(), height(),
00295             palette(), true );
00296     }
00297 
00298     painter.save();
00299 
00300     if ( d_data->isDown )
00301     {
00302         const QSize shiftSize = buttonShift( this );
00303         painter.translate( shiftSize.width(), shiftSize.height() );
00304     }
00305 
00306     painter.setClipRect( cr );
00307 
00308     drawContents( &painter );
00309 
00310     if ( !d_data->icon.isNull() )
00311     {
00312         QRect iconRect = cr;
00313         iconRect.setX( iconRect.x() + margin() );
00314         if ( d_data->itemMode != QwtLegendData::ReadOnly )
00315             iconRect.setX( iconRect.x() + ButtonFrame );
00316 
00317         iconRect.setSize( d_data->icon.size() );
00318         iconRect.moveCenter( QPoint( iconRect.center().x(), cr.center().y() ) );
00319 
00320         painter.drawPixmap( iconRect, d_data->icon );
00321     }
00322 
00323     painter.restore();
00324 }
00325 
00327 void QwtLegendLabel::mousePressEvent( QMouseEvent *e )
00328 {
00329     if ( e->button() == Qt::LeftButton )
00330     {
00331         switch ( d_data->itemMode )
00332         {
00333             case QwtLegendData::Clickable:
00334             {
00335                 setDown( true );
00336                 return;
00337             }
00338             case QwtLegendData::Checkable:
00339             {
00340                 setDown( !isDown() );
00341                 return;
00342             }
00343             default:;
00344         }
00345     }
00346     QwtTextLabel::mousePressEvent( e );
00347 }
00348 
00350 void QwtLegendLabel::mouseReleaseEvent( QMouseEvent *e )
00351 {
00352     if ( e->button() == Qt::LeftButton )
00353     {
00354         switch ( d_data->itemMode )
00355         {
00356             case QwtLegendData::Clickable:
00357             {
00358                 setDown( false );
00359                 return;
00360             }
00361             case QwtLegendData::Checkable:
00362             {
00363                 return; // do nothing, but accept
00364             }
00365             default:;
00366         }
00367     }
00368     QwtTextLabel::mouseReleaseEvent( e );
00369 }
00370 
00372 void QwtLegendLabel::keyPressEvent( QKeyEvent *e )
00373 {
00374     if ( e->key() == Qt::Key_Space )
00375     {
00376         switch ( d_data->itemMode )
00377         {
00378             case QwtLegendData::Clickable:
00379             {
00380                 if ( !e->isAutoRepeat() )
00381                     setDown( true );
00382                 return;
00383             }
00384             case QwtLegendData::Checkable:
00385             {
00386                 if ( !e->isAutoRepeat() )
00387                     setDown( !isDown() );
00388                 return;
00389             }
00390             default:;
00391         }
00392     }
00393 
00394     QwtTextLabel::keyPressEvent( e );
00395 }
00396 
00398 void QwtLegendLabel::keyReleaseEvent( QKeyEvent *e )
00399 {
00400     if ( e->key() == Qt::Key_Space )
00401     {
00402         switch ( d_data->itemMode )
00403         {
00404             case QwtLegendData::Clickable:
00405             {
00406                 if ( !e->isAutoRepeat() )
00407                     setDown( false );
00408                 return;
00409             }
00410             case QwtLegendData::Checkable:
00411             {
00412                 return; // do nothing, but accept
00413             }
00414             default:;
00415         }
00416     }
00417 
00418     QwtTextLabel::keyReleaseEvent( e );
00419 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:04