1 #include "OB_Browser.h"
4 #include "OB_ListItem.h"
5 #include "OB_ListView.h"
12 #include <qlistview.h>
13 #include <qpopupmenu.h>
16 Class: OB_Browser::ToolTip
17 Descr: Tool tip for OB_Browser.
20 class OB_Browser::ToolTip : public QToolTip
23 ToolTip( OB_Browser* b, QWidget* p = 0 );
26 void maybeTip( const QPoint& );
29 OB_Browser* myBrowser;
32 OB_Browser::ToolTip::ToolTip( OB_Browser* b, QWidget* p )
38 OB_Browser::ToolTip::~ToolTip()
42 void OB_Browser::ToolTip::maybeTip( const QPoint& pos )
44 if ( !parentWidget() || !myBrowser || !myBrowser->isShowToolTips() )
47 QListView* lv = myBrowser->listView();
49 QListViewItem* item = lv->itemAt( pos );
50 SUIT_DataObject* obj = myBrowser->dataObject( item );
54 QString aText = obj->toolTip();
56 if ( aText.isEmpty() )
59 QRect aRect = lv->itemRect( item );
66 Descr: Hierarchical tree object browser.
69 OB_Browser::OB_Browser( QWidget* parent, SUIT_DataObject* root )
75 myAutoUpdate( false ),
76 myAutoDelObjs( false ),
77 myRootDecorated( true )
79 myView = new OB_ListView( QtxListView::HeaderAuto, this );
80 myView->setAppropriate( myView->addColumn( "Data" ), false );
81 myView->setSorting( -1 );
82 myView->setRootIsDecorated( true );
83 myView->setSelectionMode( QListView::Extended );
84 myView->installEventFilter( this );
86 QVBoxLayout* main = new QVBoxLayout( this );
87 main->addWidget( myView );
89 myShowToolTips = true;
90 myTooltip = new ToolTip( this, myView->viewport() );
92 connect( myView, SIGNAL( dropped( QPtrList<QListViewItem>, QListViewItem*, int ) ),
93 this, SLOT( onDropped( QPtrList<QListViewItem>, QListViewItem*, int ) ) );
94 connect( myView, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
96 setRootObject( root );
99 OB_Browser::~OB_Browser()
105 bool OB_Browser::rootIsDecorated() const
107 return myRootDecorated;
110 void OB_Browser::setRootIsDecorated( const bool decor )
112 if ( decor == rootIsDecorated() )
115 myRootDecorated = decor;
119 int OB_Browser::autoOpenLevel() const
121 return myAutoOpenLevel;
124 void OB_Browser::setAutoOpenLevel( const int level )
126 if ( myAutoOpenLevel == level )
129 myAutoOpenLevel = level;
134 bool OB_Browser::isShowToolTips()
136 return myShowToolTips;
139 void OB_Browser::setShowToolTips( const bool theDisplay )
141 myShowToolTips = theDisplay;
144 bool OB_Browser::isAutoUpdate() const
149 void OB_Browser::setAutoUpdate( const bool on )
154 bool OB_Browser::isAutoDeleteObjects() const
156 return myAutoDelObjs;
159 void OB_Browser::setAutoDeleteObjects( const bool on )
164 SUIT_DataObject* OB_Browser::getRootObject() const
169 void OB_Browser::setRootObject( SUIT_DataObject* theRoot )
171 DataObjectKey curKey;
172 DataObjectMap selObjs, openObjs;
173 DataObjectKeyMap selKeys, openKeys;
175 int selNum = numberOfSelected();
177 SUIT_DataObject* curObj = 0;
179 curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
181 if ( myRoot != theRoot && isAutoDeleteObjects() )
186 createConnections( myRoot );
189 updateView( myRoot );
190 else if ( listView() )
196 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
200 if ( selNum != numberOfSelected() )
201 emit selectionChanged();
204 int OB_Browser::numberOfSelected() const
209 for ( QListViewItemIterator it( listView() ); it.current(); ++it )
210 if ( it.current()->isSelected() )
216 DataObjectList OB_Browser::getSelected() const
223 void OB_Browser::getSelected( DataObjectList& theObjList ) const
230 for ( QListViewItemIterator it( listView() ); it.current(); ++it )
232 if ( it.current()->isSelected() )
234 SUIT_DataObject* obj = dataObject( it.current() );
236 theObjList.append( obj );
241 void OB_Browser::setSelected( const SUIT_DataObject* theObject, const bool append )
244 lst.append( theObject );
245 setSelected( lst, append );
248 void OB_Browser::setSelected( const DataObjectList& theObjLst, const bool append )
250 QListView* lv = listView();
255 bool changed = false;
256 bool block = lv->signalsBlocked();
257 lv->blockSignals( true );
259 QMap<QListViewItem*, int> map;
260 for ( DataObjectListIterator itr( theObjLst ); itr.current(); ++itr )
261 map.insert( listViewItem( itr.current() ), 0 );
263 for ( QListViewItemIterator it( lv ); it.current(); ++it )
265 QListViewItem* item = it.current();
266 if ( map.contains( item ) && !lv->isSelected( item ) )
269 lv->setSelected( item, true );
271 if ( !append && !map.contains( item ) && lv->isSelected( item ) )
274 lv->setSelected( item, false );
278 lv->blockSignals( block );
283 QListViewItem* sel = 0;
284 QListViewItem* cur = lv->currentItem();
285 for ( QListViewItemIterator iter( lv ); iter.current() && !sel; ++iter, count++ )
287 if ( iter.current()->isSelected() && cur == iter.current() )
288 sel = iter.current();
291 for ( QListViewItemIterator itr( lv ); itr.current() && !sel; ++itr )
293 if ( itr.current()->isSelected() )
298 lv->setCurrentItem( sel );
300 if ( sel && count == 1 )
301 lv->ensureItemVisible( sel );
303 emit selectionChanged();
307 bool OB_Browser::isOpen( SUIT_DataObject* theObject ) const
311 res = listView()->isOpen( listViewItem( theObject ) );
315 void OB_Browser::setOpen( SUIT_DataObject* theObject, const bool theOpen )
318 listView()->setOpen( listViewItem( theObject ), theOpen );
321 SUIT_DataObject* OB_Browser::dataObjectAt( const QPoint& pos ) const
323 SUIT_DataObject* obj = 0;
325 QListView* lv = listView();
327 obj = dataObject( lv->itemAt( pos ) );
332 OB_Filter* OB_Browser::filter() const
334 return myView->filter();
337 void OB_Browser::setFilter( OB_Filter* f )
339 myView->setFilter( f );
342 int OB_Browser::addColumn( const QString& label, const int id, const int width )
344 return addColumn( QIconSet(), label, id, width );
347 int OB_Browser::addColumn( const QIconSet& icon, const QString& label, const int id, const int width )
349 QListView* lv = listView();
356 while ( myColumnIds.contains( theId ) )
360 if ( myColumnIds.contains( theId ) )
361 return -1; // can not reuse id
365 sec = lv->addColumn( label, width );
367 sec = lv->addColumn( icon, label, width );
372 myColumnIds.insert( theId, sec );
378 void OB_Browser::removeColumn( const int id )
380 QListView* lv = listView();
381 if ( !lv || !myColumnIds.contains( id ) )
384 int sec = myColumnIds[id];
385 lv->removeColumn( sec );
387 // update map of column indeces
388 myColumnIds.remove( id );
389 for ( QMap<int, int>::iterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
397 void OB_Browser::setNameTitle( const QString& label )
399 setNameTitle( QIconSet(), label );
402 void OB_Browser::setNameTitle( const QIconSet& icon, const QString& label )
404 QListView* lv = listView();
409 lv->setColumnText( 0, label );
411 lv->setColumnText( 0, icon, label );
414 void OB_Browser::setColumnTitle( const int id, const QString& label )
416 setColumnTitle( id, QIconSet(), label );
419 void OB_Browser::setColumnTitle( const int id, const QIconSet& icon, const QString& label )
421 QListView* lv = listView();
422 if ( !lv || !myColumnIds.contains( id ) )
426 lv->setColumnText( myColumnIds[id], label );
428 lv->setColumnText( myColumnIds[id], icon, label );
431 bool OB_Browser::isColumnVisible( const int id ) const
433 myColumnIds.contains( id ) && myView->isShown( myColumnIds[id] );
436 void OB_Browser::setColumnShown( const int id, const bool on )
438 if ( !myColumnIds.contains( id ) )
441 myView->setShown( myColumnIds[id], on );
444 void OB_Browser::updateTree( SUIT_DataObject* o )
446 updateTree( o ? o : getRootObject(), false );
449 void OB_Browser::updateTree( SUIT_DataObject* obj, const bool notify )
454 DataObjectKey curKey;
455 DataObjectMap selObjs, openObjs;
456 DataObjectKeyMap selKeys, openKeys;
458 int selNum = numberOfSelected();
460 SUIT_DataObject* curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
465 createConnections( obj );
468 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
472 if ( selNum != numberOfSelected() )
473 emit selectionChanged();
476 void OB_Browser::replaceTree( SUIT_DataObject* src, SUIT_DataObject* trg )
478 if ( !src || !trg || src->root() != getRootObject() )
481 DataObjectKey curKey;
482 DataObjectMap selObjs, openObjs;
483 DataObjectKeyMap selKeys, openKeys;
485 int selNum = numberOfSelected();
487 SUIT_DataObject* curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
489 SUIT_DataObject* parent = src->parent();
490 int pos = parent ? parent->childPos( src ) : -1;
494 if ( src != trg && isAutoDeleteObjects() )
497 if ( parent && pos != -1 )
498 parent->insertChild( trg, pos );
500 trg->setParent( parent );
502 createConnections( trg );
505 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
509 if ( selNum != numberOfSelected() )
510 emit selectionChanged();
513 void OB_Browser::updateView( const SUIT_DataObject* theStartObj )
515 QListView* lv = listView();
519 if ( !theStartObj || theStartObj->root() != getRootObject() )
522 QListViewItem* after = 0;
523 QListViewItem* parent = 0;
524 QListViewItem* startItem = listViewItem( theStartObj );
526 if ( theStartObj->parent() )
527 parent = listViewItem( theStartObj->parent() );
529 QListViewItem* prv = 0;
530 QListViewItem* cur = parent ? parent->firstChild() : lv->firstChild();
531 while ( !after && cur )
533 if ( cur == startItem )
537 cur = cur->nextSibling();
540 QPtrList<QListViewItem> delList;
541 if ( !startItem && theStartObj == getRootObject() )
543 for ( QListViewItem* item = lv->firstChild(); item; item = item->nextSibling() )
544 delList.append( item );
547 delList.append( startItem );
549 for ( QPtrListIterator<QListViewItem> it( delList ); it.current(); ++it )
551 removeReferences( it.current() );
555 // for myRoot object, if myShowRoot==false, then creating multiple top-level QListViewItem-s
556 // (which will correspond to myRoot's children = Modules).
557 if ( rootIsDecorated() && theStartObj == myRoot )
560 theStartObj->children( lst );
561 DataObjectListIterator it ( lst );
562 // iterating backward to preserve the order of elements in the tree
563 for ( it.toLast(); it.current(); --it )
564 createTree( it.current(), 0, 0 );
567 createTree( theStartObj, parent, after );
570 QListViewItem* OB_Browser::createTree( const SUIT_DataObject* obj,
571 QListViewItem* parent, QListViewItem* after )
576 QListViewItem* item = createItem( obj, parent, after );
579 obj->children( lst );
580 for ( DataObjectListIterator it ( lst ); it.current(); ++it )
581 createTree( it.current(), item );
586 QListViewItem* OB_Browser::createItem( const SUIT_DataObject* o,
587 QListViewItem* parent, QListViewItem* after )
589 QListView* lv = listView();
594 QListViewItem* item = 0;
595 SUIT_DataObject* obj = (SUIT_DataObject*)o;
599 switch ( obj->checkType() )
601 case SUIT_DataObject::CheckBox:
602 type = QCheckListItem::CheckBox;
604 case SUIT_DataObject::RadioButton:
605 type = QCheckListItem::RadioButton;
611 if ( parent->childCount() && !after )
613 after = parent->firstChild();
614 while ( after->nextSibling() )
615 after = after->nextSibling();
621 item = new OB_ListItem( obj, parent, after );
623 item = new OB_CheckListItem( obj, parent, after, (QCheckListItem::Type)type );
628 item = new OB_ListItem( obj, parent );
630 item = new OB_CheckListItem( obj, parent, (QCheckListItem::Type)type );
636 item = new OB_ListItem( obj, lv );
638 item = new OB_CheckListItem( obj, lv, (QCheckListItem::Type)type );
641 myItems.insert( obj, item );
648 void OB_Browser::adjustWidth()
653 listView()->setColumnWidth( 0, 0 );
654 if ( listView()->firstChild() )
655 adjustWidth( listView()->firstChild() );
658 void OB_Browser::adjustWidth( QListViewItem* item )
662 item->widthChanged( 0 );
663 if ( item->isOpen() )
664 adjustWidth( item->firstChild() );
665 item = item->nextSibling();
669 SUIT_DataObject* OB_Browser::dataObject( const QListViewItem* item ) const
671 SUIT_DataObject* obj = 0;
673 if ( item && item->rtti() == OB_ListItem::RTTI() )
674 obj = ((OB_ListItem*)item)->dataObject();
675 else if ( item && item->rtti() == OB_CheckListItem::RTTI() )
676 obj = ((OB_CheckListItem*)item)->dataObject();
681 QListViewItem* OB_Browser::listViewItem( const SUIT_DataObject* obj ) const
683 QListViewItem* item = 0;
685 if ( myItems.contains( (SUIT_DataObject*)obj ) )
686 item = myItems[(SUIT_DataObject*)obj];
691 QListView* OB_Browser::listView() const
696 void OB_Browser::removeReferences( QListViewItem* item )
701 SUIT_DataObject* obj = dataObject( item );
702 myItems.remove( obj );
704 QListViewItem* i = item->firstChild();
707 removeReferences( i );
708 i = i->nextSibling();
712 void OB_Browser::createConnections( SUIT_DataObject* obj )
717 DataObjectList childList;
718 obj->children( childList, true );
720 childList.prepend( obj );
722 for ( DataObjectListIterator it( childList ); it.current(); ++it )
723 it.current()->connect( this, SLOT( onDestroyed( SUIT_DataObject* ) ) );
726 SUIT_DataObject* OB_Browser::storeState( DataObjectMap& selObjs, DataObjectMap& openObjs,
727 DataObjectKeyMap& selKeys, DataObjectKeyMap& openKeys,
728 DataObjectKey& curKey ) const
730 QListView* lv = listView();
734 SUIT_DataObject* curObj = dataObject( lv->currentItem() );
736 curKey = objectKey( curObj );
738 for ( QListViewItemIterator it( lv ); it.current(); ++it )
740 SUIT_DataObject* obj = dataObject( it.current() );
744 selObjs.insert( obj, lv->isSelected( it.current() ) );
745 openObjs.insert( obj, lv->isOpen( it.current() ) );
746 if ( lv->isSelected( it.current() ) )
747 selKeys.insert( objectKey( obj ), 0 );
748 if ( lv->isOpen( it.current() ) )
749 openKeys.insert( objectKey( obj ), 0 );
755 void OB_Browser::restoreState( const DataObjectMap& selObjs, const DataObjectMap& openObjs,
756 const SUIT_DataObject* curObj, const DataObjectKeyMap& selKeys,
757 const DataObjectKeyMap& openKeys, const DataObjectKey& curKey )
759 QListView* lv = listView();
763 bool block = lv->signalsBlocked();
764 lv->blockSignals( true );
766 QListViewItem* curItem = 0;
767 for ( QListViewItemIterator it( lv ); it.current(); ++it )
769 QListViewItem* item = it.current();
770 SUIT_DataObject* obj = dataObject( item );
775 DataObjectKey key = objectKey( obj );
777 if ( selObjs.contains( obj ) )
779 if ( selObjs[obj] && !lv->isSelected( item ) )
780 lv->setSelected( item, true );
782 else if ( !key.isNull() && selKeys.contains( key ) && !lv->isSelected( item ) )
783 lv->setSelected( item, true );
785 if ( openObjs.contains( obj ) )
788 lv->setOpen( item, true );
790 else if ( !key.isNull() && openKeys.contains( key ) )
791 lv->setOpen( item, true );
793 if ( !curItem && ( curObj == obj || ( !curKey.isNull() && curKey == key )) )
798 lv->setCurrentItem( curItem );
800 lv->blockSignals( block );
803 OB_Browser::DataObjectKey OB_Browser::objectKey( QListViewItem* i ) const
805 return objectKey( dataObject( i ) );
808 OB_Browser::DataObjectKey OB_Browser::objectKey( SUIT_DataObject* obj ) const
813 return DataObjectKey( obj->key() );
816 void OB_Browser::keyPressEvent( QKeyEvent* e )
818 if ( e->key() == Qt::Key_F5 )
821 QFrame::keyPressEvent( e );
824 void OB_Browser::onExpand()
826 DataObjectList selected;
827 getSelected( selected );
828 for ( DataObjectListIterator itr( selected ); itr.current(); ++itr )
829 expand( listViewItem( itr.current() ) );
832 void OB_Browser::onRefresh()
834 updateTree( 0, true );
837 void OB_Browser::onDestroyed( SUIT_DataObject* obj )
842 void OB_Browser::onDropped( QPtrList<QListViewItem> items, QListViewItem* item, int action )
844 SUIT_DataObject* obj = dataObject( item );
849 for ( QPtrListIterator<QListViewItem> it( items ); it.current(); ++it )
851 SUIT_DataObject* o = dataObject( it.current() );
856 if ( !lst.isEmpty() )
857 emit dropped( lst, obj, action );
860 void OB_Browser::updateText()
862 if ( myColumnIds.isEmpty() )
865 QListView* lv = listView();
869 for ( QListViewItemIterator it( lv ); it.current(); ++it )
871 SUIT_DataObject* obj = dataObject( it.current() );
875 for( QMap<int, int>::iterator itr = myColumnIds.begin(); itr != myColumnIds.end(); ++itr )
876 it.current()->setText( itr.data(), obj->text( itr.key() ) );
880 void OB_Browser::updateText( QListViewItem* item )
882 SUIT_DataObject* obj = dataObject( item );
886 for( QMap<int, int>::iterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
887 item->setText( it.data(), obj->text( it.key() ) );
890 bool OB_Browser::eventFilter(QObject* watched, QEvent* e)
892 if ( watched == myView && e->type() == QEvent::ContextMenu )
894 contextMenuRequest( (QContextMenuEvent*)e );
897 return QFrame::eventFilter(watched, e);
900 void OB_Browser::contextMenuPopup( QPopupMenu* menu )
902 DataObjectList selected;
903 getSelected( selected );
906 for ( DataObjectListIterator itr( selected ); itr.current() && !closed; ++itr )
907 closed = hasClosed( listViewItem( itr.current() ) );
911 menu->insertItem( tr( "MEN_EXPAND_ALL" ), this, SLOT( onExpand() ) );
912 menu->insertSeparator();
914 menu->insertItem( tr( "MEN_REFRESH" ), this, SLOT( onRefresh() ) );
917 void OB_Browser::expand( QListViewItem* item )
922 item->setOpen( true );
923 for ( QListViewItem* child = item->firstChild(); child; child = child->nextSibling() )
927 bool OB_Browser::hasClosed( QListViewItem* item ) const
932 if ( item->childCount() && !item->isOpen() )
936 for ( QListViewItem* child = item->firstChild(); child && !has; child = child->nextSibling() )
937 has = hasClosed( child );
942 void OB_Browser::removeObject( SUIT_DataObject* obj, const bool autoUpd )
947 // Removing list view items from <myItems> recursively for all children.
948 // Otherwise, "delete item" line will destroy all item's children,
949 // and <myItems> will contain invalid pointers (see ~QListViewItem() description in Qt docs)
950 DataObjectList childList;
951 obj->children( childList );
952 for ( DataObjectListIterator it( childList ); it.current(); ++it )
953 removeObject( it.current(), false );
955 QListViewItem* item = listViewItem( obj );
957 myItems.remove( obj );
965 if ( isAutoUpdate() )
967 SUIT_DataObject* pObj = item && item->parent() ? dataObject( item->parent() ) : 0;
974 void OB_Browser::autoOpenBranches()
976 int level = autoOpenLevel();
977 QListView* lv = listView();
978 if ( !lv || level < 1 )
981 QListViewItem* item = lv->firstChild();
984 openBranch( item, level );
985 item = item->nextSibling();
989 void OB_Browser::openBranch( QListViewItem* item, const int level )
991 if ( !item || level < 1 || !item->childCount() )
994 item->setOpen( true );
997 openBranch( item, level - 1 );
998 item = item->nextSibling();