Salome HOME
Several opens of different and same documents crash fix
[modules/shaper.git] / src / Model / Model_Application.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Application.cxx
4 // Created:     Fri Sep 2 2011
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Application.h>
8 #include <Model_Document.h>
9
10 #include <ModelAPI_Events.h>
11
12 IMPLEMENT_STANDARD_HANDLE(Model_Application, TDocStd_Application)
13 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
14
15 using namespace std;
16
17 static Handle_Model_Application TheApplication = new Model_Application;
18
19 //=======================================================================
20 Handle(Model_Application) Model_Application::getApplication()
21 {
22   return TheApplication;
23 }
24
25 //=======================================================================
26 const std::shared_ptr<Model_Document>& Model_Application::getDocument(string theDocID)
27 {
28   if (myDocs.find(theDocID) != myDocs.end())
29     return myDocs[theDocID];
30
31   static const std::string thePartSetKind("PartSet");
32   static const std::string thePartKind("Part");
33   bool isRoot = theDocID == "root"; // the document is root
34   std::shared_ptr<Model_Document> aNew(
35     new Model_Document(theDocID, isRoot ? thePartSetKind : thePartKind));
36   myDocs[theDocID] = aNew;
37
38   // load it if it must be loaded by demand
39   if (myLoadedByDemand.find(theDocID) != myLoadedByDemand.end() && !myPath.empty()) {
40     aNew->load(myPath.c_str(), aNew);
41     myLoadedByDemand.erase(theDocID);  // done, don't do it anymore
42   } else {
43     aNew->setThis(aNew);
44     static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
45     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
46       <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
47     aMessage->setDocument(aNew);
48     Events_Loop::loop()->send(aMessage);
49   }
50
51   return myDocs[theDocID];
52 }
53
54 void Model_Application::deleteDocument(string theDocID)
55 {
56   if (myDocs.find(theDocID) != myDocs.end()) {
57     myDocs[theDocID]->close(true);
58     myDocs.erase(theDocID);
59   }
60   myLoadedByDemand.clear();
61 }
62
63 void Model_Application::deleteAllDocuments()
64 {
65   std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
66   for(; aDoc != myDocs.end(); aDoc++) {
67     aDoc->second->close(true);
68   }
69   myDocs.clear();
70   myLoadedByDemand.clear();
71 }
72
73 //=======================================================================
74 bool Model_Application::hasDocument(std::string theDocID)
75 {
76   return myDocs.find(theDocID) != myDocs.end();
77 }
78
79 //=======================================================================
80 void Model_Application::setLoadPath(std::string thePath)
81 {
82   myPath = thePath;
83 }
84
85 //=======================================================================
86 const std::string& Model_Application::loadPath() const
87 {
88   return myPath;
89 }
90
91 //=======================================================================
92 void Model_Application::setLoadByDemand(std::string theID)
93 {
94   myLoadedByDemand.insert(theID);
95 }
96
97 //=======================================================================
98 bool Model_Application::isLoadByDemand(std::string theID)
99 {
100   return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
101 }
102
103 //=======================================================================
104 void Model_Application::removeUselessDocuments(
105   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
106 {
107   std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
108   while(aDoc != myDocs.end()) {
109     bool aFound = false;
110     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
111     for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
112       aFound = aDoc->second == *aUsed;
113     }
114     if (!aFound) { // remove the useless
115       aDoc->second->close();
116       myDocs.erase(aDoc);
117       aDoc = myDocs.begin();
118     } else {
119       aDoc++;
120     }
121   }
122 }
123
124 //=======================================================================
125 Model_Application::Model_Application()
126 {
127   // store handle to the application to avoid nullification
128   static Handle(Model_Application) TheKeepHandle;
129   TheKeepHandle = this;
130 }
131
132 //=======================================================================
133 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
134 {
135   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
136 }
137
138 //=======================================================================
139 Standard_CString Model_Application::ResourcesName()
140 {
141   return Standard_CString("Standard");
142 }