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