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