28 #include <QVBoxLayout>
29 #include <QHeaderView>
31 #define YUILogComponent "qt-ui"
32 #include <yui/YUILog.h>
36 #include <yui/YEvent.h>
37 #include "YQMultiSelectionBox.h"
38 #include "YQSignalBlocker.h"
39 #include "YQWidgetCaption.h"
41 #define DEFAULT_VISIBLE_LINES 5
42 #define SHRINKABLE_VISIBLE_LINES 2
48 const 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 std::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 std::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() )
351 if ( !icon.isNull() )
static int _item_count
The next serial number to use.
YQMultiSelectionBoxItem(YQMultiSelectionBox *parent, QTreeWidget *listView, YItem *yItem)
Constructor.
YItem * yItem() const
Return the corresponding YItem.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
void slotSelected()
Send SelectionChanged event if opt(notify) is set.
virtual void deleteAllItems()
Delete all items.
virtual void setCurrentItem(YItem *item)
Set the keyboard focus to the specified item.
virtual void deselectAllItems()
Deselect all items.
~YQMultiSelectionBox()
Destructor.
virtual YItem * currentItem()
Return the the item that currently has the keyboard focus or 0 if no item currently has the keyboard ...
virtual int preferredHeight()
Preferred height of the widget.
virtual void setLabel(const std::string &label)
Change the label text.
virtual void addItem(YItem *item)
Add an item.
YQMultiSelectionBoxItem * findItem(YItem *item)
Find the counterpart of 'item'.
YQMultiSelectionBox(YWidget *parent, const std::string &label)
Constructor.
void valueChanged()
Emitted upon when an item changes its state.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual int preferredWidth()
Preferred width of the widget.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
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.
static YQUI * ui()
Access the global Qt-UI.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
QIcon loadIcon(const string &iconName) const
Load an icon.