Salome HOME
Portability windows
[modules/geom.git] / src / BlocksGUI / BlocksGUI_ExplodeDlg.cxx
1 //  Copyright (C) 2007-2008  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : BlocksGUI_ExplodeDlg.cxx
24 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
25 //
26 #include "BlocksGUI_ExplodeDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_ViewManager.h>
37 #include <SUIT_MessageBox.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <SALOME_ListIteratorOfListIO.hxx>
42
43 //=================================================================================
44 // class    : BlocksGUI_ExplodeDlg()
45 // purpose  : Constructs a BlocksGUI_ExplodeDlg which is a child of 'parent'.
46 //=================================================================================
47 BlocksGUI_ExplodeDlg::BlocksGUI_ExplodeDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
48   : GEOMBase_Skeleton( theGeometryGUI, parent )
49 {
50   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BLOCK_EXPLODE" ) ) );
51   QPixmap imageS( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
52
53   setWindowTitle( tr( "GEOM_BLOCK_EXPLODE_TITLE" ) );
54
55   /***************************************************************/
56   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_BLOCK_EXPLODE" ) );
57
58   mainFrame()->RadioButton1->setIcon( image1 );
59   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
60   mainFrame()->RadioButton2->close();
61   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton3->close();
63
64   // Create first group
65   myGrp1 = new DlgRef_1Sel2Spin1View1Check( centralWidget() );
66   myGrp1->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
67   myGrp1->TextLabel1->setText( tr( "GEOM_MAIN_OBJECT" ) );
68   myGrp1->PushButton1->setIcon( imageS );
69   myGrp1->LineEdit1->setReadOnly( true );
70   myGrp1->TextLabel2->setText( tr( "NB_FACES_MIN" ) );
71   myGrp1->TextLabel3->setText( tr( "NB_FACES_MAX" ) );
72   myGrp1->CheckBox1->setText( tr( "GEOM_SUBSHAPE_SELECT" ) );
73
74   // Add groups to layout
75   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
76   layout->setMargin( 0 ); layout->setSpacing( 6 );
77   layout->addWidget( myGrp1 );
78   /***************************************************************/
79
80   setHelpFileName( "explode_on_blocks_operation_page.html" );
81
82   Init();
83 }
84
85 //=================================================================================
86 // function : ~BlocksGUI_ExplodeDlg()
87 // purpose  : Destroys the object and frees any allocated resources
88 //=================================================================================
89 BlocksGUI_ExplodeDlg::~BlocksGUI_ExplodeDlg()
90 {
91   // no need to delete child widgets, Qt does it all for us
92   clearTemporary();
93 }
94
95 //=================================================================================
96 // function : Init()
97 // purpose  :
98 //=================================================================================
99 void BlocksGUI_ExplodeDlg::Init()
100 {
101   mainFrame()->GroupBoxName->hide();
102
103   // Set range of spinboxes
104   int SpecificStep = 1;
105   initSpinBox( myGrp1->SpinBox1, 0, 999, SpecificStep );
106   initSpinBox( myGrp1->SpinBox2, 0, 999, SpecificStep );
107
108   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
109        != OCCViewer_Viewer::Type() )
110     myGrp1->CheckBox1->setEnabled( false );
111
112   // signals and slots connections
113   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
114   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
115
116   connect( myGrp1->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
117
118   connect( myGrp1->SpinBox1, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
119   connect( myGrp1->SpinBox2, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
120
121   connect( myGrp1->CheckBox1, SIGNAL( stateChanged( int ) ), this, SLOT( SubShapeToggled() ) );
122
123   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
124            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
125
126   myConstructorId = -1;
127   ConstructorsClicked( 0 );
128 }
129
130 //=================================================================================
131 // function : ConstructorsClicked()
132 // purpose  : Radio button management
133 //=================================================================================
134 void BlocksGUI_ExplodeDlg::ConstructorsClicked( int constructorId )
135 {
136   if ( myConstructorId == constructorId )
137     return;
138
139   myConstructorId = constructorId;
140
141   switch ( constructorId ) {
142   case 0:
143     myGrp1->show();
144     myGrp1->SpinBox1->setValue( 6 );
145     myGrp1->SpinBox2->setValue( 6 );
146     myGrp1->CheckBox1->setChecked( false );
147     break;
148   default:
149     break;
150   }
151
152   // init fields
153   myEditCurrentArgument = myGrp1->LineEdit1;
154   myObject = GEOM::GEOM_Object::_nil();
155
156   activateSelection();
157 }
158
159 //=================================================================================
160 // function : ClickOnOk()
161 // purpose  :
162 //=================================================================================
163 void BlocksGUI_ExplodeDlg::ClickOnOk()
164 {
165   if ( ClickOnApply() )
166     ClickOnCancel();
167 }
168
169 //=================================================================================
170 // function : ClickOnApply()
171 // purpose  :
172 //=================================================================================
173 bool BlocksGUI_ExplodeDlg::ClickOnApply()
174 {
175   SUIT_Session::session()->activeApplication()->putInfo( tr( "" ) );
176
177   // Explode all sub shapes
178   if ( isAllSubShapes() ) {
179     // More than 30 subshapes : ask confirmation
180     if ( myNbBlocks > 30 ) {
181       if ( SUIT_MessageBox::warning( this, 
182                                      tr( "GEOM_CONFIRM" ),
183                                      tr( "GEOM_CONFIRM_INFO" ).arg( myNbBlocks ),
184                                      tr( "GEOM_BUT_EXPLODE" ),
185                                      tr( "GEOM_BUT_CANCEL" ) ) != 0 )
186         return false;  /* aborted */
187     }
188   }
189
190   if ( !onAccept() )
191     return false;
192
193   activateSelection();
194
195   return true;
196 }
197
198 //=================================================================================
199 // function : SelectionIntoArgument()
200 // purpose  : Called when selection has changed
201 //=================================================================================
202 void BlocksGUI_ExplodeDlg::SelectionIntoArgument()
203 {
204   if (!isAllSubShapes())
205     return;
206
207   myObject = GEOM::GEOM_Object::_nil();
208   myGrp1->LineEdit1->setText("");
209
210   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
211   SALOME_ListIO aSelList;
212   aSelMgr->selectedObjects(aSelList);
213
214   if (aSelList.Extent() == 1) {
215     Standard_Boolean aResult = Standard_False;
216     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
217
218     if (aResult && !anObj->_is_nil() && 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     bool isOnlyBlocks = GEOM::GEOM_IBlocksOperations::_narrow
336       ( getOperation() )->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   switch ( getConstructorId() ) {
405   case 0:
406     aList = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->ExplodeCompoundOfBlocks
407       ( myObject,
408         myGrp1->SpinBox1->value(),
409         myGrp1->SpinBox2->value() );
410     break;
411   }
412   
413   if ( !aList->length() )
414     return false;
415
416   if ( IsPreview() ) {
417     clearTemporary();
418     
419     // Store objects. They will be put in study when "Apply" is pressed
420     for ( int i = 0, n = aList->length(); i < n; i++ ) {
421       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
422       myTmpObjs.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
423     }
424
425     return objects.size() > 0;
426   }
427
428   QStringList aParameters;
429   aParameters << myGrp1->SpinBox1->text();
430   aParameters << myGrp1->SpinBox2->text();
431
432   // Throw away sub-shapes not selected by user if not in preview mode
433   // and manual selection is active
434   if ( !isAllSubShapes() ) {
435     QMap<QString, char> selected;
436
437     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
438     SALOME_ListIO aSelList;
439     aSelMgr->selectedObjects(aSelList);
440
441     // Get names of selected objects
442     SALOME_ListIteratorOfListIO it (aSelList);
443     for ( ; it.More(); it.Next() ) {
444       selected.insert( it.Value()->getName(), 0 );
445     }
446
447     // Iterate through result and select objects with names from selection
448     ObjectList toRemoveFromEngine;
449     ObjectList::iterator anIter;
450     for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter ) {
451       CORBA::String_var objStr = myGeomGUI->getApp()->orb()->object_to_string( *anIter );
452       if ( selected.contains( QString( objStr.in() ) ) )
453       {
454         if ( !IsPreview() )
455           (*anIter)->SetParameters(GeometryGUI::JoinObjectParameters(aParameters));
456         objects.push_back( *anIter );
457       }
458       else
459         toRemoveFromEngine.push_back( *anIter );
460     }
461
462     // Remove from engine useless objects
463     ObjectList::iterator anIter2 = toRemoveFromEngine.begin();
464     for ( ; anIter2 != toRemoveFromEngine.end(); ++anIter2 )
465       getGeomEngine()->RemoveObject( *anIter2 );
466
467     myTmpObjs.clear();
468
469   }
470   else {
471     for ( int i = 0, n = aList->length(); i < n; i++ )
472     {
473       GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_duplicate( aList[i] );
474       if ( !IsPreview() )
475         anObj->SetParameters(GeometryGUI::JoinObjectParameters(aParameters));
476       objects.push_back( anObj._retn() );
477     }
478   }
479
480   return objects.size();
481 }
482
483 //=================================================================================
484 // function : clearTemporary
485 // purpose  : Remove temporary objects from engine
486 //=================================================================================
487 void BlocksGUI_ExplodeDlg::clearTemporary()
488 {
489   ObjectList::iterator anIter;
490   for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter )
491     getGeomEngine()->RemoveObject( *anIter );
492
493   myTmpObjs.clear();
494 }
495
496 //================================================================
497 // Function : getFather
498 // Purpose  : Get father object for object to be added in study
499 //            ( called with addInStudy method )
500 //================================================================
501 GEOM::GEOM_Object_ptr BlocksGUI_ExplodeDlg::getFather( GEOM::GEOM_Object_ptr )
502 {
503   return myObject;
504 }
505
506 //================================================================
507 // Function : getNewObjectName
508 // Purpose  : Redefine this method to return proper name for a new object
509 //================================================================
510 QString BlocksGUI_ExplodeDlg::getNewObjectName() const
511 {
512   return QString::null;
513 }