]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ImmersibleZoneOp.cxx
Salome HOME
refs #529: use 'Apply and Close', 'Apply' and 'Close' buttons in HYDRO dialogs.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImmersibleZoneOp.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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_ImmersibleZoneOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ImmersibleZoneDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Shape.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31 #include "HYDROGUI_DataObject.h"
32
33 #include <HYDROData_Bathymetry.h>
34 #include <HYDROData_Iterator.h>
35 #include <HYDROData_PolylineXY.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_Desktop.h>
45
46 #include <TopoDS.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopoDS_Wire.hxx>
49
50 #include <QApplication>
51
52 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
53                                                       const bool theIsEdit )
54 : HYDROGUI_Operation( theModule ),
55   myIsEdit( theIsEdit ),
56   myPreviewPrs( 0 )
57 {
58   setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
59 }
60
61 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
62 {
63   closePreview();
64 }
65
66 void HYDROGUI_ImmersibleZoneOp::startOperation()
67 {
68   HYDROGUI_Operation::startOperation();
69
70   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
71   if ( !aPanel )
72     return;
73
74   aPanel->blockSignals( true );
75
76   aPanel->reset();
77
78   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
79
80   QString     aSelectedPolyline, aSelectedBathymetry;
81   QStringList aSelectedBathymetries;
82
83   if ( myIsEdit )
84   {
85     if ( isApplyAndClose() )
86       myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
87     if( !myEditedObject.IsNull() )
88     {
89       anObjectName = myEditedObject->GetName();
90
91       Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
92       if ( !aRefPolyline.IsNull() )
93         aSelectedPolyline = aRefPolyline->GetName();
94
95       Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
96       if ( !aRefAltitude.IsNull() )
97         aSelectedBathymetry = aRefAltitude->GetName();
98     }
99   }
100
101   aPanel->setObjectName( anObjectName );
102   aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
103   aPanel->setBathymetryNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_BATHYMETRY ) );
104
105   aPanel->blockSignals( false );
106
107   aPanel->setPolylineName( aSelectedPolyline );
108   aPanel->setBathymetryName( aSelectedBathymetry );
109 }
110
111 void HYDROGUI_ImmersibleZoneOp::abortOperation()
112 {
113   closePreview();
114
115   HYDROGUI_Operation::abortOperation();
116 }
117
118 void HYDROGUI_ImmersibleZoneOp::commitOperation()
119 {
120   closePreview();
121
122   HYDROGUI_Operation::commitOperation();
123 }
124
125 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
126 {
127   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
128   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
129            this,   SLOT( onCreatePreview( const QString& ) ) );
130   return aPanel;
131 }
132
133 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
134                                               QString& theErrorMsg,
135                                               QStringList& theBrowseObjectsEntries )
136 {
137   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
138   if ( !aPanel )
139     return false;
140
141   QString anObjectName = aPanel->getObjectName().simplified();
142   if ( anObjectName.isEmpty() )
143   {
144     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
145     return false;
146   }
147
148   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
149   {
150     // check that there are no other objects with the same name in the document
151     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
152     if( !anObject.IsNull() )
153     {
154       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
155       return false;
156     }
157   }
158
159   Handle(HYDROData_PolylineXY) aZonePolyline;
160   Handle(HYDROData_Bathymetry) aZoneBathymetry;
161
162   QString aPolylineName = aPanel->getPolylineName();
163   if ( !aPolylineName.isEmpty() )
164   {
165     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
166       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
167   }
168
169   QString aBathymetryName = aPanel->getBathymetryName();
170   if ( !aBathymetryName.isEmpty() )
171   {
172     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
173       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
174   }
175
176
177   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
178   {
179     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
180     return false;
181   }
182
183   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
184     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
185
186   aZoneObj->SetName( anObjectName );
187
188   if ( !myIsEdit )
189   {
190     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
191     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
192   }
193
194   aZoneObj->SetPolyline( aZonePolyline );
195   aZoneObj->SetAltitudeObject( aZoneBathymetry );
196   aZoneObj->Update();
197
198   closePreview();
199
200   if( !myIsEdit )
201   {
202     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
203     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
204     theBrowseObjectsEntries.append( anEntry );
205   }
206
207   module()->setIsToUpdate( aZoneObj );
208
209   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
210
211   return true;
212 }
213
214 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
215 {
216   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
217   if ( !aPanel )
218     return;
219
220   QApplication::setOverrideCursor( Qt::WaitCursor );
221   TopoDS_Shape aZoneShape;
222
223   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
224     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
225   if ( !aPolyline.IsNull() )
226   {
227     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
228     if( aZoneShape.IsNull() )
229       printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
230   }
231
232   LightApp_Application* anApp = module()->getApp();
233   if ( !getPreviewManager() )
234     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
235                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
236   OCCViewer_ViewManager* aViewManager = getPreviewManager();
237   if ( aViewManager && !myPreviewPrs )
238   {
239     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
240     {
241       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
242       if ( !aCtx.IsNull() )
243         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
244     }
245   }
246
247   if ( aViewManager && myPreviewPrs )
248   {
249     QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
250     QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
251     if ( !myEditedObject.IsNull() ) {
252       aFillingColor = myEditedObject->GetFillingColor();
253       aBorderColor = myEditedObject->GetBorderColor();
254     }
255
256     myPreviewPrs->setFillingColor( aFillingColor, false, false );
257     myPreviewPrs->setBorderColor( aBorderColor, false, false );
258     TopoDS_Face aFace;
259     if( !aZoneShape.IsNull() )
260       aFace = TopoDS::Face( aZoneShape );
261     myPreviewPrs->setFace( aFace, true, true, "" );
262   }
263
264   QApplication::restoreOverrideCursor();
265 }
266
267 void HYDROGUI_ImmersibleZoneOp::closePreview()
268 {
269   if( myPreviewPrs )
270   {
271     delete myPreviewPrs;
272     myPreviewPrs = 0;
273   }
274 }