25 #include <QHeaderView> 26 #include <QVBoxLayout> 28 #define YUILogComponent "qt-ui" 29 #include <yui/YUILog.h> 33 #include <yui/YEvent.h> 34 #include "YQSignalBlocker.h" 35 #include <yui/YUIException.h> 37 #include "QY2ListView.h" 39 #include "YQApplication.h" 43 YQTable::YQTable( YWidget * parent, YTableHeader * tableHeader,
bool multiSelectionMode )
44 : QFrame( (QWidget *) parent->widgetRep() )
45 , YTable( parent, tableHeader, multiSelectionMode )
48 QVBoxLayout* layout =
new QVBoxLayout(
this );
49 layout->setSpacing( 0 );
52 layout->setMargin( YQWidgetMargin );
55 YUI_CHECK_NEW( _qt_listView );
56 layout->addWidget( _qt_listView );
57 _qt_listView->setAllColumnsShowFocus(
true );
58 _qt_listView->header()->setStretchLastSection(
false );
62 if ( multiSelectionMode )
63 _qt_listView->setSelectionMode( QAbstractItemView::ExtendedSelection );
65 _qt_listView->setContextMenuPolicy( Qt::CustomContextMenu );
72 _qt_listView->setColumnCount( columns() );
74 for (
int i=0; i < columns(); i++ )
76 headers << fromUTF8( header(i) );
79 _qt_listView->setHeaderLabels( headers );
80 _qt_listView->header()->setSectionResizeMode( QHeaderView::Interactive );
81 _qt_listView->sortItems( 0, Qt::AscendingOrder);
88 connect( _qt_listView, &pclass(_qt_listView)::itemDoubleClicked,
91 connect( _qt_listView, &pclass(_qt_listView)::customContextMenuRequested,
94 if ( multiSelectionMode )
97 connect( _qt_listView, &pclass(_qt_listView)::itemSelectionChanged,
102 connect( _qt_listView, &pclass(_qt_listView)::currentItemChanged,
117 YTable::setKeepSorting( keepSorting );
119 _qt_listView->setSortingEnabled( ! keepSorting );
135 YTableItem * item =
dynamic_cast<YTableItem *
> (yitem);
136 YUI_CHECK_PTR( item );
138 YTable::addItem( item );
141 YUI_CHECK_NEW( clone );
143 if ( ! batchMode && item->selected() )
156 for (
int col=0; col < columns(); col++ )
158 switch ( alignment( col ) )
160 case YAlignBegin: clone->setTextAlignment( col, Qt::AlignLeft | Qt::AlignVCenter );
break;
161 case YAlignCenter: clone->setTextAlignment( col, Qt::AlignCenter | Qt::AlignVCenter );
break;
162 case YAlignEnd: clone->setTextAlignment( col, Qt::AlignRight | Qt::AlignVCenter );
break;
164 case YAlignUnchanged:
break;
169 _qt_listView->sortItems( 0, Qt::AscendingOrder);
171 if ( resizeColumnsToContent )
173 for (
int i=0; i < columns(); i++ )
174 _qt_listView->resizeColumnToContents( i );
185 for ( YItemConstIterator it = itemCollection.begin();
186 it != itemCollection.end();
197 YItem * sel = YSelectionWidget::selectedItem();
202 for (
int i=0; i < columns(); i++ )
203 _qt_listView->resizeColumnToContents( i );
212 YTableItem * item =
dynamic_cast<YTableItem *
> (yitem);
213 YUI_CHECK_PTR( item );
216 YUI_CHECK_PTR( clone );
219 if ( ! selected && clone == _qt_listView->currentItem() )
225 if ( ! hasMultiSelection() )
226 _qt_listView->setCurrentItem( clone );
228 clone->setSelected(
true );
229 YTable::selectItem( item, selected );
239 YTable::deselectAllItems();
240 _qt_listView->clearSelection();
247 _qt_listView->
clear();
248 YTable::deleteAllItems();
255 YTableItem * item = cell->parent();
256 YUI_CHECK_PTR( item );
259 YUI_CHECK_PTR( clone );
271 YUI_CHECK_PTR( tableListViewItem );
273 YTable::selectItem( tableListViewItem->
origItem(), true );
287 if ( hasItems() && YSelectionWidget::hasSelectedItem() )
291 if ( immediateMode() )
293 if ( !
YQUI::ui()->eventPendingFor(
this ) )
297 yuiDebug() <<
"Sending SelectionChanged event" << std::endl;
307 YSelectionWidget::deselectAllItems();
308 yuiDebug() << std::endl;
310 QList<QTreeWidgetItem *> selItems = _qt_listView->selectedItems();
312 for ( QList<QTreeWidgetItem *>::iterator it = selItems.begin();
313 it != selItems.end();
318 if ( tableListViewItem )
320 tableListViewItem->
origItem()->setSelected(
true );
322 yuiDebug() <<
"Selected item: " << tableListViewItem->
origItem()->label() << std::endl;
326 if ( immediateMode() )
328 if ( !
YQUI::ui()->eventPendingFor(
this ) )
332 yuiDebug() <<
"Sending SelectionChanged event" << std::endl;
346 yuiDebug() <<
"Sending Activated event" << std::endl;
355 _qt_listView->setEnabled( enabled );
357 YWidget::setEnabled( enabled );
385 resize( newWidth, newHeight );
392 _qt_listView->setFocus();
401 if ( ! _qt_listView || ! _qt_listView->viewport() )
405 if ( notifyContextMenu() )
415 YTableItem * origItem )
418 , _origItem( origItem )
420 YUI_CHECK_PTR( _table );
421 YUI_CHECK_PTR( _origItem );
423 _origItem->setData(
this );
425 for ( YTableCellIterator it = _origItem->cellsBegin();
426 it != _origItem->cellsEnd();
440 int column = cell->column();
446 setText( column, fromUTF8( cell->label() ) );
453 if ( cell->hasIconName() )
457 string iconName = _table->iconFullPath( cell->iconName() );
458 QPixmap icon = QPixmap( iconName.c_str() );
461 yuiWarning() <<
"Can't load icon " << iconName << std::endl;
463 setData( column, Qt::DecorationRole, icon );
467 if ( ! data( column, Qt::DecorationRole ).isNull() )
469 setData( column, Qt::DecorationRole, QPixmap() );
475 #include "YQTable.moc" Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
YTableItem * origItem() const
Return the corresponding YTableItem.
virtual void setSortByInsertionSequence(bool sortByInsertionSequence)
Enforce sorting by item insertion order (true) or let user change sorting by clicking on a column hea...
virtual void addItem(YItem *item)
Add an item.
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
void slotContextMenu(const QPoint &pos)
Propagate a context menu selection.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
YQTableListViewItem(YQTable *table, QY2ListView *parent, YTableItem *origItem)
Constructor.
void slotSelected(QTreeWidgetItem *)
Notification that an item is selected (single click or keyboard).
virtual void setKeepSorting(bool keepSorting)
Switch between sorting by item insertion order (keepSorting: true) or allowing the user to sort by an...
virtual void clear()
Reimplemented from Q3ListView: Adjust header sizes after clearing contents.
virtual void deleteAllItems()
Delete all items.
void selectOrigItem(QTreeWidgetItem *listViewItem)
Select the original item (the YTableItem) that corresponds to the specified listViewItem.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
Visual representation of a YTableItem.
virtual void deselectAllItems()
Deselect all items.
virtual void setContextMenuPos(QPoint contextMenuPos)
Sets the position of the context menu (in gloabl coordinates)
virtual void cellChanged(const YTableCell *cell)
Notification that a cell (its text and/or its icon) was changed from the outside. ...
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
YQTable(YWidget *parent, YTableHeader *header, bool multiSelection)
Constructor.
virtual ~YQTable()
Destructor.
void updateCell(const YTableCell *cell)
Update this item's display with the content of 'cell'.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
void slotActivated(QTreeWidgetItem *)
Notification that an item is activated (double click or keyboard).
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
virtual int preferredWidth()
Preferred width of the widget.
Enhanced QTreeWidgetItem.
virtual int preferredHeight()
Preferred height of the widget.
void slotSelectionChanged()
Notification that the item selection changed (relevant for multiSelection mode).
static YQUI * ui()
Access the global Qt-UI.