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