]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ImmersibleZoneOp.cxx
Salome HOME
294e9b2c949cdbf0dbb4791b57d7770a792d3e65
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImmersibleZoneOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ImmersibleZoneOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_ImmersibleZoneDlg.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Shape.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include "HYDROGUI_DataObject.h"
28
29 #include <HYDROData_Bathymetry.h>
30 #include <HYDROData_Iterator.h>
31 #include <HYDROData_PolylineXY.h>
32
33 #include <OCCViewer_ViewManager.h>
34 #include <OCCViewer_ViewModel.h>
35
36 #include <LightApp_Application.h>
37 #include <LightApp_UpdateFlags.h>
38
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
41
42 #include <TopoDS.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Wire.hxx>
45
46 #include <QApplication>
47
48 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
49                                                       const bool theIsEdit )
50 : HYDROGUI_Operation( theModule ),
51   myIsEdit( theIsEdit ),
52   myPreviewPrs( 0 )
53 {
54   setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
55 }
56
57 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
58 {
59   closePreview();
60 }
61
62 void HYDROGUI_ImmersibleZoneOp::startOperation()
63 {
64   HYDROGUI_Operation::startOperation();
65
66   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
67   if ( !aPanel )
68     return;
69
70   aPanel->blockSignals( true );
71
72   aPanel->reset();
73
74   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
75
76   QString     aSelectedPolyline, aSelectedBathymetry;
77   QStringList aSelectedBathymetries;
78
79   if ( myIsEdit )
80   {
81     myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82     if( !myEditedObject.IsNull() )
83     {
84       anObjectName = myEditedObject->GetName();
85
86       Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
87       if ( !aRefPolyline.IsNull() )
88         aSelectedPolyline = aRefPolyline->GetName();
89
90       Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
91       if ( !aRefAltitude.IsNull() )
92         aSelectedBathymetry = aRefAltitude->GetName();
93     }
94   }
95
96   aPanel->setObjectName( anObjectName );
97   aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
98   aPanel->setBathymetryNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_BATHYMETRY ) );
99
100   aPanel->blockSignals( false );
101
102   aPanel->setPolylineName( aSelectedPolyline );
103   aPanel->setBathymetryName( aSelectedBathymetry );
104 }
105
106 void HYDROGUI_ImmersibleZoneOp::abortOperation()
107 {
108   closePreview();
109
110   HYDROGUI_Operation::abortOperation();
111 }
112
113 void HYDROGUI_ImmersibleZoneOp::commitOperation()
114 {
115   closePreview();
116
117   HYDROGUI_Operation::commitOperation();
118 }
119
120 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
121 {
122   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
123   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
124            this,   SLOT( onCreatePreview( const QString& ) ) );
125   return aPanel;
126 }
127
128 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
129                                               QString& theErrorMsg,
130                                               QStringList& theBrowseObjectsEntries )
131 {
132   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
133   if ( !aPanel )
134     return false;
135
136   QString anObjectName = aPanel->getObjectName().simplified();
137   if ( anObjectName.isEmpty() )
138   {
139     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
140     return false;
141   }
142
143   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
144   {
145     // check that there are no other objects with the same name in the document
146     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
147     if( !anObject.IsNull() )
148     {
149       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
150       return false;
151     }
152   }
153
154   Handle(HYDROData_PolylineXY) aZonePolyline;
155   Handle(HYDROData_Bathymetry) aZoneBathymetry;
156
157   QString aPolylineName = aPanel->getPolylineName();
158   if ( !aPolylineName.isEmpty() )
159   {
160     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
161       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
162   }
163
164   QString aBathymetryName = aPanel->getBathymetryName();
165   if ( !aBathymetryName.isEmpty() )
166   {
167     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
168       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
169   }
170
171
172   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
173   {
174     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
175     return false;
176   }
177
178   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
179     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
180
181   aZoneObj->SetName( anObjectName );
182
183   if ( !myIsEdit )
184   {
185     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
186     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
187   }
188
189   aZoneObj->SetPolyline( aZonePolyline );
190   aZoneObj->SetAltitudeObject( aZoneBathymetry );
191   aZoneObj->Update();
192
193   closePreview();
194
195   if( !myIsEdit )
196   {
197     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
198     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
199     theBrowseObjectsEntries.append( anEntry );
200   }
201
202   module()->setIsToUpdate( aZoneObj );
203
204   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
205
206   return true;
207 }
208
209 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
210 {
211   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
212   if ( !aPanel )
213     return;
214
215   QApplication::setOverrideCursor( Qt::WaitCursor );
216   TopoDS_Shape aZoneShape;
217
218   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
219     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
220   if ( !aPolyline.IsNull() )
221   {
222     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
223     if( aZoneShape.IsNull() )
224       printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
225   }
226
227   LightApp_Application* anApp = module()->getApp();
228   if ( !getPreviewManager() )
229     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
230                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
231   OCCViewer_ViewManager* aViewManager = getPreviewManager();
232   if ( aViewManager && !myPreviewPrs )
233   {
234     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
235     {
236       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
237       if ( !aCtx.IsNull() )
238         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
239     }
240   }
241
242   if ( aViewManager && myPreviewPrs )
243   {
244     QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
245     QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
246     if ( !myEditedObject.IsNull() ) {
247       aFillingColor = myEditedObject->GetFillingColor();
248       aBorderColor = myEditedObject->GetBorderColor();
249     }
250
251     myPreviewPrs->setFillingColor( aFillingColor, false, false );
252     myPreviewPrs->setBorderColor( aBorderColor, false, false );
253     TopoDS_Face aFace;
254     if( !aZoneShape.IsNull() )
255       aFace = TopoDS::Face( aZoneShape );
256     myPreviewPrs->setFace( aFace, true, true, "" );
257   }
258
259   QApplication::restoreOverrideCursor();
260 }
261
262 void HYDROGUI_ImmersibleZoneOp::closePreview()
263 {
264   if( myPreviewPrs )
265   {
266     delete myPreviewPrs;
267     myPreviewPrs = 0;
268   }
269 }