25 #include <QHeaderView> 27 #include <QTreeWidget> 28 #include <QVBoxLayout> 31 #define YUILogComponent "qt-ui" 32 #include <yui/YUILog.h> 38 #include <yui/YEvent.h> 41 #include <yui/YTreeItem.h> 42 #include "YQSignalBlocker.h" 43 #include "YQWidgetCaption.h" 44 #include "YQApplication.h" 47 #define VERBOSE_TREE_ITEMS 0 50 YQTree::YQTree( YWidget * parent,
const std::string & label,
bool multiSelectionMode,
bool recursiveSelectionMode )
51 : QFrame( (QWidget *) parent->widgetRep() )
52 , YTree( parent, label, multiSelectionMode, recursiveSelectionMode )
54 QVBoxLayout* layout =
new QVBoxLayout(
this );
59 layout->setSpacing( YQWidgetSpacing );
60 layout->setMargin ( YQWidgetMargin );
65 YUI_CHECK_NEW( _caption );
66 layout->addWidget( _caption );
68 _qt_treeWidget =
new QTreeWidget(
this );
69 YUI_CHECK_NEW( _qt_treeWidget );
70 layout->addWidget( _qt_treeWidget );
74 _qt_treeWidget->header()->hide();
76 _qt_treeWidget->setRootIsDecorated (
true );
78 _qt_treeWidget->setContextMenuPolicy( Qt::CustomContextMenu );
80 _caption->setBuddy ( _qt_treeWidget );
82 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemSelectionChanged,
85 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemClicked,
86 this, &pclass(
this)::slotItemClicked );
91 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemChanged,
94 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemDoubleClicked,
97 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemExpanded,
100 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::itemCollapsed,
103 connect( _qt_treeWidget, &pclass(_qt_treeWidget)::customContextMenuRequested,
118 YTree::setLabel( label );
125 _qt_treeWidget->clear();
128 _qt_treeWidget->resizeColumnToContents( 0 );
134 for ( YItemIterator it = begin; it < end; ++it )
136 YTreeItem * orig =
dynamic_cast<YTreeItem *
> (*it);
137 YUI_CHECK_PTR( orig );
142 clone =
new YQTreeItem(
this, parentItem, orig, _nextSerialNo++ );
144 clone =
new YQTreeItem(
this, _qt_treeWidget, orig, _nextSerialNo++ );
146 YUI_CHECK_NEW( clone );
148 if (orig->selected())
153 if ( orig->hasChildren() )
164 YTreeItem * treeItem =
dynamic_cast<YTreeItem *
> (yItem);
165 YUI_CHECK_PTR( treeItem );
168 YUI_CHECK_PTR( yqTreeItem );
175 else if ( yqTreeItem == _qt_treeWidget->currentItem() )
188 _qt_treeWidget->setCurrentItem( item );
189 item->setSelected(
true );
191 if ( hasMultiSelection() )
192 item->setCheckState( 0, Qt::Checked );
194 if ( item->parent() )
197 YTree::selectItem( item->
origItem(), true );
221 _qt_treeWidget->resizeColumnToContents( 0 );
232 _qt_treeWidget->resizeColumnToContents( 0 );
240 YTree::deselectAllItems();
241 _qt_treeWidget->clearSelection();
243 if ( hasMultiSelection() )
245 QTreeWidgetItemIterator it( _qt_treeWidget);
252 treeItem->setCheckState( 0, Qt::Unchecked );
253 treeItem->
origItem()->setSelected(
false );
266 _qt_treeWidget->clear();
267 YTree::deleteAllItems();
279 YSelectionWidget::selectItem( treeItem->
origItem(), selected );
283 for (
int i=0; i < item->childCount(); ++i)
285 QTreeWidgetItem* child = item->child(i);
286 child->setCheckState(0, ( selected )? Qt::Checked : Qt::Unchecked );
299 if ( hasMultiSelection() )
301 if ( recursiveSelection() )
304 if ( item->checkState(0) == Qt::Checked )
310 if ( recursiveSelection() )
316 QList<QTreeWidgetItem *> items = _qt_treeWidget->selectedItems ();
318 if ( ! items.empty() )
320 QTreeWidgetItem *qItem = items.first();
321 selectItem( dynamic_cast<YQTreeItem *> (qItem) );
327 if ( notify() && !
YQUI::ui()->eventPendingFor(
this ) )
334 void YQTree::slotItemClicked( QTreeWidgetItem * item,
int column )
336 _qt_treeWidget->setCurrentItem( item );
338 if ( notify() && !
YQUI::ui()->eventPendingFor(
this ) )
347 QList<QTreeWidgetItem *> items = _qt_treeWidget->selectedItems ();
349 if ( ! hasMultiSelection() && ! items.empty() )
351 QTreeWidgetItem *qItem = items.first();
352 selectItem( dynamic_cast<YQTreeItem *> (qItem) );
356 if ( notify() && !
YQUI::ui()->eventPendingFor(
this ) )
363 selectItem( dynamic_cast<YQTreeItem *> (qItem) );
372 int hintWidth = !_caption->isHidden() ? _caption->sizeHint().width() : 0;
373 return max( 80, hintWidth );
379 int hintHeight = !_caption->isHidden() ? _caption->sizeHint().height() : 0;
384 return 80 + hintHeight;
390 resize( newWidth, newHeight );
396 _caption->setEnabled( enabled );
397 _qt_treeWidget->setEnabled( enabled );
398 YWidget::setEnabled( enabled );
404 _qt_treeWidget->setFocus();
412 if ( ! _qt_treeWidget || ! _qt_treeWidget->viewport() )
416 if ( notifyContextMenu() )
425 QTreeWidgetItem * currentQItem = _qt_treeWidget->currentItem();
445 QTreeWidget * listView,
448 : QTreeWidgetItem( listView )
450 init( tree, orig, serial );
452 #if VERBOSE_TREE_ITEMS 453 yuiDebug() <<
"Creating toplevel tree item \"" << orig->label() <<
"\"" << std::endl;
463 : QTreeWidgetItem( parentItem )
465 init( tree, orig, serial );
466 #if VERBOSE_TREE_ITEMS 467 yuiDebug() <<
"Creating tree item \"" << orig->label()
468 <<
"\" as child of \"" << parentItem->
origItem()->label() <<
"\"" 478 void YQTreeItem::init(
YQTree * tree,
482 YUI_CHECK_PTR( tree );
483 YUI_CHECK_PTR( orig );
489 _origItem->setData(
this );
491 setText( 0, fromUTF8 ( _origItem->label() ) );
492 setOpen( _origItem->isOpen() );
494 if ( _origItem->hasIconName() )
496 string iconName = _tree->iconFullPath( _origItem );
497 QPixmap icon( iconName.c_str() );
500 yuiWarning() <<
"Can't load icon " << iconName << std::endl;
502 setData( 0, Qt::DecorationRole, icon );
505 if ( tree->hasMultiSelection() )
506 setCheckState(0,Qt::Unchecked);
513 QTreeWidgetItem::setExpanded( open );
514 _origItem->setOpen( open );
531 strKey.sprintf(
"%08d", _serialNo );
537 #include "YQTree.moc" Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
void slotItemChanged(QTreeWidgetItem *item)
Propagate a tree item change.
virtual int preferredWidth()
Preferred width of the widget.
virtual void setOpen(bool open)
Open this item.
virtual void rebuildTree()
Rebuild the displayed tree from the internally stored YTreeItems.
virtual YTreeItem * currentItem()
Return the the item that currently has the keyboard focus or 0 if no item currently has the keyboard ...
YQTree(YWidget *parent, const std::string &label, bool multiSelection, bool recursiveSelection)
Constructor.
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 void setLabel(const std::string &label)
Change the label text.
void slotItemCollapsed(QTreeWidgetItem *item)
Propagate an "item collapsed" event to the underlying YTreeItem.
virtual void deselectAllItems()
Deselect all items.
virtual ~YQTree()
Destructor.
virtual void setContextMenuPos(QPoint contextMenuPos)
Sets the position of the context menu (in gloabl coordinates)
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an 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.
void busyCursor()
Show mouse cursor indicating busy state.
void slotSelectionChanged()
Propagate a tree item selection.
virtual void deleteAllItems()
Delete all items.
virtual void setEnabled(bool enabled)
Set enabled/disbled state.
QString key(int column, bool ascending) const
Sort key of this item.
void slotActivated(QTreeWidgetItem *item)
Propagate a double click or pressing the space key on a tree item.
void normalCursor()
Show normal mouse cursor not indicating busy status.
void buildDisplayTree(YQTreeItem *parentItem, YItemIterator begin, YItemIterator end)
Build a tree of items that will be displayed (YQTreeItems) from the original items between iterators ...
static YQUI * ui()
Access the global Qt-UI.
virtual int preferredHeight()
Preferred height of the widget.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
void slotContextMenu(const QPoint &pos)
Propagate a context menu selection.