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