26 #include <QButtonGroup>
28 #include <QHBoxLayout>
30 #include <QRadioButton>
33 #include <QVBoxLayout>
35 #define YUILogComponent "qt-ui"
36 #include <yui/YUILog.h>
37 #include <yui/YEvent.h>
39 #include "YQItemSelector.h"
40 #include "YQSignalBlocker.h"
44 #define VERBOSE_SELECTION 0
50 bool enforceSingleSelection )
51 : QScrollArea( (QWidget *) parent->widgetRep() )
52 , YItemSelector( parent, enforceSingleSelection )
59 const YItemCustomStatusVector & customStates )
60 : QScrollArea( (QWidget *) parent->widgetRep() )
61 , YItemSelector( parent, customStates )
71 setWidgetResizable(
true );
72 setSizeAdjustPolicy( QAbstractScrollArea::AdjustToContentsOnFirstShow );
74 _itemContainer =
new QWidget(
this );
75 _itemContainer->setObjectName(
"YQItemSelectorItemContainer" );
76 YUI_CHECK_NEW( _itemContainer );
78 QVBoxLayout * outerVBox =
new QVBoxLayout( _itemContainer );
79 YUI_CHECK_NEW( outerVBox );
81 _itemLayout =
new QVBoxLayout();
82 outerVBox->addLayout( _itemLayout );
83 outerVBox->addStretch( 1000 );
85 _buttonGroup =
new QButtonGroup(
this );
86 YUI_CHECK_NEW( _buttonGroup );
88 this->QScrollArea::setWidget( _itemContainer );
100 YUI_CHECK_PTR( itemWidget );
102 _itemLayout->addWidget( itemWidget );
104 if ( enforceSingleSelection() )
111 YUI_CHECK_PTR( item );
112 YItemSelector::addItem( item );
115 YUI_CHECK_NEW( itemWidget );
118 _itemWidgets[ item ] = itemWidget;
120 connect( itemWidget, &pclass( itemWidget )::selectionChanged,
123 if ( item->selected() && enforceSingleSelection() )
130 for ( YItem * item: itemCollection )
140 YUI_THROW( YUIException(
"Can't find selected item" ) );
144 if ( enforceSingleSelection() )
157 YItemSelector::deselectAllItems();
163 for ( QMap<YItem *, YQSelectorItemWidget *>::iterator it = _itemWidgets.begin();
164 it != _itemWidgets.end();
167 if ( it.key() != selectedItem )
169 it.key()->setSelected(
false );
170 it.value()->setSelected(
false );
180 qDeleteAll( _itemWidgets.values() );
181 _itemWidgets.clear();
183 YItemSelector::deleteAllItems();
189 _itemContainer->setEnabled( enabled );
195 int width = _itemContainer->sizeHint().width() + 2;
197 QScrollBar * vScrollBar = verticalScrollBar();
200 width += vScrollBar->sizeHint().width();
208 if ( _itemWidgets.size() <= visibleItems() )
209 return _itemContainer->sizeHint().height() + 2;
221 QList<YQSelectorItemWidget *> visibleItemWidgets =
222 findChildren<YQSelectorItemWidget *>().mid( 0, visibleItems() );
229 height += itemWidget->sizeHint().height();
231 if ( ! visibleItemWidgets.isEmpty() )
233 height += ( visibleItemWidgets.size() + 0.0 ) * _itemLayout->spacing();
234 height += _itemContainer->layout()->contentsMargins().top();
243 resize( newWidth, newHeight );
267 YUI_CHECK_PTR( itemWidget );
269 YItem * item = itemWidget->item();
270 item->setSelected( selected );
274 #if VERBOSE_SELECTION
275 yuiMilestone() <<
"Selected " << item->label() << endl;
278 if ( enforceSingleSelection() )
281 #if VERBOSE_SELECTION
283 yuiMilestone() <<
"Deselected " << item->label() << endl;
286 #if VERBOSE_SELECTION
290 if ( notify() && ( selected || ! enforceSingleSelection() ) )
307 for ( YItemConstIterator it = itemsBegin(); it != itemsEnd(); ++it )
310 _itemWidgets[item]->setLabel( fromUTF8( item->label() ) );
320 : QFrame( parent->itemContainer() )
336 YDescribedItem * describedItem =
dynamic_cast<YDescribedItem *
>(_item);
339 description = describedItem->description();
349 const string & description,
350 const string & iconName,
371 _descriptionLabel = 0;
379 QSizePolicy sizePol( QSizePolicy::Preferred, QSizePolicy::Fixed );
380 sizePol.setHorizontalStretch( 0 );
381 sizePol.setVerticalStretch( 0 );
382 sizePol.setHeightForWidth( sizePolicy().hasHeightForWidth() );
383 setSizePolicy( sizePol );
385 setFrameShape( QFrame::StyledPanel );
386 setFrameShadow( QFrame::Raised );
388 _hBox =
new QHBoxLayout(
this );
389 _hBox->setSpacing( 6 );
390 _hBox->setContentsMargins( -1, 6, 6, 6 );
392 _vBox =
new QVBoxLayout();
393 _vBox->setSpacing( 6 );
394 _vBox->setContentsMargins( 0, 0, 0, 0 );
402 YUI_CHECK_NEW( _headingToggle );
404 _headingToggle->setObjectName(
"YQSelectorItemHeading" );
405 _headingToggle->setChecked(
selected );
407 QFont font( _headingToggle->font() );
408 font.setBold(
true );
409 _headingToggle->setFont( font );
411 _vBox->addWidget( _headingToggle );
412 _hBox->addLayout( _vBox );
419 if ( ! description.empty() )
421 _descriptionLabel =
new QLabel( fromUTF8( description ),
this );
422 YUI_CHECK_NEW( _descriptionLabel );
423 _descriptionLabel->setObjectName(
"YQSelectorItemDescription" );
426 _vBox->addWidget( _descriptionLabel );
434 if ( ! iconName.empty() )
436 _hBox->addStretch( 1000 );
438 _iconLabel =
new QLabel(
"",
this );
439 YUI_CHECK_NEW( _iconLabel );
442 _iconLabel->setPixmap( icon.pixmap( ICON_SIZE ) );
444 _descriptionLabel->setObjectName(
"YQSelectorItemIcon" );
445 _iconLabel->setIndent(0);
447 QSizePolicy sizePol( _iconLabel->sizePolicy() );
448 sizePol.setHorizontalStretch( 0 );
449 sizePol.setVerticalStretch( 0 );
450 _iconLabel->setSizePolicy( sizePol );
452 _hBox->addWidget( _iconLabel );
455 YUI_CHECK_PTR( _parent );
464 QAbstractButton * toggle = 0;
467 toggle =
new QRadioButton( fromUTF8( label ),
this );
469 toggle =
new QCheckBox( fromUTF8( label ),
this );
471 YUI_CHECK_NEW( toggle );
473 connect( toggle, &pclass( _headingToggle )::toggled,
474 this, &pclass(
this )::slotSelectionChanged );
498 return _parent && _parent->enforceSingleSelection();
504 return _headingToggle->isChecked();
511 _headingToggle->setChecked( sel );
515 void YQSelectorItemWidget::slotSelectionChanged(
bool selected )
517 emit selectionChanged(
this,
selected );
523 _headingToggle->setText( label );