]> SALOME platform Git repositories - modules/geom.git/blob - src/BlocksGUI/BlocksGUI_ExplodeDlg.cxx
Salome HOME
Remove unnecessary function setCustomData() / getCustomData()
[modules/geom.git] / src / BlocksGUI / BlocksGUI_ExplodeDlg.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   : 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_ListIteratorOfListIO.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 )
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   if ( ClickOnApply() )
167     ClickOnCancel();
168 }
169
170 //=================================================================================
171 // function : ClickOnApply()
172 // purpose  :
173 //=================================================================================
174 bool BlocksGUI_ExplodeDlg::ClickOnApply()
175 {
176   SUIT_Session::session()->activeApplication()->putInfo( tr( "" ) );
177
178   // Explode all sub shapes
179   if ( isAllSubShapes() ) {
180     // More than 30 subshapes : ask confirmation
181     if ( myNbBlocks > 30 ) {
182       if ( SUIT_MessageBox::warning( this, 
183                                      tr( "GEOM_CONFIRM" ),
184                                      tr( "GEOM_CONFIRM_INFO" ).arg( myNbBlocks ),
185                                      tr( "GEOM_BUT_EXPLODE" ),
186                                      tr( "GEOM_BUT_CANCEL" ) ) != 0 )
187         return false;  /* aborted */
188     }
189   }
190
191   if ( !onAccept() )
192     return false;
193
194   activateSelection();
195
196   return true;
197 }
198
199 //=================================================================================
200 // function : SelectionIntoArgument()
201 // purpose  : Called when selection has changed
202 //=================================================================================
203 void BlocksGUI_ExplodeDlg::SelectionIntoArgument()
204 {
205   if (!isAllSubShapes())
206     return;
207
208   myObject = GEOM::GEOM_Object::_nil();
209   myGrp1->LineEdit1->setText("");
210
211   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
212   SALOME_ListIO aSelList;
213   aSelMgr->selectedObjects(aSelList);
214
215   if (aSelList.Extent() == 1) {
216     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
217
218     if ( GEOMBase::IsShape(anObj) ) {
219       myObject = anObj;
220       myGrp1->LineEdit1->setText(GEOMBase::GetName(anObj));
221     }
222   }
223
224   updateButtonState();
225 }
226
227 //=================================================================================
228 // function : SetEditCurrentArgument()
229 // purpose  :
230 //=================================================================================
231 void BlocksGUI_ExplodeDlg::SetEditCurrentArgument()
232 {
233   QPushButton* aSender = (QPushButton*)sender();
234
235   if ( myGrp1->PushButton1 == aSender ) {
236     myGrp1->LineEdit1->setFocus();
237     myEditCurrentArgument = myGrp1->LineEdit1;
238     myGrp1->CheckBox1->setChecked( false );
239   }
240
241   activateSelection();
242 }
243
244 //=================================================================================
245 // function : ActivateThisDialog()
246 // purpose  :
247 //=================================================================================
248 void BlocksGUI_ExplodeDlg::ActivateThisDialog()
249 {
250   GEOMBase_Skeleton::ActivateThisDialog();
251   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
252            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
253
254   activateSelection();
255 }
256
257 //=================================================================================
258 // function : enterEvent()
259 // purpose  :
260 //=================================================================================
261 void BlocksGUI_ExplodeDlg::enterEvent( QEvent* )
262 {
263   if ( !mainFrame()->GroupConstructors->isEnabled() )
264     this->ActivateThisDialog();
265 }
266
267 //=================================================================================
268 // function : ValueChangedInSpinBox()
269 // purpose  :
270 //=================================================================================
271 void BlocksGUI_ExplodeDlg::ValueChangedInSpinBox()
272 {
273   if ( !isAllSubShapes() )
274     activateSelection();
275   else
276     updateButtonState();
277 }
278
279 //=================================================================================
280 // function : SubShapeToggled()
281 // purpose  : Allow user selection of all or only selected sub shapes
282 //          : Called when 'myGrp1->CheckBox1' state change
283 //=================================================================================
284 void BlocksGUI_ExplodeDlg::SubShapeToggled()
285 {
286   activateSelection();
287 }
288
289 //=================================================================================
290 // function : activateSelection
291 // purpose  : Redisplay preview and Activate selection
292 //=================================================================================
293 void BlocksGUI_ExplodeDlg::activateSelection()
294 {
295   clearTemporary();
296   erasePreview( true );
297
298   if ( isAllSubShapes() ) { // Sub-shapes selection disabled
299     disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
300                 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
301     globalSelection( GEOM_ALLSHAPES );
302     if ( myObject->_is_nil() ) {
303       SelectionIntoArgument();
304     }
305     connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
306              SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
307   } 
308   else {
309     displayPreview( true, true, false );
310     globalSelection( GEOM_PREVIEW );
311   }
312 }
313
314 //=================================================================================
315 // function : updateButtonState
316 // purpose  :
317 //=================================================================================
318 void BlocksGUI_ExplodeDlg::updateButtonState()
319 {
320   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
321        != OCCViewer_Viewer::Type() || myObject->_is_nil() ) {
322     myGrp1->CheckBox1->setChecked( false );
323     myGrp1->CheckBox1->setEnabled( false );
324   } 
325   else {
326     myGrp1->CheckBox1->setEnabled( true );
327   }
328
329   myNbBlocks = 0;
330
331   if ( myObject->_is_nil() ) {
332     myGrp1->TextBrowser1->setText( "" );
333   } 
334   else {
335     GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow(getOperation());
336     bool isOnlyBlocks = anOper->IsCompoundOfBlocks( myObject,
337                                                     myGrp1->SpinBox1->value(),
338                                                     myGrp1->SpinBox2->value(),
339                                                     myNbBlocks );
340     if ( isOnlyBlocks )
341       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_NO_OTHERS" ).arg( myNbBlocks ) );
342     else
343       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_SOME_OTHERS" ).arg( myNbBlocks ) );
344   }
345 }
346
347 //=================================================================================
348 // function : isAllSubShapes
349 // purpose  :
350 //=================================================================================
351 bool BlocksGUI_ExplodeDlg::isAllSubShapes() const
352 {
353   return !myGrp1->CheckBox1->isChecked() || !myGrp1->CheckBox1->isEnabled();
354 }
355
356 //=================================================================================
357 // function : createOperation
358 // purpose  :
359 //=================================================================================
360 GEOM::GEOM_IOperations_ptr BlocksGUI_ExplodeDlg::createOperation()
361 {
362   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
363 }
364
365 //=================================================================================
366 // function : isValid()
367 // purpose  : Verify validity of input data
368 //=================================================================================
369 bool BlocksGUI_ExplodeDlg::isValid (QString& msg)
370 {
371   bool okSP = true;
372   okSP = myGrp1->SpinBox1->isValid( msg, !IsPreview() ) && okSP;
373   okSP = myGrp1->SpinBox2->isValid( msg, !IsPreview() ) && okSP;
374
375   bool ok = false;
376   switch ( getConstructorId() ) {
377   case 0:
378     if ( IsPreview() ) {
379       ok = !myObject->_is_nil();
380     }
381     else {
382       LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
383       SALOME_ListIO aSelList;
384       aSelMgr->selectedObjects(aSelList);
385
386       ok = !myObject->_is_nil() && (isAllSubShapes() || aSelList.Extent());
387     }
388     break;
389   default:
390     break;
391   }
392
393   return ok && okSP;
394 }
395
396 //=================================================================================
397 // function : execute
398 // purpose  :
399 //=================================================================================
400 bool BlocksGUI_ExplodeDlg::execute( ObjectList& objects )
401 {
402   GEOM::ListOfGO_var aList;
403
404   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow(getOperation());
405
406   switch ( getConstructorId() ) {
407   case 0:
408     aList = anOper->ExplodeCompoundOfBlocks( myObject,
409                                              myGrp1->SpinBox1->value(),
410                                              myGrp1->SpinBox2->value() );
411     break;
412   }
413   
414   if ( !aList->length() )
415     return false;
416
417   if ( IsPreview() ) {
418     clearTemporary();
419     
420     // Store objects. They will be put in study when "Apply" is pressed
421     for ( int i = 0, n = aList->length(); i < n; i++ ) {
422       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
423       myTmpObjs.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
424     }
425
426     return objects.size() > 0;
427   }
428
429   QStringList aParameters;
430   aParameters << myGrp1->SpinBox1->text();
431   aParameters << myGrp1->SpinBox2->text();
432
433   // Throw away sub-shapes not selected by user if not in preview mode
434   // and manual selection is active
435   if ( !isAllSubShapes() ) {
436     QMap<QString, char> selected;
437
438     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
439     SALOME_ListIO aSelList;
440     aSelMgr->selectedObjects(aSelList);
441
442     // Get names of selected objects
443     SALOME_ListIteratorOfListIO it (aSelList);
444     for ( ; it.More(); it.Next() ) {
445       selected.insert( it.Value()->getName(), 0 );
446     }
447
448     // Iterate through result and select objects with names from selection
449     ObjectList toRemoveFromEngine;
450     ObjectList::iterator anIter;
451     for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter ) {
452       CORBA::String_var objStr = myGeomGUI->getApp()->orb()->object_to_string( *anIter );
453       if ( selected.contains( QString( objStr.in() ) ) )
454       {
455         if ( !IsPreview() )
456           (*anIter)->SetParameters(aParameters.join(":").toLatin1().constData());
457         objects.push_back( *anIter );
458       }
459       else
460         toRemoveFromEngine.push_back( *anIter );
461     }
462
463     // Remove from engine useless objects
464     ObjectList::iterator anIter2 = toRemoveFromEngine.begin();
465     for ( ; anIter2 != toRemoveFromEngine.end(); ++anIter2 )
466       getGeomEngine()->RemoveObject( *anIter2 );
467
468     myTmpObjs.clear();
469
470   }
471   else {
472     for ( int i = 0, n = aList->length(); i < n; i++ )
473     {
474       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aList[i] );
475       if ( !IsPreview() )
476         anObj->SetParameters(aParameters.join(":").toLatin1().constData());
477       objects.push_back( anObj._retn() );
478     }
479   }
480
481   return objects.size();
482 }
483
484 //=================================================================================
485 // function : clearTemporary
486 // purpose  : Remove temporary objects from engine
487 //=================================================================================
488 void BlocksGUI_ExplodeDlg::clearTemporary()
489 {
490   ObjectList::iterator anIter;
491   for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter )
492     getGeomEngine()->RemoveObject( *anIter );
493
494   myTmpObjs.clear();
495 }
496
497 //================================================================
498 // Function : getFather
499 // Purpose  : Get father object for object to be added in study
500 //            ( called with addInStudy method )
501 //================================================================
502 GEOM::GEOM_Object_ptr BlocksGUI_ExplodeDlg::getFather( GEOM::GEOM_Object_ptr )
503 {
504   return myObject;
505 }
506
507 //================================================================
508 // Function : getNewObjectName
509 // Purpose  : Redefine this method to return proper name for a new object
510 //================================================================
511 QString BlocksGUI_ExplodeDlg::getNewObjectName() const
512 {
513   return QString::null;
514 }