Salome HOME
Support of wide string
[modules/shaper.git] / src / Model / Model_Application.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_Application.h>
21 #include <Model_Document.h>
22
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Tools.h>
25
26 #include <BinDrivers_DocumentRetrievalDriver.hxx>
27 #include <BinDrivers_DocumentStorageDriver.hxx>
28
29 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
30
31 static Handle_Model_Application TheApplication = new Model_Application;
32
33 //=======================================================================
34 Handle(Model_Application) Model_Application::getApplication()
35 {
36   return TheApplication;
37 }
38
39 //=======================================================================
40 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
41 {
42   if (myDocs.find(theDocID) != myDocs.end())
43     return myDocs[theDocID];
44   return std::shared_ptr<Model_Document>(); // not loaded, so return null
45 }
46
47 //=======================================================================
48 void Model_Application::createDocument(const int theDocID)
49 {
50   static const std::string thePartSetKind("PartSet");
51   static const std::string thePartKind("Part");
52   std::shared_ptr<Model_Document> aNew(
53     new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
54   myDocs[theDocID] = aNew;
55
56   aNew->setThis(aNew);
57   static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
58   std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
59     <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
60   aMessage->setDocument(aNew);
61   Events_Loop::loop()->send(aMessage);
62 }
63
64 //=======================================================================
65 bool Model_Application::loadDocument(const std::wstring theDocName, const int theDocID)
66 {
67   static const std::string thePartKind("Part"); // root document is never loaded here
68   std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
69   myDocs[theDocID] = aNew;
70
71   bool aRes = true;
72   // load it if it must be loaded by demand
73   if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
74     aRes = aNew->load(myPath.c_str(), ModelAPI_Tools::toString(theDocName).c_str(), aNew);
75     myLoadedByDemand.erase(theDocName);  // done, don't do it anymore
76   } else { // error
77     aRes = false;
78   }
79
80   return aRes;
81 }
82
83 //=======================================================================
84 void Model_Application::deleteDocument(const int theDocID)
85 {
86   if (myDocs.find(theDocID) != myDocs.end()) {
87     myDocs[theDocID]->close(true);
88     myDocs.erase(theDocID);
89   }
90   myLoadedByDemand.clear();
91 }
92
93 //=======================================================================
94 void Model_Application::deleteAllDocuments()
95 {
96   std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
97   for(; aDoc != myDocs.end(); aDoc++) {
98     if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
99       aDoc->second->close(true);
100   }
101   myDocs.clear();
102   myLoadedByDemand.clear();
103 }
104
105 //=======================================================================
106 bool Model_Application::hasDocument(const int theDocID)
107 {
108   return myDocs.find(theDocID) != myDocs.end();
109 }
110
111 //=======================================================================
112 bool Model_Application::hasRoot()
113 {
114   return !myDocs.empty();
115 }
116
117 //=======================================================================
118 std::shared_ptr<Model_Document> Model_Application::rootDocument()
119 {
120   return myDocs[0];
121 }
122
123 //=======================================================================
124 void Model_Application::setLoadPath(std::string thePath)
125 {
126   myPath = thePath;
127 }
128
129 //=======================================================================
130 const std::string& Model_Application::loadPath() const
131 {
132   return myPath;
133 }
134
135 //=======================================================================
136 void Model_Application::setLoadByDemand(std::wstring theID, const int theDocID)
137 {
138   myLoadedByDemand[theID] = theDocID;
139 }
140
141 //=======================================================================
142 bool Model_Application::isLoadByDemand(std::wstring theID, const int theDocIndex)
143 {
144   return myLoadedByDemand.find(theID) != myLoadedByDemand.end() &&
145     myLoadedByDemand[theID] == theDocIndex;
146 }
147
148 //=======================================================================
149 int Model_Application::generateDocumentId()
150 {
151   int aResult;
152   // count until the result id is unique
153   for(aResult = int(myDocs.size()); true; aResult++) {
154     if (myDocs.find(aResult) == myDocs.end()) {
155       bool aFound = false;
156       std::map<std::wstring, int>::iterator aLBDIter = myLoadedByDemand.begin();
157       for(; aLBDIter != myLoadedByDemand.end(); aLBDIter++) {
158         if (aLBDIter->second == aResult) {
159           aFound = true;
160           break;
161         }
162       }
163       if (!aFound) break;
164     }
165   }
166   return aResult;
167 }
168
169 //=======================================================================
170 Model_Application::Model_Application()
171 {
172   // store handle to the application to avoid nullification
173   static Handle(Model_Application) TheKeepHandle;
174   TheKeepHandle = this;
175   // additional file format supported
176   static TCollection_ExtendedString THE_DOC_FORMAT("BinShaperPart");
177   static TCollection_ExtendedString THE_FILE_EXT("shaperpart");
178   Handle(PCDM_RetrievalDriver) aReader = new BinDrivers_DocumentRetrievalDriver;
179   Handle(PCDM_StorageDriver) aWriter = new BinDrivers_DocumentStorageDriver;
180   TheKeepHandle->DefineFormat(THE_DOC_FORMAT, "Shaper Part document", THE_FILE_EXT,
181                               aReader, aWriter);
182 }
183
184 //=======================================================================
185 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
186 {
187   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
188 }
189
190 //=======================================================================
191 Standard_CString Model_Application::ResourcesName()
192 {
193   return Standard_CString("Standard");
194 }