10#include "qwt_legend.h"
11#include "qwt_legend_label.h"
12#include "qwt_dyngrid_layout.h"
14#include "qwt_painter.h"
15#include "qwt_graphic.h"
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;
74 void LegendMap::remove(
const QVariant& itemInfo )
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;
128class QwtLegend::PrivateData
144class QwtLegend::PrivateData::LegendView QWT_FINAL :
public QScrollArea
147 explicit LegendView( QWidget * parent ) :
148 QScrollArea( parent )
150 contentsWidget =
new QWidget(
this );
151 contentsWidget->setObjectName(
"QwtLegendViewContents" );
153 setWidget( contentsWidget );
154 setWidgetResizable(
false );
156 viewport()->setObjectName(
"QwtLegendViewport" );
160 contentsWidget->setAutoFillBackground(
false );
161 viewport()->setAutoFillBackground(
false );
164 virtual bool event( QEvent* event ) QWT_OVERRIDE
166 if ( event->type() == QEvent::PolishRequest )
168 setFocusPolicy( Qt::NoFocus );
171 if ( event->type() == QEvent::Resize )
176 const QRect cr = contentsRect();
179 int h = contentsWidget->heightForWidth( cr.width() );
182 w -= verticalScrollBar()->sizeHint().width();
183 h = contentsWidget->heightForWidth( w );
186 contentsWidget->resize( w, h );
189 return QScrollArea::event( event );
192 virtual bool viewportEvent( QEvent* event ) QWT_OVERRIDE
194 bool ok = QScrollArea::viewportEvent( event );
196 if ( event->type() == QEvent::Resize )
203 QSize viewportSize(
int w,
int h )
const
205 const int sbHeight = horizontalScrollBar()->sizeHint().height();
206 const int sbWidth = verticalScrollBar()->sizeHint().width();
208 const int cw = contentsRect().width();
209 const int ch = contentsRect().height();
220 if ( w > vw && vh == ch )
223 return QSize( vw, vh );
226 void layoutContents()
229 contentsWidget->layout() );
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 );
248 contentsWidget->resize( w, h );
251 QWidget* contentsWidget;
261 setFrameStyle( NoFrame );
263 m_data =
new QwtLegend::PrivateData;
265 m_data->view =
new QwtLegend::PrivateData::LegendView(
this );
266 m_data->view->setObjectName(
"QwtLegendView" );
267 m_data->view->setFrameStyle( NoFrame );
270 m_data->view->contentsWidget );
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 );
277 layout->addWidget( m_data->view );
299 m_data->view->contentsWidget->layout() );
315 m_data->view->contentsWidget->layout() );
337 m_data->itemMode = mode;
346 return m_data->itemMode;
357 return m_data->view->contentsWidget;
366 return m_data->view->horizontalScrollBar();
375 return m_data->view->verticalScrollBar();
387 return m_data->view->contentsWidget;
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() )
446 m_data->itemMap.insert(
itemInfo, widgetList );
452 for (
int i = 0; i < legendData.size(); i++ )
469 Q_UNUSED( legendData );
491 QwtLegendLabel* label = qobject_cast< QwtLegendLabel* >( widget );
495 if ( !legendData.
value( QwtLegendData::ModeRole ).isValid() )
505void QwtLegend::updateTabOrder()
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();
561 if (
object == m_data->view->contentsWidget )
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() );
578 m_data->itemMap.removeWidget( w );
582 case QEvent::LayoutRequest:
584 m_data->view->layoutContents();
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() );
621 const QVariant
itemInfo = m_data->itemMap.itemInfo( w );
625 m_data->itemMap.legendWidgets(
itemInfo );
627 const int index = widgetList.indexOf( w );
640 QWidget* w = qobject_cast< QWidget* >( sender() );
643 const QVariant
itemInfo = m_data->itemMap.itemInfo( w );
647 m_data->itemMap.legendWidgets(
itemInfo );
649 const int index = widgetList.indexOf( w );
666 const QRectF& rect,
bool fillBackground )
const
668 if ( m_data->itemMap.isEmpty() )
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 );
779 return m_data->itemMap.legendWidgets(
itemInfo );
791 if ( list.isEmpty() )
806 return m_data->itemMap.itemInfo( widget );
812 return m_data->itemMap.isEmpty();
825 if ( orientation == Qt::Horizontal )
833#include "moc_qwt_legend.cpp"
Abstract base class for legend widgets.
The QwtDynGridLayout class lays out widgets in a grid, adjusting the number of columns and rows to th...
virtual int heightForWidth(int) const override
virtual QLayoutItem * itemAt(int index) const override
virtual uint columnsForWidth(int width) const
Calculate the number of columns for a given width.
virtual int maxItemWidth() const
uint maxColumns() const
Return the upper limit for the number of columns.
void setMaxColumns(uint maxColumns)
QList< QRect > layoutItems(const QRect &, uint numColumns) const
virtual int count() const override
A paint device for scalable graphics.
QSizeF defaultSize() const
Default size.
void render(QPainter *) const
Replay all recorded painter commands.
Attributes of an entry on a legend.
QVariant value(int role) const
Mode
Mode defining how a legend entry interacts.
virtual QWidget * createWidget(const QwtLegendData &) const
Create a widget to be inserted into the legend.
virtual bool eventFilter(QObject *, QEvent *) override
virtual void renderLegend(QPainter *, const QRectF &, bool fillBackground) const override
void clicked(const QVariant &itemInfo, int index)
QVariant itemInfo(const QWidget *) const
virtual ~QwtLegend()
Destructor.
QScrollBar * verticalScrollBar() const
virtual bool isEmpty() const override
virtual QSize sizeHint() const override
Return a size hint.
QScrollBar * horizontalScrollBar() const
virtual int heightForWidth(int w) const override
QWidget * legendWidget(const QVariant &) const
virtual void renderItem(QPainter *, const QWidget *, const QRectF &, bool fillBackground) const
QWidget * contentsWidget()
QwtLegendData::Mode defaultItemMode() const
virtual int scrollExtent(Qt::Orientation) const override
void setMaxColumns(uint numColums)
Set the maximum number of entries in a row.
QwtLegend(QWidget *parent=NULL)
virtual void updateWidget(QWidget *, const QwtLegendData &)
Update the widget.
void checked(const QVariant &itemInfo, bool on, int index)
virtual void updateLegend(const QVariant &, const QList< QwtLegendData > &) override
Update the entries for an item.
QList< QWidget * > legendWidgets(const QVariant &) const
void setDefaultItemMode(QwtLegendData::Mode)
Set the default mode for legend labels.
A widget representing something on a QwtLegend.
void setItemMode(QwtLegendData::Mode)
const QwtLegendData & data() const
void setData(const QwtLegendData &)
static void drawBackgound(QPainter *, const QRectF &, const QWidget *)
int margin() const
Return label's text margin in pixels.