]> SALOME platform Git repositories - modules/geom.git/blob - src/BlocksGUI/BlocksGUI_ExplodeDlg.cxx
Salome HOME
*** empty log message ***
[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.htm" );
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   // Set range of spinboxes
101   int SpecificStep = 1;
102   initSpinBox( myGrp1->SpinBox1, 0, 999, SpecificStep );
103   initSpinBox( myGrp1->SpinBox2, 0, 999, SpecificStep );
104
105   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
106        != OCCViewer_Viewer::Type() )
107     myGrp1->CheckBox1->setEnabled( false );
108
109   // signals and slots connections
110   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
111   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
112
113   connect( myGrp1->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
114
115   connect( myGrp1->SpinBox1, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
116   connect( myGrp1->SpinBox2, SIGNAL( valueChanged( int ) ), this, SLOT( ValueChangedInSpinBox( int ) ) );
117
118   connect( myGrp1->CheckBox1, SIGNAL( stateChanged( int ) ), this, SLOT( SubShapeToggled() ) );
119
120   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
121            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
122
123   myConstructorId = -1;
124   ConstructorsClicked( 0 );
125 }
126
127 //=================================================================================
128 // function : ConstructorsClicked()
129 // purpose  : Radio button management
130 //=================================================================================
131 void BlocksGUI_ExplodeDlg::ConstructorsClicked( int constructorId )
132 {
133   if ( myConstructorId == constructorId )
134     return;
135
136   myConstructorId = constructorId;
137
138   switch ( constructorId ) {
139   case 0:
140     myGrp1->show();
141     myGrp1->SpinBox1->setValue( 6 );
142     myGrp1->SpinBox2->setValue( 6 );
143     myGrp1->CheckBox1->setChecked( false );
144     break;
145   default:
146     break;
147   }
148
149   // init fields
150   myEditCurrentArgument = myGrp1->LineEdit1;
151   myObject = GEOM::GEOM_Object::_nil();
152
153   activateSelection();
154 }
155
156 //=================================================================================
157 // function : ClickOnOk()
158 // purpose  :
159 //=================================================================================
160 void BlocksGUI_ExplodeDlg::ClickOnOk()
161 {
162   if ( ClickOnApply() )
163     ClickOnCancel();
164 }
165
166 //=================================================================================
167 // function : ClickOnApply()
168 // purpose  :
169 //=================================================================================
170 bool BlocksGUI_ExplodeDlg::ClickOnApply()
171 {
172   SUIT_Session::session()->activeApplication()->putInfo( tr( "" ) );
173
174   // Explode all sub shapes
175   if ( isAllSubShapes() ) {
176     // More than 30 subshapes : ask confirmation
177     if ( myNbBlocks > 30 ) {
178       if ( SUIT_MessageBox::warning( this, 
179                                      tr( "GEOM_CONFIRM" ),
180                                      tr( "GEOM_CONFIRM_INFO" ).arg( myNbBlocks ),
181                                      tr( "GEOM_BUT_EXPLODE" ),
182                                      tr( "GEOM_BUT_CANCEL" ) ) != 0 )
183         return false;  /* aborted */
184     }
185   }
186
187   if ( !onAccept() )
188     return false;
189
190   activateSelection();
191
192   return true;
193 }
194
195 //=================================================================================
196 // function : SelectionIntoArgument()
197 // purpose  : Called when selection has changed
198 //=================================================================================
199 void BlocksGUI_ExplodeDlg::SelectionIntoArgument()
200 {
201   if ( !isAllSubShapes() )
202     return;
203
204   myObject = GEOM::GEOM_Object::_nil();
205   myGrp1->LineEdit1->setText( "" );
206
207   if ( IObjectCount() == 1 ) {
208     Standard_Boolean aResult = Standard_False;
209     GEOM::GEOM_Object_var anObj =
210       GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
211
212     if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
213       myObject = anObj;
214       myGrp1->LineEdit1->setText( GEOMBase::GetName( anObj ) );
215     }
216   }
217
218   updateButtonState();
219 }
220
221 //=================================================================================
222 // function : SetEditCurrentArgument()
223 // purpose  :
224 //=================================================================================
225 void BlocksGUI_ExplodeDlg::SetEditCurrentArgument()
226 {
227   QPushButton* aSender = (QPushButton*)sender();
228
229   if ( myGrp1->PushButton1 == aSender ) {
230     myGrp1->LineEdit1->setFocus();
231     myEditCurrentArgument = myGrp1->LineEdit1;
232     myGrp1->CheckBox1->setChecked( false );
233   }
234
235   activateSelection();
236 }
237
238 //=================================================================================
239 // function : ActivateThisDialog()
240 // purpose  :
241 //=================================================================================
242 void BlocksGUI_ExplodeDlg::ActivateThisDialog()
243 {
244   GEOMBase_Skeleton::ActivateThisDialog();
245   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
246            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
247
248   activateSelection();
249 }
250
251 //=================================================================================
252 // function : enterEvent()
253 // purpose  :
254 //=================================================================================
255 void BlocksGUI_ExplodeDlg::enterEvent( QEvent* )
256 {
257   if ( !mainFrame()->GroupConstructors->isEnabled() )
258     this->ActivateThisDialog();
259 }
260
261 //=================================================================================
262 // function : ValueChangedInSpinBox()
263 // purpose  :
264 //=================================================================================
265 void BlocksGUI_ExplodeDlg::ValueChangedInSpinBox()
266 {
267   if ( !isAllSubShapes() )
268     activateSelection();
269   else
270     updateButtonState();
271 }
272
273 //=================================================================================
274 // function : SubShapeToggled()
275 // purpose  : Allow user selection of all or only selected sub shapes
276 //          : Called when 'myGrp1->CheckBox1' state change
277 //=================================================================================
278 void BlocksGUI_ExplodeDlg::SubShapeToggled()
279 {
280   activateSelection();
281 }
282
283 //=================================================================================
284 // function : activateSelection
285 // purpose  : Redisplay preview and Activate selection
286 //=================================================================================
287 void BlocksGUI_ExplodeDlg::activateSelection()
288 {
289   clearTemporary();
290   erasePreview( true );
291
292   if ( isAllSubShapes() ) { // Sub-shapes selection disabled
293     disconnect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
294                 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
295     globalSelection( GEOM_ALLSHAPES );
296     if ( myObject->_is_nil() ) {
297       SelectionIntoArgument();
298     }
299     connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
300              SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
301   } 
302   else {
303     displayPreview( true, true, false );
304     globalSelection( GEOM_PREVIEW );
305   }
306 }
307
308 //=================================================================================
309 // function : updateButtonState
310 // purpose  :
311 //=================================================================================
312 void BlocksGUI_ExplodeDlg::updateButtonState()
313 {
314   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
315        != OCCViewer_Viewer::Type() || myObject->_is_nil() ) {
316     myGrp1->CheckBox1->setChecked( false );
317     myGrp1->CheckBox1->setEnabled( false );
318   } 
319   else {
320     myGrp1->CheckBox1->setEnabled( true );
321   }
322
323   myNbBlocks = 0;
324
325   if ( myObject->_is_nil() ) {
326     myGrp1->TextBrowser1->setText( "" );
327   } 
328   else {
329     bool isOnlyBlocks = GEOM::GEOM_IBlocksOperations::_narrow
330       ( getOperation() )->IsCompoundOfBlocks( myObject,
331                                               myGrp1->SpinBox1->value(),
332                                               myGrp1->SpinBox2->value(),
333                                               myNbBlocks );
334     if ( isOnlyBlocks )
335       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_NO_OTHERS" ).arg( myNbBlocks ) );
336     else
337       myGrp1->TextBrowser1->setText( tr( "GEOM_NB_BLOCKS_SOME_OTHERS" ).arg( myNbBlocks ) );
338   }
339 }
340
341 //=================================================================================
342 // function : isAllSubShapes
343 // purpose  :
344 //=================================================================================
345 bool BlocksGUI_ExplodeDlg::isAllSubShapes() const
346 {
347   return !myGrp1->CheckBox1->isChecked() || !myGrp1->CheckBox1->isEnabled();
348 }
349
350 //=================================================================================
351 // function : createOperation
352 // purpose  :
353 //=================================================================================
354 GEOM::GEOM_IOperations_ptr BlocksGUI_ExplodeDlg::createOperation()
355 {
356   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
357 }
358
359 //=================================================================================
360 // function : isValid()
361 // purpose  : Verify validity of input data
362 //=================================================================================
363 bool BlocksGUI_ExplodeDlg::isValid( QString& )
364 {
365   bool ok = false;
366   switch ( getConstructorId() ) {
367   case 0:
368     if ( IsPreview() )
369       ok = !myObject->_is_nil();
370     else
371       ok = !myObject->_is_nil() && ( isAllSubShapes() || IObjectCount() );
372     break;
373   default:
374     break;
375   }
376
377   return false;
378 }
379
380 //=================================================================================
381 // function : execute
382 // purpose  :
383 //=================================================================================
384 bool BlocksGUI_ExplodeDlg::execute( ObjectList& objects )
385 {
386   GEOM::ListOfGO_var aList;
387
388   switch ( getConstructorId() ) {
389   case 0:
390     aList = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->ExplodeCompoundOfBlocks
391       ( myObject,
392         myGrp1->SpinBox1->value(),
393         myGrp1->SpinBox2->value() );
394     break;
395   }
396   
397   if ( !aList->length() )
398     return false;
399
400   if ( IsPreview() ) {
401     clearTemporary();
402     
403     // Store objects. They will be put in study when "Apply" is pressed
404     for ( int i = 0, n = aList->length(); i < n; i++ ) {
405       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
406       myTmpObjs.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
407     }
408
409     return objects.size() > 0;
410   }
411
412   // Throw away sub-shapes not selected by user if not in preview mode
413   // and manual selection is active
414   if ( !isAllSubShapes() ) {
415     QMap<QString, char> selected;
416
417     // Get names of selected objects
418     SALOME_ListIteratorOfListIO it ( selectedIO() );
419     for ( ; it.More(); it.Next() ) {
420       selected.insert( it.Value()->getName(), 0 );
421     }
422
423     // Iterate through result and select objects with names from selection
424     ObjectList toRemoveFromEnggine;
425     ObjectList::iterator anIter;
426     for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter ) {
427       CORBA::String_var objStr = myGeomGUI->getApp()->orb()->object_to_string( *anIter );
428       if ( selected.contains( QString( objStr.in() ) ) )
429         objects.push_back( *anIter );
430       else
431         toRemoveFromEnggine.push_back( *anIter );
432     }
433
434     // Remove from engine useless objects
435     ObjectList::iterator anIter2 = toRemoveFromEnggine.begin();
436     for ( ; anIter2 != toRemoveFromEnggine.end(); ++anIter2 )
437       getGeomEngine()->RemoveObject( *anIter2 );
438
439     myTmpObjs.clear();
440
441   }
442   else {
443     for ( int i = 0, n = aList->length(); i < n; i++ )
444       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
445   }
446
447   return objects.size();
448 }
449
450 //=================================================================================
451 // function : clearTemporary
452 // purpose  : Remove temporary objects from engine
453 //=================================================================================
454 void BlocksGUI_ExplodeDlg::clearTemporary()
455 {
456   ObjectList::iterator anIter;
457   for ( anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter )
458     getGeomEngine()->RemoveObject( *anIter );
459
460   myTmpObjs.clear();
461 }
462
463 //================================================================
464 // Function : getFather
465 // Purpose  : Get father object for object to be added in study
466 //            ( called with addInStudy method )
467 //================================================================
468 GEOM::GEOM_Object_ptr BlocksGUI_ExplodeDlg::getFather( GEOM::GEOM_Object_ptr )
469 {
470   return myObject;
471 }
472
473 //================================================================
474 // Function : getNewObjectName
475 // Purpose  : Redefine this method to return proper name for a new object
476 //================================================================
477 const char* BlocksGUI_ExplodeDlg::getNewObjectName() const
478 {
479   return "";
480 }