1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "NewGeom_DataModel.h"
4 #include "NewGeom_Module.h"
6 #include <XGUI_Workshop.h>
8 #include <ModelAPI_Session.h>
10 #include <LightApp_Study.h>
11 #include <CAM_Application.h>
12 #include <SUIT_Tools.h>
13 #include <SUIT_ResourceMgr.h>
18 NewGeom_DataModel::NewGeom_DataModel(NewGeom_Module* theModule)
19 : LightApp_DataModel(theModule), myStudyPath(""), myModule(theModule)
23 NewGeom_DataModel::~NewGeom_DataModel()
27 bool NewGeom_DataModel::open(const QString& thePath, CAM_Study* theStudy, QStringList theFiles)
29 LightApp_DataModel::open( thePath, theStudy, theFiles );
30 if (theFiles.size() == 0)
33 myStudyPath = thePath;
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();
43 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( myModule->application()->activeStudy() );
44 QString aNewTmpDir = aStudy->GetTmpDir("", false).c_str();
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;
53 QString aCurrentFile = SUIT_Tools::addSlash(aTmpDir) + aFileName;
54 QString aNewFile = SUIT_Tools::addSlash(aNewTmpDir) + aFileName;
55 if (!QFile::copy(aCurrentFile, aNewFile))
59 myTmpDirectory = aNewTmpDir;
62 removeDirectory(aNewTmpDir);
66 SessionPtr aMgr = ModelAPI_Session::get();
67 aMgr->load(qPrintable(aNewTmpDir));
68 myModule->setIsOpened(true);
72 bool NewGeom_DataModel::save(QStringList& theFiles)
74 LightApp_DataModel::save( theFiles );
75 XGUI_Workshop* aWorkShop = myModule->workshop();
76 std::list<std::string> aFileNames;
78 CAM_Application* anApp = myModule->application();
79 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
80 SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
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;
87 std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
88 //std::string aTmpDir = aStudy->GetTmpDir("", false);//true );
89 theFiles.append(QString(aTmpDir.c_str()));
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));
102 bool NewGeom_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
104 myStudyPath = thePath;
105 return save(theFiles);
108 bool NewGeom_DataModel::close()
110 myModule->workshop()->closeDocument();
111 removeDirectory(myTmpDirectory);
113 return LightApp_DataModel::close();
116 bool NewGeom_DataModel::create(CAM_Study* theStudy)
121 bool NewGeom_DataModel::isModified() const
123 SessionPtr aMgr = ModelAPI_Session::get();
124 return aMgr->isModified();
127 bool NewGeom_DataModel::isSaved() const
129 return !isModified();
132 void NewGeom_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
136 void NewGeom_DataModel::removeDirectory(const QString& theDirectoryName)
138 Qtx::rmDir(theDirectoryName);