[4271] | 1 | /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
|
---|
| 2 | * Qwt Widget Library
|
---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
---|
| 5 | *
|
---|
| 6 | * This library is free software; you can redistribute it and/or
|
---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
---|
| 8 | *****************************************************************************/
|
---|
| 9 |
|
---|
| 10 | #include "qwt_legend.h"
|
---|
[8127] | 11 | #include "qwt_legend_label.h"
|
---|
[4271] | 12 | #include "qwt_dyngrid_layout.h"
|
---|
| 13 | #include "qwt_math.h"
|
---|
[8127] | 14 | #include "qwt_plot_item.h"
|
---|
| 15 | #include "qwt_painter.h"
|
---|
[4271] | 16 | #include <qapplication.h>
|
---|
| 17 | #include <qscrollbar.h>
|
---|
| 18 | #include <qscrollarea.h>
|
---|
[8127] | 19 | #include <qpainter.h>
|
---|
| 20 | #include <qstyle.h>
|
---|
| 21 | #include <qstyleoption.h>
|
---|
[4271] | 22 |
|
---|
[8127] | 23 | class QwtLegendMap
|
---|
[4271] | 24 | {
|
---|
| 25 | public:
|
---|
[8127] | 26 | inline bool isEmpty() const { return d_entries.isEmpty(); }
|
---|
| 27 |
|
---|
| 28 | void insert( const QVariant &, const QList<QWidget *> & );
|
---|
| 29 | void remove( const QVariant & );
|
---|
| 30 |
|
---|
| 31 | void removeWidget( const QWidget * );
|
---|
| 32 |
|
---|
| 33 | QList<QWidget *> legendWidgets( const QVariant & ) const;
|
---|
| 34 | QVariant itemInfo( const QWidget * ) const;
|
---|
| 35 |
|
---|
| 36 | private:
|
---|
| 37 | // we don't know anything about itemInfo and therefore don't have
|
---|
| 38 | // any key that can be used for a map or hashtab.
|
---|
| 39 | // But a simple linear list is o.k. here, as we will never have
|
---|
| 40 | // more than a few entries.
|
---|
| 41 |
|
---|
| 42 | class Entry
|
---|
[4271] | 43 | {
|
---|
| 44 | public:
|
---|
[8127] | 45 | QVariant itemInfo;
|
---|
| 46 | QList<QWidget *> widgets;
|
---|
| 47 | };
|
---|
[4271] | 48 |
|
---|
[8127] | 49 | QList< Entry > d_entries;
|
---|
| 50 | };
|
---|
[4271] | 51 |
|
---|
[8127] | 52 | void QwtLegendMap::insert( const QVariant &itemInfo,
|
---|
| 53 | const QList<QWidget *> &widgets )
|
---|
| 54 | {
|
---|
| 55 | for ( int i = 0; i < d_entries.size(); i++ )
|
---|
| 56 | {
|
---|
| 57 | Entry &entry = d_entries[i];
|
---|
| 58 | if ( entry.itemInfo == itemInfo )
|
---|
| 59 | {
|
---|
| 60 | entry.widgets = widgets;
|
---|
| 61 | return;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
[4271] | 64 |
|
---|
[8127] | 65 | Entry newEntry;
|
---|
| 66 | newEntry.itemInfo = itemInfo;
|
---|
| 67 | newEntry.widgets = widgets;
|
---|
[4271] | 68 |
|
---|
[8127] | 69 | d_entries += newEntry;
|
---|
| 70 | }
|
---|
[4271] | 71 |
|
---|
[8127] | 72 | void QwtLegendMap::remove( const QVariant &itemInfo )
|
---|
| 73 | {
|
---|
| 74 | for ( int i = 0; i < d_entries.size(); i++ )
|
---|
| 75 | {
|
---|
| 76 | Entry &entry = d_entries[i];
|
---|
| 77 | if ( entry.itemInfo == itemInfo )
|
---|
| 78 | {
|
---|
| 79 | d_entries.removeAt( i );
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[4271] | 84 |
|
---|
[8127] | 85 | void QwtLegendMap::removeWidget( const QWidget *widget )
|
---|
| 86 | {
|
---|
| 87 | QWidget *w = const_cast<QWidget *>( widget );
|
---|
[4271] | 88 |
|
---|
[8127] | 89 | for ( int i = 0; i < d_entries.size(); i++ )
|
---|
| 90 | d_entries[ i ].widgets.removeAll( w );
|
---|
| 91 | }
|
---|
[4271] | 92 |
|
---|
[8127] | 93 | QVariant QwtLegendMap::itemInfo( const QWidget *widget ) const
|
---|
| 94 | {
|
---|
| 95 | if ( widget != NULL )
|
---|
| 96 | {
|
---|
| 97 | QWidget *w = const_cast<QWidget *>( widget );
|
---|
[4271] | 98 |
|
---|
[8127] | 99 | for ( int i = 0; i < d_entries.size(); i++ )
|
---|
| 100 | {
|
---|
| 101 | const Entry &entry = d_entries[i];
|
---|
| 102 | if ( entry.widgets.indexOf( w ) >= 0 )
|
---|
| 103 | return entry.itemInfo;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
[4271] | 106 |
|
---|
[8127] | 107 | return QVariant();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | QList<QWidget *> QwtLegendMap::legendWidgets( const QVariant &itemInfo ) const
|
---|
| 111 | {
|
---|
| 112 | if ( itemInfo.isValid() )
|
---|
| 113 | {
|
---|
| 114 | for ( int i = 0; i < d_entries.size(); i++ )
|
---|
| 115 | {
|
---|
| 116 | const Entry &entry = d_entries[i];
|
---|
| 117 | if ( entry.itemInfo == itemInfo )
|
---|
| 118 | return entry.widgets;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | return QList<QWidget *>();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | class QwtLegend::PrivateData
|
---|
| 126 | {
|
---|
| 127 | public:
|
---|
| 128 | PrivateData():
|
---|
| 129 | itemMode( QwtLegendData::ReadOnly ),
|
---|
| 130 | view( NULL )
|
---|
| 131 | {
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | QwtLegendData::Mode itemMode;
|
---|
| 135 | QwtLegendMap itemMap;
|
---|
| 136 |
|
---|
[4271] | 137 | class LegendView;
|
---|
| 138 | LegendView *view;
|
---|
| 139 | };
|
---|
| 140 |
|
---|
| 141 | class QwtLegend::PrivateData::LegendView: public QScrollArea
|
---|
| 142 | {
|
---|
| 143 | public:
|
---|
| 144 | LegendView( QWidget *parent ):
|
---|
| 145 | QScrollArea( parent )
|
---|
| 146 | {
|
---|
| 147 | contentsWidget = new QWidget( this );
|
---|
| 148 | contentsWidget->setObjectName( "QwtLegendViewContents" );
|
---|
| 149 |
|
---|
| 150 | setWidget( contentsWidget );
|
---|
| 151 | setWidgetResizable( false );
|
---|
| 152 |
|
---|
| 153 | viewport()->setObjectName( "QwtLegendViewport" );
|
---|
| 154 |
|
---|
| 155 | // QScrollArea::setWidget internally sets autoFillBackground to true
|
---|
| 156 | // But we don't want a background.
|
---|
| 157 | contentsWidget->setAutoFillBackground( false );
|
---|
| 158 | viewport()->setAutoFillBackground( false );
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[8127] | 161 | virtual bool event( QEvent *event )
|
---|
[4271] | 162 | {
|
---|
[8127] | 163 | if ( event->type() == QEvent::PolishRequest )
|
---|
| 164 | {
|
---|
| 165 | setFocusPolicy( Qt::NoFocus );
|
---|
| 166 | }
|
---|
[4271] | 167 |
|
---|
[8127] | 168 | if ( event->type() == QEvent::Resize )
|
---|
[4271] | 169 | {
|
---|
[8127] | 170 | // adjust the size to en/disable the scrollbars
|
---|
| 171 | // before QScrollArea adjusts the viewport size
|
---|
| 172 |
|
---|
| 173 | const QRect cr = contentsRect();
|
---|
| 174 |
|
---|
| 175 | int w = cr.width();
|
---|
| 176 | int h = contentsWidget->heightForWidth( cr.width() );
|
---|
| 177 | if ( h > w )
|
---|
| 178 | {
|
---|
| 179 | w -= verticalScrollBar()->sizeHint().width();
|
---|
| 180 | h = contentsWidget->heightForWidth( w );
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | contentsWidget->resize( w, h );
|
---|
[4271] | 184 | }
|
---|
[8127] | 185 |
|
---|
| 186 | return QScrollArea::event( event );
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | virtual bool viewportEvent( QEvent *event )
|
---|
| 190 | {
|
---|
| 191 | bool ok = QScrollArea::viewportEvent( event );
|
---|
| 192 |
|
---|
| 193 | if ( event->type() == QEvent::Resize )
|
---|
| 194 | {
|
---|
| 195 | layoutContents();
|
---|
| 196 | }
|
---|
[4271] | 197 | return ok;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | QSize viewportSize( int w, int h ) const
|
---|
| 201 | {
|
---|
| 202 | const int sbHeight = horizontalScrollBar()->sizeHint().height();
|
---|
| 203 | const int sbWidth = verticalScrollBar()->sizeHint().width();
|
---|
| 204 |
|
---|
| 205 | const int cw = contentsRect().width();
|
---|
| 206 | const int ch = contentsRect().height();
|
---|
| 207 |
|
---|
| 208 | int vw = cw;
|
---|
| 209 | int vh = ch;
|
---|
| 210 |
|
---|
| 211 | if ( w > vw )
|
---|
| 212 | vh -= sbHeight;
|
---|
| 213 |
|
---|
| 214 | if ( h > vh )
|
---|
| 215 | {
|
---|
| 216 | vw -= sbWidth;
|
---|
| 217 | if ( w > vw && vh == ch )
|
---|
| 218 | vh -= sbHeight;
|
---|
| 219 | }
|
---|
| 220 | return QSize( vw, vh );
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[8127] | 223 | void layoutContents()
|
---|
| 224 | {
|
---|
| 225 | const QwtDynGridLayout *tl = qobject_cast<QwtDynGridLayout *>(
|
---|
| 226 | contentsWidget->layout() );
|
---|
| 227 | if ( tl == NULL )
|
---|
| 228 | return;
|
---|
[4271] | 229 |
|
---|
[8127] | 230 | const QSize visibleSize = viewport()->contentsRect().size();
|
---|
[4271] | 231 |
|
---|
[8127] | 232 | const int minW = int( tl->maxItemWidth() ) + 2 * tl->margin();
|
---|
[4271] | 233 |
|
---|
[8127] | 234 | int w = qMax( visibleSize.width(), minW );
|
---|
| 235 | int h = qMax( tl->heightForWidth( w ), visibleSize.height() );
|
---|
[4271] | 236 |
|
---|
[8127] | 237 | const int vpWidth = viewportSize( w, h ).width();
|
---|
| 238 | if ( w > vpWidth )
|
---|
| 239 | {
|
---|
| 240 | w = qMax( vpWidth, minW );
|
---|
| 241 | h = qMax( tl->heightForWidth( w ), visibleSize.height() );
|
---|
| 242 | }
|
---|
[4271] | 243 |
|
---|
[8127] | 244 | contentsWidget->resize( w, h );
|
---|
| 245 | }
|
---|
[4271] | 246 |
|
---|
[8127] | 247 | QWidget *contentsWidget;
|
---|
| 248 | };
|
---|
[4271] | 249 |
|
---|
| 250 | /*!
|
---|
| 251 | Constructor
|
---|
| 252 | \param parent Parent widget
|
---|
| 253 | */
|
---|
| 254 | QwtLegend::QwtLegend( QWidget *parent ):
|
---|
[8127] | 255 | QwtAbstractLegend( parent )
|
---|
[4271] | 256 | {
|
---|
| 257 | setFrameStyle( NoFrame );
|
---|
| 258 |
|
---|
| 259 | d_data = new QwtLegend::PrivateData;
|
---|
| 260 |
|
---|
| 261 | d_data->view = new QwtLegend::PrivateData::LegendView( this );
|
---|
| 262 | d_data->view->setObjectName( "QwtLegendView" );
|
---|
| 263 | d_data->view->setFrameStyle( NoFrame );
|
---|
| 264 |
|
---|
| 265 | QwtDynGridLayout *gridLayout = new QwtDynGridLayout(
|
---|
| 266 | d_data->view->contentsWidget );
|
---|
| 267 | gridLayout->setAlignment( Qt::AlignHCenter | Qt::AlignTop );
|
---|
| 268 |
|
---|
| 269 | d_data->view->contentsWidget->installEventFilter( this );
|
---|
| 270 |
|
---|
| 271 | QVBoxLayout *layout = new QVBoxLayout( this );
|
---|
| 272 | layout->setContentsMargins( 0, 0, 0, 0 );
|
---|
| 273 | layout->addWidget( d_data->view );
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | //! Destructor
|
---|
| 277 | QwtLegend::~QwtLegend()
|
---|
| 278 | {
|
---|
| 279 | delete d_data;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[8127] | 282 | /*!
|
---|
| 283 | \brief Set the maximum number of entries in a row
|
---|
| 284 |
|
---|
| 285 | F.e when the maximum is set to 1 all items are aligned
|
---|
| 286 | vertically. 0 means unlimited
|
---|
| 287 |
|
---|
| 288 | \param numColums Maximum number of entries in a row
|
---|
| 289 |
|
---|
| 290 | \sa maxColumns(), QwtDynGridLayout::setMaxColumns()
|
---|
| 291 | */
|
---|
| 292 | void QwtLegend::setMaxColumns( uint numColums )
|
---|
[4271] | 293 | {
|
---|
[8127] | 294 | QwtDynGridLayout *tl = qobject_cast<QwtDynGridLayout *>(
|
---|
| 295 | d_data->view->contentsWidget->layout() );
|
---|
| 296 | if ( tl )
|
---|
| 297 | tl->setMaxColumns( numColums );
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | /*!
|
---|
| 301 | \return Maximum number of entries in a row
|
---|
| 302 | \sa setMaxColumns(), QwtDynGridLayout::maxColumns()
|
---|
| 303 | */
|
---|
| 304 | uint QwtLegend::maxColumns() const
|
---|
| 305 | {
|
---|
| 306 | uint maxCols = 0;
|
---|
| 307 |
|
---|
| 308 | const QwtDynGridLayout *tl = qobject_cast<const QwtDynGridLayout *>(
|
---|
| 309 | d_data->view->contentsWidget->layout() );
|
---|
| 310 | if ( tl )
|
---|
| 311 | maxCols = tl->maxColumns();
|
---|
| 312 |
|
---|
| 313 | return maxCols;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | /*!
|
---|
| 317 | \brief Set the default mode for legend labels
|
---|
| 318 |
|
---|
| 319 | Legend labels will be constructed according to the
|
---|
| 320 | attributes in a QwtLegendData object. When it doesn't
|
---|
| 321 | contain a value for the QwtLegendData::ModeRole the
|
---|
| 322 | label will be initialized with the default mode of the legend.
|
---|
| 323 |
|
---|
| 324 | \param mode Default item mode
|
---|
| 325 |
|
---|
| 326 | \sa itemMode(), QwtLegendData::value(), QwtPlotItem::legendData()
|
---|
| 327 | \note Changing the mode doesn't have any effect on existing labels.
|
---|
| 328 | */
|
---|
| 329 | void QwtLegend::setDefaultItemMode( QwtLegendData::Mode mode )
|
---|
| 330 | {
|
---|
[4271] | 331 | d_data->itemMode = mode;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[8127] | 334 | /*!
|
---|
| 335 | \return Default item mode
|
---|
| 336 | \sa setDefaultItemMode()
|
---|
| 337 | */
|
---|
| 338 | QwtLegendData::Mode QwtLegend::defaultItemMode() const
|
---|
[4271] | 339 | {
|
---|
| 340 | return d_data->itemMode;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | /*!
|
---|
| 344 | The contents widget is the only child of the viewport of
|
---|
[8127] | 345 | the internal QScrollArea and the parent widget of all legend items.
|
---|
[4271] | 346 |
|
---|
| 347 | \return Container widget of the legend items
|
---|
| 348 | */
|
---|
| 349 | QWidget *QwtLegend::contentsWidget()
|
---|
| 350 | {
|
---|
| 351 | return d_data->view->contentsWidget;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /*!
|
---|
| 355 | \return Horizontal scrollbar
|
---|
| 356 | \sa verticalScrollBar()
|
---|
| 357 | */
|
---|
| 358 | QScrollBar *QwtLegend::horizontalScrollBar() const
|
---|
| 359 | {
|
---|
| 360 | return d_data->view->horizontalScrollBar();
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | /*!
|
---|
| 364 | \return Vertical scrollbar
|
---|
| 365 | \sa horizontalScrollBar()
|
---|
| 366 | */
|
---|
| 367 | QScrollBar *QwtLegend::verticalScrollBar() const
|
---|
| 368 | {
|
---|
| 369 | return d_data->view->verticalScrollBar();
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /*!
|
---|
| 373 | The contents widget is the only child of the viewport of
|
---|
[8127] | 374 | the internal QScrollArea and the parent widget of all legend items.
|
---|
[4271] | 375 |
|
---|
| 376 | \return Container widget of the legend items
|
---|
| 377 |
|
---|
| 378 | */
|
---|
| 379 | const QWidget *QwtLegend::contentsWidget() const
|
---|
| 380 | {
|
---|
| 381 | return d_data->view->contentsWidget;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /*!
|
---|
[8127] | 385 | \brief Update the entries for an item
|
---|
| 386 |
|
---|
| 387 | \param itemInfo Info for an item
|
---|
| 388 | \param data List of legend entry attributes for the item
|
---|
| 389 | */
|
---|
| 390 | void QwtLegend::updateLegend( const QVariant &itemInfo,
|
---|
| 391 | const QList<QwtLegendData> &data )
|
---|
[4271] | 392 | {
|
---|
[8127] | 393 | QList<QWidget *> widgetList = legendWidgets( itemInfo );
|
---|
[4271] | 394 |
|
---|
[8127] | 395 | if ( widgetList.size() != data.size() )
|
---|
| 396 | {
|
---|
| 397 | QLayout *contentsLayout = d_data->view->contentsWidget->layout();
|
---|
[4271] | 398 |
|
---|
[8127] | 399 | while ( widgetList.size() > data.size() )
|
---|
| 400 | {
|
---|
| 401 | QWidget *w = widgetList.takeLast();
|
---|
[4271] | 402 |
|
---|
[8127] | 403 | contentsLayout->removeWidget( w );
|
---|
[4271] | 404 |
|
---|
[8127] | 405 | // updates might be triggered by signals from the legend widget
|
---|
| 406 | // itself. So we better don't delete it here.
|
---|
[4271] | 407 |
|
---|
[8127] | 408 | w->hide();
|
---|
| 409 | w->deleteLater();
|
---|
| 410 | }
|
---|
[4271] | 411 |
|
---|
[8127] | 412 | for ( int i = widgetList.size(); i < data.size(); i++ )
|
---|
| 413 | {
|
---|
| 414 | QWidget *widget = createWidget( data[i] );
|
---|
[4271] | 415 |
|
---|
[8127] | 416 | if ( contentsLayout )
|
---|
| 417 | contentsLayout->addWidget( widget );
|
---|
[4271] | 418 |
|
---|
[8127] | 419 | if ( isVisible() )
|
---|
| 420 | {
|
---|
| 421 | // QLayout does a delayed show, with the effect, that
|
---|
| 422 | // the size hint will be wrong, when applications
|
---|
| 423 | // call replot() right after changing the list
|
---|
| 424 | // of plot items. So we better do the show now.
|
---|
[4271] | 425 |
|
---|
[8127] | 426 | widget->setVisible( true );
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | widgetList += widget;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | if ( widgetList.isEmpty() )
|
---|
[4271] | 433 | {
|
---|
[8127] | 434 | d_data->itemMap.remove( itemInfo );
|
---|
| 435 | }
|
---|
| 436 | else
|
---|
| 437 | {
|
---|
| 438 | d_data->itemMap.insert( itemInfo, widgetList );
|
---|
| 439 | }
|
---|
[4271] | 440 |
|
---|
[8127] | 441 | updateTabOrder();
|
---|
[4271] | 442 | }
|
---|
[8127] | 443 |
|
---|
| 444 | for ( int i = 0; i < data.size(); i++ )
|
---|
| 445 | updateWidget( widgetList[i], data[i] );
|
---|
[4271] | 446 | }
|
---|
| 447 |
|
---|
| 448 | /*!
|
---|
[8127] | 449 | \brief Create a widget to be inserted into the legend
|
---|
[4271] | 450 |
|
---|
[8127] | 451 | The default implementation returns a QwtLegendLabel.
|
---|
| 452 |
|
---|
| 453 | \param data Attributes of the legend entry
|
---|
| 454 | \return Widget representing data on the legend
|
---|
| 455 |
|
---|
| 456 | \note updateWidget() will called soon after createWidget()
|
---|
| 457 | with the same attributes.
|
---|
| 458 | */
|
---|
| 459 | QWidget *QwtLegend::createWidget( const QwtLegendData &data ) const
|
---|
[4271] | 460 | {
|
---|
[8127] | 461 | Q_UNUSED( data );
|
---|
[4271] | 462 |
|
---|
[8127] | 463 | QwtLegendLabel *label = new QwtLegendLabel();
|
---|
| 464 | label->setItemMode( defaultItemMode() );
|
---|
[4271] | 465 |
|
---|
[8127] | 466 | connect( label, SIGNAL( clicked() ), SLOT( itemClicked() ) );
|
---|
| 467 | connect( label, SIGNAL( checked( bool ) ), SLOT( itemChecked( bool ) ) );
|
---|
| 468 |
|
---|
| 469 | return label;
|
---|
[4271] | 470 | }
|
---|
| 471 |
|
---|
| 472 | /*!
|
---|
[8127] | 473 | \brief Update the widget
|
---|
[4271] | 474 |
|
---|
[8127] | 475 | \param widget Usually a QwtLegendLabel
|
---|
| 476 | \param data Attributes to be displayed
|
---|
| 477 |
|
---|
| 478 | \sa createWidget()
|
---|
| 479 | \note When widget is no QwtLegendLabel updateWidget() does nothing.
|
---|
| 480 | */
|
---|
| 481 | void QwtLegend::updateWidget( QWidget *widget, const QwtLegendData &data )
|
---|
[4271] | 482 | {
|
---|
[8127] | 483 | QwtLegendLabel *label = qobject_cast<QwtLegendLabel *>( widget );
|
---|
| 484 | if ( label )
|
---|
| 485 | {
|
---|
| 486 | label->setData( data );
|
---|
| 487 | if ( !data.value( QwtLegendData::ModeRole ).isValid() )
|
---|
| 488 | {
|
---|
| 489 | // use the default mode, when there is no specific
|
---|
| 490 | // hint from the legend data
|
---|
| 491 |
|
---|
| 492 | label->setItemMode( defaultItemMode() );
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
[4271] | 495 | }
|
---|
| 496 |
|
---|
[8127] | 497 | void QwtLegend::updateTabOrder()
|
---|
[4271] | 498 | {
|
---|
[8127] | 499 | QLayout *contentsLayout = d_data->view->contentsWidget->layout();
|
---|
| 500 | if ( contentsLayout )
|
---|
| 501 | {
|
---|
| 502 | // set tab focus chain
|
---|
[4271] | 503 |
|
---|
[8127] | 504 | QWidget *w = NULL;
|
---|
[4271] | 505 |
|
---|
[8127] | 506 | for ( int i = 0; i < contentsLayout->count(); i++ )
|
---|
| 507 | {
|
---|
| 508 | QLayoutItem *item = contentsLayout->itemAt( i );
|
---|
| 509 | if ( w && item->widget() )
|
---|
| 510 | QWidget::setTabOrder( w, item->widget() );
|
---|
[4271] | 511 |
|
---|
[8127] | 512 | w = item->widget();
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
[4271] | 515 | }
|
---|
| 516 |
|
---|
| 517 | //! Return a size hint.
|
---|
| 518 | QSize QwtLegend::sizeHint() const
|
---|
| 519 | {
|
---|
| 520 | QSize hint = d_data->view->contentsWidget->sizeHint();
|
---|
| 521 | hint += QSize( 2 * frameWidth(), 2 * frameWidth() );
|
---|
| 522 |
|
---|
| 523 | return hint;
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | /*!
|
---|
[8127] | 527 | \return The preferred height, for a width.
|
---|
[4271] | 528 | \param width Width
|
---|
| 529 | */
|
---|
| 530 | int QwtLegend::heightForWidth( int width ) const
|
---|
| 531 | {
|
---|
| 532 | width -= 2 * frameWidth();
|
---|
| 533 |
|
---|
| 534 | int h = d_data->view->contentsWidget->heightForWidth( width );
|
---|
| 535 | if ( h >= 0 )
|
---|
| 536 | h += 2 * frameWidth();
|
---|
| 537 |
|
---|
| 538 | return h;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 |
|
---|
| 542 | /*!
|
---|
| 543 | Handle QEvent::ChildRemoved andQEvent::LayoutRequest events
|
---|
| 544 | for the contentsWidget().
|
---|
| 545 |
|
---|
| 546 | \param object Object to be filtered
|
---|
| 547 | \param event Event
|
---|
[8127] | 548 |
|
---|
| 549 | \return Forwarded to QwtAbstractLegend::eventFilter()
|
---|
[4271] | 550 | */
|
---|
| 551 | bool QwtLegend::eventFilter( QObject *object, QEvent *event )
|
---|
| 552 | {
|
---|
| 553 | if ( object == d_data->view->contentsWidget )
|
---|
| 554 | {
|
---|
| 555 | switch ( event->type() )
|
---|
| 556 | {
|
---|
| 557 | case QEvent::ChildRemoved:
|
---|
| 558 | {
|
---|
| 559 | const QChildEvent *ce =
|
---|
| 560 | static_cast<const QChildEvent *>(event);
|
---|
| 561 | if ( ce->child()->isWidgetType() )
|
---|
| 562 | {
|
---|
| 563 | QWidget *w = static_cast< QWidget * >( ce->child() );
|
---|
[8127] | 564 | d_data->itemMap.removeWidget( w );
|
---|
[4271] | 565 | }
|
---|
| 566 | break;
|
---|
| 567 | }
|
---|
| 568 | case QEvent::LayoutRequest:
|
---|
| 569 | {
|
---|
[8127] | 570 | d_data->view->layoutContents();
|
---|
| 571 |
|
---|
| 572 | if ( parentWidget() && parentWidget()->layout() == NULL )
|
---|
| 573 | {
|
---|
| 574 | /*
|
---|
| 575 | We want the parent widget ( usually QwtPlot ) to recalculate
|
---|
| 576 | its layout, when the contentsWidget has changed. But
|
---|
| 577 | because of the scroll view we have to forward the LayoutRequest
|
---|
| 578 | event manually.
|
---|
| 579 |
|
---|
| 580 | We don't use updateGeometry() because it doesn't post LayoutRequest
|
---|
| 581 | events when the legend is hidden. But we want the
|
---|
| 582 | parent widget notified, so it can show/hide the legend
|
---|
| 583 | depending on its items.
|
---|
| 584 | */
|
---|
| 585 | QApplication::postEvent( parentWidget(),
|
---|
| 586 | new QEvent( QEvent::LayoutRequest ) );
|
---|
| 587 | }
|
---|
[4271] | 588 | break;
|
---|
| 589 | }
|
---|
| 590 | default:
|
---|
| 591 | break;
|
---|
| 592 | }
|
---|
| 593 | }
|
---|
| 594 |
|
---|
[8127] | 595 | return QwtAbstractLegend::eventFilter( object, event );
|
---|
[4271] | 596 | }
|
---|
| 597 |
|
---|
[8127] | 598 | /*!
|
---|
| 599 | Called internally when the legend has been clicked on.
|
---|
| 600 | Emits a clicked() signal.
|
---|
| 601 | */
|
---|
| 602 | void QwtLegend::itemClicked()
|
---|
| 603 | {
|
---|
| 604 | QWidget *w = qobject_cast<QWidget *>( sender() );
|
---|
| 605 | if ( w )
|
---|
| 606 | {
|
---|
| 607 | const QVariant itemInfo = d_data->itemMap.itemInfo( w );
|
---|
| 608 | if ( itemInfo.isValid() )
|
---|
| 609 | {
|
---|
| 610 | const QList<QWidget *> widgetList =
|
---|
| 611 | d_data->itemMap.legendWidgets( itemInfo );
|
---|
[4271] | 612 |
|
---|
[8127] | 613 | const int index = widgetList.indexOf( w );
|
---|
| 614 | if ( index >= 0 )
|
---|
| 615 | Q_EMIT clicked( itemInfo, index );
|
---|
| 616 | }
|
---|
| 617 | }
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | /*!
|
---|
| 621 | Called internally when the legend has been checked
|
---|
| 622 | Emits a checked() signal.
|
---|
| 623 | */
|
---|
| 624 | void QwtLegend::itemChecked( bool on )
|
---|
[4271] | 625 | {
|
---|
[8127] | 626 | QWidget *w = qobject_cast<QWidget *>( sender() );
|
---|
| 627 | if ( w )
|
---|
| 628 | {
|
---|
| 629 | const QVariant itemInfo = d_data->itemMap.itemInfo( w );
|
---|
| 630 | if ( itemInfo.isValid() )
|
---|
| 631 | {
|
---|
| 632 | const QList<QWidget *> widgetList =
|
---|
| 633 | d_data->itemMap.legendWidgets( itemInfo );
|
---|
| 634 |
|
---|
| 635 | const int index = widgetList.indexOf( w );
|
---|
| 636 | if ( index >= 0 )
|
---|
| 637 | Q_EMIT checked( itemInfo, on, index );
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
[4271] | 640 | }
|
---|
| 641 |
|
---|
[8127] | 642 | /*!
|
---|
| 643 | Render the legend into a given rectangle.
|
---|
| 644 |
|
---|
| 645 | \param painter Painter
|
---|
| 646 | \param rect Bounding rectangle
|
---|
| 647 | \param fillBackground When true, fill rect with the widget background
|
---|
| 648 |
|
---|
| 649 | \sa renderLegend() is used by QwtPlotRenderer - not by QwtLegend itself
|
---|
| 650 | */
|
---|
| 651 | void QwtLegend::renderLegend( QPainter *painter,
|
---|
| 652 | const QRectF &rect, bool fillBackground ) const
|
---|
[4271] | 653 | {
|
---|
[8127] | 654 | if ( d_data->itemMap.isEmpty() )
|
---|
| 655 | return;
|
---|
| 656 |
|
---|
| 657 | if ( fillBackground )
|
---|
| 658 | {
|
---|
| 659 | if ( autoFillBackground() ||
|
---|
| 660 | testAttribute( Qt::WA_StyledBackground ) )
|
---|
| 661 | {
|
---|
| 662 | QwtPainter::drawBackgound( painter, rect, this );
|
---|
| 663 | }
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | const QwtDynGridLayout *legendLayout =
|
---|
| 667 | qobject_cast<QwtDynGridLayout *>( contentsWidget()->layout() );
|
---|
| 668 | if ( legendLayout == NULL )
|
---|
| 669 | return;
|
---|
| 670 |
|
---|
| 671 | int left, right, top, bottom;
|
---|
| 672 | getContentsMargins( &left, &top, &right, &bottom );
|
---|
| 673 |
|
---|
| 674 | QRect layoutRect;
|
---|
| 675 | layoutRect.setLeft( qCeil( rect.left() ) + left );
|
---|
| 676 | layoutRect.setTop( qCeil( rect.top() ) + top );
|
---|
| 677 | layoutRect.setRight( qFloor( rect.right() ) - right );
|
---|
| 678 | layoutRect.setBottom( qFloor( rect.bottom() ) - bottom );
|
---|
| 679 |
|
---|
| 680 | uint numCols = legendLayout->columnsForWidth( layoutRect.width() );
|
---|
| 681 | QList<QRect> itemRects =
|
---|
| 682 | legendLayout->layoutItems( layoutRect, numCols );
|
---|
| 683 |
|
---|
| 684 | int index = 0;
|
---|
| 685 |
|
---|
| 686 | for ( int i = 0; i < legendLayout->count(); i++ )
|
---|
| 687 | {
|
---|
| 688 | QLayoutItem *item = legendLayout->itemAt( i );
|
---|
| 689 | QWidget *w = item->widget();
|
---|
| 690 | if ( w )
|
---|
| 691 | {
|
---|
| 692 | painter->save();
|
---|
| 693 |
|
---|
| 694 | painter->setClipRect( itemRects[index], Qt::IntersectClip );
|
---|
| 695 | renderItem( painter, w, itemRects[index], fillBackground );
|
---|
| 696 |
|
---|
| 697 | index++;
|
---|
| 698 | painter->restore();
|
---|
| 699 | }
|
---|
| 700 | }
|
---|
[4271] | 701 | }
|
---|
| 702 |
|
---|
[8127] | 703 | /*!
|
---|
| 704 | Render a legend entry into a given rectangle.
|
---|
| 705 |
|
---|
| 706 | \param painter Painter
|
---|
| 707 | \param widget Widget representing a legend entry
|
---|
| 708 | \param rect Bounding rectangle
|
---|
| 709 | \param fillBackground When true, fill rect with the widget background
|
---|
| 710 |
|
---|
| 711 | \note When widget is not derived from QwtLegendLabel renderItem
|
---|
| 712 | does nothing beside the background
|
---|
| 713 | */
|
---|
| 714 | void QwtLegend::renderItem( QPainter *painter,
|
---|
| 715 | const QWidget *widget, const QRectF &rect, bool fillBackground ) const
|
---|
[4271] | 716 | {
|
---|
[8127] | 717 | if ( fillBackground )
|
---|
| 718 | {
|
---|
| 719 | if ( widget->autoFillBackground() ||
|
---|
| 720 | widget->testAttribute( Qt::WA_StyledBackground ) )
|
---|
| 721 | {
|
---|
| 722 | QwtPainter::drawBackgound( painter, rect, widget );
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
[4271] | 725 |
|
---|
[8127] | 726 | const QwtLegendLabel *label = qobject_cast<const QwtLegendLabel *>( widget );
|
---|
| 727 | if ( label )
|
---|
| 728 | {
|
---|
| 729 | // icon
|
---|
[4271] | 730 |
|
---|
[8127] | 731 | const QwtGraphic &icon = label->data().icon();
|
---|
| 732 | const QSizeF sz = icon.defaultSize();
|
---|
[4271] | 733 |
|
---|
[8127] | 734 | const QRectF iconRect( rect.x() + label->margin(),
|
---|
| 735 | rect.center().y() - 0.5 * sz.height(),
|
---|
| 736 | sz.width(), sz.height() );
|
---|
| 737 |
|
---|
| 738 | icon.render( painter, iconRect, Qt::KeepAspectRatio );
|
---|
| 739 |
|
---|
| 740 | // title
|
---|
| 741 |
|
---|
| 742 | QRectF titleRect = rect;
|
---|
| 743 | titleRect.setX( iconRect.right() + 2 * label->spacing() );
|
---|
| 744 |
|
---|
| 745 | painter->setFont( label->font() );
|
---|
| 746 | painter->setPen( label->palette().color( QPalette::Text ) );
|
---|
| 747 | const_cast< QwtLegendLabel *>( label )->drawText( painter, titleRect );
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | /*!
|
---|
| 752 | \return List of widgets associated to a item
|
---|
| 753 | \param itemInfo Info about an item
|
---|
| 754 | \sa legendWidget(), itemInfo(), QwtPlot::itemToInfo()
|
---|
| 755 | */
|
---|
| 756 | QList<QWidget *> QwtLegend::legendWidgets( const QVariant &itemInfo ) const
|
---|
| 757 | {
|
---|
| 758 | return d_data->itemMap.legendWidgets( itemInfo );
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | /*!
|
---|
| 762 | \return First widget in the list of widgets associated to an item
|
---|
| 763 | \param itemInfo Info about an item
|
---|
| 764 | \sa itemInfo(), QwtPlot::itemToInfo()
|
---|
| 765 | \note Almost all types of items have only one widget
|
---|
| 766 | */
|
---|
| 767 | QWidget *QwtLegend::legendWidget( const QVariant &itemInfo ) const
|
---|
| 768 | {
|
---|
| 769 | const QList<QWidget *> list = d_data->itemMap.legendWidgets( itemInfo );
|
---|
| 770 | if ( list.isEmpty() )
|
---|
| 771 | return NULL;
|
---|
| 772 |
|
---|
| 773 | return list[0];
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | /*!
|
---|
| 777 | Find the item that is associated to a widget
|
---|
| 778 |
|
---|
| 779 | \param widget Widget on the legend
|
---|
| 780 | \return Associated item info
|
---|
| 781 | \sa legendWidget()
|
---|
| 782 | */
|
---|
| 783 | QVariant QwtLegend::itemInfo( const QWidget *widget ) const
|
---|
| 784 | {
|
---|
| 785 | return d_data->itemMap.itemInfo( widget );
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | //! \return True, when no item is inserted
|
---|
| 789 | bool QwtLegend::isEmpty() const
|
---|
| 790 | {
|
---|
| 791 | return d_data->itemMap.isEmpty();
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | /*!
|
---|
| 795 | Return the extent, that is needed for the scrollbars
|
---|
| 796 |
|
---|
| 797 | \param orientation Orientation (
|
---|
| 798 | \return The width of the vertical scrollbar for Qt::Horizontal and v.v.
|
---|
| 799 | */
|
---|
| 800 | int QwtLegend::scrollExtent( Qt::Orientation orientation ) const
|
---|
| 801 | {
|
---|
| 802 | int extent = 0;
|
---|
| 803 |
|
---|
| 804 | if ( orientation == Qt::Horizontal )
|
---|
| 805 | extent = verticalScrollBar()->sizeHint().width();
|
---|
| 806 | else
|
---|
| 807 | extent = horizontalScrollBar()->sizeHint().height();
|
---|
| 808 |
|
---|
| 809 | return extent;
|
---|
| 810 | }
|
---|
| 811 |
|
---|