27 #include <QListWidget> 28 #include <qnamespace.h> 30 #include <QVBoxLayout> 31 #define YUILogComponent "qt-ui" 32 #include <yui/YUILog.h> 37 #include <yui/YEvent.h> 39 #include "YQApplication.h" 40 #include "YQSelectionBox.h" 41 #include "YQSignalBlocker.h" 43 #include <yui/YUIException.h> 44 #include "YQWidgetCaption.h" 46 #define VERBOSE_SELECTION 1 48 #define DEFAULT_VISIBLE_LINES 5 49 #define SHRINKABLE_VISIBLE_LINES 2 53 : QFrame( (QWidget *) parent->widgetRep() )
54 , YSelectionBox( parent, label )
58 QVBoxLayout* layout =
new QVBoxLayout(
this );
61 layout->setSpacing( YQWidgetSpacing );
62 layout->setMargin ( YQWidgetMargin );
65 YUI_CHECK_NEW( _caption );
66 layout->addWidget( _caption );
68 _qt_listWidget =
new QListWidget(
this );
69 YUI_CHECK_NEW( _qt_listWidget );
70 layout->addWidget( _qt_listWidget );
72 _qt_listWidget->installEventFilter(
this );
74 _qt_listWidget->setSizePolicy( QSizePolicy( QSizePolicy::Expanding,
75 QSizePolicy::Expanding ) );
77 _caption->setBuddy( _qt_listWidget );
79 connect( _qt_listWidget, &pclass(_qt_listWidget)::itemSelectionChanged,
82 connect( _qt_listWidget, &pclass(_qt_listWidget)::itemDoubleClicked,
85 connect( &_timer, &pclass(&_timer)::timeout,
99 YSelectionBox::setLabel( label );
105 for ( YItemConstIterator it = itemCollection.begin();
106 it != itemCollection.end();
113 _qt_listWidget->scrollToItem( _qt_listWidget->currentItem(),
114 QAbstractItemView::EnsureVisible );
127 YSelectionBox::addItem( item );
130 if ( item->hasIconName() )
132 icon = icon =
YQUI::ui()->loadIcon( item->iconName() );
137 _qt_listWidget->addItem( fromUTF8( item->label() ) );
141 QListWidgetItem *i =
new QListWidgetItem( _qt_listWidget );
142 i->setData(Qt::DisplayRole, fromUTF8( item->label() ) );
143 i->setData(Qt::DecorationRole, icon );
144 _qt_listWidget->addItem( i );
147 if ( item->selected() )
150 _qt_listWidget->setCurrentItem( _qt_listWidget->item( item->index() ) );
155 _qt_listWidget->scrollToItem( _qt_listWidget->currentItem(),
156 QAbstractItemView::EnsureVisible );
165 YSelectionBox::selectItem( item, selected );
166 _qt_listWidget->setCurrentRow( selected ? item->index() : -1 );
172 YSelectionBox::deselectAllItems();
173 YItem * item = YSelectionBox::itemAt( index );
177 #ifdef VERBOSE_SELECTION 178 yuiDebug() <<
this <<
": Selecting item \"" << item->label() <<
"\"" << std::endl;
181 item->setSelected(
true );
184 YUI_THROW( YUIException(
"Can't find selected item" ) );
190 YSelectionBox::deselectAllItems();
191 _qt_listWidget->clearSelection();
192 _qt_listWidget->setCurrentRow( -1 );
194 if ( _qt_listWidget->currentRow() > -1 )
206 int index = _qt_listWidget->row( _qt_listWidget->currentItem() );
216 _qt_listWidget->clear();
217 YSelectionBox::deleteAllItems();
224 int hintWidth = !_caption->isHidden() ?
225 _caption->sizeHint().width() + frameWidth() : 0;
227 return max( 80, hintWidth );
233 int hintHeight = !_caption->isHidden() ? _caption->sizeHint().height() : 0;
234 int visibleLines = shrinkable() ? SHRINKABLE_VISIBLE_LINES : DEFAULT_VISIBLE_LINES;
235 hintHeight += visibleLines * _qt_listWidget->fontMetrics().lineSpacing();
236 hintHeight += _qt_listWidget->frameWidth() * 2;
238 return max( 80, hintHeight );
244 resize( newWidth, newHeight );
250 _caption->setEnabled( enabled );
251 _qt_listWidget->setEnabled( enabled );
253 YWidget::setEnabled( enabled );
259 _qt_listWidget->setFocus();
267 if ( ev->type() == QEvent::KeyPress )
269 QKeyEvent *
event = ( QKeyEvent * ) ev;
271 if ( ( event->key() == Qt::Key_Return ||
event->key() == Qt::Key_Enter ) &&
272 ( (event->modifiers() & Qt::NoModifier) || (event->modifiers() & Qt::KeypadModifier) ) )
283 else if ( ev->type() == QEvent::MouseButtonRelease )
285 QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (ev);
287 if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
289 yuiMilestone() <<
"Right click in selecton box detected" << std::endl;
293 else if ( ev->type() == QEvent::ContextMenu )
295 QContextMenuEvent * contextMenuEvent = dynamic_cast<QContextMenuEvent *> (ev);
298 if ( notifyContextMenu() )
302 return QWidget::eventFilter( obj, ev );
308 QList<QListWidgetItem *> items = _qt_listWidget->selectedItems();
310 if ( ! items.empty() )
312 selectItem( _qt_listWidget->row( items.first() ) );
319 if ( hasItems() && hasSelectedItem() )
325 if ( immediateMode() )
329 if ( !
YQUI::ui()->eventsBlocked() )
360 if (
YQUI::ui()->eventPendingFor(
this ) )
362 YWidgetEvent *
event = dynamic_cast<YWidgetEvent *> (
YQUI::ui()->pendingEvent() );
364 if ( event && event->reason() != YEvent::SelectionChanged )
369 yuiDebug() <<
"Not overwriting more important event" << std::endl;
376 yuiDebug() <<
"Sending SelectionChanged event for " <<
this << std::endl;
383 yuiDebug() <<
"Starting selbox timer" << std::endl;
384 _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.
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.
static YQUI * ui()
Access the global Qt-UI.
void returnImmediately()
Return immediately.