Salome HOME
75eedd5f99064cedab65f0b38ec6e787f2a29fab
[modules/geom.git] / src / BuildGUI / BuildGUI_SolidDlg.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 #include <TColStd_MapOfInteger.hxx>
42
43 //=================================================================================
44 // class    : BuildGUI_SolidDlg()
45 // purpose  : Constructs a BuildGUI_SolidDlg which is a child of 'parent', with the
46 //            name 'name' and widget flags set to 'f'.
47 //            The dialog will by default be modeless, unless you set 'modal' to
48 //            TRUE to construct a modal dialog.
49 //=================================================================================
50 BuildGUI_SolidDlg::BuildGUI_SolidDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
51   : GEOMBase_Skeleton( theGeometryGUI, parent )
52 {
53   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BUILD_SOLID" ) ) );
54   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
55
56   setWindowTitle( tr( "GEOM_SOLID_TITLE" ) );
57
58   /***************************************************************/
59   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_SOLID" ) );
60   mainFrame()->RadioButton1->setIcon( image0 );
61   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton2->close();
63   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
64   mainFrame()->RadioButton3->close();
65
66   GroupSolid = new DlgRef_1Sel1Check( centralWidget() );
67
68   GroupSolid->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
69   GroupSolid->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
70   GroupSolid->CheckButton1->setText( tr( "GEOM_CREATE_SINGLE_SOLID" ) );
71   GroupSolid->PushButton1->setIcon( image1 );
72   GroupSolid->LineEdit1->setReadOnly( true );
73
74   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
75   layout->setMargin( 0 ); layout->setSpacing( 6 );
76   layout->addWidget( GroupSolid );
77   /***************************************************************/
78
79   setHelpFileName("create_solid_page.html");
80
81   /* Initialisations */
82   Init();
83 }
84
85
86 //=================================================================================
87 // function : ~BuildGUI_SolidDlg()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 BuildGUI_SolidDlg::~BuildGUI_SolidDlg()
91 {
92   // no need to delete child widgets, Qt does it all for us
93 }
94
95
96 //=================================================================================
97 // function : Init()
98 // purpose  :
99 //=================================================================================
100 void BuildGUI_SolidDlg::Init()
101 {
102   /* init variables */
103   myEditCurrentArgument = GroupSolid->LineEdit1;
104   GroupSolid->LineEdit1->setReadOnly( true );
105   GroupSolid->CheckButton1->setChecked( true );
106
107   myShells.clear();
108
109   //globalSelection( GEOM_SHELL );
110   TColStd_MapOfInteger aMap;
111   aMap.Add( GEOM_SHELL );
112   aMap.Add( GEOM_COMPOUNDFILTER );
113   QList<int> aSubShapes;
114   aSubShapes.append( GEOM_SHELL );
115   globalSelection( aMap, aSubShapes );
116
117   /* signals and slots connections */
118   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
119   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
120
121   connect( GroupSolid->PushButton1,  SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
122   connect( GroupSolid->CheckButton1, SIGNAL( toggled( bool ) ), this, SLOT( EnableNameField( bool ) ) );
123
124   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
125            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
126
127   initName( tr( "GEOM_SOLID" ) );
128   SelectionIntoArgument();
129 }
130
131
132 //=================================================================================
133 // function : ClickOnOk()
134 // purpose  :
135 //=================================================================================
136 void BuildGUI_SolidDlg::ClickOnOk()
137 {
138   setIsApplyAndClose( true );
139   if ( ClickOnApply() )
140     ClickOnCancel();
141 }
142
143 //=================================================================================
144 // function : ClickOnApply()
145 // purpose  :
146 //=================================================================================
147 bool BuildGUI_SolidDlg::ClickOnApply()
148 {
149   if ( !onAccept() )
150     return false;
151
152   initName();
153   return true;
154 }
155
156
157 //=================================================================================
158 // function : SelectionIntoArgument()
159 // purpose  : Called when selection as changed or other case
160 //=================================================================================
161 void BuildGUI_SolidDlg::SelectionIntoArgument()
162 {
163   myEditCurrentArgument->setText( "" );
164
165   //myShells = getSelected( TopAbs_SHELL, -1 );
166   QList<TopAbs_ShapeEnum> types;
167   types << TopAbs_SHELL << TopAbs_COMPOUND;
168   myShells = getSelected( types, -1 );
169
170   if ( !myShells.isEmpty() ) {
171     QString aName = myShells.count() > 1 ? QString( "%1_objects").arg( myShells.count() ) : GEOMBase::GetName( myShells[0].get() );
172     myEditCurrentArgument->setText( aName );
173   }
174 }
175
176 //=================================================================================
177 // function : SetEditCurrentArgument()
178 // purpose  :
179 //=================================================================================
180 void BuildGUI_SolidDlg::SetEditCurrentArgument()
181 {
182   QPushButton* send = (QPushButton*)sender();
183   if ( send != GroupSolid->PushButton1 )
184     return;
185
186   //globalSelection( GEOM_SHELL );
187   TColStd_MapOfInteger aMap;
188   aMap.Add( GEOM_SHELL );
189   aMap.Add( GEOM_COMPOUNDFILTER );
190   QList<int> aSubShapes;
191   aSubShapes.append( GEOM_SHELL );
192   globalSelection( aMap, aSubShapes );
193
194   myEditCurrentArgument = GroupSolid->LineEdit1;
195
196   myEditCurrentArgument->setFocus();
197   SelectionIntoArgument();
198 }
199
200
201 //=================================================================================
202 // function : ActivateThisDialog()
203 // purpose  :
204 //=================================================================================
205 void BuildGUI_SolidDlg::ActivateThisDialog()
206 {
207   GEOMBase_Skeleton::ActivateThisDialog();
208   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
209            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
210
211   //globalSelection( GEOM_SHELL );
212   TColStd_MapOfInteger aMap;
213   aMap.Add( GEOM_SHELL );
214   aMap.Add( GEOM_COMPOUNDFILTER );
215   QList<int> aSubShapes;
216   aSubShapes.append( GEOM_SHELL );
217   globalSelection( aMap, aSubShapes );
218 }
219
220
221 //=================================================================================
222 // function : enterEvent()
223 // purpose  :
224 //=================================================================================
225 void BuildGUI_SolidDlg::enterEvent( QEvent* )
226 {
227   if ( !mainFrame()->GroupConstructors->isEnabled() )
228     ActivateThisDialog();
229 }
230
231
232 //=================================================================================
233 // function :  EnableNameField()
234 // purpose  :
235 //=================================================================================
236 void  BuildGUI_SolidDlg::EnableNameField( bool toEnable )
237 {
238   mainFrame()->GroupBoxName->setEnabled( toEnable );
239 }
240
241 //=================================================================================
242 // function : createOperation
243 // purpose  :
244 //=================================================================================
245 GEOM::GEOM_IOperations_ptr BuildGUI_SolidDlg::createOperation()
246 {
247   return getGeomEngine()->GetIShapesOperations( getStudyId() );
248 }
249
250 //=================================================================================
251 // function : isValid
252 // purpose  :
253 //=================================================================================
254 bool BuildGUI_SolidDlg::isValid (QString& msg)
255 {
256   bool ok = !myShells.isEmpty();
257
258   GEOM::MeasureOpPtr anOp;
259   anOp.take(myGeomGUI->GetGeomGen()->GetIMeasureOperations(getStudyId()));
260
261   if (!GroupSolid->CheckButton1->isChecked() || myShells.count() == 1) {
262     for (int i = 0, n = myShells.count(); i < n && ok; i++) {
263       CORBA::String_var aRes = anOp->IsGoodForSolid(myShells[i].get());
264       if (strlen(aRes.in())) {
265         msg = QObject::tr(aRes.in()).arg(GEOMBase::GetName(myShells[i].get()));
266         ok = false;
267       }
268     }
269   }
270   return ok;
271 }
272
273 /*
274 //=================================================================================
275 // function : isClosed
276 // purpose  : Check the object 'i' in myShells list is closed or unclosed
277 //=================================================================================
278 bool BuildGUI_SolidDlg::isClosed( GEOM::GEOM_Object_ptr shell )
279 {
280   bool ok = false;
281
282   GEOM::GEOM_IKindOfShape::shape_kind aKind;
283   GEOM::ListOfLong_var anInts;
284   GEOM::ListOfDouble_var aDbls;
285
286   if ( !CORBA::is_nil( shell ) ) {
287     GEOM::MeasureOpPtr anOp;
288     anOp.take( myGeomGUI->GetGeomGen()->GetIMeasureOperations( getStudyId() ) );
289
290     // Detect kind of shape and parameters
291     aKind = anOp->KindOfShape(shell, anInts, aDbls);
292
293     if ( anOp->IsDone() ) {
294       if ( anInts[0] == 1 )
295         ok = true;
296       else if ( anInts[0] == 2 )
297         ok = false;
298     }
299     else {
300       MESSAGE ("KindOfShape Operation is NOT DONE!!!");
301     }
302   }
303   else {
304     MESSAGE ("Shape is NULL!!!");
305   }
306
307   return ok;
308 }
309 */
310
311 //=================================================================================
312 // function : execute
313 // purpose  :
314 //=================================================================================
315 bool BuildGUI_SolidDlg::execute( ObjectList& objects )
316 {
317   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
318
319   if ( GroupSolid->CheckButton1->isChecked() ) {
320     GEOM::ListOfGO_var objlist = new GEOM::ListOfGO();
321     objlist->length( myShells.count() );
322     for ( int i = 0; i < myShells.count(); i++ )
323       objlist[i] = myShells[i].copy();
324
325     GEOM::GEOM_Object_var anObj = anOper->MakeSolidShells( objlist.in() );
326
327     if ( !anObj->_is_nil() )
328       objects.push_back( anObj._retn() );
329   }
330   else {
331     for ( int i = 0, n = myShells.count(); i< n; i++ ) {
332       GEOM::GEOM_Object_var anObj = anOper->MakeSolidShell( myShells[ i ].get() );
333
334      if ( !anObj->_is_nil() )
335        objects.push_back( anObj._retn() );
336     }
337   }
338
339   return true;
340 }