28 #include <QVBoxLayout>
29 #include <QHeaderView>
31 #define YUILogComponent "qt-ui"
32 #include <yui/YUILog.h>
38 #include <yui/YEvent.h>
39 #include "YQMultiSelectionBox.h"
40 #include "YQSignalBlocker.h"
41 #include "YQWidgetCaption.h"
43 #define DEFAULT_VISIBLE_LINES 5
44 #define SHRINKABLE_VISIBLE_LINES 2
48 const std::string & label )
49 : QFrame( (QWidget *) parent->widgetRep() )
50 , YMultiSelectionBox( parent, label )
52 QVBoxLayout* layout =
new QVBoxLayout(
this );
57 layout->setSpacing( YQWidgetSpacing );
58 layout->setMargin( YQWidgetMargin );
61 YUI_CHECK_NEW( _caption );
62 layout->addWidget( _caption );
64 _qt_listView =
new QTreeWidget(
this );
65 YUI_CHECK_NEW( _qt_listView );
66 layout->addWidget( _qt_listView );
68 _qt_listView->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
69 _qt_listView->setHeaderLabel(
"");
70 _qt_listView->setSortingEnabled(
false );
72 _qt_listView->header()->hide();
73 _qt_listView->setRootIsDecorated (
false );
74 _caption->setBuddy( _qt_listView );
78 connect( _qt_listView, &pclass(_qt_listView)::itemSelectionChanged,
84 connect( _qt_listView, &pclass(_qt_listView)::itemChanged,
85 this, &pclass(
this)::slotItemChanged );
99 YMultiSelectionBox::setLabel( label );
107 YMultiSelectionBox::addItem( yItem );
111 YUI_CHECK_NEW( msbItem );
115 msbItem->setCheckState(0, yItem->selected() ? Qt::Checked : Qt::Unchecked );
119 if ( ! _qt_listView->currentItem() )
120 _qt_listView->setCurrentItem( msbItem );
126 YMultiSelectionBox::selectItem( yItem, selected );
130 msbItem->setCheckState( 0, selected ? Qt::Checked : Qt::Unchecked );
138 YMultiSelectionBox::deselectAllItems();
140 QTreeWidgetItemIterator it( _qt_listView );
147 item->setCheckState(0, Qt::Unchecked);
159 YMultiSelectionBox::deleteAllItems();
160 _qt_listView->clear();
189 QTreeWidgetItem * currentQItem = _qt_listView->currentItem();
196 return item->
yItem();
216 _qt_listView->clearSelection();
223 _qt_listView->setCurrentItem( msbItem );
234 _caption->setEnabled( enabled );
235 _qt_listView->setEnabled( enabled );
237 YWidget::setEnabled( enabled );
243 int hintWidth = (!_caption->isHidden()) ?
244 _caption->sizeHint().width() + frameWidth() : 0;
246 return max( 80, hintWidth );
252 int hintHeight = (!_caption->isHidden()) ? _caption->sizeHint().height() : 0;
253 int visibleLines = shrinkable() ? SHRINKABLE_VISIBLE_LINES : DEFAULT_VISIBLE_LINES;
254 hintHeight += visibleLines * _qt_listView->fontMetrics().lineSpacing();
255 hintHeight += _qt_listView->frameWidth() * 2;
257 return max( 80, hintHeight );
264 resize( newWidth, newHeight );
271 _qt_listView->setFocus();
282 if ( !
YQUI::ui()->eventPendingFor(
this ) )
301 YQMultiSelectionBox::slotItemChanged ( QTreeWidgetItem * _item,
int )
304 bool selected = item->checkState( 0 );
305 item->
yItem()->setSelected( selected );
313 QTreeWidgetItemIterator it( _qt_listView );
319 if ( item && item->
yItem() == wantedItem )
334 QTreeWidget * listView,
336 : QTreeWidgetItem( listView )
338 , _multiSelectionBox( parent )
340 YUI_CHECK_PTR( yItem );
341 setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );
342 setText(0, fromUTF8( yItem->label() ));
343 setCheckState( 0, Qt::Unchecked );
346 if ( yItem->hasIconName() )
350 string iconName = parent->iconFullPath( yItem->iconName() );
351 QPixmap icon = QPixmap( iconName.c_str() );
354 yuiWarning() <<
"Can't load icon " << iconName << std::endl;
369 #include "YQMultiSelectionBox.moc"
void slotValueChanged()
Send ValueChanged event if opt(notify) is set.
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual int preferredWidth()
Preferred width of the widget.
static int _item_count
The next serial number to use.
virtual int preferredHeight()
Preferred height of the widget.
YQMultiSelectionBox(YWidget *parent, const std::string &label)
Constructor.
YItem * yItem() const
Return the corresponding YItem.
virtual void deleteAllItems()
Delete all items.
YQMultiSelectionBoxItem(YQMultiSelectionBox *parent, QTreeWidget *listView, YItem *yItem)
Constructor.
virtual YItem * currentItem()
Return the the item that currently has the keyboard focus or 0 if no item currently has the keyboard ...
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
void valueChanged()
Emitted upon when an item changes its state.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
virtual void setLabel(const std::string &label)
Change the label text.
virtual void deselectAllItems()
Deselect all items.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
YQMultiSelectionBoxItem * findItem(YItem *item)
Find the counterpart of 'item'.
virtual void setCurrentItem(YItem *item)
Set the keyboard focus to the specified item.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
void slotSelected()
Send SelectionChanged event if opt(notify) is set.
~YQMultiSelectionBox()
Destructor.
static YQUI * ui()
Access the global Qt-UI.
virtual void addItem(YItem *item)
Add an item.