27 #include <QListWidget> 28 #include <qnamespace.h> 30 #include <QVBoxLayout> 31 #define YUILogComponent "qt-ui" 32 #include <yui/YUILog.h> 35 #include <yui/YEvent.h> 37 #include "YQApplication.h" 38 #include "YQSelectionBox.h" 39 #include "YQSignalBlocker.h" 41 #include <yui/YUIException.h> 42 #include "YQWidgetCaption.h" 44 #define VERBOSE_SELECTION 1 46 #define DEFAULT_VISIBLE_LINES 5 47 #define SHRINKABLE_VISIBLE_LINES 2 54 : QFrame( (QWidget *) parent->widgetRep() )
55 , YSelectionBox( parent, label )
59 QVBoxLayout* layout =
new QVBoxLayout(
this );
62 layout->setSpacing( YQWidgetSpacing );
63 layout->setMargin ( YQWidgetMargin );
66 YUI_CHECK_NEW( _caption );
67 layout->addWidget( _caption );
69 _qt_listWidget =
new QListWidget(
this );
70 YUI_CHECK_NEW( _qt_listWidget );
71 layout->addWidget( _qt_listWidget );
73 _qt_listWidget->installEventFilter(
this );
75 _qt_listWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding,
76 QSizePolicy::Expanding ) );
78 _caption->setBuddy( _qt_listWidget );
80 connect( _qt_listWidget, &pclass(_qt_listWidget)::itemSelectionChanged,
83 connect( _qt_listWidget, &pclass(_qt_listWidget)::itemDoubleClicked,
86 connect( &_timer, &pclass(&_timer)::timeout,
100 YSelectionBox::setLabel( label );
106 for ( YItemConstIterator it = itemCollection.begin();
107 it != itemCollection.end();
114 _qt_listWidget->scrollToItem( _qt_listWidget->currentItem(),
115 QAbstractItemView::EnsureVisible );
128 YSelectionBox::addItem( item );
131 if ( item->hasIconName() )
138 _qt_listWidget->addItem( fromUTF8( item->label() ) );
142 QListWidgetItem *i =
new QListWidgetItem( _qt_listWidget );
143 i->setData(Qt::DisplayRole, fromUTF8( item->label() ) );
144 i->setData(Qt::DecorationRole, icon );
145 _qt_listWidget->addItem( i );
148 if ( item->selected() )
151 _qt_listWidget->setCurrentItem( _qt_listWidget->item( item->index() ) );
156 _qt_listWidget->scrollToItem( _qt_listWidget->currentItem(),
157 QAbstractItemView::EnsureVisible );
166 YSelectionBox::selectItem( item, selected );
167 _qt_listWidget->setCurrentRow( selected ? item->index() : -1 );
173 YSelectionBox::deselectAllItems();
174 YItem * item = YSelectionBox::itemAt( index );
178 #ifdef VERBOSE_SELECTION 179 yuiDebug() <<
this <<
": Selecting item \"" << item->label() <<
"\"" << endl;
182 item->setSelected(
true );
185 YUI_THROW( YUIException(
"Can't find selected item" ) );
191 YSelectionBox::deselectAllItems();
192 _qt_listWidget->clearSelection();
193 _qt_listWidget->setCurrentRow( -1 );
195 if ( _qt_listWidget->currentRow() > -1 )
207 int index = _qt_listWidget->row( _qt_listWidget->currentItem() );
217 _qt_listWidget->clear();
218 YSelectionBox::deleteAllItems();
225 int hintWidth = !_caption->isHidden() ?
226 _caption->sizeHint().width() + frameWidth() : 0;
228 return std::max( 80, hintWidth );
234 int hintHeight = !_caption->isHidden() ? _caption->sizeHint().height() : 0;
235 int visibleLines = shrinkable() ? SHRINKABLE_VISIBLE_LINES : DEFAULT_VISIBLE_LINES;
236 hintHeight += visibleLines * _qt_listWidget->fontMetrics().lineSpacing();
237 hintHeight += _qt_listWidget->frameWidth() * 2;
239 return std::max( 80, hintHeight );
245 resize( newWidth, newHeight );
251 _caption->setEnabled( enabled );
252 _qt_listWidget->setEnabled( enabled );
254 YWidget::setEnabled( enabled );
260 _qt_listWidget->setFocus();
268 if ( ev->type() == QEvent::KeyPress )
270 QKeyEvent *
event = ( QKeyEvent * ) ev;
272 if ( ( event->key() == Qt::Key_Return ||
event->key() == Qt::Key_Enter ) &&
273 ( (event->modifiers() & Qt::NoModifier) || (event->modifiers() & Qt::KeypadModifier) ) )
284 else if ( ev->type() == QEvent::MouseButtonRelease )
286 QMouseEvent * mouseEvent =
dynamic_cast<QMouseEvent *
> (ev);
288 if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
290 yuiMilestone() <<
"Right click in selecton box detected" << endl;
294 else if ( ev->type() == QEvent::ContextMenu )
296 QContextMenuEvent * contextMenuEvent =
dynamic_cast<QContextMenuEvent *
> (ev);
299 if ( notifyContextMenu() )
303 return QWidget::eventFilter( obj, ev );
309 QList<QListWidgetItem *> items = _qt_listWidget->selectedItems();
311 if ( ! items.empty() )
313 selectItem( _qt_listWidget->row( items.first() ) );
320 if ( hasItems() && hasSelectedItem() )
326 if ( immediateMode() )
330 if ( !
YQUI::ui()->eventsBlocked() )
361 if (
YQUI::ui()->eventPendingFor(
this ) )
365 if ( event && event->reason() != YEvent::SelectionChanged )
370 yuiDebug() <<
"Not overwriting more important event" << endl;
377 yuiDebug() <<
"Sending SelectionChanged event for " <<
this << endl;
384 yuiDebug() <<
"Starting selbox timer" << endl;
385 _timer.setSingleShot(
true );
virtual bool eventFilter(QObject *obj, QEvent *ev)
Event filter.
virtual void deleteAllItems()
Delete all items.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
void slotActivated(QListWidgetItem *item)
Notification that an item has been activated (double clicked).
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
void maybeLeftHandedUser()
A mouse click with the wrong mouse button was detected - e.g., a right click on a push button...
YEvent * pendingEvent() const
Returns the last event that isn't processed yet or 0 if there is none.
virtual void addItem(YItem *item)
Add an item.
YQSelectionBox(YWidget *parent, const std::string &label)
Constructor.
QIcon loadIcon(const string &iconName) const
Load an icon.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual void setContextMenuPos(QPoint contextMenuPos)
Sets the position of the context menu (in gloabl coordinates)
void returnDelayed()
Return after some millseconds delay - collect multiple events.
virtual void deselectAllItems()
Deselect all items.
virtual int preferredWidth()
Preferred width of the widget.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
virtual int preferredHeight()
Preferred height of the widget.
virtual ~YQSelectionBox()
Destructor.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
bool activateDefaultButton(bool warn=true)
Activate (i.e.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
void slotSelectionChanged()
Notification that an item has been selected.
virtual void setLabel(const std::string &label)
Change the label text.
static YQUI * ui()
Access the global Qt-UI.
void returnImmediately()
Return immediately.