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