Salome HOME
Fix memory leaks
[modules/geom.git] / src / BuildGUI / BuildGUI_ShellDlg.cxx
1 //  Copyright (C) 2007-2008  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : BuildGUI_ShellDlg.cxx
24 // Author : Damien COQUERET, Open CASCADE S.A.S.
25 //
26 #include "BuildGUI_ShellDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <GEOMImpl_Types.hxx>
33
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
36 #include <SalomeApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38
39 #include <TColStd_MapOfInteger.hxx>
40
41 //=================================================================================
42 // class    : BuildGUI_ShellDlg()
43 // purpose  : Constructs a BuildGUI_ShellDlg which is a child of 'parent', with the 
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 BuildGUI_ShellDlg::BuildGUI_ShellDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
49   : GEOMBase_Skeleton( theGeometryGUI, parent )
50 {
51   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BUILD_SHELL" ) ) );
52   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
53
54   setWindowTitle( tr( "GEOM_SHELL_TITLE" ) );
55     
56   /***************************************************************/
57   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SHELL" ) );
58   mainFrame()->RadioButton1->setIcon( image0 );
59   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
60   mainFrame()->RadioButton2->close();
61   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton3->close();
63
64   GroupShell = new DlgRef_1Sel( centralWidget() );
65
66   GroupShell->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
67   GroupShell->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
68   GroupShell->PushButton1->setIcon( image1 );
69   GroupShell->LineEdit1->setReadOnly( true );
70   
71   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
72   layout->setMargin( 0 ); layout->setSpacing( 6 );
73   layout->addWidget( GroupShell );
74   /***************************************************************/
75
76   setHelpFileName("create_shell_page.html");
77
78   /* Initialisations */
79   Init();
80 }
81
82
83 //=================================================================================
84 // function : ~BuildGUI_ShellDlg()
85 // purpose  : Destroys the object and frees any allocated resources
86 //=================================================================================
87 BuildGUI_ShellDlg::~BuildGUI_ShellDlg()
88 {
89     // no need to delete child widgets, Qt does it all for us
90 }
91
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void BuildGUI_ShellDlg::Init()
98 {
99   /* init variables */
100   myEditCurrentArgument = GroupShell->LineEdit1;
101   GroupShell->LineEdit1->setReadOnly( true );
102   
103   myOkFacesAndShells = false;
104   
105   TColStd_MapOfInteger aMap;
106   aMap.Add( GEOM_SHELL );
107   aMap.Add( GEOM_FACE );
108   aMap.Add( GEOM_COMPOUNDFILTER );
109   
110   QList<int> aSubShapes;
111   aSubShapes.append( GEOM_FACE );
112   globalSelection( aMap, aSubShapes );
113   
114
115   /* signals and slots connections */
116   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
117   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
118
119   connect( GroupShell->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
120
121   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
122            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
123
124   initName( tr( "GEOM_SHELL" ) );
125 }
126
127
128 //=================================================================================
129 // function : ClickOnOk()
130 // purpose  :
131 //=================================================================================
132 void BuildGUI_ShellDlg::ClickOnOk()
133 {
134   if ( ClickOnApply() )
135     ClickOnCancel();
136 }
137
138
139 //=================================================================================
140 // function : ClickOnApply()
141 // purpose  :
142 //=================================================================================
143 bool BuildGUI_ShellDlg::ClickOnApply()
144 {
145   if ( !onAccept() )
146     return false;
147
148   initName();
149   return true;
150 }
151
152
153 //=================================================================================
154 // function : SelectionIntoArgument()
155 // purpose  : Called when selection as changed or other case
156 //=================================================================================
157 void BuildGUI_ShellDlg::SelectionIntoArgument()
158 {
159   myEditCurrentArgument->setText( "" );
160   QString aString;
161
162   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
163   SALOME_ListIO aSelList;
164   aSelMgr->selectedObjects(aSelList);
165
166   myOkFacesAndShells = false;
167   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
168   if ( nbSel == 0 )
169     return;
170   if ( nbSel != 1 )
171     aString = QString( "%1_objects ").arg( nbSel );
172
173   GEOMBase::ConvertListOfIOInListOfGO(aSelList, myFacesAndShells, true);
174   if ( !myFacesAndShells.length() )
175     return;
176
177   myEditCurrentArgument->setText( aString );
178   myOkFacesAndShells = true;
179 }
180
181
182 //=================================================================================
183 // function : SetEditCurrentArgument()
184 // purpose  :
185 //=================================================================================
186 void BuildGUI_ShellDlg::SetEditCurrentArgument()
187 {
188   QPushButton* send = (QPushButton*)sender();
189   if ( send != GroupShell->PushButton1 )
190     return;
191
192   TColStd_MapOfInteger aMap;
193   aMap.Add( GEOM_SHELL );
194   aMap.Add( GEOM_FACE );
195   aMap.Add( GEOM_COMPOUNDFILTER );
196
197   QList<int> aSubShapes;
198   aSubShapes.append( GEOM_FACE );
199   globalSelection( aMap, aSubShapes );
200   
201   myEditCurrentArgument = GroupShell->LineEdit1;
202
203   myEditCurrentArgument->setFocus();
204   SelectionIntoArgument();
205 }
206
207
208 //=================================================================================
209 // function : ActivateThisDialog()
210 // purpose  :
211 //=================================================================================
212 void BuildGUI_ShellDlg::ActivateThisDialog()
213 {
214   GEOMBase_Skeleton::ActivateThisDialog();
215   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
216            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
217   TColStd_MapOfInteger aMap;
218   aMap.Add( GEOM_SHELL );
219   aMap.Add( GEOM_FACE );
220   aMap.Add( GEOM_COMPOUNDFILTER );
221   
222   QList<int> aSubShapes;
223   aSubShapes.append( GEOM_FACE );
224   globalSelection( aMap, aSubShapes );
225 }
226
227
228 //=================================================================================
229 // function : enterEvent()
230 // purpose  :
231 //=================================================================================
232 void BuildGUI_ShellDlg::enterEvent( QEvent* )
233 {
234   if ( !mainFrame()->GroupConstructors->isEnabled() )
235     ActivateThisDialog();
236 }
237
238 //=================================================================================
239 // function : createOperation
240 // purpose  :
241 //=================================================================================
242 GEOM::GEOM_IOperations_ptr BuildGUI_ShellDlg::createOperation()
243 {
244   return getGeomEngine()->GetIShapesOperations( getStudyId() );
245 }
246
247 //=================================================================================
248 // function : isValid
249 // purpose  :
250 //=================================================================================
251 bool BuildGUI_ShellDlg::isValid( QString& )
252 {
253   return myOkFacesAndShells;
254 }
255
256 //=================================================================================
257 // function : execute
258 // purpose  :
259 //=================================================================================
260 bool BuildGUI_ShellDlg::execute( ObjectList& objects )
261 {
262   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
263   GEOM::GEOM_Object_var anObj = anOper->MakeShell( myFacesAndShells );
264
265   if ( !anObj->_is_nil() )
266     objects.push_back( anObj._retn() );
267
268   return true;
269 }
270