Salome HOME
30e21ed848b5d6d7935a736cf69ca189fe9da757
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataModel.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_DataModel.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_DataObject.h"
27
28 #include <HYDROData_Document.h>
29 #include <HYDROData_Image.h>
30 #include <HYDROData_Iterator.h>
31
32 #include <CAM_Application.h>
33 #include <CAM_DataObject.h>
34 #include <CAM_Module.h>
35 #include <CAM_Study.h>
36
37 #include <LightApp_Application.h>
38 #include <LightApp_DataObject.h>
39 #include <LightApp_Study.h>
40
41 #include <SUIT_DataObject.h>
42 #include <SUIT_DataBrowser.h>
43 #include <SUIT_ResourceMgr.h>
44 #include <SUIT_Study.h>
45 #include <SUIT_Tools.h>
46
47 #include <HYDROData_Document.h>
48
49 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
50 : LightApp_DataModel( theModule )
51 {
52   update( module()->application()->activeStudy()->id() );
53 }
54
55 HYDROGUI_DataModel::~HYDROGUI_DataModel()
56 {
57 }
58
59 bool HYDROGUI_DataModel::open( const QString& theURL,
60                                CAM_Study* theStudy,
61                                QStringList theFileList )
62 {
63   LightApp_DataModel::open( theURL, theStudy, theFileList );
64   const int aStudyId = theStudy->id();
65
66   Data_DocError res = DocError_UnknownProblem;
67   if( theFileList.count() == 2 )
68   {
69     QString aTmpDir = theFileList[0];
70     QString aFileName = theFileList[1];
71
72     myStudyURL = theURL;
73     QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
74
75     try
76     {
77       res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
78     }
79     catch(...)
80     {
81       res = DocError_UnknownProblem;
82     }
83     if( res != DocError_OK )
84     {
85       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
86       return false;
87     }
88   }
89
90   // if the document open was successful, the data model update happens
91   // in the set mode of the module
92   if( res == DocError_OK )
93     update( aStudyId );
94
95   return true;
96 }
97
98 bool HYDROGUI_DataModel::save( QStringList& theFileList )
99 {
100   if( !module()->application()->activeStudy() )
101     return false;
102   
103   const int aStudyId = module()->application()->activeStudy()->id();
104
105   LightApp_DataModel::save( theFileList );
106
107   QString aTmpDir;
108   QString aFileName;
109   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
110   bool isMultiFile = false;
111   if( resMgr )
112     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
113
114   // save data to temporary files
115   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
116   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
117   aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
118
119   QString aFullPath = aTmpDir + aFileName;
120   Data_DocError res = HYDROData_Document::Document( aStudyId )->Save( (char*)aFullPath.toLatin1().constData() );
121   if( res != DocError_OK )
122   {
123     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
124     return false;
125   }
126
127   theFileList.append( aTmpDir );
128   theFileList.append( aFileName );
129
130   return true;
131 }
132
133 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
134                                  CAM_Study*,
135                                  QStringList& theFileList )
136 {
137   myStudyURL = theURL;
138   return save( theFileList );
139 }
140
141 bool HYDROGUI_DataModel::close()
142 {
143   return true;
144 }
145
146 bool HYDROGUI_DataModel::isModified() const
147 {
148   int aStudyId = module()->application()->activeStudy()->id();
149   return HYDROData_Document::Document( aStudyId )->IsModified();
150 }
151
152 bool HYDROGUI_DataModel::isSaved() const
153 {
154   return true;
155 }
156
157 void HYDROGUI_DataModel::update( const int theStudyId )
158 {
159   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
160   if( !anApp )
161     return;
162
163   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
164   if( !aStudyRoot )
165     return;
166
167   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
168     anObjectBrowser->setAutoOpenLevel( 3 );
169
170   // create root object if not exist
171   CAM_DataObject* aRootObj = root();
172   if( !aRootObj )
173     aRootObj = createRootModuleObject( aStudyRoot );
174
175   if( !aRootObj )
176     return;
177
178   DataObjectList aList;
179   aRootObj->children( aList );
180   QListIterator<SUIT_DataObject*> anIter( aList );
181   while( anIter.hasNext() )
182     removeChild( aRootObj, anIter.next() );
183
184   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
185   if( aDocument.IsNull() )
186     return;
187
188   LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
189
190   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
191   for( ; anIterator.More(); anIterator.Next() )
192   {
193     Handle(HYDROData_Image) anImageObj =
194       Handle(HYDROData_Image)::DownCast( anIterator.Current() );
195     if( !anImageObj.IsNull() )
196       createObject( anImageRootObj, anImageObj );
197   }
198 }
199
200 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
201 {
202   return NULL; // to do if necessary
203 }
204
205 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
206 {
207   return NULL; // to do if necessary
208 }
209
210 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
211 {
212   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
213   return anApp ? anApp->findObject( theEntry ) : 0;
214 }
215
216 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
217                                  LightApp_Study* theStudy )
218 {
219   if( !theStudy )
220     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
221   if( theStudy )
222     update( theStudy->id() );
223 }
224
225 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
226 {
227   CAM_DataObject* aRootObj = createModuleObject( theParent );
228   setRoot( aRootObj );
229   return aRootObj;
230 }
231
232 void HYDROGUI_DataModel::updateModel()
233 {
234   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
235   if( aModule )
236     update( aModule->getStudyId() );
237 }
238
239 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
240                                                             const ObjectKind theObjectKind )
241 {
242   const int aStudyId = module()->application()->activeStudy()->id();
243   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
244   if( !aDocument.IsNull() )
245   {
246     HYDROData_Iterator anIterator( aDocument, theObjectKind );
247     for( ; anIterator.More(); anIterator.Next() )
248     {
249       Handle(HYDROData_Object) anObject = anIterator.Current();
250       if( !anObject.IsNull() )
251       {
252         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
253         if( anEntry == theEntry )
254           return anObject;
255       }
256     }
257   }
258   return NULL;
259 }
260
261 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
262                                                        Handle(HYDROData_Object) theModelObject )
263 {
264   return new HYDROGUI_DataObject( theParent, theModelObject );
265 }
266
267 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
268                                                        const QString& theName )
269 {
270   return new HYDROGUI_NamedObject( theParent, theName );
271 }
272
273 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
274                                       SUIT_DataObject* theChild )
275 {
276   SUIT_DataObject* aSubChild = theChild->firstChild();
277   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
278     removeChild( theChild, aSubChild );
279   theParent->removeChild( theChild );
280 }
281
282 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
283                                                       const QString& theName )
284 {
285   SUIT_DataObject* aChild = theFather->firstChild();
286   while( aChild )
287   {
288     if( aChild->name() == theName )
289       return aChild; // found
290     aChild = aChild->nextBrother();
291   }
292   return NULL; // not found
293 }