25 #include <QHeaderView>
27 #include <QTreeWidget>
28 #include <QVBoxLayout>
32 #define YUILogComponent "qt-ui"
33 #include <yui/YUILog.h>
36 #include <yui/YEvent.h>
39 #include <yui/YTreeItem.h>
40 #include "YQSignalBlocker.h"
41 #include "YQWidgetCaption.h"
42 #include "YQApplication.h"
44 #define VERBOSE_TREE_ITEMS 0
52 bool multiSelectionMode,
53 bool recursiveSelectionMode )
54 : QFrame( (QWidget *) parent->widgetRep() )
55 , YTree( parent, label, multiSelectionMode, recursiveSelectionMode )
57 QVBoxLayout* layout =
new QVBoxLayout(
this );
62 layout->setSpacing( YQWidgetSpacing );
63 layout->setMargin ( YQWidgetMargin );
68 YUI_CHECK_NEW( _caption );
69 layout->addWidget( _caption );
71 _qt_treeWidget =
new QTreeWidget(
this );
72 YUI_CHECK_NEW( _qt_treeWidget );
73 layout->addWidget( _qt_treeWidget );
77 _qt_treeWidget->header()->hide();
79 _qt_treeWidget->setRootIsDecorated (
true );
81 _qt_treeWidget->setContextMenuPolicy( Qt::CustomContextMenu );
83 _caption->setBuddy ( _qt_treeWidget );
85 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemSelectionChanged,
88 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemClicked,
89 this, &pclass(
this)::slotItemClicked );
94 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemChanged,
97 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemDoubleClicked,
100 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemExpanded,
103 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemCollapsed,
106 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::customContextMenuRequested,
121 YTree::setLabel( label );
128 _qt_treeWidget->clear();
131 _qt_treeWidget->resizeColumnToContents( 0 );
137 for ( YItemIterator it = begin; it < end; ++it )
139 YTreeItem * orig =
dynamic_cast<YTreeItem *
> (*it);
140 YUI_CHECK_PTR( orig );
145 clone =
new YQTreeItem(
this, parentItem, orig, _nextSerialNo++ );
147 clone =
new YQTreeItem(
this, _qt_treeWidget, orig, _nextSerialNo++ );
149 YUI_CHECK_NEW( clone );
151 if (orig->selected())
156 if ( orig->hasChildren() )
167 YTreeItem * treeItem =
dynamic_cast<YTreeItem *
> (yItem);
168 YUI_CHECK_PTR( treeItem );
171 YUI_CHECK_PTR( yqTreeItem );
178 else if ( yqTreeItem == _qt_treeWidget->currentItem() )
191 _qt_treeWidget->setCurrentItem( item );
192 item->setSelected(
true );
194 if ( hasMultiSelection() )
195 item->setCheckState( 0, Qt::Checked );
197 if ( item->parent() )
200 YTree::selectItem( item->
origItem(),
true );
225 _qt_treeWidget->resizeColumnToContents( 0 );
236 _qt_treeWidget->resizeColumnToContents( 0 );
244 YTree::deselectAllItems();
245 _qt_treeWidget->clearSelection();
247 if ( hasMultiSelection() )
249 QTreeWidgetItemIterator it( _qt_treeWidget);
256 treeItem->setCheckState( 0, Qt::Unchecked );
257 treeItem->
origItem()->setSelected(
false );
270 _qt_treeWidget->clear();
271 YTree::deleteAllItems();
283 YSelectionWidget::selectItem( treeItem->
origItem(), selected );
287 for (
int i=0; i < item->childCount(); ++i)
289 QTreeWidgetItem* child = item->child(i);
290 child->setCheckState(0, ( selected )? Qt::Checked : Qt::Unchecked );
303 if ( hasMultiSelection() )
305 if ( recursiveSelection() )
308 if ( item->checkState(0) == Qt::Checked )
314 if ( recursiveSelection() )
320 QList<QTreeWidgetItem *> items = _qt_treeWidget->selectedItems ();
322 if ( ! items.empty() )
324 QTreeWidgetItem *qItem = items.first();
330 if ( notify() && !
YQUI::ui()->eventPendingFor(
this ) )
335 void YQTree::slotItemClicked( QTreeWidgetItem * item,
int column )
337 _qt_treeWidget->setCurrentItem( item );
339 if ( notify() && !
YQUI::ui()->eventPendingFor(
this ) )
346 QList<QTreeWidgetItem *> items = _qt_treeWidget->selectedItems ();
348 if ( ! hasMultiSelection() && ! items.empty() )
350 QTreeWidgetItem *qItem = items.first();
355 if ( notify() && !
YQUI::ui()->eventPendingFor(
this ) )
371 int hintWidth = !_caption->isHidden() ? _caption->sizeHint().width() : 0;
372 return std::max( 80, hintWidth );
378 int hintHeight = !_caption->isHidden() ? _caption->sizeHint().height() : 0;
383 return 80 + hintHeight;
389 resize( newWidth, newHeight );
395 _caption->setEnabled( enabled );
396 _qt_treeWidget->setEnabled( enabled );
397 YWidget::setEnabled( enabled );
403 _qt_treeWidget->setFocus();
411 if ( ! _qt_treeWidget || ! _qt_treeWidget->viewport() )
415 if ( notifyContextMenu() )
424 QTreeWidgetItem * currentQItem = _qt_treeWidget->currentItem();
450 QTreeWidget * listView,
453 : QTreeWidgetItem( listView )
455 init( tree, orig, serial );
457 #if VERBOSE_TREE_ITEMS
458 yuiDebug() <<
"Creating toplevel tree item \"" << orig->label() <<
"\"" << endl;
468 : QTreeWidgetItem( parentItem )
470 init( tree, orig, serial );
471 #if VERBOSE_TREE_ITEMS
472 yuiDebug() <<
"Creating tree item \"" << orig->label()
473 <<
"\" as child of \"" << parentItem->
origItem()->label() <<
"\""
480 void YQTreeItem::init(
YQTree * tree,
484 YUI_CHECK_PTR( tree );
485 YUI_CHECK_PTR( orig );
491 _origItem->setData(
this );
493 setText( 0, fromUTF8 ( _origItem->label() ) );
494 setOpen( _origItem->isOpen() );
496 if ( _origItem->hasIconName() )
498 QIcon icon = QIcon( _tree->iconFullPath( _origItem ).c_str() );
503 if ( !icon.isNull() )
504 setData( 0, Qt::DecorationRole, icon );
507 if ( tree->hasMultiSelection() )
508 setCheckState(0,Qt::Unchecked);
515 QTreeWidgetItem::setExpanded( open );
516 _origItem->setOpen( open );
531 QString strKey = QString(
"%1" ).arg( _serialNo,
virtual void setContextMenuPos(QPoint contextMenuPos)
Sets the position of the context menu (in gloabl coordinates)
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
QString key(int column, bool ascending) const
Sort key of this item.
YQTreeItem(YQTree *tree, QTreeWidget *parent, YTreeItem *origItem, int serial)
Constructor for a top level item.
YTreeItem * origItem()
Returns the original YTreeItem of which this item is a clone.
virtual void setOpen(bool open)
Open this item.
void slotActivated(QTreeWidgetItem *item)
Propagate a double click or pressing the space key on a tree item.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
YQTree(YWidget *parent, const std::string &label, bool multiSelection, bool recursiveSelection)
Constructor.
virtual void rebuildTree()
Rebuild the displayed tree from the internally stored YTreeItems.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
void slotSelectionChanged()
Propagate a tree item selection.
virtual void setLabel(const std::string &label)
Change the label text.
void openBranch(YQTreeItem *item)
Open the branch of 'item' recursively to its toplevel item.
void slotItemExpanded(QTreeWidgetItem *item)
Propagate an "item expanded" event to the underlying YTreeItem.
virtual YTreeItem * currentItem()
Return the the item that currently has the keyboard focus or 0 if no item currently has the keyboard ...
void slotItemCollapsed(QTreeWidgetItem *item)
Propagate an "item collapsed" event to the underlying YTreeItem.
void buildDisplayTree(YQTreeItem *parentItem, YItemIterator begin, YItemIterator end)
Build a tree of items that will be displayed (YQTreeItems) from the original items between iterators ...
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual void setEnabled(bool enabled)
Set enabled/disbled state.
virtual void deleteAllItems()
Delete all items.
virtual void activate()
Activate the item selected in the tree.
virtual int preferredHeight()
Preferred height of the widget.
virtual ~YQTree()
Destructor.
virtual int preferredWidth()
Preferred width of the widget.
void slotContextMenu(const QPoint &pos)
Propagate a context menu selection.
void slotItemChanged(QTreeWidgetItem *item)
Propagate a tree item change.
virtual void deselectAllItems()
Deselect all items.
void busyCursor()
Show mouse cursor indicating busy state.
static YQUI * ui()
Access the global Qt-UI.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
QIcon loadIcon(const string &iconName) const
Load an icon.
void normalCursor()
Show normal mouse cursor not indicating busy status.