17 #include <qapplication.h>
18 #include <qscrollbar.h>
19 #include <qscrollarea.h>
28 inline bool isEmpty()
const {
return m_entries.isEmpty(); }
31 void remove(
const QVariant& );
33 void removeWidget(
const QWidget* );
36 QVariant itemInfo(
const QWidget* )
const;
54 void LegendMap::insert(
const QVariant& itemInfo,
57 for (
int i = 0; i < m_entries.size(); i++ )
59 Entry& entry = m_entries[i];
60 if ( entry.itemInfo == itemInfo )
62 entry.widgets = widgets;
68 newEntry.itemInfo = itemInfo;
69 newEntry.widgets = widgets;
71 m_entries += newEntry;
76 for (
int i = 0; i < m_entries.size(); i++ )
78 Entry& entry = m_entries[i];
79 if ( entry.itemInfo == itemInfo )
81 m_entries.removeAt( i );
87 void LegendMap::removeWidget(
const QWidget* widget )
89 QWidget* w =
const_cast< QWidget*
>( widget );
91 for (
int i = 0; i < m_entries.size(); i++ )
92 m_entries[ i ].widgets.removeAll( w );
95 QVariant LegendMap::itemInfo(
const QWidget* widget )
const
99 QWidget* w =
const_cast< QWidget*
>( widget );
101 for (
int i = 0; i < m_entries.size(); i++ )
103 const Entry& entry = m_entries[i];
104 if ( entry.widgets.indexOf( w ) >= 0 )
105 return entry.itemInfo;
114 if ( itemInfo.isValid() )
116 for (
int i = 0; i < m_entries.size(); i++ )
118 const Entry& entry = m_entries[i];
119 if ( entry.itemInfo == itemInfo )
120 return entry.widgets;
144 class QwtLegend::PrivateData::LegendView
QWT_FINAL :
public QScrollArea
148 QScrollArea( parent )
154 setWidgetResizable(
false );
156 viewport()->setObjectName(
"QwtLegendViewport" );
161 viewport()->setAutoFillBackground(
false );
166 if ( event->type() == QEvent::PolishRequest )
168 setFocusPolicy( Qt::NoFocus );
171 if ( event->type() == QEvent::Resize )
176 const QRect cr = contentsRect();
189 return QScrollArea::event( event );
194 bool ok = QScrollArea::viewportEvent( event );
196 if ( event->type() == QEvent::Resize )
208 const int cw = contentsRect().width();
209 const int ch = contentsRect().height();
220 if ( w > vw && vh == ch )
223 return QSize( vw, vh );
233 const QSize visibleSize = viewport()->contentsRect().size();
235 const QMargins m = tl->contentsMargins();
236 const int minW = tl->
maxItemWidth() + m.left() + m.right();
238 int w = qMax( visibleSize.width(), minW );
241 const int vpWidth = viewportSize( w, h ).width();
244 w = qMax( vpWidth, minW );
261 setFrameStyle( NoFrame );
265 m_data->
view =
new QwtLegend::PrivateData::LegendView(
this );
266 m_data->
view->setObjectName(
"QwtLegendView" );
271 gridLayout->setAlignment( Qt::AlignHCenter | Qt::AlignTop );
273 m_data->
view->contentsWidget->installEventFilter(
this );
275 QVBoxLayout* layout =
new QVBoxLayout(
this );
276 layout->setContentsMargins( 0, 0, 0, 0 );
401 if ( widgetList.size() != legendData.size() )
403 QLayout* contentsLayout =
m_data->
view->contentsWidget->layout();
405 while ( widgetList.size() > legendData.size() )
407 QWidget* w = widgetList.takeLast();
409 contentsLayout->removeWidget( w );
418 widgetList.reserve( legendData.size() );
420 for (
int i = widgetList.size(); i < legendData.size(); i++ )
424 if ( contentsLayout )
425 contentsLayout->addWidget( widget );
434 widget->setVisible(
true );
437 widgetList += widget;
440 if ( widgetList.isEmpty() )
452 for (
int i = 0; i < legendData.size(); i++ )
469 Q_UNUSED( legendData );
491 QwtLegendLabel* label = qobject_cast< QwtLegendLabel* >( widget );
507 QLayout* contentsLayout =
m_data->
view->contentsWidget->layout();
508 if ( contentsLayout )
514 for (
int i = 0; i < contentsLayout->count(); i++ )
516 QLayoutItem* item = contentsLayout->itemAt( i );
517 if ( w && item->widget() )
518 QWidget::setTabOrder( w, item->widget() );
528 QSize hint =
m_data->
view->contentsWidget->sizeHint();
529 hint += QSize( 2 * frameWidth(), 2 * frameWidth() );
540 width -= 2 * frameWidth();
542 int h =
m_data->
view->contentsWidget->heightForWidth( width );
544 h += 2 * frameWidth();
563 switch ( event->type() )
565 case QEvent::ChildRemoved:
567 const QChildEvent* ce =
568 static_cast< const QChildEvent*
>( event );
570 if ( ce->child()->isWidgetType() )
577 QWidget* w =
reinterpret_cast< QWidget*
>( ce->child() );
582 case QEvent::LayoutRequest:
586 if ( parentWidget() && parentWidget()->layout() == NULL )
599 QApplication::postEvent( parentWidget(),
600 new QEvent( QEvent::LayoutRequest ) );
609 return QwtAbstractLegend::eventFilter(
object, event );
618 QWidget* w = qobject_cast< QWidget* >( sender() );
627 const int index = widgetList.indexOf( w );
640 QWidget* w = qobject_cast< QWidget* >( sender() );
649 const int index = widgetList.indexOf( w );
666 const QRectF& rect,
bool fillBackground )
const
671 if ( fillBackground )
673 if ( autoFillBackground() ||
674 testAttribute( Qt::WA_StyledBackground ) )
682 if ( legendLayout == NULL )
685 const QMargins m = contentsMargins();
688 layoutRect.setLeft(
qwtCeil( rect.left() ) + m.left() );
689 layoutRect.setTop(
qwtCeil( rect.top() ) + m.top() );
690 layoutRect.setRight(
qwtFloor( rect.right() ) - m.right() );
691 layoutRect.setBottom(
qwtFloor( rect.bottom() ) - m.bottom() );
699 for (
int i = 0; i < legendLayout->
count(); i++ )
701 QLayoutItem* item = legendLayout->
itemAt( i );
702 QWidget* w = item->widget();
707 painter->setClipRect( itemRects[index], Qt::IntersectClip );
708 renderItem( painter, w, itemRects[index], fillBackground );
728 const QWidget* widget,
const QRectF& rect,
bool fillBackground )
const
730 if ( fillBackground )
732 if ( widget->autoFillBackground() ||
733 widget->testAttribute( Qt::WA_StyledBackground ) )
739 const QwtLegendLabel* label = qobject_cast< const QwtLegendLabel* >( widget );
747 const QRectF iconRect( rect.x() + label->
margin(),
748 rect.center().y() - 0.5 * sz.height(),
749 sz.width(), sz.height() );
751 icon.
render( painter, iconRect, Qt::KeepAspectRatio );
755 QRectF titleRect = rect;
756 titleRect.setX( iconRect.right() + 2 * label->
spacing() );
758 QFont labelFont = label->font();
759 #if QT_VERSION >= 0x060000
760 labelFont.setResolveMask( QFont::AllPropertiesResolved );
762 labelFont.resolve( QFont::AllPropertiesResolved );
765 painter->setFont( labelFont );
766 painter->setPen( label->palette().color( QPalette::Text ) );
768 const_cast< QwtLegendLabel*
>( label )->drawText( painter, titleRect );
791 if ( list.isEmpty() )
825 if ( orientation == Qt::Horizontal )
834 #include "moc_qwt_legend.cpp"