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 if ( myRoot != theRoot && isAutoDeleteObjects() )
187 createConnections( myRoot );
190 updateView( myRoot );
191 else if ( listView() )
197 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
201 if ( selNum != numberOfSelected() )
202 emit selectionChanged();
205 int OB_Browser::numberOfSelected() const
210 for ( QListViewItemIterator it( listView() ); it.current(); ++it )
211 if ( it.current()->isSelected() )
217 DataObjectList OB_Browser::getSelected() const
224 void OB_Browser::getSelected( DataObjectList& theObjList ) const
231 for ( QListViewItemIterator it( listView() ); it.current(); ++it )
233 if ( it.current()->isSelected() )
235 SUIT_DataObject* obj = dataObject( it.current() );
237 theObjList.append( obj );
242 void OB_Browser::setSelected( const SUIT_DataObject* theObject, const bool append )
245 lst.append( theObject );
246 setSelected( lst, append );
249 void OB_Browser::setSelected( const DataObjectList& theObjLst, const bool append )
251 QListView* lv = listView();
256 bool changed = false;
257 bool block = lv->signalsBlocked();
258 lv->blockSignals( true );
260 QMap<QListViewItem*, int> map;
261 for ( DataObjectListIterator itr( theObjLst ); itr.current(); ++itr )
262 map.insert( listViewItem( itr.current() ), 0 );
264 for ( QListViewItemIterator it( lv ); it.current(); ++it )
266 QListViewItem* item = it.current();
267 if ( map.contains( item ) && !lv->isSelected( item ) )
270 lv->setSelected( item, true );
272 if ( !append && !map.contains( item ) && lv->isSelected( item ) )
275 lv->setSelected( item, false );
279 lv->blockSignals( block );
284 QListViewItem* sel = 0;
285 QListViewItem* cur = lv->currentItem();
286 for ( QListViewItemIterator iter( lv ); iter.current() && !sel; ++iter, count++ )
288 if ( iter.current()->isSelected() && cur == iter.current() )
289 sel = iter.current();
292 for ( QListViewItemIterator itr( lv ); itr.current() && !sel; ++itr )
294 if ( itr.current()->isSelected() )
299 lv->setCurrentItem( sel );
301 if ( sel && count == 1 )
302 lv->ensureItemVisible( sel );
304 emit selectionChanged();
308 bool OB_Browser::isOpen( SUIT_DataObject* theObject ) const
312 res = listView()->isOpen( listViewItem( theObject ) );
316 void OB_Browser::setOpen( SUIT_DataObject* theObject, const bool theOpen )
319 listView()->setOpen( listViewItem( theObject ), theOpen );
322 SUIT_DataObject* OB_Browser::dataObjectAt( const QPoint& pos ) const
324 SUIT_DataObject* obj = 0;
326 QListView* lv = listView();
328 obj = dataObject( lv->itemAt( pos ) );
333 OB_Filter* OB_Browser::filter() const
335 return myView->filter();
338 void OB_Browser::setFilter( OB_Filter* f )
340 myView->setFilter( f );
343 int OB_Browser::addColumn( const QString& label, const int id, const int width )
345 return addColumn( QIconSet(), label, id, width );
348 int OB_Browser::addColumn( const QIconSet& icon, const QString& label, const int id, const int width )
350 QListView* lv = listView();
357 while ( myColumnIds.contains( theId ) )
361 if ( myColumnIds.contains( theId ) )
362 return -1; // can not reuse id
366 sec = lv->addColumn( label, width );
368 sec = lv->addColumn( icon, label, width );
373 myColumnIds.insert( theId, sec );
379 void OB_Browser::removeColumn( const int id )
381 QListView* lv = listView();
382 if ( !lv || !myColumnIds.contains( id ) )
385 int sec = myColumnIds[id];
386 lv->removeColumn( sec );
388 // update map of column indeces
389 myColumnIds.remove( id );
390 for ( QMap<int, int>::iterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
398 void OB_Browser::setNameTitle( const QString& label )
400 setNameTitle( QIconSet(), label );
403 void OB_Browser::setNameTitle( const QIconSet& icon, const QString& label )
405 QListView* lv = listView();
410 lv->setColumnText( 0, label );
412 lv->setColumnText( 0, icon, label );
415 void OB_Browser::setColumnTitle( const int id, const QString& label )
417 setColumnTitle( id, QIconSet(), label );
420 void OB_Browser::setColumnTitle( const int id, const QIconSet& icon, const QString& label )
422 QListView* lv = listView();
423 if ( !lv || !myColumnIds.contains( id ) )
427 lv->setColumnText( myColumnIds[id], label );
429 lv->setColumnText( myColumnIds[id], icon, label );
432 QString OB_Browser::nameTitle() const
434 return myView->columnText( 0 );
437 QString OB_Browser::columnTitle( const int id ) const
440 if ( myColumnIds.contains( id ) )
441 txt = myView->columnText( myColumnIds[id] );
445 bool OB_Browser::isColumnVisible( const int id ) const
447 return myColumnIds.contains( id ) && myView->isShown( myColumnIds[id] );
450 void OB_Browser::setColumnShown( const int id, const bool on )
452 if ( !myColumnIds.contains( id ) )
455 myView->setShown( myColumnIds[id], on );
458 QValueList<int> OB_Browser::columns() const
461 for ( QMap<int, int>::ConstIterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
462 lst.append( it.key() );
466 bool OB_Browser::appropriateColumn( const int id ) const
469 if ( myColumnIds.contains( id ) )
470 res = myView->appropriate( myColumnIds[id] );
474 void OB_Browser::setAppropriateColumn( const int id, const bool on )
476 if ( !myColumnIds.contains( id ) )
479 myView->setAppropriate( myColumnIds[id], on );
482 void OB_Browser::updateTree( SUIT_DataObject* obj )
484 if ( !obj && !(obj = getRootObject()) )
487 DataObjectKey curKey;
488 DataObjectMap selObjs, openObjs;
489 DataObjectKeyMap selKeys, openKeys;
491 int selNum = numberOfSelected();
493 SUIT_DataObject* curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
495 createConnections( obj );
498 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
502 if ( selNum != numberOfSelected() )
503 emit selectionChanged();
506 void OB_Browser::replaceTree( SUIT_DataObject* src, SUIT_DataObject* trg )
508 if ( !src || !trg || src->root() != getRootObject() )
511 DataObjectKey curKey;
512 DataObjectMap selObjs, openObjs;
513 DataObjectKeyMap selKeys, openKeys;
515 int selNum = numberOfSelected();
517 SUIT_DataObject* curObj = storeState( selObjs, openObjs, selKeys, openKeys, curKey );
519 SUIT_DataObject* parent = src->parent();
520 int pos = parent ? parent->childPos( src ) : -1;
524 if ( src != trg && isAutoDeleteObjects() )
527 if ( parent && pos != -1 )
528 parent->insertChild( trg, pos );
530 trg->setParent( parent );
532 createConnections( trg );
535 restoreState( selObjs, openObjs, curObj, selKeys, openKeys, curKey );
539 if ( selNum != numberOfSelected() )
540 emit selectionChanged();
543 void OB_Browser::updateView( const SUIT_DataObject* theStartObj )
545 QListView* lv = listView();
549 if ( !theStartObj || theStartObj->root() != getRootObject() )
552 QListViewItem* after = 0;
553 QListViewItem* parent = 0;
554 QListViewItem* startItem = listViewItem( theStartObj );
556 if ( theStartObj->parent() )
557 parent = listViewItem( theStartObj->parent() );
559 QListViewItem* prv = 0;
560 QListViewItem* cur = parent ? parent->firstChild() : lv->firstChild();
561 while ( !after && cur )
563 if ( cur == startItem )
567 cur = cur->nextSibling();
570 QPtrList<QListViewItem> delList;
571 if ( !startItem && theStartObj == getRootObject() )
573 for ( QListViewItem* item = lv->firstChild(); item; item = item->nextSibling() )
574 delList.append( item );
577 delList.append( startItem );
579 for ( QPtrListIterator<QListViewItem> it( delList ); it.current(); ++it )
581 removeReferences( it.current() );
585 // for myRoot object, if myShowRoot==false, then creating multiple top-level QListViewItem-s
586 // (which will correspond to myRoot's children = Modules).
587 if ( rootIsDecorated() && theStartObj == myRoot )
590 theStartObj->children( lst );
591 DataObjectListIterator it ( lst );
592 // iterating backward to preserve the order of elements in the tree
593 for ( it.toLast(); it.current(); --it )
594 createTree( it.current(), 0, 0 );
597 createTree( theStartObj, parent, after ? after : parent );
600 QListViewItem* OB_Browser::createTree( const SUIT_DataObject* obj,
601 QListViewItem* parent, QListViewItem* after )
606 QListViewItem* item = createItem( obj, parent, after );
609 obj->children( lst );
610 for ( DataObjectListIterator it ( lst ); it.current(); ++it )
611 createTree( it.current(), item );
614 item->setOpen( obj->isOpen() );
619 QListViewItem* OB_Browser::createItem( const SUIT_DataObject* o,
620 QListViewItem* parent, QListViewItem* after )
622 QListView* lv = listView();
627 QListViewItem* item = 0;
628 SUIT_DataObject* obj = (SUIT_DataObject*)o;
632 switch ( obj->checkType() )
634 case SUIT_DataObject::CheckBox:
635 type = QCheckListItem::CheckBox;
637 case SUIT_DataObject::RadioButton:
638 type = QCheckListItem::RadioButton;
644 if ( parent->childCount() && !after )
646 after = parent->firstChild();
647 while ( after->nextSibling() )
648 after = after->nextSibling();
654 item = new OB_ListItem( obj, parent, after );
656 item = new OB_CheckListItem( obj, parent, after, (QCheckListItem::Type)type );
661 item = new OB_ListItem( obj, parent );
663 item = new OB_CheckListItem( obj, parent, (QCheckListItem::Type)type );
669 item = new OB_ListItem( obj, lv );
671 item = new OB_CheckListItem( obj, lv, (QCheckListItem::Type)type );
674 myItems.insert( obj, item );
681 void OB_Browser::adjustWidth()
686 listView()->setColumnWidth( 0, 0 );
687 if ( listView()->firstChild() )
688 adjustWidth( listView()->firstChild() );
691 void OB_Browser::adjustWidth( QListViewItem* item )
695 item->widthChanged( 0 );
696 if ( item->isOpen() )
697 adjustWidth( item->firstChild() );
698 item = item->nextSibling();
702 SUIT_DataObject* OB_Browser::dataObject( const QListViewItem* item ) const
704 SUIT_DataObject* obj = 0;
706 if ( item && item->rtti() == OB_ListItem::RTTI() )
707 obj = ((OB_ListItem*)item)->dataObject();
708 else if ( item && item->rtti() == OB_CheckListItem::RTTI() )
709 obj = ((OB_CheckListItem*)item)->dataObject();
714 QListViewItem* OB_Browser::listViewItem( const SUIT_DataObject* obj ) const
716 QListViewItem* item = 0;
718 if ( myItems.contains( (SUIT_DataObject*)obj ) )
719 item = myItems[(SUIT_DataObject*)obj];
724 QListView* OB_Browser::listView() const
729 void OB_Browser::removeReferences( QListViewItem* item )
734 SUIT_DataObject* obj = dataObject( item );
735 myItems.remove( obj );
737 QListViewItem* i = item->firstChild();
740 removeReferences( i );
741 i = i->nextSibling();
745 void OB_Browser::createConnections( SUIT_DataObject* obj )
750 DataObjectList childList;
751 obj->children( childList, true );
753 childList.prepend( obj );
755 for ( DataObjectListIterator it( childList ); it.current(); ++it )
756 it.current()->connect( this, SLOT( onDestroyed( SUIT_DataObject* ) ) );
759 SUIT_DataObject* OB_Browser::storeState( DataObjectMap& selObjs, DataObjectMap& openObjs,
760 DataObjectKeyMap& selKeys, DataObjectKeyMap& openKeys,
761 DataObjectKey& curKey ) const
763 QListView* lv = listView();
767 SUIT_DataObject* curObj = dataObject( lv->currentItem() );
769 curKey = objectKey( curObj );
771 for ( QListViewItemIterator it( lv ); it.current(); ++it )
773 SUIT_DataObject* obj = dataObject( it.current() );
777 selObjs.insert( obj, lv->isSelected( it.current() ) );
778 openObjs.insert( obj, lv->isOpen( it.current() ) );
779 if ( lv->isSelected( it.current() ) )
780 selKeys.insert( objectKey( obj ), 0 );
781 if ( lv->isOpen( it.current() ) )
782 openKeys.insert( objectKey( obj ), 0 );
788 void OB_Browser::restoreState( const DataObjectMap& selObjs, const DataObjectMap& openObjs,
789 const SUIT_DataObject* curObj, const DataObjectKeyMap& selKeys,
790 const DataObjectKeyMap& openKeys, const DataObjectKey& curKey )
792 QListView* lv = listView();
796 bool block = lv->signalsBlocked();
797 lv->blockSignals( true );
799 QListViewItem* curItem = 0;
800 for ( QListViewItemIterator it( lv ); it.current(); ++it )
802 QListViewItem* item = it.current();
803 SUIT_DataObject* obj = dataObject( item );
808 DataObjectKey key = objectKey( obj );
810 if ( selObjs.contains( obj ) )
812 if ( selObjs[obj] && !lv->isSelected( item ) )
813 lv->setSelected( item, true );
815 else if ( !key.isNull() && selKeys.contains( key ) && !lv->isSelected( item ) )
816 lv->setSelected( item, true );
818 if ( openObjs.contains( obj ) )
821 lv->setOpen( item, true );
823 else if ( !key.isNull() && openKeys.contains( key ) )
824 lv->setOpen( item, true );
826 if ( !curItem && ( curObj == obj || ( !curKey.isNull() && curKey == key )) )
831 lv->setCurrentItem( curItem );
833 lv->blockSignals( block );
836 OB_Browser::DataObjectKey OB_Browser::objectKey( QListViewItem* i ) const
838 return objectKey( dataObject( i ) );
841 OB_Browser::DataObjectKey OB_Browser::objectKey( SUIT_DataObject* obj ) const
846 return DataObjectKey( obj->key() );
849 void OB_Browser::keyPressEvent( QKeyEvent* e )
851 if ( e->key() == Qt::Key_F5 )
854 QFrame::keyPressEvent( e );
857 void OB_Browser::onExpand()
859 DataObjectList selected;
860 getSelected( selected );
861 for ( DataObjectListIterator itr( selected ); itr.current(); ++itr )
862 expand( listViewItem( itr.current() ) );
865 void OB_Browser::onColumnVisible( int id )
867 setColumnShown( id, !isColumnVisible( id ) );
870 void OB_Browser::onDestroyed( SUIT_DataObject* obj )
875 void OB_Browser::onDropped( QPtrList<QListViewItem> items, QListViewItem* item, int action )
877 SUIT_DataObject* obj = dataObject( item );
882 for ( QPtrListIterator<QListViewItem> it( items ); it.current(); ++it )
884 SUIT_DataObject* o = dataObject( it.current() );
889 if ( !lst.isEmpty() )
890 emit dropped( lst, obj, action );
893 void OB_Browser::updateText()
895 if ( myColumnIds.isEmpty() )
898 QListView* lv = listView();
902 for ( QListViewItemIterator it( lv ); it.current(); ++it )
904 SUIT_DataObject* obj = dataObject( it.current() );
908 for( QMap<int, int>::iterator itr = myColumnIds.begin(); itr != myColumnIds.end(); ++itr )
909 it.current()->setText( itr.data(), obj->text( itr.key() ) );
913 void OB_Browser::updateText( QListViewItem* item )
915 SUIT_DataObject* obj = dataObject( item );
919 for( QMap<int, int>::iterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
920 item->setText( it.data(), obj->text( it.key() ) );
923 bool OB_Browser::eventFilter( QObject* o, QEvent* e )
925 if ( o == myView && e->type() == QEvent::ContextMenu )
927 QContextMenuEvent* ce = (QContextMenuEvent*)e;
928 if ( ce->reason() != QContextMenuEvent::Mouse )
929 contextMenuRequest( ce );
932 if ( o == myView->viewport() && e->type() == QEvent::MouseButtonRelease )
934 QMouseEvent* me = (QMouseEvent*)e;
935 if ( me->button() == RightButton )
937 QContextMenuEvent ce( QContextMenuEvent::Mouse, me->pos(), me->globalPos(), me->state() );
938 contextMenuRequest( &ce );
943 return QFrame::eventFilter( o, e );
946 void OB_Browser::contextMenuPopup( QPopupMenu* menu )
948 /* QValueList<int> cols;
949 for ( QMap<int, int>::ConstIterator it = myColumnIds.begin(); it != myColumnIds.end(); ++it )
951 if ( appropriateColumn( it.key() ) )
952 cols.append( it.key() );
955 uint num = menu->count();
956 menu->setCheckable( true );
957 for ( QValueList<int>::const_iterator iter = cols.begin(); iter != cols.end(); ++iter )
959 QString name = columnTitle( *iter );
960 if ( name.isEmpty() )
963 int id = menu->insertItem( name, this, SLOT( onColumnVisible( int ) ) );
964 menu->setItemChecked( id, isColumnVisible( *iter ) );
965 menu->setItemParameter( id, *iter );
967 if ( menu->count() != num )
968 menu->insertSeparator();*/
970 DataObjectList selected;
971 getSelected( selected );
974 for ( DataObjectListIterator itr( selected ); itr.current() && !closed; ++itr )
975 closed = hasClosed( listViewItem( itr.current() ) );
979 menu->insertItem( tr( "MEN_EXPAND_ALL" ), this, SLOT( onExpand() ) );
980 menu->insertSeparator();
984 void OB_Browser::expand( QListViewItem* item )
989 item->setOpen( true );
990 for ( QListViewItem* child = item->firstChild(); child; child = child->nextSibling() )
994 bool OB_Browser::hasClosed( QListViewItem* item ) const
999 if ( item->childCount() && !item->isOpen() )
1003 for ( QListViewItem* child = item->firstChild(); child && !has; child = child->nextSibling() )
1004 has = hasClosed( child );
1009 void OB_Browser::removeObject( SUIT_DataObject* obj, const bool autoUpd )
1014 // Removing list view items from <myItems> recursively for all children.
1015 // Otherwise, "delete item" line will destroy all item's children,
1016 // and <myItems> will contain invalid pointers (see ~QListViewItem() description in Qt docs)
1017 DataObjectList childList;
1018 obj->children( childList );
1019 for ( DataObjectListIterator it( childList ); it.current(); ++it )
1020 removeObject( it.current(), false );
1022 QListViewItem* item = listViewItem( obj );
1024 myItems.remove( obj );
1026 if ( obj == myRoot )
1032 if ( isAutoUpdate() )
1034 SUIT_DataObject* pObj = item && item->parent() ? dataObject( item->parent() ) : 0;
1041 void OB_Browser::autoOpenBranches()
1043 int level = autoOpenLevel();
1044 QListView* lv = listView();
1045 if ( !lv || level < 1 )
1048 QListViewItem* item = lv->firstChild();
1051 openBranch( item, level );
1052 item = item->nextSibling();
1056 void OB_Browser::openBranch( QListViewItem* item, const int level )
1058 if ( !item || level < 1 || !item->childCount() )
1061 item->setOpen( true );
1064 openBranch( item->firstChild(), level - 1 );
1065 item = item->nextSibling();