Salome HOME
Rename classes NewGEOM_ to SHAPERGUI_
[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
10 #include <LightApp_Study.h>
11 #include <CAM_Application.h>
12 #include <CAM_DataObject.h>
13 #include <SUIT_Tools.h>
14 #include <SUIT_ResourceMgr.h>
15
16 #include <QFile>
17 #include <QDir>
18
19 SHAPERGUI_DataModel::SHAPERGUI_DataModel(SHAPERGUI* theModule)
20     : LightApp_DataModel(theModule), myStudyPath(""), myModule(theModule)
21 {
22 }
23
24 SHAPERGUI_DataModel::~SHAPERGUI_DataModel()
25 {
26 }
27
28 bool SHAPERGUI_DataModel::open(const QString& thePath, CAM_Study* theStudy, QStringList theFiles)
29 {
30   LightApp_DataModel::open( thePath, theStudy, theFiles );
31   if (theFiles.size() == 0)
32     return false;
33
34   myStudyPath = thePath;
35
36   // If the file is Multi(contain all module files inside), the open SALOME functionality creates
37   // these files in a temporary directory. After the open functionality is finished, it removes
38   // these files (in the full SALOME mode).
39   // The postponed loading of the files is realized in the NewGEOM module. So, it is important do
40   // not remove the opened files.
41   // The following code creates a new tmp directory with a copy of files.
42   QString aTmpDir = theFiles.first();
43
44   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( myModule->application()->activeStudy() );
45   QString aNewTmpDir = aStudy->GetTmpDir("", false).c_str();
46
47   bool isDone = true;
48   QDir aDir(aTmpDir);
49   QStringList aFiles = aDir.entryList(QDir::Files);
50   QStringList::const_iterator anIt = aFiles.begin(), aLast = aFiles.end();
51   for (; anIt != aLast; anIt++) {
52     QString aFileName = *anIt;
53
54     QString aCurrentFile = SUIT_Tools::addSlash(aTmpDir) + aFileName;
55     QString aNewFile = SUIT_Tools::addSlash(aNewTmpDir) + aFileName;
56     if (!QFile::copy(aCurrentFile, aNewFile))
57       isDone = false;
58   }
59   if (isDone) {
60     myTmpDirectory = aNewTmpDir;
61   }
62   else {
63     removeDirectory(aNewTmpDir);
64     myTmpDirectory = "";
65   }
66
67   SessionPtr aMgr = ModelAPI_Session::get();
68   aMgr->load(qPrintable(aNewTmpDir));
69   myModule->setIsOpened(true);
70   return true;
71 }
72
73 bool SHAPERGUI_DataModel::save(QStringList& theFiles)
74 {
75   LightApp_DataModel::save( theFiles );
76   XGUI_Workshop* aWorkShop = myModule->workshop();
77   std::list<std::string> aFileNames;
78
79   CAM_Application* anApp = myModule->application();
80   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
81   SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
82
83   // it is important to check whether the file is saved in the multi-files mode in order to save
84   // files in temporary directories, which are removed in the full SALOME mode after copiying
85   // the files content in a result file.
86   bool isMultiFile = aResMgr ? aResMgr->booleanValue("Study", "multi_file", false) : false;
87
88   std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
89   //std::string aTmpDir = aStudy->GetTmpDir("", false);//true );
90   theFiles.append(QString(aTmpDir.c_str()));
91
92   aWorkShop->saveDocument(QString(aTmpDir.c_str()), aFileNames);
93   std::list<std::string>::iterator aIt;
94   for (aIt = aFileNames.begin(); aIt != aFileNames.end(); ++aIt) {
95     QString aName((*aIt).c_str());
96     aName.replace(QChar('\\'), QChar('/'));
97     int aN = aName.lastIndexOf('/');
98     theFiles.append(aName.right(aName.length() - aN - 1));
99   }
100   return true;
101 }
102
103 bool SHAPERGUI_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
104 {
105   myStudyPath = thePath;
106   return save(theFiles);
107 }
108
109 bool SHAPERGUI_DataModel::close()
110 {
111   myModule->workshop()->closeDocument();
112   removeDirectory(myTmpDirectory);
113   myTmpDirectory = "";
114   return LightApp_DataModel::close();
115 }
116
117 bool SHAPERGUI_DataModel::create(CAM_Study* theStudy)
118 {
119   return true;
120 }
121
122 bool SHAPERGUI_DataModel::isModified() const
123 {
124   SessionPtr aMgr = ModelAPI_Session::get();
125   return aMgr->isModified();
126 }
127
128 bool SHAPERGUI_DataModel::isSaved() const
129 {
130   return !isModified();
131 }
132
133 void SHAPERGUI_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
134 {
135   // Nothing to do here: we always keep the data tree in the up-to-date state
136   // The only goal of this method is to hide default behavior from LightApp_DataModel
137   return;
138 }
139
140 void SHAPERGUI_DataModel::initRootObject()
141 {
142   LightApp_Study* study = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
143   CAM_ModuleObject *aModelRoot = dynamic_cast<CAM_ModuleObject*>(root());
144   if(study && aModelRoot == NULL) {
145     aModelRoot = createModuleObject( study->root() );
146     aModelRoot->setDataModel( this );
147     setRoot(aModelRoot);
148   }
149 }
150
151 void SHAPERGUI_DataModel::removeDirectory(const QString& theDirectoryName)
152 {
153   Qtx::rmDir(theDirectoryName);
154 }
155