Salome HOME
Merging with V7_main branch.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportGeomObjectOp.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_ImportGeomObjectOp.h"
24
25 #include "HYDROGUI_ObstacleDlg.h"
26
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30
31 #include <HYDROData_Obstacle.h>
32 #include <HYDROData_Iterator.h>
33
34 #include <GEOMBase.h>
35
36 #include <SalomeApp_Study.h>
37
38 #include <LightApp_Application.h>
39 #include <LightApp_UpdateFlags.h>
40
41 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,  
42                                                           const int theOpType, 
43                                                           const bool theIsToShowPanel )
44 : HYDROGUI_Operation( theModule ),
45   myOpType ( theOpType ),
46   myIsToShowPanel ( theIsToShowPanel )
47 {
48   setName( tr( "IMPORT_GEOM_OBJECT" ) );
49 }
50
51 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
52 {
53 }
54
55 void HYDROGUI_ImportGeomObjectOp::startOperation()
56 {
57   HYDROGUI_Operation::startOperation();
58
59   // Get GEOM objects to import
60   myGeomObjects.clear();
61   if ( myOpType == ImportCreated ) {
62     myGeomObjects = module()->GetGeomObjectsToImport();
63   } else if ( myOpType == ImportSelected ) {
64     myGeomObjects = HYDROGUI_Tool::GetSelectedGeomObjects( module() );
65   }
66
67   HYDROGUI_ObstacleDlg* aPanel = 0;
68
69   if ( myGeomObjects.count() == 1 ) {
70     // Get panel
71     aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
72
73     if ( aPanel ) {
74       // Reset the panel state
75       aPanel->reset();
76
77       // Pass the existing obstacle names to the panel
78       QStringList anObstacles = 
79         HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
80
81       aPanel->setObstacleNames( anObstacles );
82     }
83   }
84
85   if ( !aPanel ) {
86     onApply();
87   }
88 }
89
90 void HYDROGUI_ImportGeomObjectOp::abortOperation()
91 {
92   HYDROGUI_Operation::abortOperation();
93 }
94
95 void HYDROGUI_ImportGeomObjectOp::commitOperation()
96 {
97   HYDROGUI_Operation::commitOperation();
98 }
99
100 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
101                                                 QString& theErrorMsg )
102 {
103   // Get active SalomeApp_Study
104   SalomeApp_Study* aStudy = 
105     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
106   if ( !aStudy ) {
107     return false;
108   }
109
110   QString anObstacleName;
111   Handle(HYDROData_Obstacle) anObstacleToEdit;  
112
113   // Get panel
114   HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
115   if ( aPanel ) {
116     QString anEditedName = aPanel->getEditedObstacleName().simplified();
117
118     if ( !anEditedName.isEmpty() ) {
119       anObstacleToEdit = Handle(HYDROData_Obstacle)::DownCast(
120         HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
121     }
122
123     anObstacleName = anEditedName; //TODO: aPanel->getObstacleName();
124   }
125
126   bool anIsOk = false;
127
128   // Get the created object as SObject
129   foreach ( const QString& anEntry, myGeomObjects ) {
130     _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
131     if (aSObject) {
132       // Get the corresponding TopoDS_Shape
133       TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
134       if ( !aShape.IsNull() ) {
135         // Create/edit an obstacle object
136         // TODO refactoring: get rid of obstacle from TopoDS_Shape creation code copy/paste
137         Handle(HYDROData_Obstacle) anObstacle; 
138
139         if ( anObstacleToEdit.IsNull() ) {
140           anObstacle = 
141             Handle(HYDROData_Obstacle)::DownCast( doc()->CreateObject(KIND_OBSTACLE) );
142         } else {
143           anObstacle = anObstacleToEdit;
144         }
145
146         // Set name
147         if ( anObstacleName.isEmpty() ) {
148           QString aName = QString::fromStdString( aSObject->GetName() );
149           anObstacleName = HYDROGUI_Tool::GenerateObjectName( module(), aName );
150         }
151         if ( anObstacle->GetName() != anObstacleName ) {
152           anObstacle->SetName( anObstacleName );
153         }
154
155         // Set shape
156         anObstacle->SetShape3D( aShape );
157
158         // Set operation status
159         anIsOk = true;
160         theUpdateFlags = UF_Model;
161       }
162     }
163   }
164
165   return anIsOk;
166 }
167
168 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
169 {
170   HYDROGUI_InputPanel* aPanel = 0;
171   if ( myIsToShowPanel ) {
172     aPanel = new HYDROGUI_ObstacleDlg( module(), getName() );
173   }
174
175   return aPanel;
176 }