From d79dbdcf60652db94cddfa94b7765dd15479e098 Mon Sep 17 00:00:00 2001 From: ouv Date: Tue, 26 Jul 2011 10:06:51 +0000 Subject: [PATCH] Added methods storeState() and restoreState() intended to store/restore object browser tree state (expanding and selection). --- src/LightApp/LightApp_DataModel.cxx | 153 ++++++++++++++++++++++++++++ src/LightApp/LightApp_DataModel.h | 11 ++ 2 files changed, 164 insertions(+) diff --git a/src/LightApp/LightApp_DataModel.cxx b/src/LightApp/LightApp_DataModel.cxx index cccf466c9..0b8b2cef6 100644 --- a/src/LightApp/LightApp_DataModel.cxx +++ b/src/LightApp/LightApp_DataModel.cxx @@ -30,6 +30,9 @@ #include #include #include +#include + +#include /*! Constructor @@ -208,3 +211,153 @@ void LightApp_DataModel::unregisterColumn( SUIT_DataBrowser* browser, const QStr if( m ) m->unregisterColumn( groupId(), name ); } + +/*! + Store object browser tree state (expanding and selection) + \param theOB - object browser + \param theExpanded - map of entry to expanded state + \param theSelected - list of selected entries +*/ +void LightApp_DataModel::storeTree( SUIT_DataBrowser* theOB, + QMap& theExpanded, + QStringList& theSelected ) +{ + theExpanded.clear(); + theSelected.clear(); + if ( !theOB ) + return; + + SUIT_AbstractModel* aModel = dynamic_cast( theOB->model() ); + if ( !aModel ) + return; + + SUIT_DataObject* aRoot = theOB->root(); + if ( !aRoot ) + return; + + QtxTreeView* aTreeView = theOB->treeView(); + if ( !aTreeView ) + return; + + // expanded + QList aChildren; + aRoot->children( aChildren, true ); + + QList::iterator aCIter; + for ( aCIter = aChildren.begin(); aCIter != aChildren.end(); ++aCIter ) + { + LightApp_DataObject* aCurr = dynamic_cast( *aCIter ); + if ( !aCurr ) + continue; + + QString anEntry = aCurr->entry(); + + QModelIndex anIndex = aModel->index( aCurr ); + if ( !anIndex.isValid() ) + continue; + + bool isExp = aTreeView->isExpanded( anIndex ); + theExpanded.insert( anEntry, isExp ); + } + + // selected + QModelIndexList aList = theOB->selectedIndexes(); + QModelIndexList::iterator anIter; + for ( anIter = aList.begin(); anIter != aList.end(); ++anIter ) + { + LightApp_DataObject* aCurr = + dynamic_cast( aModel->object( *anIter ) ); + if ( !aCurr ) + continue; + + QString anEntry = aCurr->entry(); + theSelected.append( anEntry ); + } +} + +/*! + Restore object browser tree state (expanding and selection) + \param theOB - object browser + \param theExpanded - map of entry to expanded state + \param theSelected - list of selected entries +*/ +void LightApp_DataModel::restoreTree( SUIT_DataBrowser* theOB, + const QMap& theExpanded, + const QStringList& theSelected ) +{ + if ( !theOB ) + return; + + SUIT_AbstractModel* aModel = dynamic_cast( theOB->model() ); + if ( !aModel ) + return; + + SUIT_DataObject* aRoot = theOB->root(); + if ( !aRoot ) + return; + + QtxTreeView* aTreeView = theOB->treeView(); + if ( !aTreeView ) + return; + + // expanded + QList aChildren; + aRoot->children( aChildren, true ); + + QList::iterator aCIter; + for ( aCIter = aChildren.begin(); aCIter != aChildren.end(); ++aCIter ) + { + LightApp_DataObject* aCurr = dynamic_cast( *aCIter ); + if ( !aCurr ) + continue; + + QString anEntry = aCurr->entry(); + if ( !theExpanded.contains( anEntry ) ) + continue; + + QModelIndex anIndex = aModel->index( aCurr ); + if ( !anIndex.isValid() ) + continue; + + bool isExp = theExpanded[ anEntry ]; + aTreeView->setExpanded( anIndex, isExp ); + } + + // selected + QModelIndexList toSel; + QStringList::const_iterator anIter; + for ( anIter = theSelected.begin(); anIter != theSelected.end(); ++anIter ) + { + QString anEntry = *anIter; + + LightApp_DataObject* aGuiObj = findObjectByEntry( theOB, anEntry ); + if ( !aGuiObj ) + continue; + + QModelIndex anIndex = aModel->index( aGuiObj ); + if ( anIndex.isValid() ) + toSel.append( anIndex ); + } + + if ( toSel.count() > 0 ) + theOB->select( toSel, true ); +} + +/*! + Find object in the object browser by the specified entry + \param theOB - object browser + \param theEntry - entry of object to find +*/ +LightApp_DataObject* LightApp_DataModel::findObjectByEntry( SUIT_DataBrowser* theOB, + const QString& theEntry ) +{ + LightApp_DataObject* aCurrent; + SUIT_DataObjectIterator anIter( theOB->root(), SUIT_DataObjectIterator::DepthLeft ); + for ( ; anIter.current(); ++anIter ) + { + aCurrent = dynamic_cast( anIter.current() ); + if ( aCurrent && aCurrent->entry() == theEntry ) + return aCurrent; + } + return NULL; +} diff --git a/src/LightApp/LightApp_DataModel.h b/src/LightApp/LightApp_DataModel.h index 838270010..2148aab41 100644 --- a/src/LightApp/LightApp_DataModel.h +++ b/src/LightApp/LightApp_DataModel.h @@ -63,6 +63,17 @@ public: void registerColumn( SUIT_DataBrowser*, const QString&, const int ); void unregisterColumn( SUIT_DataBrowser*, const QString& ); + void storeTree( SUIT_DataBrowser* theOB, + QMap& theExpanded, + QStringList& theSelected ); + + void restoreTree( SUIT_DataBrowser* theOB, + const QMap& theExpanded, + const QStringList& theSelected ); + + LightApp_DataObject* findObjectByEntry( SUIT_DataBrowser* theOB, + const QString& theEntry ); + signals: void opened(); void saved(); -- 2.39.2