1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "SHAPERGUI_DataModel.h"
6 #include <XGUI_Workshop.h>
8 #include <ModelAPI_Session.h>
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>
19 SHAPERGUI_DataModel::SHAPERGUI_DataModel(SHAPERGUI* theModule)
20 : LightApp_DataModel(theModule), myStudyPath(""), myModule(theModule)
24 SHAPERGUI_DataModel::~SHAPERGUI_DataModel()
28 bool SHAPERGUI_DataModel::open(const QString& thePath, CAM_Study* theStudy, QStringList theFiles)
30 LightApp_DataModel::open( thePath, theStudy, theFiles );
31 if (theFiles.size() == 0)
34 myStudyPath = thePath;
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 SHAPER 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();
44 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( myModule->application()->activeStudy() );
45 QString aNewTmpDir = aStudy->GetTmpDir("", false).c_str();
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;
54 QString aCurrentFile = SUIT_Tools::addSlash(aTmpDir) + aFileName;
55 QString aNewFile = SUIT_Tools::addSlash(aNewTmpDir) + aFileName;
56 if (!QFile::copy(aCurrentFile, aNewFile))
60 myTmpDirectory = aNewTmpDir;
63 removeDirectory(aNewTmpDir);
67 SessionPtr aMgr = ModelAPI_Session::get();
68 aMgr->load(qPrintable(aNewTmpDir));
69 myModule->setIsOpened(true);
73 bool SHAPERGUI_DataModel::save(QStringList& theFiles)
75 LightApp_DataModel::save( theFiles );
76 XGUI_Workshop* aWorkShop = myModule->workshop();
77 std::list<std::string> aFileNames;
79 CAM_Application* anApp = myModule->application();
80 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(anApp->activeStudy());
81 SUIT_ResourceMgr* aResMgr = anApp->resourceMgr();
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;
88 std::string aTmpDir = aStudy->GetTmpDir(qPrintable(myStudyPath), isMultiFile);
89 //std::string aTmpDir = aStudy->GetTmpDir("", false);//true );
90 theFiles.append(QString(aTmpDir.c_str()));
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));
103 bool SHAPERGUI_DataModel::saveAs(const QString& thePath, CAM_Study* theStudy, QStringList& theFiles)
105 myStudyPath = thePath;
106 return save(theFiles);
109 bool SHAPERGUI_DataModel::close()
111 myModule->workshop()->closeDocument();
112 removeDirectory(myTmpDirectory);
114 return LightApp_DataModel::close();
117 bool SHAPERGUI_DataModel::create(CAM_Study* theStudy)
122 bool SHAPERGUI_DataModel::isModified() const
124 SessionPtr aMgr = ModelAPI_Session::get();
125 return aMgr->isModified();
128 bool SHAPERGUI_DataModel::isSaved() const
130 return !isModified();
133 void SHAPERGUI_DataModel::update(LightApp_DataObject* theObj, LightApp_Study* theStudy)
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
140 void SHAPERGUI_DataModel::initRootObject()
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 );
151 void SHAPERGUI_DataModel::removeDirectory(const QString& theDirectoryName)
153 Qtx::rmDir(theDirectoryName);