Salome HOME
Merge branch 'BR_v14_rc' into BR_quadtree
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_SplitPolylinesDlg.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_SplitPolylinesDlg.h>
20 #include <HYDROGUI_ObjComboBox.h>
21 #include <HYDROGUI_ObjListBox.h>
22 #include <HYDROGUI_Tool.h>
23 #include <OCCViewer_ViewManager.h>
24 #include <OCCViewer_ViewWindow.h>
25 #include <OCCViewer_ViewPort3d.h>
26 #include <CurveCreator_Utils.hxx>
27 #include <SUIT_ViewWindow.h>
28 #include <QtxDoubleSpinBox.h>
29 #include <QFrame>
30 #include <QLabel>
31 #include <QGridLayout>
32 #include <QTabWidget>
33 #include <QMouseEvent>
34 #include <QLineEdit>
35 #include <gp_Pnt2d.hxx>
36
37 const double MIN_COORD = -1000000;
38 const double MAX_COORD =  1000000;
39 const double STEP      =  10;
40
41 HYDROGUI_SplitPolylinesDlg::HYDROGUI_SplitPolylinesDlg( HYDROGUI_Module* theModule, const QString& theTitle )
42 : HYDROGUI_InputPanel( theModule, theTitle ), myOCCViewer( 0 )
43 {
44   QFrame* aFrame = new QFrame( mainFrame() );
45   addWidget( aFrame, 1 );
46
47   QGridLayout* aLayout = new QGridLayout( aFrame );
48   aLayout->setMargin( 5 );
49   aLayout->setSpacing( 5 );
50
51   myName = new QLineEdit( mainFrame() );
52   aLayout->addWidget( new QLabel( tr( "RESULT_NAME" ) ), 0, 0 );
53   aLayout->addWidget( myName, 0, 1 );
54
55   myTab = new QTabWidget( aFrame );
56   aLayout->addWidget( myTab, 1, 0, 1, 2 );
57
58   QFrame* aPointPage = new QFrame();
59   myMainPolyline1 = new HYDROGUI_ObjComboBox( theModule, tr( "POLYLINE" ), KIND_POLYLINEXY, aPointPage );
60   myX = new QtxDoubleSpinBox( aPointPage );
61   myX->setRange( MIN_COORD, MAX_COORD );
62   myX->setSingleStep( STEP );
63   myY = new QtxDoubleSpinBox( aPointPage );
64   myY->setRange( MIN_COORD, MAX_COORD );
65   myY->setSingleStep( STEP );
66   
67   QGridLayout* aPointPageLayout = new QGridLayout( aPointPage );
68   aPointPageLayout->addWidget( myMainPolyline1, 0, 0, 1, 3 );
69   aPointPageLayout->addWidget( new QLabel( tr( "POINT" ) ), 1, 0 );
70   aPointPageLayout->addWidget( myX, 1, 1 );
71   aPointPageLayout->addWidget( myY, 1, 2 );
72   aPointPageLayout->setColumnStretch( 1, 1 );
73   aPointPageLayout->setColumnStretch( 2, 1 );
74   aPointPageLayout->setRowStretch( 2, 1 );
75   myTab->addTab( aPointPage, tr( "BY_POINT" ) );
76
77   QFrame* aToolPage = new QFrame();
78   myMainPolyline2 = new HYDROGUI_ObjComboBox( theModule, tr( "POLYLINE" ), KIND_POLYLINEXY, aToolPage );
79   myToolPolyline = new HYDROGUI_ObjComboBox( theModule, tr( "TOOL_POLYLINE" ), KIND_POLYLINEXY, aToolPage );
80
81   QGridLayout* aToolPageLayout = new QGridLayout( aToolPage );
82   aToolPageLayout->addWidget( myMainPolyline2, 0, 0 );
83   aToolPageLayout->addWidget( myToolPolyline, 1, 0 );
84   aToolPageLayout->setRowStretch( 2, 1 );
85   myTab->addTab( aToolPage, tr( "BY_TOOL" ) );
86
87   QFrame* aSplitPage = new QFrame();
88   myPolylines = new HYDROGUI_ObjListBox( theModule, tr( "POLYLINES" ), KIND_POLYLINEXY, aSplitPage );
89   QGridLayout* aSplitPageLayout = new QGridLayout( aSplitPage );
90   aSplitPageLayout->addWidget( myPolylines, 0, 0 );
91   myTab->addTab( aSplitPage, tr( "COMPLETE_SPLIT" ) );
92
93   connect( myX, SIGNAL( valueChanged( double ) ), this, SIGNAL( pointMoved() ) );
94   connect( myY, SIGNAL( valueChanged( double ) ), this, SIGNAL( pointMoved() ) );
95   connect( myTab, SIGNAL( currentChanged( int ) ), this, SIGNAL( modeChanged() ) );
96   connect( myMainPolyline1, SIGNAL( objectSelected( const QString& ) ), this, SIGNAL( selectionChanged() ) );
97   connect( myMainPolyline2, SIGNAL( objectSelected( const QString& ) ), this, SIGNAL( selectionChanged() ) );
98   connect( myToolPolyline, SIGNAL( objectSelected( const QString& ) ), this, SIGNAL( selectionChanged() ) );
99   connect( myPolylines, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
100 }
101
102 HYDROGUI_SplitPolylinesDlg::~HYDROGUI_SplitPolylinesDlg()
103 {
104 }
105
106 HYDROGUI_SplitPolylinesDlg::Mode HYDROGUI_SplitPolylinesDlg::GetMode() const
107 {
108   return ( Mode )myTab->currentIndex();
109 }
110
111 Handle( HYDROData_PolylineXY ) HYDROGUI_SplitPolylinesDlg::GetMainPolyline() const
112 {
113   switch( GetMode() )
114   {
115   case ByPoint:
116     if( !myMainPolyline1->GetObject().IsNull() )
117       return Handle( HYDROData_PolylineXY )::DownCast( myMainPolyline1->GetObject() );
118   case ByTool:
119     if( !myMainPolyline2->GetObject().IsNull() )
120       return Handle( HYDROData_PolylineXY )::DownCast( myMainPolyline2->GetObject() );
121   default:
122     return Handle( HYDROData_PolylineXY )();
123   }
124   return Handle( HYDROData_PolylineXY )();
125 }
126
127 Handle( HYDROData_PolylineXY ) HYDROGUI_SplitPolylinesDlg::GetToolPolyline() const
128 {
129   if( GetMode()==ByTool )
130     return Handle( HYDROData_PolylineXY )::DownCast( myToolPolyline->GetObject() );
131   else
132     return Handle( HYDROData_PolylineXY )();
133 }
134
135 gp_Pnt2d HYDROGUI_SplitPolylinesDlg::GetPoint() const
136 {
137   return gp_Pnt2d( myX->value(), myY->value() );
138 }
139
140 HYDROData_SequenceOfObjects HYDROGUI_SplitPolylinesDlg::GetPolylines() const
141 {
142   if( GetMode()==Split )
143     return myPolylines->selectedObjects();
144   else
145     return HYDROData_SequenceOfObjects();
146 }
147
148 void HYDROGUI_SplitPolylinesDlg::setPolylinesFromSelection()
149 {
150   myMainPolyline1->reset();
151   myMainPolyline2->reset();
152   myPolylines->reset();
153
154   Handle( HYDROData_Entity ) anObject = HYDROGUI_Tool::GetSelectedObject( module() );
155   if( anObject.IsNull() || anObject->GetKind() != KIND_POLYLINEXY )
156     return;
157
158   QString aName = anObject->GetName();
159   myMainPolyline1->setObjectName( aName );
160   myMainPolyline2->setObjectName( aName );
161   myPolylines->setObjectsFromSelection();
162 }
163
164 void HYDROGUI_SplitPolylinesDlg::setOCCViewer( OCCViewer_Viewer* theViewer )
165 {
166   if( !theViewer )
167     return;
168   
169   myOCCViewer = theViewer;
170   OCCViewer_ViewManager* aViewManager = dynamic_cast<OCCViewer_ViewManager*>( myOCCViewer->getViewManager() );
171   disconnect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
172               this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
173   connect( aViewManager, SIGNAL( mousePress( SUIT_ViewWindow*, QMouseEvent* ) ),
174            this, SLOT( onMousePress( SUIT_ViewWindow*, QMouseEvent* ) ) );
175 }
176
177 void HYDROGUI_SplitPolylinesDlg::onMousePress( SUIT_ViewWindow* theWindow, QMouseEvent* theEvent )
178 {
179   gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), getViewPort()->getView() );
180   myX->setValue( aPnt.X() );
181   myY->setValue( aPnt.Y() );
182 }
183
184 OCCViewer_ViewPort3d* HYDROGUI_SplitPolylinesDlg::getViewPort() const
185 {
186   OCCViewer_ViewPort3d* aViewPort = 0;
187   if ( myOCCViewer )
188     aViewPort = dynamic_cast<OCCViewer_ViewWindow*>( myOCCViewer->getViewManager()->getActiveView() )->getViewPort();
189     
190   return aViewPort;
191 }
192
193 QString HYDROGUI_SplitPolylinesDlg::GetResultName() const
194 {
195   return myName->text();
196 }