27 #include <QListWidget> 28 #include <qnamespace.h> 31 #include <QVBoxLayout> 32 #define YUILogComponent "qt-ui" 33 #include <yui/YUILog.h> 38 #include <yui/YEvent.h> 40 #include "YQApplication.h" 41 #include "YQSelectionBox.h" 42 #include "YQSignalBlocker.h" 44 #include <yui/YUIException.h> 45 #include "YQWidgetCaption.h" 47 #define VERBOSE_SELECTION 1 49 #define DEFAULT_VISIBLE_LINES 5 50 #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() )
133 string iconName = iconFullPath( item );
134 icon = QPixmap( iconName.c_str() );
137 yuiWarning() <<
"Can't load icon " << iconName << std::endl;
142 _qt_listWidget->addItem( fromUTF8( item->label() ) );
146 QListWidgetItem *i =
new QListWidgetItem( _qt_listWidget );
147 i->setData(Qt::DisplayRole, fromUTF8( item->label() ) );
148 i->setData(Qt::DecorationRole, icon );
149 _qt_listWidget->addItem( i );
152 if ( item->selected() )
155 _qt_listWidget->setCurrentItem( _qt_listWidget->item( item->index() ) );
160 _qt_listWidget->scrollToItem( _qt_listWidget->currentItem(),
161 QAbstractItemView::EnsureVisible );
170 YSelectionBox::selectItem( item, selected );
171 _qt_listWidget->setCurrentRow( selected ? item->index() : -1 );
177 YSelectionBox::deselectAllItems();
178 YItem * item = YSelectionBox::itemAt( index );
182 #ifdef VERBOSE_SELECTION 183 yuiDebug() <<
this <<
": Selecting item \"" << item->label() <<
"\"" << std::endl;
186 item->setSelected(
true );
189 YUI_THROW( YUIException(
"Can't find selected item" ) );
195 YSelectionBox::deselectAllItems();
196 _qt_listWidget->clearSelection();
197 _qt_listWidget->setCurrentRow( -1 );
199 if ( _qt_listWidget->currentRow() > -1 )
211 int index = _qt_listWidget->row( _qt_listWidget->currentItem() );
221 _qt_listWidget->clear();
222 YSelectionBox::deleteAllItems();
229 int hintWidth = !_caption->isHidden() ?
230 _caption->sizeHint().width() + frameWidth() : 0;
232 return max( 80, hintWidth );
238 int hintHeight = !_caption->isHidden() ? _caption->sizeHint().height() : 0;
239 int visibleLines = shrinkable() ? SHRINKABLE_VISIBLE_LINES : DEFAULT_VISIBLE_LINES;
240 hintHeight += visibleLines * _qt_listWidget->fontMetrics().lineSpacing();
241 hintHeight += _qt_listWidget->frameWidth() * 2;
243 return max( 80, hintHeight );
249 resize( newWidth, newHeight );
255 _caption->setEnabled( enabled );
256 _qt_listWidget->setEnabled( enabled );
258 YWidget::setEnabled( enabled );
264 _qt_listWidget->setFocus();
272 if ( ev->type() == QEvent::KeyPress )
274 QKeyEvent *
event = ( QKeyEvent * ) ev;
276 if ( ( event->key() == Qt::Key_Return ||
event->key() == Qt::Key_Enter ) &&
277 ( (event->modifiers() & Qt::NoModifier) || (event->modifiers() & Qt::KeypadModifier) ) )
288 else if ( ev->type() == QEvent::MouseButtonRelease )
290 QMouseEvent * mouseEvent =
dynamic_cast<QMouseEvent *
> (ev);
292 if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
294 yuiMilestone() <<
"Right click in selecton box detected" << std::endl;
298 else if ( ev->type() == QEvent::ContextMenu )
300 QContextMenuEvent * contextMenuEvent =
dynamic_cast<QContextMenuEvent *
> (ev);
303 if ( notifyContextMenu() )
307 return QWidget::eventFilter( obj, ev );
313 QList<QListWidgetItem *> items = _qt_listWidget->selectedItems();
315 if ( ! items.empty() )
317 selectItem( _qt_listWidget->row( items.first() ) );
324 if ( hasItems() && hasSelectedItem() )
330 if ( immediateMode() )
334 if ( !
YQUI::ui()->eventsBlocked() )
365 if (
YQUI::ui()->eventPendingFor(
this ) )
369 if ( event && event->reason() != YEvent::SelectionChanged )
374 yuiDebug() <<
"Not overwriting more important event" << std::endl;
381 yuiDebug() <<
"Sending SelectionChanged event for " <<
this << std::endl;
388 yuiDebug() <<
"Starting selbox timer" << std::endl;
389 _timer.setSingleShot(
true );
395 #include "YQSelectionBox.moc" 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...
virtual void addItem(YItem *item)
Add an item.
YQSelectionBox(YWidget *parent, const std::string &label)
Constructor.
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.
YEvent * pendingEvent() const
Returns the last event that isn't processed yet or 0 if there is none.
static YQUI * ui()
Access the global Qt-UI.
void returnImmediately()
Return immediately.