Salome HOME
merge BR_hydro_v_1_0_4 on BR_quadtree
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ProfileDlg.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ProfileDlg.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_AISTrihedron.h"
28
29 #include <CurveCreator_Widget.h>
30 #include <CurveCreator_ICurve.hxx>
31 #include <CurveCreator_Utils.hxx>
32
33 #include <OCCViewer_ViewPort3d.h>
34 #include <OCCViewer_Utilities.h>
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewFrame.h>
37 #include <LightApp_Application.h>
38
39 #include <SUIT_Session.h>
40 #include <SUIT_ResourceMgr.h>
41
42 #include <QGroupBox>
43 #include <QHBoxLayout>
44 #include <QLabel>
45 #include <QLineEdit>
46 #include <QMouseEvent>
47
48 HYDROGUI_ProfileDlg::HYDROGUI_ProfileDlg( HYDROGUI_Module* theModule, const QString& theTitle )
49 : HYDROGUI_InputPanel( theModule, theTitle ), myName(NULL)
50 {
51   QHBoxLayout* aNameLayout = new QHBoxLayout();
52   QLabel* aNameLabel = new QLabel(tr("PROFILE_NAME_TLT"), this);
53   aNameLayout->addWidget(aNameLabel);
54   myName = new QLineEdit(this);
55   aNameLayout->addWidget(myName);
56
57   addLayout(aNameLayout);
58
59   int anActionFlags = 
60     CurveCreator_Widget::DisableNewSection | CurveCreator_Widget::DisableDetectionMode |
61     CurveCreator_Widget::DisableClosedSection;
62   QStringList aCoordTitles;
63   aCoordTitles << tr( "U_TITLE" ) << tr( "Z_TITLE" );
64   myEditorWidget = new CurveCreator_Widget( this, NULL, anActionFlags, aCoordTitles );
65   addWidget( myEditorWidget, 3 );
66
67   myAddElementBox = new QGroupBox( tr( "ADD_ELEMENT" ), this );
68   addWidget( myAddElementBox, 2 );
69
70   QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
71   anAddElementLayout->setMargin( 0 );
72   anAddElementLayout->setSpacing( 5 );
73
74   myViewManager = new OCCViewer_ViewManager( theModule->getApp()->activeStudy(), 0 );
75   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( false /*erase trihedron*/);
76
77   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
78   aViewer->setBackground( OCCViewer_ViewFrame::TOP_LEFT,
79                      aResMgr->backgroundValue( "OCCViewer", "xz_background", aViewer->background(OCCViewer_ViewFrame::TOP_LEFT) ) );
80   aViewer->setBackground( OCCViewer_ViewFrame::TOP_RIGHT,
81                      aResMgr->backgroundValue( "OCCViewer", "yz_background", aViewer->background(OCCViewer_ViewFrame::TOP_RIGHT) ) );
82   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_LEFT,
83                      aResMgr->backgroundValue( "OCCViewer", "xy_background", aViewer->background(OCCViewer_ViewFrame::BOTTOM_LEFT) ) );
84   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_RIGHT,
85                      aResMgr->backgroundValue( "OCCViewer", "background", aViewer->background(OCCViewer_ViewFrame::MAIN_VIEW) ) );
86
87   aViewer->setTrihedronSize( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ),
88                         aResMgr->booleanValue( "3DViewer", "relative_size", aViewer->trihedronRelative() ));
89   aViewer->setInteractionStyle( aResMgr->integerValue( "3DViewer", "navigation_mode", aViewer->interactionStyle() ) );
90   aViewer->setZoomingStyle( aResMgr->integerValue( "3DViewer", "zooming_mode", aViewer->zoomingStyle() ) );
91   aViewer->enablePreselection( aResMgr->booleanValue( "OCCViewer", "enable_preselection", aViewer->isPreselectionEnabled() ) );
92   aViewer->enableSelection( aResMgr->booleanValue( "OCCViewer", "enable_selection", aViewer->isSelectionEnabled() ) );
93
94   myViewManager->setViewModel( aViewer );// custom view model, which extends SALOME_View interface
95   SUIT_ViewWindow* aViewWin = myViewManager->createViewWindow();
96   aViewer->setStaticTrihedronDisplayed( false );
97   Handle(AIS_Trihedron) aTrihedron = HYDROGUI_AISTrihedron::createTrihedron(
98                          aResMgr->doubleValue( "3DViewer", "trihedron_size",
99                                                aViewer->trihedronSize() ));
100   Handle(AIS_InteractiveContext) anAISContext = aViewer->getAISContext();
101   if ( !anAISContext.IsNull() ) {
102     anAISContext->Display( aTrihedron );
103     anAISContext->Deactivate( aTrihedron );
104   }
105
106   addWidget( aViewWin, 4 );
107   myEditorWidget->setOCCViewer( aViewer );
108
109   connect( myEditorWidget, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
110   connect( myEditorWidget, SIGNAL( subOperationStarted(QWidget*, bool) ), this, SLOT( processStartedSubOperation(QWidget*, bool) ) );
111   connect( myEditorWidget, SIGNAL( subOperationFinished(QWidget*) ), this, SLOT( processFinishedSubOperation(QWidget*) ) );
112
113   myAddElementBox->hide();
114
115   // Coordinates
116   connect( myViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
117            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
118   if ( aViewWin ) {
119     OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWin );
120     if ( aViewFrame && aViewFrame->getViewPort() ) {
121       aViewFrame->getViewPort()->installEventFilter( this );
122     }
123   }
124
125   myCoordLabel = new QLabel( this );
126   QHBoxLayout* aCoordLayout = new QHBoxLayout();
127   aCoordLayout->addWidget( myCoordLabel );
128   aCoordLayout->addStretch();
129
130   addLayout( aCoordLayout );
131 }
132
133 HYDROGUI_ProfileDlg::~HYDROGUI_ProfileDlg()
134 {
135   delete myViewManager;
136 }
137
138 void HYDROGUI_ProfileDlg::reset()
139 {
140   myEditorWidget->reset();
141   myEditorWidget->setActionMode( CurveCreator_Widget::AdditionMode );
142 }
143
144 Handle(AIS_InteractiveContext) HYDROGUI_ProfileDlg::getAISContext()
145 {
146   OCCViewer_Viewer* aViewer = (OCCViewer_Viewer*)myViewManager->getViewModel();
147   return aViewer ? aViewer->getAISContext() : 0;
148 }
149
150 void HYDROGUI_ProfileDlg::setProfileName( const QString& theName )
151 {
152   myName->setText(theName);
153 }
154
155 QString HYDROGUI_ProfileDlg::getProfileName() const
156 {
157   return myName->text();
158 }
159
160 void HYDROGUI_ProfileDlg::setProfile( CurveCreator_ICurve* theProfile )
161 {
162   myEditorWidget->setCurve( theProfile );
163
164   // select the single section by default
165   QList<int> aSections;
166   aSections << 0;
167   myEditorWidget->setSelectedSections( aSections );
168 }
169
170 QList<int> HYDROGUI_ProfileDlg::getSelectedSections()
171 {
172   return myEditorWidget->getSelectedSections();
173 }
174
175 /**
176  * Redirect the delete action to editor widget
177  */
178 void HYDROGUI_ProfileDlg::deleteSelected()
179 {
180   myEditorWidget->removeSelected();
181 }
182
183 /**
184  * Checks whether there are some to delete
185  */
186 bool HYDROGUI_ProfileDlg::deleteEnabled()
187 {
188   return myEditorWidget->removeEnabled();
189 }
190
191 void HYDROGUI_ProfileDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
192 {
193   myEditorWidget->setEnabled( false );
194
195   myAddElementBox->setTitle( theIsEdit ? tr( "EDIT_ELEMENT" ) : tr( "ADD_ELEMENT" ) );
196   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
197   anAddElementLayout->addWidget( theWidget );
198
199   theWidget->show();
200   myAddElementBox->show();
201 }
202
203 void HYDROGUI_ProfileDlg::processFinishedSubOperation( QWidget* theWidget )
204 {
205   myEditorWidget->setEnabled( true );
206
207   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
208   anAddElementLayout->removeWidget( theWidget );
209
210   theWidget->hide();
211   myAddElementBox->hide();
212 }
213
214 void HYDROGUI_ProfileDlg::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
215 {
216   OCCViewer_ViewWindow* anOCCViewWindow = 
217     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
218   if ( anOCCViewWindow && anOCCViewWindow->getViewPort() ) {
219     gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( 
220       theEvent->x(), theEvent->y(), anOCCViewWindow->getViewPort()->getView() );
221
222     // Show the coordinates
223     QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), true );
224     QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), true );
225     myCoordLabel->setText( tr("UZ_COORDINATES_INFO").arg( aX ).arg( anY ) );
226   }
227 }
228
229 bool HYDROGUI_ProfileDlg::eventFilter( QObject* theObj, QEvent* theEvent )
230 {
231   if ( theObj->inherits( "OCCViewer_ViewPort" ) )
232   {
233     if ( theEvent->type() == QEvent::Leave )
234       myCoordLabel->clear();
235
236     return false;
237   }
238
239   return HYDROGUI_InputPanel::eventFilter( theObj, theEvent );
240 }