Salome HOME
debug of DTM object
[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 ( aCtx->HasOpenedContext() )
202             aCtx->CloseLocalContext();
203           if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
204           {
205             if ( !aCtx->IsDisplayed( mySplitPointPreview ) )
206               aCtx->Display( mySplitPointPreview, Standard_False );
207           }
208           else
209             aCtx->Erase( mySplitPointPreview, Standard_False );  
210         }
211
212         aCtx->ClearSelected();
213       }
214
215       if ( aPanel->GetMode() == HYDROGUI_SplitPolylinesDlg::ByPoint )
216       {
217         disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
218           aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
219         connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
220           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
221       }
222       else
223       {
224         disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
225           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
226         connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
227           aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
228       }
229     }
230   }
231
232   if( myPreviewPrs && aPanel )
233   {
234     BRep_Builder aBB;
235     TopoDS_Compound aCmp;
236     aBB.MakeCompound( aCmp );
237
238     switch( aPanel->GetMode() )
239     {
240     case HYDROGUI_SplitPolylinesDlg::ByPoint:
241       {
242         gp_Pnt2d aPnt = aPanel->GetPoint();
243         TopoDS_Vertex aVertex = BRepLib_MakeVertex( gp_Pnt( aPnt.X(), aPnt.Y(), 0.0 ) );
244         if( !aPanel->GetMainPolyline().IsNull() )
245           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
246         aBB.Add( aCmp, aVertex );
247         break;
248       }
249     case HYDROGUI_SplitPolylinesDlg::ByTool:
250       {
251         if( !aPanel->GetMainPolyline().IsNull() )
252           aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
253         if( !aPanel->GetToolPolyline().IsNull() )
254           aBB.Add( aCmp, aPanel->GetToolPolyline()->GetShape() );
255         break;
256       }
257     case HYDROGUI_SplitPolylinesDlg::Split:
258       {
259         HYDROData_SequenceOfObjects aPolylines = aPanel->GetPolylines();
260         for( int i=aPolylines.Lower(), n=aPolylines.Upper(); i<=n; i++ )
261         {
262           Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( aPolylines.Value( i ) );
263           if( !aPolyline.IsNull() )
264             aBB.Add( aCmp, aPolyline->GetShape() );
265         }
266         break;
267       }
268     }
269
270
271     myPreviewPrs->setShape( aCmp );
272   }
273 }
274
275 void HYDROGUI_SplitPolylinesOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
276 {
277   OCCViewer_ViewManager* aViewManager = getPreviewManager();
278   if ( !aViewManager )
279     return;
280
281   OCCViewer_ViewWindow* aViewWindow = (OCCViewer_ViewWindow*)aViewManager->getActiveView();
282   if ( !aViewWindow )
283     return;
284
285   OCCViewer_ViewPort3d* aViewPort = aViewWindow->getViewPort();
286   if ( !aViewPort )
287     return;
288
289   Handle(V3d_View) aView = aViewPort->getView();
290   if ( aView.IsNull() )
291     return;
292
293   OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
294   if ( !aViewer )
295     return;
296
297   Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
298   if ( aCtx.IsNull() )
299     return;
300
301   gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), aView );
302   BRep_Builder aBuilder;
303   TopoDS_Compound aShape;
304   aBuilder.MakeCompound(aShape);
305   aBuilder.Add(aShape, BRepBuilderAPI_MakeVertex( aPnt ) );
306   
307   if ( mySplitPointPreview )
308   {
309     if ( aCtx->HasOpenedContext() )
310       aCtx->CloseLocalContext();
311     aCtx->Erase( mySplitPointPreview, Standard_False );
312   }
313
314   mySplitPointPreview = new AIS_Shape( aShape );
315   if ( aCtx->HasOpenedContext() )
316     aCtx->CloseLocalContext();
317   aCtx->Display( mySplitPointPreview, Standard_False );
318   
319   aCtx->UpdateCurrentViewer();
320 }