]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_DataModel.cxx
Salome HOME
Edit polyline operation was added
[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_DataObject.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Image.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_Polyline.h>
33
34 #include <CAM_Application.h>
35 #include <CAM_DataObject.h>
36 #include <CAM_Module.h>
37 #include <CAM_Study.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_DataObject.h>
41 #include <LightApp_Study.h>
42
43 #include <SUIT_DataObject.h>
44 #include <SUIT_DataBrowser.h>
45 #include <SUIT_ResourceMgr.h>
46 #include <SUIT_Study.h>
47 #include <SUIT_Tools.h>
48
49 #include <HYDROData_Document.h>
50
51 #include <TDF_Delta.hxx>
52 #include <TDF_ListIteratorOfDeltaList.hxx>
53
54 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
55 : LightApp_DataModel( theModule )
56 {
57   update( module()->application()->activeStudy()->id() );
58 }
59
60 HYDROGUI_DataModel::~HYDROGUI_DataModel()
61 {
62 }
63
64 bool HYDROGUI_DataModel::open( const QString& theURL,
65                                CAM_Study* theStudy,
66                                QStringList theFileList )
67 {
68   LightApp_DataModel::open( theURL, theStudy, theFileList );
69   const int aStudyId = theStudy->id();
70
71   Data_DocError res = DocError_UnknownProblem;
72   if( theFileList.count() == 2 )
73   {
74     QString aTmpDir = theFileList[0];
75     QString aFileName = theFileList[1];
76
77     myStudyURL = theURL;
78     QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
79
80     try
81     {
82       res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
83     }
84     catch(...)
85     {
86       res = DocError_UnknownProblem;
87     }
88     if( res != DocError_OK )
89     {
90       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
91       return false;
92     }
93   }
94
95   // if the document open was successful, the data model update happens
96   // in the set mode of the module
97   if( res == DocError_OK )
98     update( aStudyId );
99
100   return true;
101 }
102
103 bool HYDROGUI_DataModel::save( QStringList& theFileList )
104 {
105   if( !module()->application()->activeStudy() )
106     return false;
107   
108   LightApp_DataModel::save( theFileList );
109
110   QString aTmpDir;
111   QString aFileName;
112   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
113   bool isMultiFile = false;
114   if( resMgr )
115     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
116
117   // save data to temporary files
118   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
119   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
120   aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
121
122   QString aFullPath = aTmpDir + aFileName;
123   Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
124   if( res != DocError_OK )
125   {
126     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
127     return false;
128   }
129
130   theFileList.append( aTmpDir );
131   theFileList.append( aFileName );
132
133   return true;
134 }
135
136 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
137                                  CAM_Study*,
138                                  QStringList& theFileList )
139 {
140   myStudyURL = theURL;
141   return save( theFileList );
142 }
143
144 bool HYDROGUI_DataModel::close()
145 {
146   return true;
147 }
148
149 bool HYDROGUI_DataModel::isModified() const
150 {
151   int aStudyId = module()->application()->activeStudy()->id();
152   return HYDROData_Document::Document( aStudyId )->IsModified();
153 }
154
155 bool HYDROGUI_DataModel::isSaved() const
156 {
157   return true;
158 }
159
160 void HYDROGUI_DataModel::update( const int theStudyId )
161 {
162   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
163   if( !anApp )
164     return;
165
166   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
167   if( !aStudyRoot )
168     return;
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   LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, "POLYLINES" );
200
201   HYDROData_Iterator aPolyIterator( aDocument, KIND_POLYLINE );
202   for( ; aPolyIterator.More(); aPolyIterator.Next() )
203   {
204     Handle(HYDROData_Polyline) aPolylineObj =
205       Handle(HYDROData_Polyline)::DownCast( aPolyIterator.Current() );
206     if( !aPolylineObj.IsNull() )
207       createObject( aPolylineRootObj, aPolylineObj );
208   }
209
210   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
211   {
212     anObjectBrowser->setAutoOpenLevel( 3 );
213     anObjectBrowser->openLevels();
214   }
215 }
216
217 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
218 {
219   return NULL; // to do if necessary
220 }
221
222 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
223 {
224   return NULL; // to do if necessary
225 }
226
227 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
228 {
229   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
230   return anApp ? anApp->findObject( theEntry ) : 0;
231 }
232
233 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
234                                  LightApp_Study* theStudy )
235 {
236   if( !theStudy )
237     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
238   if( theStudy )
239     update( theStudy->id() );
240 }
241
242 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
243 {
244   CAM_DataObject* aRootObj = createModuleObject( theParent );
245   setRoot( aRootObj );
246   return aRootObj;
247 }
248
249 void HYDROGUI_DataModel::updateModel()
250 {
251   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
252   if( aModule )
253     update( aModule->getStudyId() );
254 }
255
256 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
257                                                             const ObjectKind theObjectKind )
258 {
259   const int aStudyId = module()->application()->activeStudy()->id();
260   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
261   if( !aDocument.IsNull() )
262   {
263     HYDROData_Iterator anIterator( aDocument, theObjectKind );
264     for( ; anIterator.More(); anIterator.Next() )
265     {
266       Handle(HYDROData_Object) anObject = anIterator.Current();
267       if( !anObject.IsNull() )
268       {
269         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObject );
270         if( anEntry == theEntry )
271           return anObject;
272       }
273     }
274   }
275   return NULL;
276 }
277
278 bool HYDROGUI_DataModel::canUndo() const
279 {
280   return getDocument()->CanUndo();
281 }
282
283 bool HYDROGUI_DataModel::canRedo() const
284 {
285   return getDocument()->CanRedo();
286 }
287
288 QStringList HYDROGUI_DataModel::undoNames() const
289 {
290   QStringList aNames;
291   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
292     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
293   return aNames;
294 }
295
296 QStringList HYDROGUI_DataModel::redoNames() const
297 {
298   QStringList aNames;
299   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
300     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
301   return aNames;
302 }
303
304 void HYDROGUI_DataModel::clearUndos()
305 {
306   getDocument()->ClearUndos();
307 }
308
309 void HYDROGUI_DataModel::clearRedos()
310 {
311   getDocument()->ClearRedos();
312 }
313
314 bool HYDROGUI_DataModel::undo()
315 {
316   try 
317   {
318     getDocument()->Undo();
319   }
320   catch ( Standard_Failure )
321   {
322     return false;
323   }
324   return true;
325 }
326
327 bool HYDROGUI_DataModel::redo()
328 {
329   try 
330   {
331     getDocument()->Redo();
332   }
333   catch ( Standard_Failure )
334   {
335     return false;
336   }
337   return true;
338 }
339
340 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
341 {
342   int aStudyId = module()->application()->activeStudy()->id();
343   return HYDROData_Document::Document( aStudyId );
344 }
345
346 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
347                                                        Handle(HYDROData_Object) theModelObject )
348 {
349   return new HYDROGUI_DataObject( theParent, theModelObject );
350 }
351
352 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
353                                                        const QString& theName )
354 {
355   return new HYDROGUI_NamedObject( theParent, theName );
356 }
357
358 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
359                                       SUIT_DataObject* theChild )
360 {
361   SUIT_DataObject* aSubChild = theChild->firstChild();
362   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
363     removeChild( theChild, aSubChild );
364   theParent->removeChild( theChild );
365 }
366
367 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
368                                                       const QString& theName )
369 {
370   SUIT_DataObject* aChild = theFather->firstChild();
371   while( aChild )
372   {
373     if( aChild->name() == theName )
374       return aChild; // found
375     aChild = aChild->nextBrother();
376   }
377   return NULL; // not found
378 }