Salome HOME
To implement issue 0019962: MakePipeBiNormalAlongAxis implementation.
[modules/geom.git] / src / BlocksGUI / BlocksGUI_ExplodeDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  CEA
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 //
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 // File   : BlocksGUI_ExplodeDlg.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
23 //
24
25 #include "BlocksGUI_ExplodeDlg.h"
26
27 #include <DlgRef.h>
28 #include <GeometryGUI.h>
29 #include <GEOMBase.h>
30
31 #include <SUIT_Session.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_ViewWindow.h>
35 #include <SUIT_ViewManager.h>
36 #include <SUIT_MessageBox.h>
37 #include <SalomeApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39 #include <OCCViewer_ViewModel.h>
40 #include <SALOME_ListIteratorOfListIO.hxx>
41
42 //=================================================================================
43 // class    : BlocksGUI_ExplodeDlg()
44 // purpose  : Constructs a BlocksGUI_ExplodeDlg which is a child of 'parent'.
45 //=================================================================================
46 BlocksGUI_ExplodeDlg::BlocksGUI_ExplodeDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
47   : GEOMBase_Skeleton( theGeometryGUI, parent )
48 {
49   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BLOCK_EXPLODE" ) ) );
50   QPixmap imageS( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
51
52   setWindowTitle( tr( "GEOM_BLOCK_EXPLODE_TITLE" ) );
53
54   /***************************************************************/
55   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_BLOCK_EXPLODE" ) );
56
57   mainFrame()->RadioButton1->setIcon( image1 );
58   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
59   mainFrame()->RadioButton2->close();
60   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->RadioButton3->close();
62
63   // Create first group
64   myGrp1 = new DlgRef_1Sel2Spin1View1Check( centralWidget() );
65   myGrp1->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
66   myGrp1->TextLabel1->setText( tr( "GEOM_MAIN_OBJECT" ) );
67   myGrp1->PushButton1->setIcon( imageS );
68   myGrp1->LineEdit1->setReadOnly( true );
69   myGrp1->TextLabel2->setText( tr( "NB_FACES_MIN" ) );
70   myGrp1->TextLabel3->setText( tr( "NB_FACES_MAX" ) );
71   myGrp1->CheckBox1->setText( tr( "GEOM_SUBSHAPE_SELECT" ) );
72
73   // Add groups to layout
74   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
75   layout->setMargin( 0 ); layout->setSpacing( 6 );
76   layout->addWidget( myGrp1 );
77   /***************************************************************/
78
79   setHelpFileName( "explode_on_blocks_operation_page.html" );
80
81   Init();
82 }
83
84 //=================================================================================
85 // function : ~BlocksGUI_ExplodeDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 BlocksGUI_ExplodeDlg::~BlocksGUI_ExplodeDlg()
89 {
90   // no need to delete child widgets, Qt does it all for us
91   clearTemporary();
92 }
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void BlocksGUI_ExplodeDlg::Init()
99 {
100   mainFrame()->GroupBoxName->hide();
101
102   // Set range of spinboxes
103   int SpecificStep = 1;
104   initSpinBox( myGrp1->SpinBox1, 0, 999, SpecificStep );
105   initSpinBox( myGrp1->SpinBox2, 0, 999, SpecificStep );
106
107   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
108        != OCCViewer_Viewer::Type() )
109     myGrp1->CheckBox1->setEnabled( false );
110
111   // signals and slots connections
112   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
113   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
114
115   connect( myGrp1->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
116
117   connect( myGrp1->SpinBox1, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
118   connect( myGrp1->SpinBox2, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
119
120   connect( myGrp1->CheckBox1, SIGNAL( stateChanged( int ) ), this, SLOT( SubShapeToggled() ) );
121
122   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
123            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
124
125   myConstructorId = -1;
126   ConstructorsClicked( 0 );
127 }
128
129 //=================================================================================
130 // function : ConstructorsClicked()
131 // purpose  : Radio button management
132 //=================================================================================
133 void BlocksGUI_ExplodeDlg::ConstructorsClicked( int constructorId )
134 {
135   if ( myConstructorId == constructorId )
136     return;
137
138   myConstructorId = constructorId;
139
140   switch ( constructorId ) {
141   case 0:
142     myGrp1->show();
143     myGrp1->SpinBox1->setValue( 6 );
144     myGrp1->SpinBox2->setValue( 6 );
145     myGrp1->CheckBox1->setChecked( false );
146     break;
147   default:
148     break;
149   }
150
151   // init fields
152   myEditCurrentArgument = myGrp1->LineEdit1;
153   myObject = GEOM::GEOM_Object::_nil();
154
155   activateSelection();
156 }
157
158 //=================================================================================
159 // function : ClickOnOk()
160 // purpose  :
161 //=================================================================================
162 void BlocksGUI_ExplodeDlg::ClickOnOk()
163 {
164   if ( ClickOnApply() )
165     ClickOnCancel();
166 }
167
168 //=================================================================================
169 // function : ClickOnApply()
170 // purpose  :
171 //=================================================================================
172 bool BlocksGUI_ExplodeDlg::ClickOnApply()
173 {
174   SUIT_Session::session()->activeApplication()->putInfo( tr( "" ) );
175
176   // Explode all sub shapes
177   if ( isAllSubShapes() ) {
178     // More than 30 subshapes : ask confirmation
179     if ( myNbBlocks > 30 ) {
180       if ( SUIT_MessageBox::warning( this, 
181                                      tr( "GEOM_CONFIRM" ),
182                                      tr( "GEOM_CONFIRM_INFO" ).arg( myNbBlocks ),
183                                      tr( "GEOM_BUT_EXPLODE" ),
184                                      tr( "GEOM_BUT_CANCEL" ) ) != 0 )
185         return false;  /* aborted */
186     }
187   }
188
189   if ( !onAccept() )
190     return false;
191
192   activateSelection();
193
194   return true;
195 }
196
197 //=================================================================================
198 // function : SelectionIntoArgument()
199 // purpose  : Called when selection has changed
200 //=================================================================================
201 void BlocksGUI_ExplodeDlg::SelectionIntoArgument()
202 {
203   if (!isAllSubShapes())
204     return;
205
206   myObject = GEOM::GEOM_Object::_nil();
207   myGrp1->LineEdit1->setText("");
208
209   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
210   SALOME_ListIO aSelList;
211   aSelMgr->selectedObjects(aSelList);
212
213   if (aSelList.Extent() == 1) {
214     Standard_Boolean aResult = Standard_False;
215     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
216
217     if (aResult && !anObj->_is_nil() && GEOMBase::IsShape(anObj)) {
218       myObject = anObj;
219       myGrp1->LineEdit1->setText(GEOMBase::GetName(anObj));
220     }
221   }
222
223   updateButtonState();
224 }
225
226 //=================================================================================
227 // function : SetEditCurrentArgument()
228 // purpose  :
229 //=================================================================================
230 void BlocksGUI_ExplodeDlg::SetEditCurrentArgument()
231 {
232   QPushButton* aSender = (QPushButton*)sender();
233
234   if ( myGrp1->PushButton1 == aSender ) {
235     myGrp1->LineEdit1->setFocus();
236     myEditCurrentArgument = myGrp1->LineEdit1;
237     myGrp1->CheckBox1->setChecked( false );
238   }
239
240   activateSelection();
241 }
242
243 //=================================================================================
244 // function : ActivateThisDialog()
245 // purpose  :
246 //=================================================================================
247 void BlocksGUI_ExplodeDlg::ActivateThisDialog()
248 {
249   GEOMBase_Skeleton::ActivateThisDialog();
250   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
251            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
252
253   activateSelection();
254 }
255
256 //=================================================================================
257 // function : enterEvent()
258 // purpose  :
259 //=================================================================================
260 void BlocksGUI_ExplodeDlg::enterEvent( QEvent* )
261 {
262   if ( !mainFrame()->GroupConstructors->isEnabled() )
263     this->ActivateThisDialog();
264 }
265
266 //=================================================================================
267 // function : ValueChangedInSpinBox()
268 // purpose  :
269 //=================================================================================
270 void BlocksGUI_ExplodeDlg::ValueChangedInSpinBox()
271 {
272   if ( !isAllSubShapes() )
273     activateSelection();
274   else
275     updateButtonState();
276 }
277
278 //=================================================================================
279 // function : SubShapeToggled()
280 // purpose  : Allow user selection of all or only selected sub shapes
281 //          : Called when 'myGrp1->CheckBox1' state change
282 //=================================================================================
283 void BlocksGUI_ExplodeDlg::SubShapeToggled()
284 {
285   activateSelection();
286 }
287
288 //=================================================================================
289 // function : activateSelection
290 // purpose  : Redisplay preview and Activate selection
291 //=================================================================================
292 void BlocksGUI_ExplodeDlg::activateSelection()
293 {
294   clearTemporary();
295   erasePreview( true );
296
297   if ( isAllSubShapes() ) { // Sub-shapes selection disabled
298     disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
299                 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
300     globalSelection( GEOM_ALLSHAPES );
301     if ( myObject->_is_nil() ) {
302       SelectionIntoArgument();
303     }
304     connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
305              SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
306   } 
307   else {
308     displayPreview( true, true, false );
309     globalSelection( GEOM_PREVIEW );
310   }
311 }
312
313 //=================================================================================
314 // function : updateButtonState
315 // purpose  :
316 //=================================================================================
317 void BlocksGUI_ExplodeDlg::updateButtonState()
318 {
319   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
320        != OCCViewer_Viewer::Type() || myObject->_is_nil() ) {
321     myGrp1->CheckBox1->setChecked( false );
322     myGrp1->CheckBox1->setEnabled( false );
323   } 
324   else {
325     myGrp1->CheckBox1->setEnabled( true );
326   }
327
328   myNbBlocks = 0;
329
330   if ( myObject->_is_nil() ) {
331     myGrp1->TextBrowser1->setText( "" );
332   } 
333   else {
334     bool isOnlyBlocks = GEOM::GEOM_IBlocksOperations::_narrow
335       ( getOperation() )->IsCompoundOfBlocks( myObject,
336                                               myGrp1->SpinBox1->value(),
337                                               myGrp1->SpinBox2->value(),
338                                               myNbBlocks );
339     if ( isOnlyBlocks )
340       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_NO_OTHERS" ).arg( myNbBlocks ) );
341     else
342       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_SOME_OTHERS" ).arg( myNbBlocks ) );
343   }
344 }
345
346 //=================================================================================
347 // function : isAllSubShapes
348 // purpose  :
349 //=================================================================================
350 bool BlocksGUI_ExplodeDlg::isAllSubShapes() const
351 {
352   return !myGrp1->CheckBox1->isChecked() || !myGrp1->CheckBox1->isEnabled();
353 }
354
355 //=================================================================================
356 // function : createOperation
357 // purpose  :
358 //=================================================================================
359 GEOM::GEOM_IOperations_ptr BlocksGUI_ExplodeDlg::createOperation()
360 {
361   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
362 }
363
364 //=================================================================================
365 // function : isValid()
366 // purpose  : Verify validity of input data
367 //=================================================================================
368 bool BlocksGUI_ExplodeDlg::isValid (QString&)
369 {
370   bool ok = false;
371   switch ( getConstructorId() ) {
372   case 0:
373     if ( IsPreview() ) {
374       ok = !myObject->_is_nil();
375     }
376     else {
377       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
378       SALOME_ListIO aSelList;
379       aSelMgr->selectedObjects(aSelList);
380
381       ok = !myObject->_is_nil() && (isAllSubShapes() || aSelList.Extent());
382     }
383     break;
384   default:
385     break;
386   }
387
388   return ok;
389 }
390
391 //=================================================================================
392 // function : execute
393 // purpose  :
394 //=================================================================================
395 bool BlocksGUI_ExplodeDlg::execute( ObjectList& objects )
396 {
397   GEOM::ListOfGO_var aList;
398
399   switch ( getConstructorId() ) {
400   case 0:
401     aList = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->ExplodeCompoundOfBlocks
402       ( myObject,
403         myGrp1->SpinBox1->value(),
404         myGrp1->SpinBox2->value() );
405     break;
406   }
407   
408   if ( !aList->length() )
409     return false;
410
411   if ( IsPreview() ) {
412     clearTemporary();
413     
414     // Store objects. They will be put in study when "Apply" is pressed
415     for ( int i = 0, n = aList->length(); i < n; i++ ) {
416       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
417       myTmpObjs.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
418     }
419
420     return objects.size() > 0;
421   }
422
423   // Throw away sub-shapes not selected by user if not in preview mode
424   // and manual selection is active
425   if ( !isAllSubShapes() ) {
426     QMap<QString, char> selected;
427
428     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
429     SALOME_ListIO aSelList;
430     aSelMgr->selectedObjects(aSelList);
431
432     // Get names of selected objects
433     SALOME_ListIteratorOfListIO it (aSelList);
434     for ( ; it.More(); it.Next() ) {
435       selected.insert( it.Value()->getName(), 0 );
436     }
437
438     // Iterate through result and select objects with names from selection
439     ObjectList toRemoveFromEngine;
440     ObjectList::iterator anIter;
441     for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter ) {
442       CORBA::String_var objStr = myGeomGUI->getApp()->orb()->object_to_string( *anIter );
443       if ( selected.contains( QString( objStr.in() ) ) )
444         objects.push_back( *anIter );
445       else
446         toRemoveFromEngine.push_back( *anIter );
447     }
448
449     // Remove from engine useless objects
450     ObjectList::iterator anIter2 = toRemoveFromEngine.begin();
451     for ( ; anIter2 != toRemoveFromEngine.end(); ++anIter2 )
452       getGeomEngine()->RemoveObject( *anIter2 );
453
454     myTmpObjs.clear();
455
456   }
457   else {
458     for ( int i = 0, n = aList->length(); i < n; i++ )
459       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
460   }
461
462   return objects.size();
463 }
464
465 //=================================================================================
466 // function : clearTemporary
467 // purpose  : Remove temporary objects from engine
468 //=================================================================================
469 void BlocksGUI_ExplodeDlg::clearTemporary()
470 {
471   ObjectList::iterator anIter;
472   for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter )
473     getGeomEngine()->RemoveObject( *anIter );
474
475   myTmpObjs.clear();
476 }
477
478 //================================================================
479 // Function : getFather
480 // Purpose  : Get father object for object to be added in study
481 //            ( called with addInStudy method )
482 //================================================================
483 GEOM::GEOM_Object_ptr BlocksGUI_ExplodeDlg::getFather( GEOM::GEOM_Object_ptr )
484 {
485   return myObject;
486 }
487
488 //================================================================
489 // Function : getNewObjectName
490 // Purpose  : Redefine this method to return proper name for a new object
491 //================================================================
492 QString BlocksGUI_ExplodeDlg::getNewObjectName() const
493 {
494   return QString::null;
495 }