Salome HOME
Bug 158: Crash in profile edition
[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 <CurveCreator_Widget.h>
27 #include <CurveCreator_ICurve.hxx>
28
29 #include <OCCViewer_ViewPort3d.h>
30 #include <OCCViewer_Utilities.h>
31 #include <OCCViewer_ViewManager.h>
32 #include <OCCViewer_ViewFrame.h>
33 #include <LightApp_Application.h>
34
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37
38 #include <QGroupBox>
39 #include <QHBoxLayout>
40 #include <QLabel>
41 #include <QLineEdit>
42
43 HYDROGUI_ProfileDlg::HYDROGUI_ProfileDlg( HYDROGUI_Module* theModule, const QString& theTitle )
44 : HYDROGUI_InputPanel( theModule, theTitle ), myName(NULL)
45 {
46   QHBoxLayout* aNameLayout = new QHBoxLayout();
47   QLabel* aNameLabel = new QLabel(tr("PROFILE_NAME_TLT"), this);
48   aNameLayout->addWidget(aNameLabel);
49   myName = new QLineEdit(this);
50   aNameLayout->addWidget(myName);
51
52   addLayout(aNameLayout);
53
54   int anActionFlags = 
55     CurveCreator_Widget::DisableNewSection | CurveCreator_Widget::DisableDetectionMode;
56   myEditorWidget = new CurveCreator_Widget( this, NULL, anActionFlags );
57   addWidget( myEditorWidget, 3 );
58
59   myAddElementBox = new QGroupBox( tr( "ADD_ELEMENT" ), this );
60   addWidget( myAddElementBox, 2 );
61
62   QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
63   anAddElementLayout->setMargin( 0 );
64   anAddElementLayout->setSpacing( 5 );
65
66   myViewManager = new OCCViewer_ViewManager( theModule->getApp()->activeStudy(), 0 );
67   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
68
69   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
70   aViewer->setBackground( OCCViewer_ViewFrame::TOP_LEFT,
71                      aResMgr->backgroundValue( "OCCViewer", "xz_background", aViewer->background(OCCViewer_ViewFrame::TOP_LEFT) ) );
72   aViewer->setBackground( OCCViewer_ViewFrame::TOP_RIGHT,
73                      aResMgr->backgroundValue( "OCCViewer", "yz_background", aViewer->background(OCCViewer_ViewFrame::TOP_RIGHT) ) );
74   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_LEFT,
75                      aResMgr->backgroundValue( "OCCViewer", "xy_background", aViewer->background(OCCViewer_ViewFrame::BOTTOM_LEFT) ) );
76   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_RIGHT,
77                      aResMgr->backgroundValue( "OCCViewer", "background", aViewer->background(OCCViewer_ViewFrame::MAIN_VIEW) ) );
78
79   aViewer->setTrihedronSize( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ),
80                         aResMgr->booleanValue( "3DViewer", "relative_size", aViewer->trihedronRelative() ));
81   aViewer->setInteractionStyle( aResMgr->integerValue( "3DViewer", "navigation_mode", aViewer->interactionStyle() ) );
82   aViewer->setZoomingStyle( aResMgr->integerValue( "3DViewer", "zooming_mode", aViewer->zoomingStyle() ) );
83   aViewer->enablePreselection( aResMgr->booleanValue( "OCCViewer", "enable_preselection", aViewer->isPreselectionEnabled() ) );
84   aViewer->enableSelection( aResMgr->booleanValue( "OCCViewer", "enable_selection", aViewer->isSelectionEnabled() ) );
85
86   myViewManager->setViewModel( aViewer );// custom view model, which extends SALOME_View interface
87   SUIT_ViewWindow* aViewWin = myViewManager->createViewWindow();
88   addWidget( aViewWin, 4 );
89   myEditorWidget->setOCCViewer( aViewer );
90
91   connect( myEditorWidget, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
92   connect( myEditorWidget, SIGNAL( subOperationStarted(QWidget*) ), this, SLOT( processStartedSubOperation(QWidget*) ) );
93   connect( myEditorWidget, SIGNAL( subOperationFinished(QWidget*) ), this, SLOT( processFinishedSubOperation(QWidget*) ) );
94
95   myAddElementBox->hide();
96 }
97
98 HYDROGUI_ProfileDlg::~HYDROGUI_ProfileDlg()
99 {
100   delete myViewManager;
101 }
102
103 void HYDROGUI_ProfileDlg::reset()
104 {
105   myEditorWidget->reset();
106 }
107
108 Handle(AIS_InteractiveContext) HYDROGUI_ProfileDlg::getAISContext()
109 {
110   OCCViewer_Viewer* aViewer = (OCCViewer_Viewer*)myViewManager->getViewModel();
111   return aViewer ? aViewer->getAISContext() : 0;
112 }
113
114 void HYDROGUI_ProfileDlg::setProfileName( const QString& theName )
115 {
116   myName->setText(theName);
117 }
118
119 QString HYDROGUI_ProfileDlg::getProfileName() const
120 {
121   return myName->text();
122 }
123
124 void HYDROGUI_ProfileDlg::setProfile( CurveCreator_ICurve* theProfile )
125 {
126   myEditorWidget->setCurve( theProfile );
127 }
128
129 QList<int> HYDROGUI_ProfileDlg::getSelectedSections()
130 {
131   return myEditorWidget->getSelectedSections();
132 }
133
134 QList< QPair< int, int > > HYDROGUI_ProfileDlg::getSelectedPoints()
135 {
136   return myEditorWidget->getSelectedPoints();
137 }
138
139 /**
140  * Redirect the delete action to editor widget
141  */
142 void HYDROGUI_ProfileDlg::deleteSelected()
143 {
144   myEditorWidget->removeSelected();
145 }
146
147 /**
148  * Checks whether there are some to delete
149  */
150 bool HYDROGUI_ProfileDlg::deleteEnabled()
151 {
152   return myEditorWidget->removeEnabled();
153 }
154
155 void HYDROGUI_ProfileDlg::processStartedSubOperation( QWidget* theWidget )
156 {
157   myEditorWidget->setEnabled( false );
158
159   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
160   anAddElementLayout->addWidget( theWidget );
161
162   theWidget->show();
163   myAddElementBox->show();
164 }
165
166 void HYDROGUI_ProfileDlg::processFinishedSubOperation( QWidget* theWidget )
167 {
168   myEditorWidget->setEnabled( true );
169
170   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
171   anAddElementLayout->removeWidget( theWidget );
172
173   theWidget->hide();
174   myAddElementBox->hide();
175 }