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() ) )
306 : QFrame( parent->itemContainer() )
322 YDescribedItem * describedItem =
dynamic_cast<YDescribedItem *
>(_item);
325 description = describedItem->description();
335 const string & description,
336 const string & iconName,
357 _descriptionLabel = 0;
365 QSizePolicy sizePol( QSizePolicy::Preferred, QSizePolicy::Fixed );
366 sizePol.setHorizontalStretch( 0 );
367 sizePol.setVerticalStretch( 0 );
368 sizePol.setHeightForWidth( sizePolicy().hasHeightForWidth() );
369 setSizePolicy( sizePol );
371 setFrameShape( QFrame::StyledPanel );
372 setFrameShadow( QFrame::Raised );
374 _hBox =
new QHBoxLayout(
this );
375 _hBox->setSpacing( 6 );
376 _hBox->setContentsMargins( -1, 6, 6, 6 );
378 _vBox =
new QVBoxLayout();
379 _vBox->setSpacing( 6 );
380 _vBox->setContentsMargins( 0, 0, 0, 0 );
388 YUI_CHECK_NEW( _headingToggle );
390 _headingToggle->setObjectName(
"YQSelectorItemHeading" );
391 _headingToggle->setChecked(
selected );
393 QFont font( _headingToggle->font() );
394 font.setBold(
true );
395 _headingToggle->setFont( font );
397 _vBox->addWidget( _headingToggle );
398 _hBox->addLayout( _vBox );
405 if ( ! description.empty() )
407 _descriptionLabel =
new QLabel( fromUTF8( description ),
this );
408 YUI_CHECK_NEW( _descriptionLabel );
409 _descriptionLabel->setObjectName(
"YQSelectorItemDescription" );
412 _vBox->addWidget( _descriptionLabel );
420 if ( ! iconName.empty() )
422 _hBox->addStretch( 1000 );
424 _iconLabel =
new QLabel(
"",
this );
425 YUI_CHECK_NEW( _iconLabel );
428 _iconLabel->setPixmap( icon.pixmap( ICON_SIZE ) );
430 _descriptionLabel->setObjectName(
"YQSelectorItemIcon" );
431 _iconLabel->setIndent(0);
433 QSizePolicy sizePol( _iconLabel->sizePolicy() );
434 sizePol.setHorizontalStretch( 0 );
435 sizePol.setVerticalStretch( 0 );
436 _iconLabel->setSizePolicy( sizePol );
438 _hBox->addWidget( _iconLabel );
441 YUI_CHECK_PTR( _parent );
450 QAbstractButton * toggle = 0;
453 toggle =
new QRadioButton( fromUTF8( label ),
this );
455 toggle =
new QCheckBox( fromUTF8( label ),
this );
457 YUI_CHECK_NEW( toggle );
459 connect( toggle, &pclass( _headingToggle )::toggled,
460 this, &pclass(
this )::slotSelectionChanged );
484 return _parent && _parent->enforceSingleSelection();
490 return _headingToggle->isChecked();
497 _headingToggle->setChecked( sel );
501 void YQSelectorItemWidget::slotSelectionChanged(
bool selected )
503 emit selectionChanged(
this,
selected );
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
virtual ~YQItemSelector()
Destructor.
YQItemSelector(YWidget *parent, bool enforceSingleSelection=true)
Standard constructor.
virtual int preferredWidth()
Preferred width of the widget.
QIcon loadIcon(const string &iconName) const
Load an icon.
void init()
Common initializations for all constructors.
void deselectOtherItems(YItem *selectedItem)
Deselect all items except 'selectedItem'.
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
virtual void deselectAllItems()
Deselect all items.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
void slotSelectionChanged(YQSelectorItemWidget *itemWidget, bool selected)
Notification that an item has been selected.
virtual int preferredHeight()
Preferred height of the widget.
virtual void activateItem(YItem *item)
Activate selected item.
virtual void addItem(YItem *item)
Add an item.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual void deleteAllItems()
Delete all items.
static YQUI * ui()
Access the global Qt-UI.
void addItemWidget(YQSelectorItemWidget *itemWidget)
Add an item widget to the appropriate layout.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.