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