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 );
85 myView->viewport()->installEventFilter( this );
87 QVBoxLayout* main = new QVBoxLayout( this );
88 main->addWidget( myView );
90 myShowToolTips = true;
91 myTooltip = new ToolTip( this, myView->viewport() );
93 connect( myView, SIGNAL( dropped( QPtrList<QListViewItem>, QListViewItem*, int ) ),
94 this, SLOT( onDropped( QPtrList<QListViewItem>, QListViewItem*, int ) ) );
95 connect( myView, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
97 setRootObject( root );
100 OB_Browser::~OB_Browser()
106 bool OB_Browser::rootIsDecorated() const
108 return myRootDecorated;
111 void OB_Browser::setRootIsDecorated( const bool decor )
113 if ( decor == rootIsDecorated() )
116 myRootDecorated = decor;
120 int OB_Browser::autoOpenLevel() const
122 return myAutoOpenLevel;
125 void OB_Browser::setAutoOpenLevel( const int level )
127 if ( myAutoOpenLevel == level )
130 myAutoOpenLevel = level;
135 bool OB_Browser::isShowToolTips()
137 return myShowToolTips;
140 void OB_Browser::setShowToolTips( const bool theDisplay )
142 myShowToolTips = theDisplay;
145 bool OB_Browser::isAutoUpdate() const
150 void OB_Browser::setAutoUpdate( const bool on )
155 bool OB_Browser::isAutoDeleteObjects() const
157 return myAutoDelObjs;
160 void OB_Browser::setAutoDeleteObjects( const bool on )
165 SUIT_DataObject* OB_Browser::getRootObject() const
170 void OB_Browser::setRootObject( SUIT_DataObject* theRoot )
172 DataObjectKey curKey;
173 DataObjectMap selObjs, openObjs;
174 DataObjectKeyMap selKeys, openKeys;
176 int selNum = numberOfSelected();
178 SUIT_DataObject* curObj = 0;
180 curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
182 removeConnections( myRoot );
183 if ( myRoot != theRoot && isAutoDeleteObjects() )
188 createConnections( myRoot );
191 updateView( myRoot );
192 else if ( listView() )
198 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
202 if ( selNum != numberOfSelected() )
203 emit selectionChanged();
206 int OB_Browser::numberOfSelected() const
211 for ( QListViewItemIterator it( listView() ); it.current(); ++it )
212 if ( it.current()->isSelected() )
218 DataObjectList OB_Browser::getSelected() const
225 void OB_Browser::getSelected( DataObjectList& theObjList ) const
232 for ( QListViewItemIterator it( listView() ); it.current(); ++it )
234 if ( it.current()->isSelected() )
236 SUIT_DataObject* obj = dataObject( it.current() );
238 theObjList.append( obj );
243 void OB_Browser::setSelected( const SUIT_DataObject* theObject, const bool append )
246 lst.append( theObject );
247 setSelected( lst, append );
250 void OB_Browser::setSelected( const DataObjectList& theObjLst, const bool append )
252 QListView* lv = listView();
257 bool changed = false;
258 bool block = lv->signalsBlocked();
259 lv->blockSignals( true );
261 QMap<QListViewItem*, int> map;
262 for ( DataObjectListIterator itr( theObjLst ); itr.current(); ++itr )
263 map.insert( listViewItem( itr.current() ), 0 );
265 for ( QListViewItemIterator it( lv ); it.current(); ++it )
267 QListViewItem* item = it.current();
268 if ( map.contains( item ) && !lv->isSelected( item ) )
271 lv->setSelected( item, true );
273 if ( !append && !map.contains( item ) && lv->isSelected( item ) )
276 lv->setSelected( item, false );
280 lv->blockSignals( block );
285 QListViewItem* sel = 0;
286 QListViewItem* cur = lv->currentItem();
287 for ( QListViewItemIterator iter( lv ); iter.current() && !sel; ++iter, count++ )
289 if ( iter.current()->isSelected() && cur == iter.current() )
290 sel = iter.current();
293 for ( QListViewItemIterator itr( lv ); itr.current() && !sel; ++itr )
295 if ( itr.current()->isSelected() )
300 lv->setCurrentItem( sel );
302 if ( sel && count == 1 )
303 lv->ensureItemVisible( sel );
305 emit selectionChanged();
309 bool OB_Browser::isOpen( SUIT_DataObject* theObject ) const
313 res = listView()->isOpen( listViewItem( theObject ) );
317 void OB_Browser::setOpen( SUIT_DataObject* theObject, const bool theOpen )
320 listView()->setOpen( listViewItem( theObject ), theOpen );
323 SUIT_DataObject* OB_Browser::dataObjectAt( const QPoint& pos ) const
325 SUIT_DataObject* obj = 0;
327 QListView* lv = listView();
329 obj = dataObject( lv->itemAt( pos ) );
334 OB_Filter* OB_Browser::filter() const
336 return myView->filter();
339 void OB_Browser::setFilter( OB_Filter* f )
341 myView->setFilter( f );
344 int OB_Browser::addColumn( const QString& label, const int id, const int width )
346 return addColumn( QIconSet(), label, id, width );
349 int OB_Browser::addColumn( const QIconSet& icon, const QString& label, const int id, const int width )
351 QListView* lv = listView();
358 while ( myColumnIds.contains( theId ) )
362 if ( myColumnIds.contains( theId ) )
363 return -1; // can not reuse id
367 sec = lv->addColumn( label, width );
369 sec = lv->addColumn( icon, label, width );
374 myColumnIds.insert( theId, sec );
380 void OB_Browser::removeColumn( const int id )
382 QListView* lv = listView();
383 if ( !lv || !myColumnIds.contains( id ) )
386 int sec = myColumnIds[id];
387 lv->removeColumn( sec );
389 // update map of column indeces
390 myColumnIds.remove( id );
391 for ( QMap<int, int>::iterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
399 void OB_Browser::setNameTitle( const QString& label )
401 setNameTitle( QIconSet(), label );
404 void OB_Browser::setNameTitle( const QIconSet& icon, const QString& label )
406 QListView* lv = listView();
411 lv->setColumnText( 0, label );
413 lv->setColumnText( 0, icon, label );
416 void OB_Browser::setColumnTitle( const int id, const QString& label )
418 setColumnTitle( id, QIconSet(), label );
421 void OB_Browser::setColumnTitle( const int id, const QIconSet& icon, const QString& label )
423 QListView* lv = listView();
424 if ( !lv || !myColumnIds.contains( id ) )
428 lv->setColumnText( myColumnIds[id], label );
430 lv->setColumnText( myColumnIds[id], icon, label );
433 QString OB_Browser::nameTitle() const
435 return myView->columnText( 0 );
438 QString OB_Browser::columnTitle( const int id ) const
441 if ( myColumnIds.contains( id ) )
442 txt = myView->columnText( myColumnIds[id] );
446 bool OB_Browser::isColumnVisible( const int id ) const
448 return myColumnIds.contains( id ) && myView->isShown( myColumnIds[id] );
451 void OB_Browser::setColumnShown( const int id, const bool on )
453 if ( !myColumnIds.contains( id ) )
456 myView->setShown( myColumnIds[id], on );
459 QValueList<int> OB_Browser::columns() const
462 for ( QMap<int, int>::ConstIterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
463 lst.append( it.key() );
467 bool OB_Browser::appropriateColumn( const int id ) const
470 if ( myColumnIds.contains( id ) )
471 res = myView->appropriate( myColumnIds[id] );
475 void OB_Browser::setAppropriateColumn( const int id, const bool on )
477 if ( !myColumnIds.contains( id ) )
480 myView->setAppropriate( myColumnIds[id], on );
483 void OB_Browser::updateTree( SUIT_DataObject* obj )
485 if ( !obj && !(obj = getRootObject()) )
488 DataObjectKey curKey;
489 DataObjectMap selObjs, openObjs;
490 DataObjectKeyMap selKeys, openKeys;
492 int selNum = numberOfSelected();
494 SUIT_DataObject* curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
496 createConnections( obj );
499 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
503 if ( selNum != numberOfSelected() )
504 emit selectionChanged();
507 void OB_Browser::replaceTree( SUIT_DataObject* src, SUIT_DataObject* trg )
509 if ( !src || !trg || src == trg || src->root() != getRootObject() )
512 DataObjectKey curKey;
513 DataObjectMap selObjs, openObjs;
514 DataObjectKeyMap selKeys, openKeys;
516 int selNum = numberOfSelected();
518 SUIT_DataObject* curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
520 SUIT_DataObject* parent = src->parent();
521 int pos = parent ? parent->childPos( src ) : -1;
525 removeConnections( src );
526 if ( isAutoDeleteObjects() )
529 if ( parent && pos != -1 )
530 parent->insertChild( trg, pos );
532 trg->setParent( parent );
534 createConnections( trg );
537 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
541 if ( selNum != numberOfSelected() )
542 emit selectionChanged();
545 void OB_Browser::updateView( const SUIT_DataObject* theStartObj )
547 QListView* lv = listView();
551 if ( !theStartObj || theStartObj->root() != getRootObject() )
554 QListViewItem* after = 0;
555 QListViewItem* parent = 0;
556 QListViewItem* startItem = listViewItem( theStartObj );
558 if ( theStartObj->parent() )
559 parent = listViewItem( theStartObj->parent() );
561 QListViewItem* prv = 0;
562 QListViewItem* cur = parent ? parent->firstChild() : lv->firstChild();
563 while ( !after && cur )
565 if ( cur == startItem )
569 cur = cur->nextSibling();
572 QPtrList<QListViewItem> delList;
573 if ( !startItem && theStartObj == getRootObject() )
575 for ( QListViewItem* item = lv->firstChild(); item; item = item->nextSibling() )
576 delList.append( item );
579 delList.append( startItem );
581 for ( QPtrListIterator<QListViewItem> it( delList ); it.current(); ++it )
583 removeReferences( it.current() );
587 // for myRoot object, if myShowRoot==false, then creating multiple top-level QListViewItem-s
588 // (which will correspond to myRoot's children = Modules).
589 if ( rootIsDecorated() && theStartObj == myRoot )
592 theStartObj->children( lst );
593 DataObjectListIterator it ( lst );
594 // iterating backward to preserve the order of elements in the tree
595 for ( it.toLast(); it.current(); --it )
596 createTree( it.current(), 0, 0 );
599 createTree( theStartObj, parent, after ? after : parent );
602 QListViewItem* OB_Browser::createTree( const SUIT_DataObject* obj,
603 QListViewItem* parent, QListViewItem* after )
608 QListViewItem* item = createItem( obj, parent, after );
611 obj->children( lst );
612 for ( DataObjectListIterator it ( lst ); it.current(); ++it )
613 createTree( it.current(), item );
616 item->setOpen( obj->isOpen() );
621 QListViewItem* OB_Browser::createItem( const SUIT_DataObject* o,
622 QListViewItem* parent, QListViewItem* after )
624 QListView* lv = listView();
629 QListViewItem* item = 0;
630 SUIT_DataObject* obj = (SUIT_DataObject*)o;
634 switch ( obj->checkType() )
636 case SUIT_DataObject::CheckBox:
637 type = QCheckListItem::CheckBox;
639 case SUIT_DataObject::RadioButton:
640 type = QCheckListItem::RadioButton;
646 if ( parent->childCount() && !after )
648 after = parent->firstChild();
649 while ( after->nextSibling() )
650 after = after->nextSibling();
656 item = new OB_ListItem( obj, parent, after );
658 item = new OB_CheckListItem( obj, parent, after, (QCheckListItem::Type)type );
663 item = new OB_ListItem( obj, parent );
665 item = new OB_CheckListItem( obj, parent, (QCheckListItem::Type)type );
671 item = new OB_ListItem( obj, lv );
673 item = new OB_CheckListItem( obj, lv, (QCheckListItem::Type)type );
676 myItems.insert( obj, item );
683 void OB_Browser::adjustWidth()
688 listView()->setColumnWidth( 0, 0 );
689 if ( listView()->firstChild() )
690 adjustWidth( listView()->firstChild() );
693 void OB_Browser::adjustWidth( QListViewItem* item )
697 item->widthChanged( 0 );
698 if ( item->isOpen() )
699 adjustWidth( item->firstChild() );
700 item = item->nextSibling();
704 SUIT_DataObject* OB_Browser::dataObject( const QListViewItem* item ) const
706 SUIT_DataObject* obj = 0;
708 if ( item && item->rtti() == OB_ListItem::RTTI() )
709 obj = ((OB_ListItem*)item)->dataObject();
710 else if ( item && item->rtti() == OB_CheckListItem::RTTI() )
711 obj = ((OB_CheckListItem*)item)->dataObject();
716 QListViewItem* OB_Browser::listViewItem( const SUIT_DataObject* obj ) const
718 QListViewItem* item = 0;
720 if ( myItems.contains( (SUIT_DataObject*)obj ) )
721 item = myItems[(SUIT_DataObject*)obj];
726 QListView* OB_Browser::listView() const
731 void OB_Browser::removeReferences( QListViewItem* item )
736 SUIT_DataObject* obj = dataObject( item );
737 obj->disconnect( this, SLOT( onDestroyed( SUIT_DataObject* ) ) );
738 myItems.remove( obj );
740 QListViewItem* i = item->firstChild();
743 removeReferences( i );
744 i = i->nextSibling();
748 void OB_Browser::createConnections( SUIT_DataObject* obj )
753 DataObjectList childList;
754 obj->children( childList, true );
756 childList.prepend( obj );
758 for ( DataObjectListIterator it( childList ); it.current(); ++it )
759 it.current()->connect( this, SLOT( onDestroyed( SUIT_DataObject* ) ) );
762 void OB_Browser::removeConnections( SUIT_DataObject* obj )
767 DataObjectList childList;
768 obj->children( childList, true );
770 childList.prepend( obj );
772 for ( DataObjectListIterator it( childList ); it.current(); ++it )
773 it.current()->disconnect( this, SLOT( onDestroyed( SUIT_DataObject* ) ) );
776 SUIT_DataObject* OB_Browser::storeState( DataObjectMap& selObjs, DataObjectMap& openObjs,
777 DataObjectKeyMap& selKeys, DataObjectKeyMap& openKeys,
778 DataObjectKey& curKey ) const
780 QListView* lv = listView();
784 SUIT_DataObject* curObj = dataObject( lv->currentItem() );
786 curKey = objectKey( curObj );
788 for ( QListViewItemIterator it( lv ); it.current(); ++it )
790 SUIT_DataObject* obj = dataObject( it.current() );
794 selObjs.insert( obj, lv->isSelected( it.current() ) );
795 openObjs.insert( obj, lv->isOpen( it.current() ) );
796 if ( lv->isSelected( it.current() ) )
797 selKeys.insert( objectKey( obj ), 0 );
798 if ( lv->isOpen( it.current() ) )
799 openKeys.insert( objectKey( obj ), 0 );
805 void OB_Browser::restoreState( const DataObjectMap& selObjs, const DataObjectMap& openObjs,
806 const SUIT_DataObject* curObj, const DataObjectKeyMap& selKeys,
807 const DataObjectKeyMap& openKeys, const DataObjectKey& curKey )
809 QListView* lv = listView();
813 bool block = lv->signalsBlocked();
814 lv->blockSignals( true );
816 QListViewItem* curItem = 0;
817 for ( QListViewItemIterator it( lv ); it.current(); ++it )
819 QListViewItem* item = it.current();
820 SUIT_DataObject* obj = dataObject( item );
825 DataObjectKey key = objectKey( obj );
827 if ( selObjs.contains( obj ) )
829 if ( selObjs[obj] && !lv->isSelected( item ) )
830 lv->setSelected( item, true );
832 else if ( !key.isNull() && selKeys.contains( key ) && !lv->isSelected( item ) )
833 lv->setSelected( item, true );
835 if ( openObjs.contains( obj ) )
838 lv->setOpen( item, true );
840 else if ( !key.isNull() && openKeys.contains( key ) )
841 lv->setOpen( item, true );
843 if ( !curItem && ( curObj == obj || ( !curKey.isNull() && curKey == key )) )
848 lv->setCurrentItem( curItem );
850 lv->blockSignals( block );
853 OB_Browser::DataObjectKey OB_Browser::objectKey( QListViewItem* i ) const
855 return objectKey( dataObject( i ) );
858 OB_Browser::DataObjectKey OB_Browser::objectKey( SUIT_DataObject* obj ) const
863 return DataObjectKey( obj->key() );
866 void OB_Browser::keyPressEvent( QKeyEvent* e )
868 if ( e->key() == Qt::Key_F5 )
871 QFrame::keyPressEvent( e );
874 void OB_Browser::onExpand()
876 DataObjectList selected;
877 getSelected( selected );
878 for ( DataObjectListIterator itr( selected ); itr.current(); ++itr )
879 expand( listViewItem( itr.current() ) );
882 void OB_Browser::onColumnVisible( int id )
884 setColumnShown( id, !isColumnVisible( id ) );
887 void OB_Browser::onDestroyed( SUIT_DataObject* obj )
892 void OB_Browser::onDropped( QPtrList<QListViewItem> items, QListViewItem* item, int action )
894 SUIT_DataObject* obj = dataObject( item );
899 for ( QPtrListIterator<QListViewItem> it( items ); it.current(); ++it )
901 SUIT_DataObject* o = dataObject( it.current() );
906 if ( !lst.isEmpty() )
907 emit dropped( lst, obj, action );
910 void OB_Browser::updateText()
912 if ( myColumnIds.isEmpty() )
915 QListView* lv = listView();
919 for ( QListViewItemIterator it( lv ); it.current(); ++it )
921 SUIT_DataObject* obj = dataObject( it.current() );
925 for( QMap<int, int>::iterator itr = myColumnIds.begin(); itr != myColumnIds.end(); ++itr )
926 it.current()->setText( itr.data(), obj->text( itr.key() ) );
930 void OB_Browser::updateText( QListViewItem* item )
932 SUIT_DataObject* obj = dataObject( item );
936 for( QMap<int, int>::iterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
937 item->setText( it.data(), obj->text( it.key() ) );
940 bool OB_Browser::eventFilter( QObject* o, QEvent* e )
942 if ( o == myView && e->type() == QEvent::ContextMenu )
944 QContextMenuEvent* ce = (QContextMenuEvent*)e;
945 if ( ce->reason() != QContextMenuEvent::Mouse )
946 contextMenuRequest( ce );
949 if ( o == myView->viewport() && e->type() == QEvent::MouseButtonRelease )
951 QMouseEvent* me = (QMouseEvent*)e;
952 if ( me->button() == RightButton )
954 QContextMenuEvent ce( QContextMenuEvent::Mouse, me->pos(), me->globalPos(), me->state() );
955 contextMenuRequest( &ce );
960 return QFrame::eventFilter( o, e );
963 void OB_Browser::contextMenuPopup( QPopupMenu* menu )
965 /* QValueList<int> cols;
966 for ( QMap<int, int>::ConstIterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
968 if ( appropriateColumn( it.key() ) )
969 cols.append( it.key() );
972 uint num = menu->count();
973 menu->setCheckable( true );
974 for ( QValueList<int>::const_iterator iter = cols.begin(); iter != cols.end(); ++iter )
976 QString name = columnTitle( *iter );
977 if ( name.isEmpty() )
980 int id = menu->insertItem( name, this, SLOT( onColumnVisible( int ) ) );
981 menu->setItemChecked( id, isColumnVisible( *iter ) );
982 menu->setItemParameter( id, *iter );
984 if ( menu->count() != num )
985 menu->insertSeparator();*/
987 DataObjectList selected;
988 getSelected( selected );
991 for ( DataObjectListIterator itr( selected ); itr.current() && !closed; ++itr )
992 closed = hasClosed( listViewItem( itr.current() ) );
996 menu->insertItem( tr( "MEN_EXPAND_ALL" ), this, SLOT( onExpand() ) );
997 menu->insertSeparator();
1001 void OB_Browser::expand( QListViewItem* item )
1006 item->setOpen( true );
1007 for ( QListViewItem* child = item->firstChild(); child; child = child->nextSibling() )
1011 bool OB_Browser::hasClosed( QListViewItem* item ) const
1016 if ( item->childCount() && !item->isOpen() )
1020 for ( QListViewItem* child = item->firstChild(); child && !has; child = child->nextSibling() )
1021 has = hasClosed( child );
1026 void OB_Browser::removeObject( SUIT_DataObject* obj, const bool autoUpd )
1031 // Removing list view items from <myItems> recursively for all children.
1032 // Otherwise, "delete item" line will destroy all item's children,
1033 // and <myItems> will contain invalid pointers (see ~QListViewItem() description in Qt docs)
1034 DataObjectList childList;
1035 obj->children( childList );
1036 for ( DataObjectListIterator it( childList ); it.current(); ++it )
1037 removeObject( it.current(), false );
1039 QListViewItem* item = listViewItem( obj );
1041 obj->disconnect( this, SLOT( onDestroyed( SUIT_DataObject* ) ) );
1042 myItems.remove( obj );
1044 if ( obj == myRoot )
1050 if ( isAutoUpdate() )
1052 SUIT_DataObject* pObj = item && item->parent() ? dataObject( item->parent() ) : 0;
1059 void OB_Browser::autoOpenBranches()
1061 int level = autoOpenLevel();
1062 QListView* lv = listView();
1063 if ( !lv || level < 1 )
1066 QListViewItem* item = lv->firstChild();
1069 openBranch( item, level );
1070 item = item->nextSibling();
1074 void OB_Browser::openBranch( QListViewItem* item, const int level )
1076 if ( !item || level < 1 || !item->childCount() )
1081 item->setOpen( true );
1082 openBranch( item->firstChild(), level - 1 );
1083 item = item->nextSibling();