Salome HOME
refs #585: preview in split operation
[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 <LightApp_Application.h>
26 #include <OCCViewer_ViewModel.h>
27 #include <OCCViewer_ViewManager.h>
28 #include <gp_Pnt2d.hxx>
29 #include <BRepLib_MakeVertex.hxx>
30 #include <BRep_Builder.hxx>
31 #include <TopoDS_Vertex.hxx>
32 #include <TopoDS_Compound.hxx>
33
34 HYDROGUI_SplitPolylinesOp::HYDROGUI_SplitPolylinesOp( HYDROGUI_Module* theModule )
35 : HYDROGUI_Operation( theModule ),
36   myPreviewPrs( 0 )
37 {
38   setName( tr( "SPLIT_POLYLINES" ) );
39 }
40
41 HYDROGUI_SplitPolylinesOp::~HYDROGUI_SplitPolylinesOp()
42 {
43 }
44
45 void HYDROGUI_SplitPolylinesOp::startOperation()
46 {
47   HYDROGUI_Operation::startOperation();
48
49   HYDROGUI_SplitPolylinesDlg* aPanel = 
50     ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
51   if ( !aPanel )
52     return;
53
54   aPanel->setPolylinesFromSelection();
55   LightApp_Application* anApp = module()->getApp();
56   OCCViewer_ViewManager* aViewManager =
57     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
58   aPanel->setOCCViewer( aViewManager ? aViewManager->getOCCViewer() : 0 );
59   setPreviewManager( aViewManager );
60
61   OnUpdatePreview();
62 }
63
64 HYDROGUI_InputPanel* HYDROGUI_SplitPolylinesOp::createInputPanel() const
65 {
66   HYDROGUI_SplitPolylinesDlg* aDlg = new HYDROGUI_SplitPolylinesDlg( module(), getName() );
67   connect( aDlg, SIGNAL( pointMoved() ), this, SLOT( OnUpdatePreview() ) );
68   connect( aDlg, SIGNAL( modeChanged() ), this, SLOT( OnUpdatePreview() ) );
69   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( OnUpdatePreview() ) );
70   return aDlg;
71 }
72
73 bool HYDROGUI_SplitPolylinesOp::processApply( int& theUpdateFlags,
74                                               QString& theErrorMsg,
75                                               QStringList& theBrowseObjectsEntries )
76 {
77   HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
78   if ( !aPanel )
79     return false;
80
81   Handle( HYDROData_PolylineXY ) aMainPolyline = aPanel->GetMainPolyline();
82   Handle( HYDROData_PolylineXY ) aToolPolyline = aPanel->GetToolPolyline();
83   HYDROData_SequenceOfObjects aPolylinesList = aPanel->GetPolylines();
84   gp_Pnt2d aPoint = aPanel->GetPoint();
85
86   HYDROData_PolylineOperator anOp;
87   switch( aPanel->GetMode() )
88   {
89   case HYDROGUI_SplitPolylinesDlg::ByPoint:
90     anOp.Split( aMainPolyline, aPoint );
91     break;
92   case HYDROGUI_SplitPolylinesDlg::ByTool:
93     anOp.Split( aMainPolyline, aToolPolyline );
94     break;
95   case HYDROGUI_SplitPolylinesDlg::Split:
96     anOp.Split( aPolylinesList );
97     break;
98   }
99   
100   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
101   return true;
102 }
103
104 void HYDROGUI_SplitPolylinesOp::abortOperation()
105 {
106   erasePreview();
107   HYDROGUI_Operation::abortOperation();
108 }
109
110 void HYDROGUI_SplitPolylinesOp::commitOperation()
111 {
112   erasePreview();
113   HYDROGUI_Operation::commitOperation();
114 }
115
116 void HYDROGUI_SplitPolylinesOp::erasePreview()
117 {
118   if( myPreviewPrs )
119   {
120     delete myPreviewPrs;
121     myPreviewPrs = 0;
122   }
123 }
124
125 void HYDROGUI_SplitPolylinesOp::OnUpdatePreview()
126 {
127   OCCViewer_ViewManager* aViewManager = getPreviewManager();
128   if ( aViewManager )
129   {
130     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
131     {
132       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
133       if ( !aCtx.IsNull() )
134       {
135         if( !myPreviewPrs )
136           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
137         aCtx->ClearSelected();
138       }
139     }
140   }
141
142   HYDROGUI_SplitPolylinesDlg* aPanel = ::qobject_cast<HYDROGUI_SplitPolylinesDlg*>( inputPanel() );
143   if( myPreviewPrs && aPanel )
144   {
145     BRep_Builder aBB;
146     TopoDS_Compound aCmp;
147     aBB.MakeCompound( aCmp );
148
149     switch( aPanel->GetMode() )
150     {
151     case HYDROGUI_SplitPolylinesDlg::ByPoint:
152       {
153         gp_Pnt2d aPnt = aPanel->GetPoint();
154         TopoDS_Vertex aVertex = BRepLib_MakeVertex( gp_Pnt( aPnt.X(), aPnt.Y(), 0.0 ) );
155         aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
156         aBB.Add( aCmp, aVertex );
157         break;
158       }
159     case HYDROGUI_SplitPolylinesDlg::ByTool:
160       {
161         aBB.Add( aCmp, aPanel->GetMainPolyline()->GetShape() );
162         aBB.Add( aCmp, aPanel->GetToolPolyline()->GetShape() );
163         break;
164       }
165     case HYDROGUI_SplitPolylinesDlg::Split:
166       {
167         HYDROData_SequenceOfObjects aPolylines = aPanel->GetPolylines();
168         for( int i=aPolylines.Lower(), n=aPolylines.Upper(); i<=n; i++ )
169         {
170           Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( aPolylines.Value( i ) );
171           if( !aPolyline.IsNull() )
172             aBB.Add( aCmp, aPolyline->GetShape() );
173         }
174         break;
175       }
176     }
177
178
179     myPreviewPrs->setShape( aCmp );
180   }
181 }