1 // File: SalomeApp_DataModel.cxx
2 // Created: 10/25/2004 10:36:06 AM
3 // Author: Sergey LITONIN
4 // Copyright (C) CEA 2004
6 #include "SalomeApp_DataModel.h"
7 #include "SalomeApp_Study.h"
8 #include "SalomeApp_RootObject.h"
9 #include "SalomeApp_DataObject.h"
10 #include "SalomeApp_Module.h"
11 #include "SalomeApp_Application.h"
12 #include "SalomeApp_SelectionMgr.h"
13 #include "SalomeApp_Engine_i.hxx"
15 #include <CAM_DataObject.h>
17 #include <SUIT_Application.h>
18 #include <SUIT_ResourceMgr.h>
20 #include "SALOMEDS_Tool.hxx"
22 #include <SALOMEconfig.h>
23 #include CORBA_SERVER_HEADER(SALOME_Exception)
25 //=======================================================================
27 // Purpose : static method used by SalomeApp_Study and SalomeApp_DataModel classes
28 // to create default SALOMEDS-based data object tree
29 //=======================================================================
30 SUIT_DataObject* SalomeApp_DataModel::BuildTree( const _PTR(SObject)& obj,
31 SUIT_DataObject* parent,
32 SalomeApp_Study* study,
35 SalomeApp_DataObject* aDataObj = 0;
40 if ( obj->GetName().size() || obj->ReferencedObject( refObj ) ) // skip nameless non references SObjects
42 _PTR(SComponent) aSComp( obj );
44 // patch for bug IPAL9313
45 if ( aSComp && parent && skip )
47 QString aSName( aSComp->GetName().c_str() );
48 DataObjectList allComponents = parent->children( /*recursive=*/false );
49 for ( DataObjectListIterator it( allComponents ); it.current(); ++it )
50 if ( it.current()->name() == aSName )
54 aDataObj = aSComp ? new SalomeApp_ModuleObject( aSComp, parent ) :
55 new SalomeApp_DataObject ( obj, parent );
57 _PTR(ChildIterator) it ( study->studyDS()->NewChildIterator( obj ) );
58 for ( ; it->More();it->Next() ) {
59 // don't use shared_ptr here, for Data Object will take
60 // ownership of this pointer
61 _PTR(SObject) aSO( it->Value() );
62 BuildTree( aSO, aDataObj, study );
68 //=======================================================================
69 // name : SalomeApp_DataModel::SalomeApp_DataModel
70 // Purpose : Constructor
71 //=======================================================================
72 SalomeApp_DataModel::SalomeApp_DataModel( CAM_Module* theModule )
73 : CAM_DataModel( theModule )
77 //=======================================================================
78 // name : SalomeApp_DataModel::~SalomeApp_DataModel
79 // Purpose : Destructor
80 //=======================================================================
81 SalomeApp_DataModel::~SalomeApp_DataModel()
85 //================================================================
88 //================================================================
89 bool SalomeApp_DataModel::open( const QString&, CAM_Study* study )
91 SalomeApp_Study* aDoc = dynamic_cast<SalomeApp_Study*>( study );
95 QString anId = getRootEntry( aDoc );
97 return true; // Probably nothing to load
99 QString anEngine = getModule()->engineIOR();
100 if ( anEngine == "-1" ) {
101 // Module doesn't have a CORBA engine and doesn't use
102 // a default one -> SALOMEDS persistence cannot be used
106 if ( anEngine.isEmpty() ) {
107 // Module use a default engine
108 //TODO: deside, if the below code has to be copyed in a light data model to avoid bulding of data tree twice
109 anEngine = SalomeApp_Application::defaultEngineIOR();
112 _PTR(Study) aStudy ( aDoc->studyDS() ); // shared_ptr cannot be used here
113 _PTR(SComponent) aSComp ( aStudy->FindComponentID( std::string( anId.latin1() ) ) );
116 _PTR(StudyBuilder) aBuilder( aStudy->NewBuilder() );
119 aBuilder->LoadWith( aSComp, std::string( anEngine.latin1() ) );
121 catch( const SALOME::SALOME_Exception& ) {
122 // Oops, something went wrong while loading -> return an error
126 // Something has been read -> create data model tree
127 buildTree( aSComp, 0, aDoc );
130 // Don't return false here, for there might be no data
131 // for a given component in the study yet
134 emit opened(); //TODO: is it really needed? to be removed maybe...
138 //================================================================
141 //================================================================
142 bool SalomeApp_DataModel::save()
148 //================================================================
151 //================================================================
152 bool SalomeApp_DataModel::saveAs( const QString&, CAM_Study* )
158 //================================================================
161 //================================================================
162 bool SalomeApp_DataModel::close()
168 //================================================================
171 //================================================================
172 void SalomeApp_DataModel::update( SalomeApp_DataObject*, SalomeApp_Study* study )
174 SalomeApp_RootObject* studyRoot = 0;
176 SalomeApp_DataObject* modelRoot = dynamic_cast<SalomeApp_DataObject*>( root() );
177 if ( !modelRoot ){ // not yet connected to a study -> try using <study> argument
179 study = dynamic_cast<SalomeApp_Study*>( getModule()->getApp()->activeStudy() );
182 studyRoot = dynamic_cast<SalomeApp_RootObject*>( study->root() );
183 QString anId = getRootEntry( study );
184 if ( !anId.isEmpty() ){ // if nothing is published in the study for this module -> do nothing
185 _PTR(Study) aStudy ( study->studyDS() );
186 sobj = aStudy->FindComponentID( std::string( anId.latin1() ) );
191 studyRoot = dynamic_cast<SalomeApp_RootObject*>( modelRoot->root() );
192 study = studyRoot->study(); // <study> value should not change here theoretically, but just to make sure
193 _PTR(Study) aStudy ( study->studyDS() );
195 // modelRoot->object() cannot be reused here: it is about to be deleted by buildTree() soon
196 sobj = aStudy->FindComponentID( std::string( modelRoot->entry().latin1() ) );
198 buildTree( sobj, studyRoot, study );
201 //================================================================
202 // Function : buildTree
203 // Purpose : private method
204 //================================================================
205 void SalomeApp_DataModel::buildTree( const _PTR(SObject)& obj,
206 SUIT_DataObject* parent,
207 SalomeApp_Study* study )
211 //if ( !root() ){ // Build default SALOMEDS-based data object tree and insert it into study
212 SalomeApp_ModuleObject* aNewRoot = dynamic_cast<SalomeApp_ModuleObject*>( BuildTree( obj, parent, study ) );
214 aNewRoot->setDataModel( this );
220 //================================================================
221 // Function : getModule
223 //================================================================
225 SalomeApp_Module* SalomeApp_DataModel::getModule() const
227 return dynamic_cast<SalomeApp_Module*>( module() );
230 //================================================================
231 // Function : getStudy
233 //================================================================
234 SalomeApp_Study* SalomeApp_DataModel::getStudy() const
236 SalomeApp_RootObject* aRoot = dynamic_cast<SalomeApp_RootObject*>( root()->root() );
239 return aRoot->study();
242 //================================================================
243 // Function : getRootEntry
244 // Purpose : returns study entry corresponding to this data model
245 //================================================================
246 QString SalomeApp_DataModel::getRootEntry( SalomeApp_Study* study ) const
249 if ( root() && root()->root() ) { // data model already in a study
250 SalomeApp_DataObject* anObj = dynamic_cast<SalomeApp_DataObject*>( root() );
252 anEntry = anObj->entry();
254 else if ( study && study->studyDS() ) { // this works even if <myRoot> is null
255 _PTR(SComponent) aSComp( study->studyDS()->FindComponent( module()->name() ) );
257 anEntry = aSComp->GetID().c_str();
262 //================================================================
263 // Function : isModified
264 // Purpose : default implementation, always returns false so as not to mask study's isModified()
265 //================================================================
266 bool SalomeApp_DataModel::isModified() const
271 //================================================================
272 // Function : isSaved
273 // Purpose : default implementation, always returns true so as not to mask study's isSaved()
274 //================================================================
275 bool SalomeApp_DataModel::isSaved() const
280 // BEGIN: methods to be used by CORBAless modules
282 //================================================================
283 // Function : GetListOfFiles
284 // Purpose : to be used by CORBAless modules
285 //================================================================
286 std::vector<std::string> SalomeApp_DataModel::GetListOfFiles() const
287 //(const int theStudyId, const char* theComponentName) const
289 SUIT_Study* anActiveStudy = getModule()->getApp()->activeStudy();
291 int aStudyId = anActiveStudy->id();
292 SalomeApp_Engine_i* aDefaultEngine = SalomeApp_Engine_i::GetInstance();
293 if (aDefaultEngine) {
294 return aDefaultEngine->GetListOfFiles(aStudyId, module()->name());
298 std::vector<std::string> aListOfFiles;
302 //================================================================
303 // Function : SetListOfFiles
304 // Purpose : to be used by CORBAless modules
305 //================================================================
306 void SalomeApp_DataModel::SetListOfFiles (const std::vector<std::string> theListOfFiles)
307 //(const std::vector<std::string> theListOfFiles,
308 // const int theStudyId,
309 // const char* theComponentName)
311 SUIT_Study* anActiveStudy = getModule()->getApp()->activeStudy();
313 int aStudyId = anActiveStudy->id();
314 SalomeApp_Engine_i* aDefaultEngine = SalomeApp_Engine_i::GetInstance();
315 if (aDefaultEngine) {
316 aDefaultEngine->SetListOfFiles(theListOfFiles, aStudyId, module()->name());
321 //================================================================
322 // Function : GetTmpDir
323 // Purpose : Static method. To be used by CORBAless modules
324 //================================================================
325 std::string SalomeApp_DataModel::GetTmpDir (const char* theURL,
326 const bool isMultiFile)
328 std::string anURLDir = SALOMEDS_Tool::GetDirFromPath(theURL);
329 std::string aTmpDir = isMultiFile ? anURLDir : SALOMEDS_Tool::GetTmpDir();
333 //================================================================
334 // Function : RemoveTemporaryFiles
335 // Purpose : to be used by CORBAless modules
336 //================================================================
337 void SalomeApp_DataModel::RemoveTemporaryFiles (const bool isMultiFile) const
342 std::vector<std::string> aListOfFiles = GetListOfFiles();
343 if (aListOfFiles.size() > 0) {
344 std::string aTmpDir = aListOfFiles[0];
346 const int n = aListOfFiles.size() - 1;
347 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
349 for (int i = 0; i < n; i++)
350 aSeq[i] = CORBA::string_dup(aListOfFiles[i + 1].c_str());
352 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
356 // END: methods to be used by CORBAless modules