Salome HOME
refs #567: add "POLYLINES" partition and modified icon for Land Cover 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_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     if ( isApplyAndClose() )
82       myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
83     if( !myEditedObject.IsNull() )
84     {
85       anObjectName = myEditedObject->GetName();
86
87       Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
88       if ( !aRefPolyline.IsNull() )
89         aSelectedPolyline = aRefPolyline->GetName();
90
91       Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
92       if ( !aRefAltitude.IsNull() )
93         aSelectedBathymetry = aRefAltitude->GetName();
94     }
95   }
96
97   aPanel->setObjectName( anObjectName );
98   aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
99   aPanel->setAdditionalParams( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_BATHYMETRY ) );
100
101   aPanel->blockSignals( false );
102
103   aPanel->setPolylineName( aSelectedPolyline );
104   aPanel->setSelectedAdditionalParamName( aSelectedBathymetry );
105 }
106
107 void HYDROGUI_ImmersibleZoneOp::abortOperation()
108 {
109   closePreview();
110
111   HYDROGUI_Operation::abortOperation();
112 }
113
114 void HYDROGUI_ImmersibleZoneOp::commitOperation()
115 {
116   closePreview();
117
118   HYDROGUI_Operation::commitOperation();
119 }
120
121 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
122 {
123   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
124   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
125            this,   SLOT( onCreatePreview( const QString& ) ) );
126   return aPanel;
127 }
128
129 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
130                                               QString& theErrorMsg,
131                                               QStringList& theBrowseObjectsEntries )
132 {
133   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
134   if ( !aPanel )
135     return false;
136
137   QString anObjectName = aPanel->getObjectName().simplified();
138   if ( anObjectName.isEmpty() )
139   {
140     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
141     return false;
142   }
143
144   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
145   {
146     // check that there are no other objects with the same name in the document
147     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
148     if( !anObject.IsNull() )
149     {
150       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
151       return false;
152     }
153   }
154
155   Handle(HYDROData_PolylineXY) aZonePolyline;
156   Handle(HYDROData_Bathymetry) aZoneBathymetry;
157
158   QString aPolylineName = aPanel->getPolylineName();
159   if ( !aPolylineName.isEmpty() )
160   {
161     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
162       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
163   }
164
165   QString aBathymetryName = aPanel->getSelectedAdditionalParamName();
166   if ( !aBathymetryName.isEmpty() )
167   {
168     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
169       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
170   }
171
172
173   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
174   {
175     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
176     return false;
177   }
178
179   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
180     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
181
182   aZoneObj->SetName( anObjectName );
183
184   if ( !myIsEdit )
185   {
186     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
187     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
188   }
189
190   aZoneObj->SetPolyline( aZonePolyline );
191   aZoneObj->SetAltitudeObject( aZoneBathymetry );
192   aZoneObj->Update();
193
194   closePreview();
195
196   if( !myIsEdit )
197   {
198     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
199     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aZoneObj );
200     theBrowseObjectsEntries.append( anEntry );
201   }
202
203   module()->setIsToUpdate( aZoneObj );
204
205   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
206
207   return true;
208 }
209
210 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
211 {
212   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
213   if ( !aPanel )
214     return;
215
216   QApplication::setOverrideCursor( Qt::WaitCursor );
217   TopoDS_Shape aZoneShape;
218
219   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
220     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
221   if ( !aPolyline.IsNull() )
222   {
223     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
224     if( aZoneShape.IsNull() )
225       printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
226   }
227
228   LightApp_Application* anApp = module()->getApp();
229   if ( !getPreviewManager() )
230     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
231                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
232   OCCViewer_ViewManager* aViewManager = getPreviewManager();
233   if ( aViewManager && !myPreviewPrs )
234   {
235     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
236     {
237       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
238       if ( !aCtx.IsNull() )
239         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
240     }
241   }
242
243   if ( aViewManager && myPreviewPrs )
244   {
245     QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
246     QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
247     if ( !myEditedObject.IsNull() ) {
248       aFillingColor = myEditedObject->GetFillingColor();
249       aBorderColor = myEditedObject->GetBorderColor();
250     }
251
252     myPreviewPrs->setFillingColor( aFillingColor, false, false );
253     myPreviewPrs->setBorderColor( aBorderColor, false, false );
254     TopoDS_Face aFace;
255     if( !aZoneShape.IsNull() )
256       aFace = TopoDS::Face( aZoneShape );
257     myPreviewPrs->setFace( aFace, true, true, "" );
258   }
259
260   QApplication::restoreOverrideCursor();
261 }
262
263 void HYDROGUI_ImmersibleZoneOp::closePreview()
264 {
265   if( myPreviewPrs )
266   {
267     delete myPreviewPrs;
268     myPreviewPrs = 0;
269   }
270 }