Salome HOME
145701827eb6f16b886db05a37c08c044c0d6b6c
[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 <SUIT_MessageBox.h>
43 #include <SUIT_Desktop.h>
44
45 #include <QApplication>
46
47 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ),
49   myIsEdit( theIsEdit ),
50   myActiveViewManager( NULL ),
51   myPreviewViewManager( NULL )
52 {
53   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
54 }
55
56 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
57 {
58   closePreview();
59 }
60
61 void HYDROGUI_CalculationOp::startOperation()
62 {
63   HYDROGUI_Operation::startOperation();
64   
65   // Begin transaction
66   startDocOperation();
67
68   HYDROGUI_CalculationDlg* aPanel = 
69     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
70   if ( !aPanel )
71     return;
72
73   myRegionsList.clear();
74   aPanel->reset();
75
76   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
77
78   QStringList aSelectedObjects;
79
80   myEditedObject.Nullify();
81   if ( myIsEdit )
82   {
83     myEditedObject = Handle(HYDROData_Calculation)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
84     if ( !myEditedObject.IsNull() )
85     {
86       anObjectName = myEditedObject->GetName();
87
88       HYDROData_SequenceOfObjects aRefObjects = myEditedObject->GetGeometryObjects();
89       HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
90       for ( ; anIter.More(); anIter.Next() )
91       {
92         Handle(HYDROData_Object) aRefbject = 
93           Handle(HYDROData_Object)::DownCast( anIter.Value() );
94         if ( aRefbject.IsNull() )
95           continue;
96
97         QString aRefObjectName = aRefbject->GetName();
98         if ( aRefObjectName.isEmpty() )
99           continue;
100
101         aSelectedObjects.append( aRefObjectName );
102       }
103     }
104   }
105   else
106   {
107     myEditedObject =
108       Handle(HYDROData_Calculation)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
109     myEditedObject->SetName(anObjectName);
110   }
111
112   aPanel->setObjectName( anObjectName );
113   aPanel->setEditedObject( myEditedObject );
114   aPanel->setSelectedGeomObjects( aSelectedObjects );
115
116   createPreview();
117 }
118
119 void HYDROGUI_CalculationOp::abortOperation()
120 {
121   closePreview();
122   // Abort transaction
123   abortDocOperation();
124   HYDROGUI_Operation::abortOperation();
125 }
126
127 void HYDROGUI_CalculationOp::commitOperation()
128 {
129   closePreview();
130   // Commit transaction
131   commitDocOperation();
132   HYDROGUI_Operation::commitOperation();
133 }
134
135 HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
136 {
137   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
138
139   // Connect signals and slots
140
141   return aPanel;
142 }
143
144 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
145                                            QString& theErrorMsg )
146 {
147   HYDROGUI_CalculationDlg* aPanel = 
148     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
149   if ( !aPanel )
150     return false;
151
152   //QString anObjectName = aPanel->getObjectName().simplified();
153   //if ( anObjectName.isEmpty() )
154   //{
155   //  theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
156   //  return false;
157   //}
158
159   //// check that there are no other objects with the same name in the document
160   //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
161   //{
162   //  Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
163   //  if ( !anObject.IsNull() )
164   //  {
165   //    theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
166   //    return false;
167   //  }
168   //}
169
170   //Handle(HYDROData_Document) aDocument = doc();
171
172   //Handle(HYDROData_Calculation) aCalculObj = myIsEdit ? myEditedObject :
173   //  Handle(HYDROData_Calculation)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
174   //if ( aCalculObj.IsNull() )
175   //  return false;
176
177   //aCalculObj->SetName( anObjectName );
178
179   //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
180   //HYDROData_SequenceOfObjects aGeomObjects = 
181   //  HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
182
183   theUpdateFlags = UF_Model;
184
185   return true;
186 }
187
188 void HYDROGUI_CalculationOp::onApply()
189 {
190   QApplication::setOverrideCursor( Qt::WaitCursor );
191
192   int anUpdateFlags = 0;
193   QString anErrorMsg;
194
195   bool aResult = false;
196   
197   try
198   {
199     aResult = processApply( anUpdateFlags, anErrorMsg );
200   }
201   catch ( Standard_Failure )
202   {
203     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
204     anErrorMsg = aFailure->GetMessageString();
205     aResult = false;
206   }
207   catch ( ... )
208   {
209     aResult = false;
210   }
211   
212   QApplication::restoreOverrideCursor();
213
214   if ( aResult )
215   {
216     module()->update( anUpdateFlags );
217     commit();
218   }
219   else
220   {
221     abort();
222     QString aMsg = tr( "INPUT_VALID_DATA" );
223     if( !anErrorMsg.isEmpty() )
224       aMsg.prepend( anErrorMsg + "\n" );
225     SUIT_MessageBox::critical( module()->getApp()->desktop(),
226                                tr( "INSUFFICIENT_INPUT_DATA" ),
227                                aMsg ); 
228   }
229 }
230
231 void HYDROGUI_CalculationOp::onSplitZones()
232 {
233   myRegionsList.clear();
234
235   HYDROGUI_CalculationDlg* aPanel = 
236     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
237   if ( !aPanel )
238     return;
239
240   QApplication::setOverrideCursor( Qt::WaitCursor );
241
242   QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
243   HYDROData_SequenceOfObjects aGeomObjects = 
244     HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
245
246   QStringList aResSplittedZones;
247
248   HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
249     HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
250
251   QStringList aUsedNames;
252
253   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
254   while( anIter.hasNext() )
255   {
256     Region aRegion;
257     aRegion.SplitData = anIter.next();
258
259     aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
260     aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
261
262     aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
263
264     aUsedNames.append( aRegion.RegionName );
265
266     aResSplittedZones.append( aRegion.RegionName );
267
268     myRegionsList.append( aRegion );
269   }
270   
271   createPreview();
272
273   QApplication::restoreOverrideCursor();
274 }
275
276 void HYDROGUI_CalculationOp::createPreview()
277 {
278   LightApp_Application* anApp = module()->getApp();
279
280   if ( !myActiveViewManager )
281   {
282     if ( myRegionsList.isEmpty() )
283       return;
284
285     myActiveViewManager = anApp->activeViewManager();
286   }
287
288   if ( !myPreviewViewManager )
289   {
290     myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
291       anApp->createViewManager( OCCViewer_Viewer::Type() ) );
292     if ( myPreviewViewManager )
293     {
294       connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
295                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
296
297       module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
298       myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
299     }
300   }
301
302   if ( !myPreviewViewManager )
303     return;
304
305   if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
306   {
307     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
308     if ( !aCtx.IsNull() )
309     {
310       RegionsList::iterator anIter = myRegionsList.begin();
311       for ( ; anIter != myRegionsList.end(); ++anIter )
312       {
313         Region& aRegion = *anIter;
314         if ( aRegion.Shape )
315         {
316           aRegion.Shape->erase( false );
317           delete aRegion.Shape;
318         }
319
320         aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
321
322         aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
323         aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
324         aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
325       }
326
327       //Process the draw events for viewer
328       QApplication::processEvents();
329       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
330         vw->onTopView();
331     }
332   }
333 }
334
335 void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
336 {
337   closePreview();
338 }
339
340 void HYDROGUI_CalculationOp::closePreview()
341 {
342   RegionsList::iterator anIter= myRegionsList.begin();
343   for ( ; anIter != myRegionsList.end(); ++anIter )
344   {
345     Region& aRegion = *anIter;
346     if ( aRegion.Shape )
347     {
348       aRegion.Shape->erase( false );
349       delete aRegion.Shape;
350       aRegion.Shape = NULL;
351     }
352   }
353
354   if( myPreviewViewManager )
355   {
356     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
357                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
358
359     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
360     myPreviewViewManager = NULL;
361   }
362
363   if( myActiveViewManager )
364   {
365     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
366     myActiveViewManager = NULL;
367   }
368 }
369
370