Salome HOME
Image positioning by two points.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImmersibleZoneOp.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_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
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_Polyline.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <TopoDS.hxx>
43 #include <TopoDS_Wire.hxx>
44
45 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
46                                                       const bool theIsEdit )
47 : HYDROGUI_Operation( theModule ),
48   myIsEdit( theIsEdit ),
49   myViewManager( 0 ),
50   myPreviewPrs( 0 )
51 {
52   setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
53 }
54
55 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
56 {
57   closePreview();
58 }
59
60 void HYDROGUI_ImmersibleZoneOp::startOperation()
61 {
62   HYDROGUI_Operation::startOperation();
63
64   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
65   if ( !aPanel )
66     return;
67
68   aPanel->blockSignals( true );
69
70   aPanel->reset();
71
72   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Zone" );
73
74   QColor      aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
75   QColor      aBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
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       aFillingColor = myEditedObject->GetFillingColor();
87       aBorderColor = myEditedObject->GetBorderColor();
88
89       Handle(HYDROData_Polyline) aRefPolyline = myEditedObject->GetPolyline();
90       if ( !aRefPolyline.IsNull() )
91         aSelectedPolyline = aRefPolyline->GetName();
92
93       Handle(HYDROData_Bathymetry) aRefBathymetry = myEditedObject->GetBathymetry();
94       if ( !aRefBathymetry.IsNull() )
95         aSelectedBathymetry = aRefBathymetry->GetName();
96     }
97   }
98
99   // collect information about existing closed polylines
100   QStringList aPolylines;
101
102   HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
103   for ( ; anIter.More(); anIter.Next() )
104   {
105     Handle(HYDROData_Polyline) aPolylineObj = 
106       Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
107     if ( aPolylineObj.IsNull() || !aPolylineObj->IsClosed() )
108       continue;
109
110     QString aPolylineName = aPolylineObj->GetName();
111     if ( aPolylineName.isEmpty() )
112       continue;
113
114     aPolylines.append( aPolylineName );
115   }
116
117   // collect information about existing bathymetries
118   QStringList aBathymetries;
119
120   anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
121   for ( ; anIter.More(); anIter.Next() )
122   {
123     Handle(HYDROData_Bathymetry) aBathymetryObj = 
124       Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
125     if ( aBathymetryObj.IsNull() )
126       continue;
127
128     QString aBathymetryName = aBathymetryObj->GetName();
129     if ( aBathymetryName.isEmpty() )
130       continue;
131
132     aBathymetries.append( aBathymetryName );
133   }
134   
135   aPanel->setObjectName( anObjectName );
136   
137   aPanel->setFillingColor( aFillingColor );
138   aPanel->setBorderColor( aBorderColor );
139
140   aPanel->setPolylineNames( aPolylines );
141   aPanel->setBathymetryNames( aBathymetries );
142
143   aPanel->blockSignals( false );
144
145   aPanel->setPolylineName( aSelectedPolyline );
146   aPanel->setBathymetryName( aSelectedBathymetry );
147 }
148
149 void HYDROGUI_ImmersibleZoneOp::abortOperation()
150 {
151   closePreview();
152
153   HYDROGUI_Operation::abortOperation();
154 }
155
156 void HYDROGUI_ImmersibleZoneOp::commitOperation()
157 {
158   closePreview();
159
160   HYDROGUI_Operation::commitOperation();
161 }
162
163 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
164 {
165   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
166   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
167            this,   SLOT( onCreatePreview( const QString& ) ) );
168   return aPanel;
169 }
170
171 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
172                                               QString& theErrorMsg )
173 {
174   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
175   if ( !aPanel )
176     return false;
177
178   QString anObjectName = aPanel->getObjectName().simplified();
179   if ( anObjectName.isEmpty() )
180   {
181     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
182     return false;
183   }
184
185   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
186   {
187     // check that there are no other objects with the same name in the document
188     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
189     if( !anObject.IsNull() )
190     {
191       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
192       return false;
193     }
194   }
195
196   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
197     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
198   if ( aZoneObj.IsNull() )
199     return false;
200
201   Handle(HYDROData_Polyline) aZonePolyline;
202   Handle(HYDROData_Bathymetry) aZoneBathymetry;
203
204   QString aPolylineName = aPanel->getPolylineName();
205   if ( !aPolylineName.isEmpty() )
206   {
207     aZonePolyline = Handle(HYDROData_Polyline)::DownCast(
208       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
209   }
210
211   QString aBathymetryName = aPanel->getBathymetryName();
212   if ( !aBathymetryName.isEmpty() )
213   {
214     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
215       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
216   }
217
218   aZoneObj->SetName( anObjectName );
219
220   aZoneObj->SetFillingColor( aPanel->getFillingColor() );
221   aZoneObj->SetBorderColor( aPanel->getBorderColor() );
222
223   aZoneObj->SetPolyline( aZonePolyline );
224   aZoneObj->SetBathymetry( aZoneBathymetry );
225
226   closePreview();
227
228   if( !myIsEdit )
229     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
230
231   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
232
233   return true;
234 }
235
236 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
237 {
238   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
239   if ( !aPanel )
240     return;
241
242   TopoDS_Wire aWire;
243
244   Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast(
245     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINE ) );
246   if ( !aPolyline.IsNull() )
247   {
248     aWire = TopoDS::Wire( aPolyline->GetTopShape() );
249   }
250
251   LightApp_Application* anApp = module()->getApp();
252   if ( !myViewManager )
253     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
254       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
255
256   if ( myViewManager && !myPreviewPrs )
257   {
258     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
259     {
260       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
261       if ( !aCtx.IsNull() )
262         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
263     }
264   }
265
266   if ( !myViewManager || !myPreviewPrs )
267     return;
268
269   myPreviewPrs->setFillingColor( aPanel->getFillingColor(), false, false );
270   myPreviewPrs->setBorderColor( aPanel->getBorderColor(), false, false );
271   myPreviewPrs->setFace( aWire );
272 }
273
274 void HYDROGUI_ImmersibleZoneOp::closePreview()
275 {
276   if( myPreviewPrs )
277   {
278     delete myPreviewPrs;
279     myPreviewPrs = 0;
280   }
281 }