Salome HOME
Update the internal persistence mechanism of documents identification: on load/save...
[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 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
27 {
28   if (myDocs.find(theDocID) != myDocs.end())
29     return myDocs[theDocID];
30   return std::shared_ptr<Model_Document>(); // not loaded, so return null
31 }
32
33 //=======================================================================
34 void Model_Application::createDocument(const int theDocID)
35 {
36   static const std::string thePartSetKind("PartSet");
37   static const std::string thePartKind("Part");
38   std::shared_ptr<Model_Document> aNew(
39     new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
40   myDocs[theDocID] = aNew;
41
42   aNew->setThis(aNew);
43   static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
44   std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
45     <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
46   aMessage->setDocument(aNew);
47   Events_Loop::loop()->send(aMessage);
48 }
49
50 //=======================================================================
51 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
52 {
53   static const std::string thePartKind("Part"); // root document is never loaded here
54   std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
55   myDocs[theDocID] = aNew;
56
57   bool aRes = true;
58   // load it if it must be loaded by demand
59   if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
60     aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
61     myLoadedByDemand.erase(theDocName);  // done, don't do it anymore
62   } else { // error
63     aRes = false;
64   }
65
66   return aRes;
67 }
68
69 //=======================================================================
70 void Model_Application::deleteDocument(const int theDocID)
71 {
72   if (myDocs.find(theDocID) != myDocs.end()) {
73     myDocs[theDocID]->close(true);
74     myDocs.erase(theDocID);
75   }
76   myLoadedByDemand.clear();
77 }
78
79 //=======================================================================
80 void Model_Application::deleteAllDocuments()
81 {
82   std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
83   for(; aDoc != myDocs.end(); aDoc++) {
84     if (!aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
85       aDoc->second->close(true);
86   }
87   myDocs.clear();
88   myLoadedByDemand.clear();
89 }
90
91 //=======================================================================
92 bool Model_Application::hasDocument(const int theDocID)
93 {
94   return myDocs.find(theDocID) != myDocs.end();
95 }
96
97 //=======================================================================
98 bool Model_Application::hasRoot()
99 {
100   return !myDocs.empty();
101 }
102
103 //=======================================================================
104 std::shared_ptr<Model_Document> Model_Application::rootDocument()
105 {
106   return myDocs[0];
107 }
108
109 //=======================================================================
110 void Model_Application::setLoadPath(std::string thePath)
111 {
112   myPath = thePath;
113 }
114
115 //=======================================================================
116 const std::string& Model_Application::loadPath() const
117 {
118   return myPath;
119 }
120
121 //=======================================================================
122 void Model_Application::setLoadByDemand(std::string theID)
123 {
124   myLoadedByDemand.insert(theID);
125 }
126
127 //=======================================================================
128 bool Model_Application::isLoadByDemand(std::string theID)
129 {
130   return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
131 }
132
133 //=======================================================================
134 void Model_Application::removeUselessDocuments(
135   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
136 {
137   std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
138   while(aDoc != myDocs.end()) {
139     bool aFound = false;
140     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
141     for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
142       aFound = aDoc->second == *aUsed;
143     }
144     if (!aFound) { // remove the useless
145       aDoc->second->close();
146       myDocs.erase(aDoc);
147       aDoc = myDocs.begin();
148     } else {
149       aDoc++;
150     }
151   }
152 }
153
154 int Model_Application::generateDocumentId()
155 {
156   int aResult = myDocs.size();
157   for(; myDocs.find(aResult) != myDocs.end(); aResult++); // count until the result id is unique
158   return aResult;
159 }
160
161 //=======================================================================
162 Model_Application::Model_Application()
163 {
164   // store handle to the application to avoid nullification
165   static Handle(Model_Application) TheKeepHandle;
166   TheKeepHandle = this;
167 }
168
169 //=======================================================================
170 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
171 {
172   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
173 }
174
175 //=======================================================================
176 Standard_CString Model_Application::ResourcesName()
177 {
178   return Standard_CString("Standard");
179 }