Salome HOME
Calculation Case Creation dialog is refactored. It is now a two-page wizard.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.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_CalculationOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_CalculationDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_Polyline.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_ImmersibleZone.h>
34
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewWindow.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <QApplication>
43
44 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
45 : HYDROGUI_Operation( theModule ),
46   myIsEdit( theIsEdit ),
47   myActiveViewManager( NULL ),
48   myPreviewViewManager( NULL )
49 {
50   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
51 }
52
53 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
54 {
55   closePreview();
56 }
57
58 void HYDROGUI_CalculationOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   HYDROGUI_CalculationDlg* aPanel = 
63     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
64   if ( !aPanel )
65     return;
66
67   myRegionsList.clear();
68   aPanel->reset();
69
70   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
71
72   QStringList aSelectedObjects;
73
74   myEditedObject.Nullify();
75   if ( myIsEdit )
76   {
77     myEditedObject = Handle(HYDROData_Calculation)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
78     if ( !myEditedObject.IsNull() )
79     {
80       anObjectName = myEditedObject->GetName();
81
82       HYDROData_SequenceOfObjects aRefObjects = myEditedObject->GetGeometryObjects();
83       HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
84       for ( ; anIter.More(); anIter.Next() )
85       {
86         Handle(HYDROData_Object) aRefbject = 
87           Handle(HYDROData_Object)::DownCast( anIter.Value() );
88         if ( aRefbject.IsNull() )
89           continue;
90
91         QString aRefObjectName = aRefbject->GetName();
92         if ( aRefObjectName.isEmpty() )
93           continue;
94
95         aSelectedObjects.append( aRefObjectName );
96       }
97     }
98   }
99   else
100   {
101     myEditedObject =
102       Handle(HYDROData_Calculation)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
103   }
104
105   aPanel->setObjectName( anObjectName );
106   aPanel->setEditedObject( myEditedObject );
107   aPanel->setSelectedGeomObjects( aSelectedObjects );
108
109   createPreview();
110 }
111
112 void HYDROGUI_CalculationOp::abortOperation()
113 {
114   closePreview();
115
116   HYDROGUI_Operation::abortOperation();
117 }
118
119 void HYDROGUI_CalculationOp::commitOperation()
120 {
121   closePreview();
122
123   HYDROGUI_Operation::commitOperation();
124 }
125
126 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
127 {
128   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
129
130   // Connect signals and slots
131
132   return aPanel;
133 }
134
135 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
136                                            QString& theErrorMsg )
137 {
138   HYDROGUI_CalculationDlg* aPanel = 
139     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
140   if ( !aPanel )
141     return false;
142
143   QString anObjectName = aPanel->getObjectName().simplified();
144   if ( anObjectName.isEmpty() )
145   {
146     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
147     return false;
148   }
149
150   // check that there are no other objects with the same name in the document
151   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
152   {
153     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
154     if ( !anObject.IsNull() )
155     {
156       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
157       return false;
158     }
159   }
160
161   Handle(HYDROData_Document) aDocument = doc();
162
163   Handle(HYDROData_Calculation) aCalculObj = myIsEdit ? myEditedObject :
164     Handle(HYDROData_Calculation)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
165   if ( aCalculObj.IsNull() )
166     return false;
167
168   aCalculObj->SetName( anObjectName );
169
170   QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
171   HYDROData_SequenceOfObjects aGeomObjects = 
172     HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
173
174   theUpdateFlags = UF_Model;
175
176   return true;
177 }
178
179 void HYDROGUI_CalculationOp::onSplitZones()
180 {
181   myRegionsList.clear();
182
183   HYDROGUI_CalculationDlg* aPanel = 
184     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
185   if ( !aPanel )
186     return;
187
188   QApplication::setOverrideCursor( Qt::WaitCursor );
189
190   QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
191   HYDROData_SequenceOfObjects aGeomObjects = 
192     HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
193
194   QStringList aResSplittedZones;
195
196   HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
197     HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
198
199   QStringList aUsedNames;
200
201   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
202   while( anIter.hasNext() )
203   {
204     Region aRegion;
205     aRegion.SplitData = anIter.next();
206
207     aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
208     aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
209
210     aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
211
212     aUsedNames.append( aRegion.RegionName );
213
214     aResSplittedZones.append( aRegion.RegionName );
215
216     myRegionsList.append( aRegion );
217   }
218   
219   createPreview();
220
221   QApplication::restoreOverrideCursor();
222 }
223
224 void HYDROGUI_CalculationOp::createPreview()
225 {
226   LightApp_Application* anApp = module()->getApp();
227
228   if ( !myActiveViewManager )
229   {
230     if ( myRegionsList.isEmpty() )
231       return;
232
233     myActiveViewManager = anApp->activeViewManager();
234   }
235
236   if ( !myPreviewViewManager )
237   {
238     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
239       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
240     if ( myPreviewViewManager )
241     {
242       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
243                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
244
245       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
246       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
247     }
248   }
249
250   if ( !myPreviewViewManager )
251     return;
252
253   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
254   {
255     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
256     if ( !aCtx.IsNull() )
257     {
258       RegionsList::iterator anIter = myRegionsList.begin();
259       for ( ; anIter != myRegionsList.end(); ++anIter )
260       {
261         Region& aRegion = *anIter;
262         if ( aRegion.Shape )
263         {
264           aRegion.Shape->erase( false );
265           delete aRegion.Shape;
266         }
267
268         aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
269
270         aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
271         aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
272         aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
273       }
274
275       //Process the draw events for viewer
276       QApplication::processEvents();
277       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
278         vw->onTopView();
279     }
280   }
281 }
282
283 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
284 {
285   closePreview();
286 }
287
288 void HYDROGUI_CalculationOp::closePreview()
289 {
290   RegionsList::iterator anIter= myRegionsList.begin();
291   for ( ; anIter != myRegionsList.end(); ++anIter )
292   {
293     Region& aRegion = *anIter;
294     if ( aRegion.Shape )
295     {
296       aRegion.Shape->erase( false );
297       delete aRegion.Shape;
298       aRegion.Shape = NULL;
299     }
300   }
301
302   if( myPreviewViewManager )
303   {
304     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
305                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
306
307     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
308     myPreviewViewManager = NULL;
309   }
310
311   if( myActiveViewManager )
312   {
313     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
314     myActiveViewManager = NULL;
315   }
316 }
317
318