Back to index

salome-gui  6.5.0
LightApp_Study.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "LightApp_Study.h"
00024 
00025 #include "CAM_DataModel.h"
00026 #include "CAM_Module.h"
00027 #include "LightApp_Application.h"
00028 #include "LightApp_DataModel.h"
00029 #include "LightApp_DataObject.h"
00030 #include "LightApp_HDFDriver.h"
00031 
00032 #include "SUIT_ResourceMgr.h"
00033 #include "SUIT_DataObjectIterator.h"
00034 #include "SUIT_DataBrowser.h"
00035 #include "SUIT_TreeModel.h"
00036 
00037 #include <set>
00038 
00042 LightApp_Study::LightApp_Study( SUIT_Application* app )
00043 : CAM_Study( app )
00044 {
00045   // HDF persistence
00046   myDriver = new LightApp_HDFDriver();
00047   //myDriver = new LightApp_Driver();
00048 }
00049 
00053 LightApp_Study::~LightApp_Study()
00054 {
00055 }
00056 
00060 bool LightApp_Study::createDocument( const QString& theStr )
00061 {
00062   setStudyName( QString( "Study%1" ).arg( LightApp_Application::studyId() ) );
00063 
00064   // create myRoot
00065   setRoot( new LightApp_RootObject( this ) );
00066 
00067   bool aRet = CAM_Study::createDocument( theStr );
00068 
00069   emit created( this );
00070 
00071   return aRet;
00072 }
00073 
00077 bool LightApp_Study::openDocument( const QString& theFileName )
00078 {
00079   myDriver->ClearDriverContents();
00080   // create files for models from theFileName
00081   if( !openStudyData(theFileName))
00082     return false;
00083 
00084   setRoot( new LightApp_RootObject( this ) ); // create myRoot
00085 
00086   // update loaded data models: call open() and update() on them.
00087   ModelList dm_s;
00088   dataModels( dm_s );
00089   QListIterator<CAM_DataModel*> it( dm_s );
00090   while ( it.hasNext() )
00091     openDataModel( studyName(), it.next() );
00092   // this will build a SUIT_DataObject-s tree under myRoot member field
00093   // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
00094   // but tree that corresponds to not-loaded data models will be updated any way. 
00095   ((LightApp_Application*)application())->updateObjectBrowser( false ); 
00096 
00097   bool res = CAM_Study::openDocument( theFileName );
00098 
00099   emit opened( this );
00100   return res;
00101 }
00102 
00106 bool LightApp_Study::loadDocument( const QString& theStudyName )
00107 {
00108   myDriver->ClearDriverContents();
00109   if( !openStudyData(theStudyName))
00110     return false;
00111 
00112   setRoot( new LightApp_RootObject( this ) ); // create myRoot
00113 
00114   //SRN: BugID IPAL9021, put there the same code as in a method openDocument
00115 
00116   // update loaded data models: call open() and update() on them.
00117   ModelList dm_s;
00118   dataModels( dm_s );
00119 
00120   QListIterator<CAM_DataModel*> it( dm_s );
00121   while ( it.hasNext() )
00122     openDataModel( studyName(), it.next() );
00123 
00124   // this will build a SUIT_DataObject-s tree under myRoot member field
00125   // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
00126   // but tree that corresponds to not-loaded data models will be updated any way. 
00127   ((LightApp_Application*)application())->updateObjectBrowser( false ); 
00128 
00129   bool res = CAM_Study::openDocument( theStudyName );
00130   emit opened( this );
00131   //SRN: BugID IPAL9021: End
00132   return res;
00133 }
00134 
00138 bool LightApp_Study::saveDocumentAs( const QString& theFileName )
00139 {
00140   SUIT_ResourceMgr* resMgr = application()->resourceMgr();
00141   if( !resMgr )
00142     return false;
00143 
00144   ModelList list; 
00145   dataModels( list );
00146 
00147   QStringList listOfFiles;
00148   bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
00149   QListIterator<CAM_DataModel*> itList( list );
00150   while ( itList.hasNext() )
00151   {
00152     LightApp_DataModel* aModel = (LightApp_DataModel*)itList.next();
00153     if ( !aModel ) continue;
00154 
00155     std::vector<std::string> anOldList = myDriver->GetListOfFiles( aModel->module()->name().toLatin1().constData() );
00156     listOfFiles.clear();
00157     aModel->saveAs( theFileName, this, listOfFiles );
00158     if ( !listOfFiles.isEmpty() )
00159       saveModuleData(aModel->module()->name(), listOfFiles);
00160 
00161     // Remove files if necessary. File is removed if it was in the list of files before
00162     // saving and it is not contained in the list after saving. This provides correct 
00163     // removing previous temporary files. These files are not removed before saving
00164     // because they may be required for it.
00165 
00166     std::vector<std::string> aNewList = myDriver->GetListOfFiles( aModel->module()->name().toLatin1().constData() );
00167     
00168     std::set<std::string> aNewNames;
00169     std::set<std::string> toRemove;
00170     int i, n;
00171     for( i = 0, n = aNewList.size(); i < n; i++ )
00172       aNewNames.insert( aNewList[ i ] );
00173     for( i = 0, n = anOldList.size(); i < n; i++ )
00174     {
00175       if ( i == 0 ) // directory is always inserted in list
00176         toRemove.insert( anOldList[ i ] );
00177       else if ( aNewNames.find( anOldList[ i ] ) == aNewNames.end() )
00178         toRemove.insert( anOldList[ i ] );
00179     }
00180         
00181     std::vector<std::string> toRemoveList( toRemove.size() );
00182     std::set<std::string>::iterator anIter;
00183     for( anIter = toRemove.begin(), i = 0; anIter != toRemove.end(); ++anIter, ++i )
00184       toRemoveList[ i ] = *anIter;
00185 
00186     
00187     myDriver->RemoveFiles( toRemoveList, isMultiFile );
00188   }
00189 
00190   bool res = saveStudyData(theFileName);
00191   res = res && CAM_Study::saveDocumentAs( theFileName );
00192   //SRN: BugID IPAL9377, removed usage of uninitialized variable <res>
00193   if ( res )
00194     emit saved( this );
00195 
00196   return res;
00197 }
00198 
00202 bool LightApp_Study::saveDocument()
00203 {
00204   ModelList list; dataModels( list );
00205 
00206   myDriver->ClearDriverContents();
00207   QStringList listOfFiles;
00208   QListIterator<CAM_DataModel*> itList( list );
00209   while ( itList.hasNext() ) {
00210     LightApp_DataModel* aModel = (LightApp_DataModel*)itList.next();
00211     if ( !aModel ) continue;
00212 
00213     listOfFiles.clear();
00214     aModel->save( listOfFiles );
00215     saveModuleData(aModel->module()->name(), listOfFiles);
00216   }
00217 
00218   bool res = saveStudyData(studyName());
00219   res = res && CAM_Study::saveDocument();
00220   if (res)
00221     emit saved( this );
00222 
00223   return res;
00224 }
00225 
00229 void LightApp_Study::closeDocument(bool permanently)
00230 {
00231   // Inform everybody that this study is going to close when it's most safe to,
00232   // i.e. in the very beginning
00233   emit closed( this );
00234 
00235   CAM_Study::closeDocument(permanently);
00236   
00237   // Remove temporary files
00238   myDriver->ClearDriverContents();
00239 }
00240 
00245 QString LightApp_Study::referencedToEntry( const QString& entry ) const
00246 {
00247   return entry;
00248 }
00249 
00253 void LightApp_Study::children( const QString&, QStringList& ) const
00254 {
00255 }
00256 
00260 bool LightApp_Study::isComponent( const QString& entry ) const
00261 {
00262   if( !root() )
00263     return false;
00264 
00265   DataObjectList ch;
00266   root()->children( ch );
00267   DataObjectList::const_iterator anIt = ch.begin(), aLast = ch.end();
00268   for( ; anIt!=aLast; anIt++ )
00269   {
00270     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
00271     if( obj && obj->entry()==entry )
00272       return true;
00273   }
00274   return false;
00275 }
00276 
00280 QString LightApp_Study::componentDataType( const QString& entry ) const
00281 {
00282   LightApp_DataObject* aCurObj;
00283   for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
00284     aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
00285     if ( aCurObj && aCurObj->entry() == entry ) {
00286       return aCurObj->componentDataType();
00287     }
00288   }
00289   return "";
00290 }
00291 
00295 bool LightApp_Study::isModified() const
00296 {
00297   bool isAnyChanged = CAM_Study::isModified();
00298   ModelList list; dataModels( list );
00299 
00300   LightApp_DataModel* aModel = 0;
00301   QListIterator<CAM_DataModel*> it( list );
00302   while ( it.hasNext() && !isAnyChanged ) {
00303     aModel = dynamic_cast<LightApp_DataModel*>( it.next() );
00304     if ( aModel )
00305       isAnyChanged = aModel->isModified();
00306   }
00307   return isAnyChanged; 
00308 }
00309 
00313 bool LightApp_Study::isSaved() const
00314 {
00315   return CAM_Study::isSaved();
00316 }
00317 
00321 void LightApp_Study::addComponent(const CAM_DataModel* dm)
00322 {
00323 }
00324 
00328 void LightApp_Study::saveModuleData(QString theModuleName, QStringList theListOfFiles)
00329 {
00330   int aNb = theListOfFiles.count();
00331   if ( aNb == 0 )
00332     return;
00333 
00334   std::vector<std::string> aListOfFiles ( aNb );
00335   int anIndex = 0;
00336   for ( QStringList::Iterator it = theListOfFiles.begin(); it != theListOfFiles.end(); ++it ) {
00337     if ( (*it).isEmpty() )
00338       continue;
00339     aListOfFiles[anIndex] = (*it).toLatin1().constData();
00340     anIndex++;
00341   }
00342   myDriver->SetListOfFiles(theModuleName.toLatin1().constData(), aListOfFiles);
00343 }
00344 
00348 void LightApp_Study::openModuleData(QString theModuleName, QStringList& theListOfFiles)
00349 {
00350   std::vector<std::string> aListOfFiles =  myDriver->GetListOfFiles(theModuleName.toLatin1().constData());
00351   int i, aLength = aListOfFiles.size() - 1;
00352   if (aLength < 0)
00353     return;
00354 
00355   //Get a temporary directory for saved a file
00356   theListOfFiles.append(aListOfFiles[0].c_str());
00357   for(i = 0; i < aLength; i++)
00358     theListOfFiles.append(aListOfFiles[i+1].c_str());
00359 }
00360 
00364 bool LightApp_Study::saveStudyData( const QString& theFileName )
00365 {
00366   ModelList list; dataModels( list );
00367   SUIT_ResourceMgr* resMgr = application()->resourceMgr();
00368   if( !resMgr )
00369     return false;
00370   bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
00371 
00372   bool aRes = myDriver->SaveDatasInFile(theFileName.toLatin1(), isMultiFile);
00373   return aRes;
00374 }
00375 
00379 bool LightApp_Study::openStudyData( const QString& theFileName )
00380 {
00381   SUIT_ResourceMgr* resMgr = application()->resourceMgr();
00382   if( !resMgr )
00383     return false;
00384   bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
00385 
00386   bool aRes = myDriver->ReadDatasFromFile(theFileName.toLatin1(), isMultiFile);
00387   return aRes;
00388 }
00389 
00393 bool LightApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
00394 {
00395   if (!dm)
00396     return false;
00397 
00398   QStringList listOfFiles;
00399   openModuleData(dm->module()->name(), listOfFiles);
00400   if (dm && dm->open(studyName, this, listOfFiles)) {
00401     // Something has been read -> create data model tree
00402     LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
00403     if ( aDM )
00404       aDM->update(NULL, this);
00405     return true;
00406   }
00407   return false;
00408 }
00409 
00413 std::string LightApp_Study::GetTmpDir (const char* theURL,
00414                                        const bool  isMultiFile)
00415 {
00416   return myDriver->GetTmpDir(theURL, isMultiFile);
00417 }
00418 
00423 std::vector<std::string> LightApp_Study::GetListOfFiles(const char* theModuleName) const
00424 {
00425   std::vector<std::string> aListOfFiles;
00426   aListOfFiles = myDriver->GetListOfFiles(theModuleName);
00427   return aListOfFiles;
00428 }
00429 
00435 void LightApp_Study::SetListOfFiles (const char* theModuleName, const std::vector<std::string> theListOfFiles)
00436 {
00437   myDriver->SetListOfFiles(theModuleName, theListOfFiles);
00438 }
00439 
00443 void LightApp_Study::RemoveTemporaryFiles (const char* theModuleName, const bool isMultiFile) const
00444 {
00445   if (isMultiFile)
00446     return;
00447   bool isDirDeleted = true;
00448   myDriver->RemoveTemporaryFiles(theModuleName, isDirDeleted);
00449 }
00450 
00462 CAM_ModuleObject* LightApp_Study::createModuleObject( LightApp_DataModel* theDataModel, 
00463                                                 SUIT_DataObject* theParent ) const
00464 {
00465   // Calling addComponent() for symmetry with SalomeApp_Study
00466   // Currently it has empty implementation, but maybe in the future things will change...
00467   LightApp_Study* that = const_cast<LightApp_Study*>( this );
00468   that->addComponent( theDataModel );
00469 
00470   // Avoid creating multiple module objects for the same module
00471   CAM_ModuleObject* res = 0;
00472 
00473   DataObjectList children = root()->children();
00474   DataObjectList::const_iterator anIt = children.begin(), aLast = children.end();
00475   for( ; !res && anIt!=aLast; anIt++ )
00476   {
00477     LightApp_ModuleObject* obj = dynamic_cast<LightApp_ModuleObject*>( *anIt );
00478     if ( obj && obj->name() == theDataModel->module()->moduleName() )
00479       res = obj;
00480   }
00481 
00482   if ( !res ){
00483     res = new LightApp_ModuleObject( theDataModel, theParent );
00484   }
00485 
00486   return res;
00487 }
00488 
00493 void LightApp_Study::components( QStringList& comp ) const
00494 {
00495   DataObjectList children = root()->children();
00496   DataObjectList::const_iterator anIt = children.begin(), aLast = children.end();
00497   for( ; anIt!=aLast; anIt++ )
00498   {
00499     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
00500     if ( obj && obj->entry() != getVisualComponentName() )
00501       comp.append( obj->entry() );
00502   }
00503 }
00504 
00510 QString LightApp_Study::centry( const QString& comp ) const
00511 {
00512   QString e;
00513   ModelList dmlist;
00514   dataModels( dmlist );
00515   QListIterator<CAM_DataModel*> it( dmlist );
00516   while ( it.hasNext() && e.isEmpty() ) {
00517     CAM_DataModel* dm = it.next();
00518     if ( dm->module() && dm->module()->name() == comp ) {
00519       LightApp_DataObject* r = dynamic_cast<LightApp_DataObject*>( dm->root() );
00520       if ( r ) e = r->entry();
00521     }
00522   }
00523   return e;
00524 }
00525 
00529 QString LightApp_Study::getVisualComponentName() const
00530 {
00531   return "Interface Applicative";
00532 }
00533 
00534 
00535 
00536 
00537 
00545 void LightApp_Study::setObjectProperty(int theViewId, QString theEntry, QString thePropName, QVariant theValue) {
00546   
00547   //Try to find viewer manager in the map
00548   ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewId);
00549   if(v_it == myViewMgrMap.end()) {
00550 
00551     //1) Create property map
00552     PropMap aPropMap;
00553     aPropMap.insert(thePropName, theValue);
00554     
00555     //2) Create object map
00556     ObjMap anObjMap;
00557     anObjMap.insert(theEntry,aPropMap);
00558 
00559     //3) Insert in the view manager map
00560     myViewMgrMap.insert(theViewId, anObjMap);
00561     
00562   } else {
00563     ObjMap& anObjMap = v_it.value();
00564     ObjMap::Iterator o_it = anObjMap.find(theEntry);
00565     if(o_it == anObjMap.end()) {
00566       //1) Create property map
00567       PropMap aPropMap;
00568       aPropMap.insert(thePropName, theValue);
00569       
00570       //2) Insert in the object map
00571       anObjMap.insert(theEntry, aPropMap);
00572     } else {
00573       PropMap& aPropMap = o_it.value();
00574       aPropMap.insert(thePropName, theValue);
00575     }
00576   }
00577 }
00578 
00587 QVariant LightApp_Study::getObjectProperty(int theViewMgrId, QString theEntry, QString thePropName, QVariant theDefValue) const {
00588   QVariant& aResult = theDefValue;
00589   ViewMgrMap::ConstIterator v_it = myViewMgrMap.find(theViewMgrId);
00590   if(v_it != myViewMgrMap.end()){
00591     const ObjMap& anObjectMap = v_it.value();
00592     ObjMap::ConstIterator o_it = anObjectMap.find(theEntry);
00593     if(o_it != anObjectMap.end()) {
00594       const PropMap& aPropMap = o_it.value();
00595       PropMap::ConstIterator p_it = aPropMap.find(thePropName);
00596       if(p_it != aPropMap.end()) {
00597        aResult = p_it.value();
00598       }
00599     }
00600   }
00601   return aResult;
00602 }
00603 
00608 void LightApp_Study::removeViewMgr( int theViewMgrId ) { 
00609   myViewMgrMap.remove(theViewMgrId);
00610 }
00611 
00612 
00619 const PropMap& LightApp_Study::getObjectPropMap(int theViewMgrId, QString theEntry) {
00620   ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewMgrId);
00621   if (v_it != myViewMgrMap.end()) {
00622     ObjMap& anObjectMap = v_it.value();
00623     ObjMap::Iterator o_it = anObjectMap.find(theEntry);
00624     if(o_it != anObjectMap.end()) {
00625       return o_it.value();
00626     } else {
00627       PropMap aPropMap;
00628       anObjectMap.insert(theEntry, aPropMap);
00629       return anObjectMap.find(theEntry).value();
00630     }
00631   } else {
00632     PropMap aPropMap;
00633     ObjMap anObjMap;
00634     anObjMap.insert(theEntry,aPropMap);
00635     myViewMgrMap.insert(theViewMgrId, anObjMap);
00636 
00637     ObjMap& anObjectMap = myViewMgrMap.find(theViewMgrId).value();
00638     return anObjectMap.find(theEntry).value();
00639   }
00640 }
00641 
00647 void LightApp_Study::setObjectPropMap(int theViewMgrId, QString theEntry, PropMap thePropMap) {
00648   //Try to find viewer manager in the map
00649   ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewMgrId);
00650   if(v_it == myViewMgrMap.end()) {
00651     
00652     //1) Create object map
00653     ObjMap anObjMap;
00654     anObjMap.insert(theEntry,thePropMap);
00655 
00656     //3) Insert in the view manager map
00657     myViewMgrMap.insert(theViewMgrId, anObjMap);
00658   } else {
00659     ObjMap& anObjMap = v_it.value();
00660     anObjMap.insert(theEntry,thePropMap);
00661   }
00662 }
00663 
00668 void LightApp_Study::removeObjectFromAll( QString theEntry ) {
00669   ViewMgrMap::Iterator v_it = myViewMgrMap.begin();
00670   for( ;v_it != myViewMgrMap.end(); v_it++ ) {
00671     v_it.value().remove(theEntry);
00672   }
00673 }
00674 
00679 const ObjMap& LightApp_Study::getObjectMap ( int theViewMgrId ) {
00680   ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewMgrId);
00681   if( v_it == myViewMgrMap.end() ) {
00682     ObjMap anObjMap;
00683     myViewMgrMap.insert(theViewMgrId , anObjMap);
00684     return myViewMgrMap.find(theViewMgrId).value();
00685   }
00686   return v_it.value();
00687 }
00688 
00694 void LightApp_Study::setVisibilityState(const QString& theEntry, Qtx::VisibilityState theState) {
00695   LightApp_Application* app = (LightApp_Application*)application();
00696   if(!app)
00697     return;
00698   SUIT_DataBrowser* db = app->objectBrowser();
00699   if(!db)
00700     return;
00701 
00702   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>(db->model());
00703   
00704   if(treeModel)
00705     treeModel->setVisibilityState(theEntry,theState);
00706 }
00707 
00712 void LightApp_Study::setVisibilityStateForAll(Qtx::VisibilityState theState) {
00713   
00714   LightApp_Application* app = (LightApp_Application*)application();
00715   if(!app)
00716     return;
00717   SUIT_DataBrowser* db = app->objectBrowser();
00718   if(!db)
00719     return;
00720 
00721   SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>(db->model());
00722   
00723   if(treeModel)
00724     treeModel->setVisibilityStateForAll(theState);
00725 }
00726 
00732 Qtx::VisibilityState LightApp_Study::visibilityState(const QString& theEntry) const {
00733   LightApp_Application* app = (LightApp_Application*)application();
00734   if(app) {
00735     
00736     SUIT_DataBrowser* db = app->objectBrowser();
00737     if(db) {
00738       SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>(db->model());
00739       if(treeModel)
00740        return treeModel->visibilityState(theEntry);
00741     }
00742   }
00743   return Qtx::UnpresentableState;
00744 }
00745 
00751 LightApp_DataObject* LightApp_Study::findObjectByEntry( const QString& theEntry )
00752 {
00753   LightApp_DataObject* aCurObj;
00754   for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
00755     aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
00756     if ( aCurObj && aCurObj->entry() == theEntry )
00757       return aCurObj;
00758   }
00759   return NULL;
00760 }