Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / BuildGUI / BuildGUI_SolidDlg.cxx
1 //  Copyright (C) 2007-2010  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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BuildGUI_SolidDlg.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26 //
27 #include "BuildGUI_SolidDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <GEOMImpl_Types.hxx>
34
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_Session.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40
41 //=================================================================================
42 // class    : BuildGUI_SolidDlg()
43 // purpose  : Constructs a BuildGUI_SolidDlg 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_SolidDlg::BuildGUI_SolidDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
49   : GEOMBase_Skeleton( theGeometryGUI, parent )
50 {
51   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BUILD_SOLID" ) ) );
52   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
53
54   setWindowTitle( tr( "GEOM_SOLID_TITLE" ) );
55     
56   /***************************************************************/
57   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SOLID" ) );
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   GroupSolid = new DlgRef_1Sel1Check( centralWidget() );
65
66   GroupSolid->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
67   GroupSolid->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
68   GroupSolid->CheckButton1->setText( tr( "GEOM_CREATE_SINGLE_SOLID" ) );
69   GroupSolid->PushButton1->setIcon( image1 );
70   GroupSolid->LineEdit1->setReadOnly( true );
71   
72   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
73   layout->setMargin( 0 ); layout->setSpacing( 6 );
74   layout->addWidget( GroupSolid );
75   /***************************************************************/
76
77   setHelpFileName("create_solid_page.html");
78
79   /* Initialisations */
80   Init();
81 }
82
83
84 //=================================================================================
85 // function : ~BuildGUI_SolidDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 BuildGUI_SolidDlg::~BuildGUI_SolidDlg()
89 {
90   // no need to delete child widgets, Qt does it all for us
91 }
92
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void BuildGUI_SolidDlg::Init()
99 {
100   /* init variables */
101   myEditCurrentArgument = GroupSolid->LineEdit1;
102   GroupSolid->LineEdit1->setReadOnly( true );
103   GroupSolid->CheckButton1->setChecked( true );
104
105   myOkShells = false;
106
107   globalSelection( GEOM_SHELL );
108
109   /* signals and slots connections */
110   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
111   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
112
113   connect( GroupSolid->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
114   connect( GroupSolid->CheckButton1, SIGNAL( toggled( bool ) ), this, SLOT( EnableNameField( bool ) ) );
115
116   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
117            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
118
119   initName( tr( "GEOM_SOLID" ) );
120 }
121
122
123 //=================================================================================
124 // function : ClickOnOk()
125 // purpose  :
126 //=================================================================================
127 void BuildGUI_SolidDlg::ClickOnOk()
128 {
129   if ( ClickOnApply() )
130     ClickOnCancel();
131 }
132
133 //=================================================================================
134 // function : ClickOnApply()
135 // purpose  :
136 //=================================================================================
137 bool BuildGUI_SolidDlg::ClickOnApply()
138 {
139   for ( int i = 0, n = myShells.length(); i< n; i++ ) {
140     if ( !isClosed(i)) {
141       QString aName = GEOMBase::GetName( myShells[i] );
142       SUIT_MessageBox::warning( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
143                                  QObject::tr("WRN_WARNING"),
144                                  QObject::tr("WRN_SHAPE_UNCLOSED").arg(aName) ,
145                                  QObject::tr("BUT_OK") );
146        return false;
147     }
148   }
149
150   if ( !onAccept() )
151     return false;
152
153   initName();
154   return true;
155 }
156
157
158 //=================================================================================
159 // function : SelectionIntoArgument()
160 // purpose  : Called when selection as changed or other case
161 //=================================================================================
162 void BuildGUI_SolidDlg::SelectionIntoArgument()
163 {
164   myEditCurrentArgument->setText( "" );
165   QString aString = "";
166   
167   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
168   SALOME_ListIO aSelList;
169   aSelMgr->selectedObjects(aSelList);
170
171   myOkShells = false;
172   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString);
173   if ( nbSel == 0 )
174     return;
175   if ( nbSel != 1 )
176     aString = tr( "%1_objects" ).arg( nbSel );
177   
178   GEOMBase::ConvertListOfIOInListOfGO(aSelList, myShells);
179   if ( !myShells.length() )
180     return;
181   
182   myEditCurrentArgument->setText( aString );
183   myOkShells = true;
184 }
185
186 //=================================================================================
187 // function : SetEditCurrentArgument()
188 // purpose  :
189 //=================================================================================
190 void BuildGUI_SolidDlg::SetEditCurrentArgument()
191 {
192   QPushButton* send = (QPushButton*)sender();
193   if ( send != GroupSolid->PushButton1 )
194     return;
195   
196   globalSelection( GEOM_SHELL );
197   myEditCurrentArgument = GroupSolid->LineEdit1;
198
199   myEditCurrentArgument->setFocus();
200   SelectionIntoArgument();
201 }
202
203
204 //=================================================================================
205 // function : ActivateThisDialog()
206 // purpose  :
207 //=================================================================================
208 void BuildGUI_SolidDlg::ActivateThisDialog()
209 {
210   GEOMBase_Skeleton::ActivateThisDialog();
211   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
212            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
213   globalSelection( GEOM_SHELL );
214 }
215
216
217 //=================================================================================
218 // function : enterEvent()
219 // purpose  :
220 //=================================================================================
221 void BuildGUI_SolidDlg::enterEvent( QEvent* )
222 {
223   if ( !mainFrame()->GroupConstructors->isEnabled() )
224     ActivateThisDialog();
225 }
226
227
228 //=================================================================================
229 // function :  EnableNameField()
230 // purpose  :
231 //=================================================================================
232 void  BuildGUI_SolidDlg::EnableNameField( bool toEnable )
233 {
234   mainFrame()->GroupBoxName->setEnabled( toEnable ); 
235 }
236
237 //=================================================================================
238 // function : createOperation
239 // purpose  :
240 //=================================================================================
241 GEOM::GEOM_IOperations_ptr BuildGUI_SolidDlg::createOperation()
242 {
243   return getGeomEngine()->GetIShapesOperations( getStudyId() );
244 }
245
246 //=================================================================================
247 // function : isValid
248 // purpose  :
249 //=================================================================================
250 bool BuildGUI_SolidDlg::isValid( QString& )
251 {
252   return myOkShells;
253 }
254
255 //=================================================================================
256 // function : isClosed
257 // purpose  : Check the object 'i' in myShells list is closed or unclosed
258 //=================================================================================
259 bool BuildGUI_SolidDlg::isClosed(int i)
260 {
261   GEOM::GEOM_Object_var aShape = myShells[i];
262   GEOM::GEOM_IKindOfShape::shape_kind aKind;
263   GEOM::ListOfLong_var anInts;
264   GEOM::ListOfDouble_var aDbls;
265
266   if (aShape->_is_nil()) {
267     MESSAGE ("Shape is NULL!!!");
268     return false;
269   }
270
271   GEOM::GEOM_IMeasureOperations_var anOp = myGeomGUI->GetGeomGen()->GetIMeasureOperations( getStudyId() );
272
273   // Detect kind of shape and parameters
274   aKind = anOp->KindOfShape(aShape, anInts, aDbls);
275
276   if ( !anOp->IsDone() ) {
277     MESSAGE ("KindOfShape Operation is NOT DONE!!!");
278     return false;
279   }
280   
281   if ( anInts[0] == 1 )
282     return true;
283   else if ( anInts[0] == 2 )
284     return false;
285  
286   return false;
287 }
288
289 //=================================================================================
290 // function : execute
291 // purpose  :
292 //=================================================================================
293 bool BuildGUI_SolidDlg::execute( ObjectList& objects )
294 {
295   bool toCreateSingleSolid = GroupSolid->CheckButton1->isChecked();
296   
297   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
298     
299   if ( toCreateSingleSolid ) {
300     GEOM::GEOM_Object_var anObj = anOper->MakeSolidShells( myShells );
301
302     if ( !anObj->_is_nil() )
303       objects.push_back( anObj._retn() );
304   }
305   else {
306     for ( int i = 0, n = myShells.length(); i< n; i++ ) {
307       GEOM::GEOM_Object_var anObj = anOper->MakeSolidShell( myShells[ i ] );
308
309      if ( !anObj->_is_nil() )
310        objects.push_back( anObj._retn() );
311     }
312   }
313
314   return true;
315 }
316