Salome HOME
Fix for the bug #45: check and warning when the same image is used in 2 arguments.
[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
28 #include <CurveCreator_Widget.h>
29 #include <CurveCreator_ICurve.hxx>
30
31 #include <OCCViewer_ViewPort3d.h>
32 #include <OCCViewer_Utilities.h>
33 #include <OCCViewer_ViewManager.h>
34 #include <OCCViewer_ViewFrame.h>
35 #include <LightApp_Application.h>
36
37 #include <SUIT_Session.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <GEOMUtils.hxx>
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   myEditorWidget = new CurveCreator_Widget( this, NULL, anActionFlags );
62   addWidget( myEditorWidget, 3 );
63
64   myAddElementBox = new QGroupBox( tr( "ADD_ELEMENT" ), this );
65   addWidget( myAddElementBox, 2 );
66
67   QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
68   anAddElementLayout->setMargin( 0 );
69   anAddElementLayout->setSpacing( 5 );
70
71   myViewManager = new OCCViewer_ViewManager( theModule->getApp()->activeStudy(), 0 );
72   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
73
74   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
75   aViewer->setBackground( OCCViewer_ViewFrame::TOP_LEFT,
76                      aResMgr->backgroundValue( "OCCViewer", "xz_background", aViewer->background(OCCViewer_ViewFrame::TOP_LEFT) ) );
77   aViewer->setBackground( OCCViewer_ViewFrame::TOP_RIGHT,
78                      aResMgr->backgroundValue( "OCCViewer", "yz_background", aViewer->background(OCCViewer_ViewFrame::TOP_RIGHT) ) );
79   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_LEFT,
80                      aResMgr->backgroundValue( "OCCViewer", "xy_background", aViewer->background(OCCViewer_ViewFrame::BOTTOM_LEFT) ) );
81   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_RIGHT,
82                      aResMgr->backgroundValue( "OCCViewer", "background", aViewer->background(OCCViewer_ViewFrame::MAIN_VIEW) ) );
83
84   aViewer->setTrihedronSize( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ),
85                         aResMgr->booleanValue( "3DViewer", "relative_size", aViewer->trihedronRelative() ));
86   aViewer->setInteractionStyle( aResMgr->integerValue( "3DViewer", "navigation_mode", aViewer->interactionStyle() ) );
87   aViewer->setZoomingStyle( aResMgr->integerValue( "3DViewer", "zooming_mode", aViewer->zoomingStyle() ) );
88   aViewer->enablePreselection( aResMgr->booleanValue( "OCCViewer", "enable_preselection", aViewer->isPreselectionEnabled() ) );
89   aViewer->enableSelection( aResMgr->booleanValue( "OCCViewer", "enable_selection", aViewer->isSelectionEnabled() ) );
90
91   myViewManager->setViewModel( aViewer );// custom view model, which extends SALOME_View interface
92   SUIT_ViewWindow* aViewWin = myViewManager->createViewWindow();
93   addWidget( aViewWin, 4 );
94   myEditorWidget->setOCCViewer( aViewer );
95
96   connect( myEditorWidget, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
97   connect( myEditorWidget, SIGNAL( subOperationStarted(QWidget*) ), this, SLOT( processStartedSubOperation(QWidget*) ) );
98   connect( myEditorWidget, SIGNAL( subOperationFinished(QWidget*) ), this, SLOT( processFinishedSubOperation(QWidget*) ) );
99
100   myAddElementBox->hide();
101
102   // Coordinates
103   connect( myViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
104            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
105   if ( aViewWin ) {
106     OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWin );
107     if ( aViewFrame && aViewFrame->getViewPort() ) {
108       aViewFrame->getViewPort()->installEventFilter( this );
109     }
110   }
111
112   myCoordLabel = new QLabel( this );
113   QHBoxLayout* aCoordLayout = new QHBoxLayout();
114   aCoordLayout->addWidget( myCoordLabel );
115   aCoordLayout->addStretch();
116
117   addLayout( aCoordLayout );
118 }
119
120 HYDROGUI_ProfileDlg::~HYDROGUI_ProfileDlg()
121 {
122   delete myViewManager;
123 }
124
125 void HYDROGUI_ProfileDlg::reset()
126 {
127   myEditorWidget->reset();
128 }
129
130 Handle(AIS_InteractiveContext) HYDROGUI_ProfileDlg::getAISContext()
131 {
132   OCCViewer_Viewer* aViewer = (OCCViewer_Viewer*)myViewManager->getViewModel();
133   return aViewer ? aViewer->getAISContext() : 0;
134 }
135
136 void HYDROGUI_ProfileDlg::setProfileName( const QString& theName )
137 {
138   myName->setText(theName);
139 }
140
141 QString HYDROGUI_ProfileDlg::getProfileName() const
142 {
143   return myName->text();
144 }
145
146 void HYDROGUI_ProfileDlg::setProfile( CurveCreator_ICurve* theProfile )
147 {
148   myEditorWidget->setCurve( theProfile );
149 }
150
151 QList<int> HYDROGUI_ProfileDlg::getSelectedSections()
152 {
153   return myEditorWidget->getSelectedSections();
154 }
155
156 QList< QPair< int, int > > HYDROGUI_ProfileDlg::getSelectedPoints()
157 {
158   return myEditorWidget->getSelectedPoints();
159 }
160
161 /**
162  * Redirect the delete action to editor widget
163  */
164 void HYDROGUI_ProfileDlg::deleteSelected()
165 {
166   myEditorWidget->removeSelected();
167 }
168
169 /**
170  * Checks whether there are some to delete
171  */
172 bool HYDROGUI_ProfileDlg::deleteEnabled()
173 {
174   return myEditorWidget->removeEnabled();
175 }
176
177 void HYDROGUI_ProfileDlg::processStartedSubOperation( QWidget* theWidget )
178 {
179   myEditorWidget->setEnabled( false );
180
181   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
182   anAddElementLayout->addWidget( theWidget );
183
184   theWidget->show();
185   myAddElementBox->show();
186 }
187
188 void HYDROGUI_ProfileDlg::processFinishedSubOperation( QWidget* theWidget )
189 {
190   myEditorWidget->setEnabled( true );
191
192   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
193   anAddElementLayout->removeWidget( theWidget );
194
195   theWidget->hide();
196   myAddElementBox->hide();
197 }
198
199 void HYDROGUI_ProfileDlg::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
200 {
201   OCCViewer_ViewWindow* anOCCViewWindow = 
202     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
203   if ( anOCCViewWindow && anOCCViewWindow->getViewPort() ) {
204     gp_Pnt aPnt = GEOMUtils::ConvertClickToPoint( 
205       theEvent->x(), theEvent->y(), anOCCViewWindow->getViewPort()->getView() );
206
207     // Show the coordinates
208     QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X() );
209     QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y() );
210     myCoordLabel->setText( tr("COORDINATES_INFO").arg( aX ).arg( anY ) );
211   }
212 }
213
214 bool HYDROGUI_ProfileDlg::eventFilter( QObject* theObj, QEvent* theEvent )
215 {
216   if ( theObj->inherits( "OCCViewer_ViewPort" ) && 
217        theEvent->type() == QEvent::Leave ) {
218     myCoordLabel->clear();
219   }
220
221   return HYDROGUI_InputPanel::eventFilter( theObj, theEvent );
222 }