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" 47 YTableHeader * tableHeader,
48 bool multiSelectionMode )
49 : QFrame( (QWidget *) parent->widgetRep() )
50 , YTable( parent, tableHeader, multiSelectionMode )
53 QVBoxLayout* layout =
new QVBoxLayout(
this );
54 layout->setSpacing( 0 );
57 layout->setMargin( YQWidgetMargin );
60 YUI_CHECK_NEW( _qt_listView );
61 layout->addWidget( _qt_listView );
62 _qt_listView->setAllColumnsShowFocus(
true );
63 _qt_listView->header()->setStretchLastSection(
false );
67 if ( multiSelectionMode )
68 _qt_listView->setSelectionMode( QAbstractItemView::ExtendedSelection );
70 _qt_listView->setContextMenuPolicy( Qt::CustomContextMenu );
77 _qt_listView->setColumnCount( columns() );
79 for (
int i=0; i < columns(); i++ )
81 headers << fromUTF8( header(i) );
84 _qt_listView->setHeaderLabels( headers );
85 _qt_listView->header()->setSectionResizeMode( QHeaderView::Interactive );
86 _qt_listView->sortItems( 0, Qt::AscendingOrder);
93 connect( _qt_listView, &pclass(_qt_listView)::itemDoubleClicked,
96 connect( _qt_listView, &pclass(_qt_listView)::customContextMenuRequested,
99 if ( multiSelectionMode )
102 connect( _qt_listView, &pclass(_qt_listView)::itemSelectionChanged,
107 connect( _qt_listView, &pclass(_qt_listView)::currentItemChanged,
122 YTable::setKeepSorting( keepSorting );
124 _qt_listView->setSortingEnabled( ! keepSorting );
140 YTableItem * item =
dynamic_cast<YTableItem *
> (yitem);
141 YUI_CHECK_PTR( item );
143 YTable::addItem( item );
146 YUI_CHECK_NEW( clone );
148 if ( ! batchMode && item->selected() )
161 for (
int col=0; col < columns(); col++ )
163 switch ( alignment( col ) )
165 case YAlignBegin: clone->setTextAlignment( col, Qt::AlignLeft | Qt::AlignVCenter );
break;
166 case YAlignCenter: clone->setTextAlignment( col, Qt::AlignCenter | Qt::AlignVCenter );
break;
167 case YAlignEnd: clone->setTextAlignment( col, Qt::AlignRight | Qt::AlignVCenter );
break;
169 case YAlignUnchanged:
break;
174 _qt_listView->sortItems( 0, Qt::AscendingOrder);
176 if ( resizeColumnsToContent )
178 for (
int i=0; i < columns(); i++ )
179 _qt_listView->resizeColumnToContents( i );
190 for ( YItemConstIterator it = itemCollection.begin();
191 it != itemCollection.end();
202 YItem * sel = YSelectionWidget::selectedItem();
207 for (
int i=0; i < columns(); i++ )
208 _qt_listView->resizeColumnToContents( i );
217 YTableItem * item =
dynamic_cast<YTableItem *
> (yitem);
218 YUI_CHECK_PTR( item );
221 YUI_CHECK_PTR( clone );
224 if ( ! selected && clone == _qt_listView->currentItem() )
230 if ( ! hasMultiSelection() )
231 _qt_listView->setCurrentItem( clone );
233 clone->setSelected(
true );
234 YTable::selectItem( item, selected );
244 YTable::deselectAllItems();
245 _qt_listView->clearSelection();
252 _qt_listView->
clear();
253 YTable::deleteAllItems();
260 YTableItem * item = cell->parent();
261 YUI_CHECK_PTR( item );
264 YUI_CHECK_PTR( clone );
276 YUI_CHECK_PTR( tableListViewItem );
278 YTable::selectItem( tableListViewItem->
origItem(), true );
292 if ( hasItems() && YSelectionWidget::hasSelectedItem() )
296 if ( immediateMode() )
298 if ( !
YQUI::ui()->eventPendingFor(
this ) )
302 yuiDebug() <<
"Sending SelectionChanged event" << endl;
312 YSelectionWidget::deselectAllItems();
315 QList<QTreeWidgetItem *> selItems = _qt_listView->selectedItems();
317 for ( QList<QTreeWidgetItem *>::iterator it = selItems.begin();
318 it != selItems.end();
323 if ( tableListViewItem )
325 tableListViewItem->
origItem()->setSelected(
true );
327 yuiDebug() <<
"Selected item: " << tableListViewItem->
origItem()->label() << endl;
331 if ( immediateMode() )
333 if ( !
YQUI::ui()->eventPendingFor(
this ) )
337 yuiDebug() <<
"Sending SelectionChanged event" << endl;
351 yuiDebug() <<
"Sending Activated event" << endl;
360 _qt_listView->setEnabled( enabled );
362 YWidget::setEnabled( enabled );
390 resize( newWidth, newHeight );
397 _qt_listView->setFocus();
406 if ( ! _qt_listView || ! _qt_listView->viewport() )
410 if ( notifyContextMenu() )
420 YTableItem * origItem )
423 , _origItem( origItem )
425 YUI_CHECK_PTR( _table );
426 YUI_CHECK_PTR( _origItem );
428 _origItem->setData(
this );
430 for ( YTableCellIterator it = _origItem->cellsBegin();
431 it != _origItem->cellsEnd();
445 int column = cell->column();
451 setText( column, fromUTF8( cell->label() ) );
458 if ( cell->hasIconName() )
463 if ( ! icon.isNull() )
464 setData( column, Qt::DecorationRole, icon );
468 if ( ! data( column, Qt::DecorationRole ).isNull() )
470 setData( column, Qt::DecorationRole, QIcon() );
479 const YTableCell* tableCell =
origItem()->cell(column);
481 if (tableCell->hasSortKey())
482 return QString::fromUtf8(tableCell->sortKey().c_str());
484 return text(column).trimmed();
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.
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.
QIcon loadIcon(const string &iconName) const
Load an icon.
YTableItem * origItem() const
Return the corresponding 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 QString smartSortKey(int column) const override
The text of the table cell or the sort-key if available.
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.