Salome HOME
cleaning some comments
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_SplitPolylinesOp.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_SplitPolylinesOp.h>
20 #include <HYDROGUI_SplitPolylinesDlg.h>
21 #include <HYDROGUI_Module.h>
22 #include <HYDROGUI_UpdateFlags.h>
23 #include <HYDROGUI_Shape.h>
24 #include <HYDROData_PolylineOperator.h>
25 #include <HYDROData_Document.h>
26 #include <LightApp_Application.h>
27 #include <OCCViewer_ViewModel.h>
28 #include <OCCViewer_ViewManager.h>
29 #include <OCCViewer_ViewPort3d.h>
30 #include <OCCViewer_ViewWindow.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_MessageBox.h>
33 #include <CurveCreator_Utils.hxx>
34 #include <gp_Pnt2d.hxx>
35 #include <BRepLib_MakeVertex.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRepBuilderAPI_MakeVertex.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <AIS_Shape.hxx>
41
42 #include <QMouseEvent>
43
44 HYDROGUI_SplitPolylinesOp::HYDROGUI_SplitPolylinesOp( HYDROGUI_Module* theModule )
45 : HYDROGUI_Operation( theModule ),
46   myPreviewPrs( 0 ),
47   mySplitPointPreview( 0 )
48 {
49   setName( tr( "SPLIT_POLYLINES" ) );
50 }
51
52 HYDROGUI_SplitPolylinesOp::~HYDROGUI_SplitPolylinesOp()
53 {
54   erasePreview();
55 }
56
57 void HYDROGUI_SplitPolylinesOp::startOperation()
58 {
59   HYDROGUI_Operation::startOperation();
60
61   HYDROGUI_SplitPolylinesDlg* aPanel =
62     ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
63   if ( !aPanel )
64     return;
65
66   aPanel->setPolylinesFromSelection();
67   LightApp_Application* anApp = module()->getApp();
68   OCCViewer_ViewManager* aViewManager =
69     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
70   aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
71   setPreviewManager( aViewManager );
72
73   OnUpdatePreview();
74 }
75
76 HYDROGUI_InputPanel* HYDROGUI_SplitPolylinesOp::createInputPanel() const
77 {
78   HYDROGUI_SplitPolylinesDlg* aDlg = new HYDROGUI_SplitPolylinesDlg( module(), getName() );
79   connect( aDlg, SIGNAL( pointMoved() ), this, SLOT( OnUpdatePreview() ) );
80   connect( aDlg, SIGNAL( modeChanged() ), this, SLOT( OnUpdatePreview() ) );
81   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( OnUpdatePreview() ) );
82   return aDlg;
83 }
84
85 bool HYDROGUI_SplitPolylinesOp::processApply( int& theUpdateFlags,
86                                               QString& theErrorMsg,
87                                               QStringList& theBrowseObjectsEntries )
88 {
89   HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
90   if ( !aPanel )
91     return false;
92
93   //TODO: QString aName = aPanel->GetResultName();
94   Handle( HYDROData_PolylineXY ) aMainPolyline = aPanel->GetMainPolyline();
95   Handle( HYDROData_PolylineXY ) aToolPolyline = aPanel->GetToolPolyline();
96   HYDROData_SequenceOfObjects aPolylinesList = aPanel->GetPolylines();
97   gp_Pnt2d aPoint = aPanel->GetPoint();
98   double aTolerance = 1E-2; //TODO
99
100   HYDROData_PolylineOperator anOp;
101   switch( aPanel->GetMode() )
102   {
103   case HYDROGUI_SplitPolylinesDlg::ByPoint:
104     anOp.Split( doc(), aMainPolyline, aPoint, aTolerance );
105     break;
106   case HYDROGUI_SplitPolylinesDlg::ByTool:
107   {
108     bool isIntersected = false;
109     anOp.Split( doc(), aMainPolyline, aToolPolyline, aTolerance, isIntersected);
110
111     if (!isIntersected)
112     {
113       const QString aTitle = tr("SPLIT_POLYLINE_BY_TOOL_WARNING_TITLE");
114       const QString aMsg = tr("SPLIT_POLYLINE_BY_TOOL_WARNING_MSG");
115       SUIT_MessageBox::warning(module()->getApp()->desktop(), aTitle, aMsg);
116     }
117     break;
118   }
119   case HYDROGUI_SplitPolylinesDlg::Split:
120     anOp.Split( doc(), aPolylinesList, aTolerance );
121     break;
122   }
123
124   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
125   return true;
126 }
127
128 void HYDROGUI_SplitPolylinesOp::abortOperation()
129 {
130   erasePreview();
131   HYDROGUI_Operation::abortOperation();
132 }
133
134 void HYDROGUI_SplitPolylinesOp::commitOperation()
135 {
136   erasePreview();
137   HYDROGUI_Operation::commitOperation();
138 }
139
140 void HYDROGUI_SplitPolylinesOp::erasePreview()
141 {
142   if( myPreviewPrs )
143   {
144     delete myPreviewPrs;
145     myPreviewPrs = 0;
146   }
147
148   HYDROGUI_SplitPolylinesDlg* aPanel =
149     ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
150   if ( !aPanel )
151     return;
152
153   OCCViewer_ViewManager* aViewManager = getPreviewManager();
154   if ( aViewManager )
155   {
156     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
157     {
158       if ( mySplitPointPreview )
159       {
160         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
161         if ( !aCtx.IsNull() )
162         {
163 //           if ( aCtx->HasOpenedContext() )
164 //             aCtx->CloseLocalContext();
165           aCtx->Erase( mySplitPointPreview, Standard_False );
166         }
167         mySplitPointPreview = 0;
168       }
169
170       if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
171       {
172         disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
173           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
174         connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
175           aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
176       }
177     }
178   }
179 }
180
181 void HYDROGUI_SplitPolylinesOp::OnUpdatePreview()
182 {
183   HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
184
185   OCCViewer_ViewManager* aViewManager = getPreviewManager();
186   if ( aViewManager )
187   {
188     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
189     {
190       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
191       if ( !aCtx.IsNull() )
192       {
193         if( !myPreviewPrs )
194         {
195           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
196           myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) );
197         }
198
199         if ( mySplitPointPreview )
200         {
201           if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
202           {
203             if ( !aCtx->IsDisplayed( mySplitPointPreview ) )
204               aCtx->Display( mySplitPointPreview, Standard_False );
205           }
206           else
207             aCtx->Erase( mySplitPointPreview, Standard_False );
208         }
209
210         aCtx->ClearSelected(true);
211       }
212
213       if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
214       {
215         disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
216           aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
217         connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
218           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
219       }
220       else
221       {
222         disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
223           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
224         connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)),
225           aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
226       }
227     }
228   }
229
230   if( myPreviewPrs && aPanel )
231   {
232     BRep_Builder aBB;
233     TopoDS_Compound aCmp;
234     aBB.MakeCompound( aCmp );
235
236     switch( aPanel->GetMode() )
237     {
238     case HYDROGUI_SplitPolylinesDlg::ByPoint:
239       {
240         gp_Pnt2d aPnt = aPanel->GetPoint();
241         TopoDS_Vertex aVertex = BRepLib_MakeVertex( gp_Pnt( aPnt.X(), aPnt.Y(), 0.0 ) );
242         if( !aPanel->GetMainPolyline().IsNull() )
243           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
244         aBB.Add( aCmp, aVertex );
245         break;
246       }
247     case HYDROGUI_SplitPolylinesDlg::ByTool:
248       {
249         if( !aPanel->GetMainPolyline().IsNull() )
250           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
251         if( !aPanel->GetToolPolyline().IsNull() )
252           aBB.Add( aCmp, aPanel->GetToolPolyline()->GetShape() );
253         break;
254       }
255     case HYDROGUI_SplitPolylinesDlg::Split:
256       {
257         HYDROData_SequenceOfObjects aPolylines = aPanel->GetPolylines();
258         for( int i=aPolylines.Lower(), n=aPolylines.Upper(); i<=n; i++ )
259         {
260           Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( aPolylines.Value( i ) );
261           if( !aPolyline.IsNull() )
262             aBB.Add( aCmp, aPolyline->GetShape() );
263         }
264         break;
265       }
266     }
267
268
269     myPreviewPrs->setShape( aCmp );
270   }
271 }
272
273 void HYDROGUI_SplitPolylinesOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
274 {
275   OCCViewer_ViewManager* aViewManager = getPreviewManager();
276   if ( !aViewManager )
277     return;
278
279   OCCViewer_ViewWindow* aViewWindow = (OCCViewer_ViewWindow*)aViewManager->getActiveView();
280   if ( !aViewWindow )
281     return;
282
283   OCCViewer_ViewPort3d* aViewPort = aViewWindow->getViewPort();
284   if ( !aViewPort )
285     return;
286
287   Handle(V3d_View) aView = aViewPort->getView();
288   if ( aView.IsNull() )
289     return;
290
291   OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
292   if ( !aViewer )
293     return;
294
295   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
296   if ( aCtx.IsNull() )
297     return;
298
299   gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), aView );
300   BRep_Builder aBuilder;
301   TopoDS_Compound aShape;
302   aBuilder.MakeCompound(aShape);
303   aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex( aPnt ) );
304
305   if ( mySplitPointPreview )
306   {
307     aCtx->Erase( mySplitPointPreview, Standard_False );
308   }
309
310   mySplitPointPreview = new AIS_Shape( aShape );
311   aCtx->Display( mySplitPointPreview, Standard_False );
312
313   aCtx->UpdateCurrentViewer();
314 }