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