17 #include <qapplication.h> 18 #include <qscrollbar.h> 19 #include <qscrollarea.h> 28 inline bool isEmpty()
const {
return d_entries.isEmpty(); }
31 void remove(
const QVariant & );
33 void removeWidget(
const QWidget * );
36 QVariant itemInfo(
const QWidget * )
const;
55 void QwtLegendMap::insert(
const QVariant &itemInfo,
58 for (
int i = 0; i < d_entries.size(); i++ )
60 Entry &entry = d_entries[i];
61 if ( entry.itemInfo == itemInfo )
63 entry.widgets = widgets;
69 newEntry.itemInfo = itemInfo;
70 newEntry.widgets = widgets;
72 d_entries += newEntry;
77 for (
int i = 0; i < d_entries.size(); i++ )
79 Entry &entry = d_entries[i];
80 if ( entry.itemInfo == itemInfo )
82 d_entries.removeAt( i );
88 void QwtLegendMap::removeWidget(
const QWidget *widget )
90 QWidget *w =
const_cast<QWidget *
>( widget );
92 for (
int i = 0; i < d_entries.size(); i++ )
93 d_entries[ i ].widgets.removeAll( w );
96 QVariant QwtLegendMap::itemInfo(
const QWidget *widget )
const 100 QWidget *w =
const_cast<QWidget *
>( widget );
102 for (
int i = 0; i < d_entries.size(); i++ )
104 const Entry &entry = d_entries[i];
105 if ( entry.widgets.indexOf( w ) >= 0 )
106 return entry.itemInfo;
113 QList<QWidget *> QwtLegendMap::legendWidgets(
const QVariant &itemInfo )
const 115 if ( itemInfo.isValid() )
117 for (
int i = 0; i < d_entries.size(); i++ )
119 const Entry &entry = d_entries[i];
120 if ( entry.itemInfo == itemInfo )
121 return entry.widgets;
144 class QwtLegend::PrivateData::LegendView
QWT_FINAL:
public QScrollArea
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 );
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 );
194 bool ok = QScrollArea::viewportEvent( event );
196 if ( event->type() == QEvent::Resize )
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 );
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 );
261 setFrameStyle( NoFrame );
265 d_data->
view =
new QwtLegend::PrivateData::LegendView(
this );
266 d_data->
view->setObjectName(
"QwtLegendView" );
271 gridLayout->setAlignment( Qt::AlignHCenter | Qt::AlignTop );
273 d_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 =
d_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 );
507 QLayout *contentsLayout =
d_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 =
d_data->
view->contentsWidget->sizeHint();
529 hint += QSize( 2 * frameWidth(), 2 * frameWidth() );
540 width -= 2 * frameWidth();
542 int h =
d_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() );
622 if ( itemInfo.isValid() )
627 const int index = widgetList.indexOf( w );
629 Q_EMIT
clicked( itemInfo, index );
640 QWidget *w = qobject_cast<QWidget *>( sender() );
644 if ( itemInfo.isValid() )
649 const int index = widgetList.indexOf( w );
651 Q_EMIT
checked( itemInfo, on, index );
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 ) )
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 labelFont.resolve( QFont::AllPropertiesResolved );
761 painter->setFont( labelFont );
762 painter->setPen( label->palette().color( QPalette::Text ) );
764 const_cast< QwtLegendLabel *
>( label )->drawText( painter, titleRect );
787 if ( list.isEmpty() )
821 if ( orientation == Qt::Horizontal )
830 #include "moc_qwt_legend.cpp" A widget representing something on a QwtLegend.
virtual bool event(QEvent *event) QWT_OVERRIDE
virtual int heightForWidth(int) const QWT_OVERRIDE
virtual int maxItemWidth() const
void render(QPainter *) const
Replay all recorded painter commands.
virtual bool eventFilter(QObject *, QEvent *) QWT_OVERRIDE
virtual bool viewportEvent(QEvent *event) QWT_OVERRIDE
virtual bool isEmpty() const QWT_OVERRIDE
virtual uint columnsForWidth(int width) const
Calculate the number of columns for a given width.
QScrollBar * horizontalScrollBar() const
const QwtLegendData & data() const
QSizeF defaultSize() const
Default size.
int qwtFloor(qreal value)
QwtLegendData::Mode defaultItemMode() const
void setDefaultItemMode(QwtLegendData::Mode)
Set the default mode for legend labels.
QVariant itemInfo(const QWidget *) const
The QwtDynGridLayout class lays out widgets in a grid, adjusting the number of columns and rows to th...
virtual void updateLegend(const QVariant &, const QList< QwtLegendData > &) QWT_OVERRIDE
Update the entries for an item.
QScrollBar * verticalScrollBar() const
virtual int heightForWidth(int w) const QWT_OVERRIDE
void setMaxColumns(uint maxColumns)
virtual QLayoutItem * itemAt(int index) const QWT_OVERRIDE
QVariant value(int role) const
QList< QRect > layoutItems(const QRect &, uint numColumns) const
A paint device for scalable graphics.
QwtLegend(QWidget *parent=NULL)
QwtLegendData::Mode itemMode
static void drawBackgound(QPainter *, const QRectF &, const QWidget *)
QSize viewportSize(int w, int h) const
virtual void renderLegend(QPainter *, const QRectF &, bool fillBackground) const QWT_OVERRIDE
virtual QSize sizeHint() const QWT_OVERRIDE
Return a size hint.
QList< QWidget * > legendWidgets(const QVariant &) const
QWidget * legendWidget(const QVariant &) const
Abstract base class for legend widgets.
QWidget * contentsWidget()
int margin
Return label's text margin in pixels.
virtual QWidget * createWidget(const QwtLegendData &) const
Create a widget to be inserted into the legend.
virtual ~QwtLegend()
Destructor.
virtual int scrollExtent(Qt::Orientation) const QWT_OVERRIDE
void setMaxColumns(uint numColums)
Set the maximum number of entries in a row.
void setData(const QwtLegendData &)
void remove(lua_State *L, int rawindex, int count)
void checked(const QVariant &itemInfo, bool on, int index)
void clicked(const QVariant &itemInfo, int index)
virtual void updateWidget(QWidget *, const QwtLegendData &)
Update the widget.
Mode
Mode defining how a legend entry interacts.
uint maxColumns() const
Return the upper limit for the number of columns.
virtual int count() const QWT_OVERRIDE
Attributes of an entry on a legend.
virtual void renderItem(QPainter *, const QWidget *, const QRectF &, bool fillBackground) const
void setItemMode(QwtLegendData::Mode)
LegendView(QWidget *parent)