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