]> SALOME platform Git repositories - modules/shaper.git/blob - src/SHAPERGUI/SHAPERGUI_DataModel.cpp
Salome HOME
e1ca3a7e52bdceee61dc9713ec8c8ef3075262e6
[modules/shaper.git] / src / SHAPERGUI / SHAPERGUI_DataModel.cpp
1 // Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "SHAPERGUI_DataModel.h"
21 #include "SHAPERGUI.h"
22
23 #include <XGUI_Workshop.h>
24
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ExchangePlugin_Dump.h>
28
29 #include <LightApp_Study.h>
30 #include <CAM_Application.h>
31 #include <CAM_DataObject.h>
32 #include <SUIT_Tools.h>
33 #include <SUIT_ResourceMgr.h>
34
35 #include <QFile>
36 #include <QDir>
37 #include <QTextStream>
38
39 #define DUMP_NAME "shaper_dump.py"
40
41
42 SHAPERGUI_DataModel::SHAPERGUI_DataModel(SHAPERGUI* theModule)
43     : LightApp_DataModel(theModule), myStudyPath(""), myModule(theModule)
44 {
45 }
46
47 SHAPERGUI_DataModel::~SHAPERGUI_DataModel()
48 {
49 }
50
51 bool SHAPERGUI_DataModel::open(const QString& thePath, CAM_Study* theStudy, QStringList theFiles)
52 {
53   LightApp_DataModel::open( thePath, theStudy, theFiles );
54   if (theFiles.size() == 0)
55     return false;
56
57   myStudyPath = thePath;
58
59   // If the file is Multi(contain all module files inside), the open SALOME functionality creates
60   // these files in a temporary directory. After the open functionality is finished, it removes
61   // these files (in the full SALOME mode).
62   // The postponed loading of the files is realized in the SHAPER module. So, it is important do
63   // not remove the opened files.
64   // The following code creates a new tmp directory with a copy of files.
65   QString aTmpDir = theFiles.first();
66
67   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( myModule->application()->activeStudy() );
68   QString aNewTmpDir = aStudy->GetTmpDir("", false).c_str();
69
70   bool isDone = true;
71   QDir aDir(aTmpDir);
72   QStringList aFiles = aDir.entryList(QDir::Files);
73   QStringList::const_iterator anIt = aFiles.begin(), aLast = aFiles.end();
74   for (; anIt != aLast; anIt++) {
75     QString aFileName = *anIt;
76
77     QString aCurrentFile = SUIT_Tools::addSlash(aTmpDir) + aFileName;
78     QString aNewFile = SUIT_Tools::addSlash(aNewTmpDir) + aFileName;
79     if (!QFile::copy(aCurrentFile, aNewFile))
80       isDone = false;
81   }
82   if (isDone) {
83     myTmpDirectory = aNewTmpDir;
84   }
85   else {
86     removeDirectory(aNewTmpDir);
87     myTmpDirectory = "";
88   }
89
90   SessionPtr aMgr = ModelAPI_Session::get();
91   aMgr->load(qPrintable(aNewTmpDir));
92   myModule->setIsOpened(true);
93   return true;
94 }
95
96 bool SHAPERGUI_DataModel::save(QStringList& theFiles)
97 {
98   LightApp_DataModel::save( theFiles );
99   XGUI_Workshop* aWorkShop = myModule->workshop();
100   std::list<std::string> aFileNames;
101
102   CAM_Application* anApp = myModule->application();
103   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
104   SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
105
106   // it is important to check whether the file is saved in the multi-files mode in order to save
107   // files in temporary directories, which are removed in the full SALOME mode after copiying
108   // the files content in a result file.
109   bool isMultiFile = aResMgr ? aResMgr->booleanValue("Study", "multi_file", false) : false;
110
111   std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
112   //std::string aTmpDir = aStudy->GetTmpDir("", false);//true );
113   theFiles.append(QString(aTmpDir.c_str()));
114
115   aWorkShop->saveDocument(QString(aTmpDir.c_str()), aFileNames);
116   std::list<std::string>::iterator aIt;
117   for (aIt = aFileNames.begin(); aIt != aFileNames.end(); ++aIt) {
118     QString aName((*aIt).c_str());
119     aName.replace(QChar('\\'), QChar('/'));
120     int aN = aName.lastIndexOf('/');
121     theFiles.append(aName.right(aName.length() - aN - 1));
122   }
123   return true;
124 }
125
126 bool SHAPERGUI_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
127 {
128   myStudyPath = thePath;
129   return save(theFiles);
130 }
131
132 bool SHAPERGUI_DataModel::close()
133 {
134   myModule->workshop()->closeDocument();
135   removeDirectory(myTmpDirectory);
136   myTmpDirectory = "";
137   return LightApp_DataModel::close();
138 }
139
140 bool SHAPERGUI_DataModel::create(CAM_Study* theStudy)
141 {
142   return true;
143 }
144
145 bool SHAPERGUI_DataModel::isModified() const
146 {
147   SessionPtr aMgr = ModelAPI_Session::get();
148   return aMgr->isModified();
149 }
150
151 bool SHAPERGUI_DataModel::isSaved() const
152 {
153   return !isModified();
154 }
155
156 void SHAPERGUI_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
157 {
158   // Nothing to do here: we always keep the data tree in the up-to-date state
159   // The only goal of this method is to hide default behavior from LightApp_DataModel
160   return;
161 }
162
163 void SHAPERGUI_DataModel::initRootObject()
164 {
165   LightApp_Study* study = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
166   CAM_ModuleObject *aModelRoot = dynamic_cast<CAM_ModuleObject*>(root());
167   if(study && aModelRoot == NULL) {
168     aModelRoot = createModuleObject( study->root() );
169     aModelRoot->setDataModel( this );
170     setRoot(aModelRoot);
171   }
172 }
173
174 void SHAPERGUI_DataModel::removeDirectory(const QString& theDirectoryName)
175 {
176   Qtx::rmDir(theDirectoryName);
177 }
178
179 bool SHAPERGUI_DataModel::dumpPython(const QString& thePath, CAM_Study* theStudy,
180                                      bool isMultiFile,  QStringList& theListOfFiles)
181 {
182   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(theStudy);
183   if (!aStudy)
184     return false;
185
186   std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
187   ModelAPI_Session::get()->startOperation(ExchangePlugin_Dump::ID());
188   FeaturePtr aFeature = aDoc->addFeature(ExchangePlugin_Dump::ID());
189   if (aFeature.get()) {
190     std::string aTmpDir = aStudy->GetTmpDir(thePath.toStdString().c_str(), isMultiFile);
191     std::string aFileName = aTmpDir + DUMP_NAME;
192
193     if (QFile::exists(aFileName.c_str())) {
194       QFile::remove(aFileName.c_str());
195     }
196
197     AttributeStringPtr aAttr = aFeature->string(ExchangePlugin_Dump::FILE_PATH_ID());
198     if (aAttr.get())
199       aAttr->setValue(aFileName);
200
201     aAttr = aFeature->string(ExchangePlugin_Dump::FILE_FORMAT_ID());
202     if (aAttr.get())
203       aAttr->setValue(".py");
204     ModelAPI_Session::get()->finishOperation();
205
206     if (QFile::exists(aFileName.c_str())) {
207       if (isMultiFile) {
208         QFile aInFile(aFileName.c_str());
209         if (!aInFile.open(QIODevice::ReadOnly | QIODevice::Text))
210           return false;
211         QTextStream aText(&aInFile);
212         QString aTrace(aText.readAll());
213         aInFile.close();
214
215         QStringList aBuffer;
216         aBuffer.push_back(QString("def RebuildData( theStudy ):"));
217         QStringList aList(aTrace.split("\n"));
218         foreach(QString aStr, aList) {
219           QString s = "  " + aStr;
220           aBuffer.push_back(s);
221         }
222         aTrace = aBuffer.join("\n");
223
224         QFile aOutFile(aFileName.c_str());
225         if (!aOutFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
226           return false;
227
228         QTextStream aOut(&aOutFile);
229         aOut << aTrace.toStdString().c_str() << "\n";
230         aOut.flush();
231         aOutFile.close();
232       }
233       theListOfFiles.append(aTmpDir.c_str());
234       theListOfFiles.append(DUMP_NAME);
235       return true;
236     }
237   }
238   return false;
239 }
240