1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_DataModel.h"
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
29 #include <HYDROData_Bathymetry.h>
30 #include <HYDROData_Calculation.h>
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Image.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Polyline.h>
35 #include <HYDROData_VisualState.h>
36 #include <HYDROData_Region.h>
37 #include <HYDROData_Zone.h>
39 #include <CAM_Application.h>
40 #include <CAM_DataObject.h>
41 #include <CAM_Module.h>
42 #include <CAM_Study.h>
44 #include <LightApp_Application.h>
45 #include <LightApp_DataObject.h>
46 #include <LightApp_Study.h>
48 #include <SUIT_DataObject.h>
49 #include <SUIT_DataBrowser.h>
50 #include <SUIT_ResourceMgr.h>
51 #include <SUIT_Study.h>
52 #include <SUIT_Tools.h>
54 #include <HYDROData_Document.h>
56 #include <TDF_Delta.hxx>
57 #include <TDF_ListIteratorOfDeltaList.hxx>
59 #include <QApplication>
62 static HYDROData_SequenceOfObjects myCopyingObjects;
64 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
65 : LightApp_DataModel( theModule )
67 update( module()->application()->activeStudy()->id() );
70 HYDROGUI_DataModel::~HYDROGUI_DataModel()
74 bool HYDROGUI_DataModel::open( const QString& theURL,
76 QStringList theFileList )
78 LightApp_DataModel::open( theURL, theStudy, theFileList );
79 const int aStudyId = theStudy->id();
81 Data_DocError res = DocError_UnknownProblem;
82 if( theFileList.count() == 2 )
84 QString aTmpDir = theFileList[0];
85 QString aFileName = theFileList[1];
88 QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
92 res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
96 res = DocError_UnknownProblem;
98 if( res != DocError_OK )
100 module()->application()->putInfo( tr( "LOAD_ERROR" ) );
105 // if the document open was successful, the data model update happens
106 // in the set mode of the module
107 if( res == DocError_OK )
113 bool HYDROGUI_DataModel::save( QStringList& theFileList )
115 if( !module()->application()->activeStudy() )
118 LightApp_DataModel::save( theFileList );
122 SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
123 bool isMultiFile = false;
125 isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
127 // save data to temporary files
128 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
129 aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
130 aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
132 QString aFullPath = aTmpDir + aFileName;
133 Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
134 if( res != DocError_OK )
136 module()->application()->putInfo( tr( "SAVE_ERROR" ) );
140 theFileList.append( aTmpDir );
141 theFileList.append( aFileName );
146 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
148 QStringList& theFileList )
151 return save( theFileList );
154 bool HYDROGUI_DataModel::close()
159 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
162 QStringList& theListOfFiles )
164 LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
166 int aStudyId = theStudy->id();
168 LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
169 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
170 if ( aDocument.IsNull() || !aStudy )
173 QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
174 aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
176 bool aRes = aDocument->DumpToPython( aFileToExport );
180 theListOfFiles.append( aFileToExport );
186 bool HYDROGUI_DataModel::isModified() const
188 return getDocument()->IsModified();
191 bool HYDROGUI_DataModel::isSaved() const
196 void HYDROGUI_DataModel::update( const int theStudyId )
198 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
202 SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
206 // create root object if not exist
207 CAM_DataObject* aRootObj = root();
209 aRootObj = createRootModuleObject( aStudyRoot );
214 DataObjectList aList;
215 aRootObj->children( aList );
216 QListIterator<SUIT_DataObject*> anIter( aList );
217 while( anIter.hasNext() )
218 removeChild( aRootObj, anIter.next() );
220 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
221 if( aDocument.IsNull() )
224 LightApp_DataObject* anImageRootObj = createObject( aRootObj, partitionName( KIND_IMAGE ) );
226 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
227 for( ; anIterator.More(); anIterator.Next() )
229 Handle(HYDROData_Image) anImageObj =
230 Handle(HYDROData_Image)::DownCast( anIterator.Current() );
231 if( !anImageObj.IsNull() )
232 createObject( anImageRootObj, anImageObj );
235 LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, partitionName( KIND_BATHYMETRY ) );
237 anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
238 for( ; anIterator.More(); anIterator.Next() )
240 Handle(HYDROData_Bathymetry) aBathymetryObj =
241 Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
242 if( !aBathymetryObj.IsNull() )
243 createObject( aBathymetryRootObj, aBathymetryObj );
246 LightApp_DataObject* aCalculRootObj = createObject( aRootObj, partitionName( KIND_CALCULATION ) );
248 anIterator = HYDROData_Iterator( aDocument, KIND_CALCULATION );
249 for( ; anIterator.More(); anIterator.Next() )
251 Handle(HYDROData_Calculation) aCalculObj =
252 Handle(HYDROData_Calculation)::DownCast( anIterator.Current() );
253 if( !aCalculObj.IsNull() )
254 createObject( aCalculRootObj, aCalculObj );
257 LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, partitionName( KIND_POLYLINE ) );
259 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
260 for( ; anIterator.More(); anIterator.Next() )
262 Handle(HYDROData_Polyline) aPolylineObj =
263 Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
264 if( !aPolylineObj.IsNull() )
265 createObject( aPolylineRootObj, aPolylineObj );
268 LightApp_DataObject* aZonesRootObj = createObject( aRootObj, partitionName( KIND_ZONE ) );
270 anIterator = HYDROData_Iterator( aDocument, KIND_ZONE );
271 for( ; anIterator.More(); anIterator.Next() )
273 Handle(HYDROData_Zone) aZoneObj =
274 Handle(HYDROData_Zone)::DownCast( anIterator.Current() );
275 if( !aZoneObj.IsNull() )
276 createObject( aZonesRootObj, aZoneObj );
279 LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, partitionName( KIND_VISUAL_STATE ) );
281 anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
282 for( ; anIterator.More(); anIterator.Next() )
284 Handle(HYDROData_VisualState) aVisualStateObj =
285 Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
286 if( !aVisualStateObj.IsNull() )
287 createObject( aVisualStateRootObj, aVisualStateObj );
290 if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
292 anObjectBrowser->setAutoOpenLevel( 3 );
293 anObjectBrowser->openLevels();
297 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
299 return NULL; // to do if necessary
302 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
304 return NULL; // to do if necessary
307 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
309 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
310 return anApp ? anApp->findObject( theEntry ) : 0;
313 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
314 LightApp_Study* theStudy )
317 theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
319 update( theStudy->id() );
322 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
324 CAM_DataObject* aRootObj = createModuleObject( theParent );
329 void HYDROGUI_DataModel::updateModel()
331 HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
333 update( aModule->getStudyId() );
336 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
337 const ObjectKind theObjectKind )
339 QString anEntry = theEntry;
340 if( anEntry.indexOf( "_" ) != -1 ) // reference object
341 anEntry = anEntry.section( "_", -1 );
343 Handle(HYDROData_Document) aDocument = getDocument();
344 if( !aDocument.IsNull() )
346 HYDROData_Iterator anIterator( aDocument, theObjectKind );
347 for( ; anIterator.More(); anIterator.Next() )
349 Handle(HYDROData_Object) anObject = anIterator.Current();
350 if( !anObject.IsNull() )
352 QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
353 if( anEntryRef == anEntry )
361 bool HYDROGUI_DataModel::canUndo() const
363 return getDocument()->CanUndo();
366 bool HYDROGUI_DataModel::canRedo() const
368 return getDocument()->CanRedo();
371 QStringList HYDROGUI_DataModel::undoNames() const
374 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
375 aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
379 QStringList HYDROGUI_DataModel::redoNames() const
382 for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
383 aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
387 void HYDROGUI_DataModel::clearUndos()
389 getDocument()->ClearUndos();
392 void HYDROGUI_DataModel::clearRedos()
394 getDocument()->ClearRedos();
397 bool HYDROGUI_DataModel::undo()
401 getDocument()->Undo();
403 catch ( Standard_Failure )
410 bool HYDROGUI_DataModel::redo()
414 getDocument()->Redo();
416 catch ( Standard_Failure )
423 bool HYDROGUI_DataModel::canCopy()
425 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
426 if( aSeq.Length() != 1 )
429 Handle(HYDROData_Object) anObject = aSeq.First();
430 if( anObject.IsNull() )
433 ObjectKind aKind = anObject->GetKind();
434 if( aKind == KIND_IMAGE ||
435 aKind == KIND_POLYLINE ||
436 aKind == KIND_CALCULATION )
442 bool HYDROGUI_DataModel::canPaste()
444 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
446 Handle(HYDROData_Object) anObject = myCopyingObjects.Value( anIndex );
447 if( !anObject.IsNull() && !anObject->IsRemoved() )
453 bool HYDROGUI_DataModel::copy()
455 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
456 changeCopyingObjects( aSeq );
460 bool HYDROGUI_DataModel::paste()
462 bool anIsChanged = false;
463 for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
465 Handle(HYDROData_Object) anObject = myCopyingObjects.Value( anIndex );
466 if( !anObject.IsNull() && !anObject->IsRemoved() )
468 ObjectKind aKind = anObject->GetKind();
469 Handle(HYDROData_Object) aClone = getDocument()->CreateObject( aKind );
470 if( !aClone.IsNull() )
472 anObject->CopyTo( aClone );
475 // generate a new unique name for the clone object:
476 // case 1: Image_1 -> Image_2
477 // case 2: ImageObj -> ImageObj_1
478 QString aName = aClone->GetName();
479 QString aPrefix = aName;
480 if( aName.contains( '_' ) ) // case 1
482 QString aSuffix = aName.section( '_', -1 );
483 bool anIsInteger = false;
484 aSuffix.toInt( &anIsInteger );
486 aPrefix = aName.section( '_', 0, -2 );
490 aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix );
491 aClone->SetName( aName );
498 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
500 myCopyingObjects.Assign( theSeq );
503 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
505 switch( theObjectKind )
507 case KIND_IMAGE: return "IMAGES";
508 case KIND_POLYLINE: return "POLYLINES";
509 case KIND_VISUAL_STATE: return "VISUAL_STATES";
510 case KIND_BATHYMETRY: return "BATHYMETRIES";
511 case KIND_CALCULATION: return "CALCULATION_CASES";
512 case KIND_ZONE: return "ZONES";
518 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
520 int aStudyId = module()->application()->activeStudy()->id();
521 return HYDROData_Document::Document( aStudyId );
524 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
525 Handle(HYDROData_Object) theModelObject,
526 const QString& theParentEntry,
527 const bool theIsBuildTree )
529 HYDROGUI_DataObject* aResObj =
530 new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
532 if ( theIsBuildTree )
534 buildObjectTree( theParent, aResObj, theParentEntry );
540 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
541 const QString& theName,
542 const QString& theParentEntry )
544 return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
547 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
548 SUIT_DataObject* theObject,
549 const QString& theParentEntry )
551 HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
555 Handle(HYDROData_Object) aDataObj = aGuiObj->modelObject();
556 if ( aDataObj.IsNull() )
559 ObjectKind anObjectKind = aDataObj->GetKind();
561 if ( anObjectKind == KIND_IMAGE )
563 Handle(HYDROData_Image) anImageObj =
564 Handle(HYDROData_Image)::DownCast( aDataObj );
565 for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
567 Handle(HYDROData_Object) aRefObj = anImageObj->Reference( anIndex );
568 if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
569 createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
572 else if ( anObjectKind == KIND_ZONE )
574 Handle(HYDROData_Zone) aZoneObj =
575 Handle(HYDROData_Zone)::DownCast( aDataObj );
577 LightApp_DataObject* aPolylineSect =
578 createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
580 Handle(HYDROData_Polyline) aPolyline = aZoneObj->GetPolyline();
581 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
582 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
584 LightApp_DataObject* aBathsSect =
585 createObject( aGuiObj, tr( "ZONE_BATHYMETRIES" ), aGuiObj->entry() );
587 HYDROData_SequenceOfObjects aZoneBaths = aZoneObj->GetBathymetries();
588 HYDROData_SequenceOfObjects::Iterator aBathsIter( aZoneBaths );
589 for ( ; aBathsIter.More(); aBathsIter.Next() )
591 Handle(HYDROData_Bathymetry) aRefBath =
592 Handle(HYDROData_Bathymetry)::DownCast( aBathsIter.Value() );
593 if( !aRefBath.IsNull() && !aRefBath->IsRemoved() )
594 createObject( aBathsSect, aRefBath, aGuiObj->entry(), false );
597 else if ( anObjectKind == KIND_CALCULATION )
599 Handle(HYDROData_Calculation) aCaseObj =
600 Handle(HYDROData_Calculation)::DownCast( aDataObj );
602 LightApp_DataObject* aPolylineSect =
603 createObject( aGuiObj, tr( "CASE_BND_POLYLINE" ), aGuiObj->entry() );
605 Handle(HYDROData_Polyline) aPolyline = aCaseObj->GetBoundaryPolyline();
606 if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
607 createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
609 LightApp_DataObject* aRefZonesSect =
610 createObject( aGuiObj, tr( "CASE_REFERENCE_ZONES" ), aGuiObj->entry() );
612 HYDROData_SequenceOfObjects aRefZones = aCaseObj->GetZones();
613 HYDROData_SequenceOfObjects::Iterator anIter( aRefZones );
614 for ( ; anIter.More(); anIter.Next() )
616 Handle(HYDROData_Zone) aRefZone =
617 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
618 if( !aRefZone.IsNull() && !aRefZone->IsRemoved() )
619 createObject( aRefZonesSect, aRefZone, aGuiObj->entry(), false );
622 LightApp_DataObject* aCaseRegionsSect =
623 createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
625 HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
626 anIter.Init( aCaseRegions );
627 for ( ; anIter.More(); anIter.Next() )
629 Handle(HYDROData_Region) aCaseRegion =
630 Handle(HYDROData_Region)::DownCast( anIter.Value() );
631 if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
632 createObject( aCaseRegionsSect, aCaseRegion, aGuiObj->entry(), false );
637 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
638 SUIT_DataObject* theChild )
640 SUIT_DataObject* aSubChild = theChild->firstChild();
641 for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
642 removeChild( theChild, aSubChild );
643 theParent->removeChild( theChild );
646 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
647 const QString& theName )
649 SUIT_DataObject* aChild = theFather->firstChild();
652 if( aChild->name() == theName )
653 return aChild; // found
654 aChild = aChild->nextBrother();
656 return NULL; // not found