Salome HOME
Fix bug 13341: geompy.SubShapeAllSortedIDs() works wrong.
[modules/geom.git] / src / EntityGUI / EntityGUI_SubShapeDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : EntityGUI_SubShapeDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "EntityGUI_SubShapeDlg.h"
30 #include "GEOM_Displayer.h"
31
32 #include "SUIT_Desktop.h"
33 #include "SUIT_Session.h"
34 #include "OCCViewer_ViewModel.h"
35 #include "SalomeApp_Application.h"
36 #include "LightApp_SelectionMgr.h"
37 #include "SALOME_ListIteratorOfListIO.hxx"
38
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_MapOfShape.hxx>
43
44 #include <qmessagebox.h>
45 #include <qlabel.h>
46 #include <qcombobox.h>
47
48 //=================================================================================
49 // class    : EntityGUI_SubShapeDlg
50 // purpose  : Constructs a EntityGUI_SubShapeDlg which is a child of 'parent', with the
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            TRUE to construct a modal dialog.
54 //=================================================================================
55 EntityGUI_SubShapeDlg::EntityGUI_SubShapeDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
56                                              const char* name, bool modal, WFlags fl)
57   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, fl)
58 {
59   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SUBSHAPE")));
60   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
61
62   setCaption(tr("GEOM_SUBSHAPE_TITLE"));
63
64   /***************************************************************/
65   GroupConstructors->setTitle(tr("GEOM_SUB_SHAPE"));
66   RadioButton1->setPixmap(image0);
67   RadioButton2->close(TRUE);
68   RadioButton3->close(TRUE);
69
70   GroupPoints = new DlgRef_1Sel1Check1List_QTD(this, "GroupPoints");
71   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
72   GroupPoints->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
73   GroupPoints->TextLabel2->setText(tr("GEOM_SUBSHAPE_TYPE"));
74   GroupPoints->CheckButton1->setText(tr("GEOM_SUBSHAPE_SELECT"));
75   GroupPoints->PushButton1->setPixmap(image1);
76   GroupPoints->LineEdit1->setReadOnly( true );
77
78   Layout1->addWidget(GroupPoints, 1, 0);
79   /***************************************************************/
80
81   setHelpFileName("explode.htm");
82
83   Init();
84 }
85
86
87 //=================================================================================
88 // function : ~EntityGUI_SubShapeDlg()
89 // purpose  : Destroys the object and frees any allocated resources
90 //=================================================================================
91 EntityGUI_SubShapeDlg::~EntityGUI_SubShapeDlg()
92 {
93 }
94
95
96 //=================================================================================
97 // function : Init()
98 // purpose  :
99 //=================================================================================
100 void EntityGUI_SubShapeDlg::Init()
101 {
102   /* init variables */
103   myEditCurrentArgument = GroupPoints->LineEdit1;
104   myObject = GEOM::GEOM_Object::_nil();
105
106   myWithShape = true;
107
108   /* type for sub shape selection */
109   GroupPoints->ComboBox1->insertItem("Compound");
110   GroupPoints->ComboBox1->insertItem("Compsolid");
111   GroupPoints->ComboBox1->insertItem("Solid");
112   GroupPoints->ComboBox1->insertItem("Shell");
113   GroupPoints->ComboBox1->insertItem("Face");
114   GroupPoints->ComboBox1->insertItem("Wire");
115   GroupPoints->ComboBox1->insertItem("Edge");
116   GroupPoints->ComboBox1->insertItem("Vertex");
117   GroupPoints->ComboBox1->insertItem("Shape");
118
119   if (SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
120       != OCCViewer_Viewer::Type())
121     GroupPoints->CheckButton1->setEnabled(false);
122
123   /* signals and slots connections */
124   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
125
126   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
127   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
128
129   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
130   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
131
132   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
133   connect(GroupPoints->CheckButton1, SIGNAL(stateChanged(int)), this, SLOT(SubShapeToggled()));
134
135   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
136           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
137
138   updateButtonState();
139
140   SelectionIntoArgument();
141 }
142
143
144 //=================================================================================
145 // function : ClickOnOk()
146 // purpose  :
147 //=================================================================================
148 void EntityGUI_SubShapeDlg::ClickOnOk()
149 {
150   if ( ClickOnApply() )
151     ClickOnCancel();
152 }
153
154
155 //=================================================================================
156 // function : ClickOnApply()
157 // purpose  :
158 //=================================================================================
159 bool EntityGUI_SubShapeDlg::ClickOnApply()
160 {
161   SUIT_Session::session()->activeApplication()->putInfo(tr(""));
162     
163   /* Explode all sub shapes */
164   if( isAllSubShapes() ) {
165     /* More than 30 subshapes : ask confirmation */
166     unsigned int nb = NumberOfSubShapes(myShape, shapeType());
167     if(nb > 30) {
168       const QString caption = tr("GEOM_CONFIRM");
169       const QString text = tr("GEOM_CONFIRM_INFO").arg(nb);
170       const QString button0 = tr("GEOM_BUT_EXPLODE");
171       const QString button1 = tr("GEOM_BUT_CANCEL");
172
173       if(QMessageBox::warning(this, caption, text, button0, button1) != 0)
174         return false;  /* aborted */
175     }
176   }
177
178   return onAccept();
179 }
180
181
182 //=================================================================================
183 // function : SelectionIntoArgument()
184 // purpose  : Called when selection as changed or other case
185 //          : used only by SelectButtonC1A1 (LineEditC1A1)
186 //=================================================================================
187 void EntityGUI_SubShapeDlg::SelectionIntoArgument()
188 {
189   if ( !isAllSubShapes() )
190     return;
191
192   ResetStateOfDialog();
193
194   QString aString = ""; /* name of selection */
195
196   int nbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aString, true );
197   if( nbSel != 1 )
198     return;
199
200   TopoDS_Shape S;
201   Handle(SALOME_InteractiveObject) IO = firstIObject();
202   if ( !IO->hasEntry() )
203   {
204     SUIT_Session::session()->activeApplication()->putInfo( tr( "GEOM_PRP_SHAPE_IN_STUDY" ) );
205     updateButtonState();
206     return;
207   }
208
209   if ( !GEOMBase::GetTopoFromSelection( selectedIO(), S ) ||
210        S.IsNull() ||
211        S.ShapeType() == TopAbs_VERTEX )
212   {
213     updateButtonState();
214     return;
215   }
216
217
218   Standard_Boolean testResult;
219   myObject = GEOMBase::ConvertIOinGEOMObject( IO, testResult );
220   if ( !testResult || myObject->_is_nil() )
221   {
222     updateButtonState();
223     return;
224   }
225
226   myShape = S;
227   GroupPoints->LineEdit1->setText( aString );
228
229
230   int SelectedShapeType = GroupPoints->ComboBox1->currentItem();
231   int count = GroupPoints->ComboBox1->count();
232
233   if ( myWithShape )
234     count = count - 1;
235
236   int i = 0;
237   // Solving PAL5590
238   if ( myShape.ShapeType() == TopAbs_COMPOUND ) {
239     unsigned int nb = NumberOfSubShapes(myShape, TopAbs_COMPOUND);
240     if (nb > 0)
241       i++;
242   }
243   while ( i <= myShape.ShapeType())
244   {
245     GroupPoints->ComboBox1->removeItem( 0 );
246     i++;
247   }
248
249   if ( myShape.ShapeType() == TopAbs_COMPOUND )
250   {
251     if ( myWithShape == false )
252     {
253       GroupPoints->ComboBox1->insertItem( "Shape" );
254       myWithShape = true;
255     }
256   }
257   else
258   {
259     if ( myWithShape == true )
260     {
261       GroupPoints->ComboBox1->removeItem( GroupPoints->ComboBox1->count() - 1 );
262       myWithShape = false;
263     }
264   }
265
266   int count1 = GroupPoints->ComboBox1->count();
267   if ( myWithShape )
268     count1 = count1 - 1;
269
270   if ( SelectedShapeType > myShape.ShapeType() )
271   {
272     if ( SelectedShapeType == 8 )
273     {
274       if ( myShape.ShapeType() != TopAbs_COMPOUND )
275       {
276         GroupPoints->ComboBox1->setCurrentItem( 0 );
277         ComboTextChanged();
278       }
279     }
280     else
281       GroupPoints->ComboBox1->setCurrentItem(count1 - count + SelectedShapeType);
282   }
283   else
284   {
285     GroupPoints->ComboBox1->setCurrentItem( 0 );
286     ComboTextChanged();
287   }
288
289   updateButtonState();
290 }
291
292
293 //=================================================================================
294 // function : SetEditCurrentArgument()
295 // purpose  :
296 //=================================================================================
297 void EntityGUI_SubShapeDlg::SetEditCurrentArgument()
298 {
299   GroupPoints->LineEdit1->setFocus();
300   myEditCurrentArgument = GroupPoints->LineEdit1;
301   
302   GroupPoints->CheckButton1->setChecked( FALSE );
303   SubShapeToggled();
304   SelectionIntoArgument();
305 }
306
307
308 //=================================================================================
309 // function : LineEditReturnPressed()
310 // purpose  :
311 //=================================================================================
312 void EntityGUI_SubShapeDlg::LineEditReturnPressed()
313 {  
314   QLineEdit* send = (QLineEdit*)sender();
315   if(send == GroupPoints->LineEdit1)
316     SetEditCurrentArgument();
317   else
318     return;
319
320   GEOMBase_Skeleton::LineEditReturnPressed();
321 }
322
323
324 //=================================================================================
325 // function : DeactivateActiveDialog()
326 // purpose  :
327 //=================================================================================
328 void EntityGUI_SubShapeDlg::DeactivateActiveDialog()
329 {
330   if (GroupConstructors->isEnabled()) {
331     GEOMBase_Skeleton::DeactivateActiveDialog();
332   }
333 }
334
335
336 //=================================================================================
337 // function : ActivateThisDialog()
338 // purpose  :
339 //=================================================================================
340 void EntityGUI_SubShapeDlg::ActivateThisDialog()
341 {
342   GEOMBase_Skeleton::ActivateThisDialog();
343   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
344           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
345   SubShapeToggled();
346   updateButtonState();
347 }
348
349
350 //=================================================================================
351 // function : enterEvent()
352 // purpose  :
353 //=================================================================================
354 void EntityGUI_SubShapeDlg::enterEvent(QEvent* e)
355 {
356   if (!GroupConstructors->isEnabled())
357     ActivateThisDialog();
358 }
359
360 //=================================================================================
361 // function : ResetStateOfDialog()
362 // purpose  : Completely reset the state of method including local context
363 //=================================================================================
364 void EntityGUI_SubShapeDlg::ResetStateOfDialog()
365 {
366   myObject = GEOM::GEOM_Object::_nil();
367   myShape.Nullify();
368   myEditCurrentArgument->setText("");
369
370   int SelectedShapeType = GroupPoints->ComboBox1->currentItem();
371   int count = GroupPoints->ComboBox1->count();
372   if ( myWithShape )
373     count = count - 1;
374
375   /* type for sub shape selection */
376   GroupPoints->ComboBox1->clear();
377   GroupPoints->ComboBox1->insertItem("Compound");
378   GroupPoints->ComboBox1->insertItem("Compsolid");
379   GroupPoints->ComboBox1->insertItem("Solid");
380   GroupPoints->ComboBox1->insertItem("Shell");
381   GroupPoints->ComboBox1->insertItem("Face");
382   GroupPoints->ComboBox1->insertItem("Wire");
383   GroupPoints->ComboBox1->insertItem("Edge");
384   GroupPoints->ComboBox1->insertItem("Vertex");
385   GroupPoints->ComboBox1->insertItem("Shape");
386   
387   myWithShape = true;
388   
389   GroupPoints->ComboBox1->setCurrentItem( 8 - count + SelectedShapeType );
390   ComboTextChanged();
391
392   updateButtonState();
393 }
394
395
396 //=================================================================================
397 // function : SubShapeToggled()
398 // purpose  : Allow user selection of all or only selected sub shapes
399 //          : Called when 'CheckButton1' state change
400 //=================================================================================
401 void EntityGUI_SubShapeDlg::SubShapeToggled()
402 {
403   if ( isAllSubShapes() )
404     globalSelection( GEOM_ALLSHAPES );
405   else
406     localSelection( myObject, shapeType() );
407 }
408
409
410 //=================================================================================
411 // function : ComboTextChanged()
412 // purpose  : 
413 //=================================================================================
414 void EntityGUI_SubShapeDlg::ComboTextChanged()
415 {
416   /* Select sub shapes mode not checked */
417   updateButtonState();
418   SubShapeToggled();    
419 }
420
421
422 //=================================================================================
423 // function : NumberOfSubShapes()
424 // purpose  :
425 //=================================================================================
426 unsigned int EntityGUI_SubShapeDlg::NumberOfSubShapes (const TopoDS_Shape& S,
427                                                        const int shapeType) const
428 {
429   if (S.IsNull())
430     return 0;
431
432   unsigned int index = 0;
433   TopTools_MapOfShape M;
434
435   if (S.ShapeType() == TopAbs_COMPOUND &&
436       (TopAbs_ShapeEnum(shapeType) == TopAbs_SHAPE ||
437        TopAbs_ShapeEnum(shapeType) == TopAbs_COMPSOLID ||
438        TopAbs_ShapeEnum(shapeType) == TopAbs_COMPOUND)) {
439     TopoDS_Iterator It (S, Standard_True, Standard_True);
440     for (; It.More(); It.Next()) {
441       if (M.Add(It.Value())) {
442         if (TopAbs_ShapeEnum(shapeType) == TopAbs_SHAPE ||
443             TopAbs_ShapeEnum(shapeType) == It.Value().ShapeType()) {
444           index++;
445         }
446       }
447     }
448   } else {
449     TopExp_Explorer Exp (S, TopAbs_ShapeEnum(shapeType));
450     for (; Exp.More(); Exp.Next()) {
451       if (M.Add(Exp.Current())) {
452         index++;
453       }
454     }
455   }
456
457   M.Clear();
458   return index;
459 }
460
461 //=================================================================================
462 // function : updateButtonState
463 // purpose  :
464 //=================================================================================
465 void EntityGUI_SubShapeDlg::updateButtonState()
466 {
467   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() != OCCViewer_Viewer::Type() ||
468        myObject->_is_nil() || shapeType() == TopAbs_SHAPE || shapeType() == TopAbs_COMPOUND ) {
469     GroupPoints->CheckButton1->setChecked( FALSE );
470     GroupPoints->CheckButton1->setEnabled( FALSE );
471   }
472   else
473     GroupPoints->CheckButton1->setEnabled( TRUE );
474 }
475
476 //=================================================================================
477 // function : isAllSubShapes
478 // purpose  :
479 //=================================================================================
480 bool EntityGUI_SubShapeDlg::isAllSubShapes() const
481 {
482   return !GroupPoints->CheckButton1->isChecked() || !GroupPoints->CheckButton1->isEnabled();
483 }
484
485 //=================================================================================
486 // function : shapeType
487 // purpose  :
488 //=================================================================================
489 int EntityGUI_SubShapeDlg::shapeType() const
490 {
491   int type = GroupPoints->ComboBox1->currentItem();
492
493   if (myObject->_is_nil())
494     return type;
495
496   // Solving PAL5590
497   type += myShape.ShapeType() + 1;
498   if (myShape.ShapeType() == TopAbs_COMPOUND &&
499       NumberOfSubShapes(myShape, TopAbs_COMPOUND) > 0) {
500     type--;
501   }
502
503   return type;
504 }
505
506 //=================================================================================
507 // function : createOperation
508 // purpose  :
509 //=================================================================================
510 GEOM::GEOM_IOperations_ptr EntityGUI_SubShapeDlg::createOperation()
511 {
512   return getGeomEngine()->GetIShapesOperations( getStudyId() );
513 }
514
515 //=================================================================================
516 // function : isValid
517 // purpose  :
518 //=================================================================================
519 bool EntityGUI_SubShapeDlg::isValid( QString& msg )
520 {
521   bool isOk = false;
522   if ( !myObject->_is_nil() ) {
523     if ( isAllSubShapes() )
524       isOk = true;
525     else if ( IObjectCount() == 1 ) {
526       Standard_Boolean aResult = Standard_False;
527       GEOM::GEOM_Object_var anObj =
528         GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
529       
530       if ( aResult && !anObj->_is_nil() ) {
531         TColStd_IndexedMapOfInteger aMapIndex;
532         ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
533         isOk = aMapIndex.Extent() > 0;
534         if ( !isOk )
535           msg += tr( "NO_SUBSHAPES_SELECTED" );
536       }
537     }
538   }
539   return isOk;
540 }
541
542 //=================================================================================
543 // function : execute
544 // purpose  :
545 //=================================================================================
546 bool EntityGUI_SubShapeDlg::execute( ObjectList& objects )
547 {
548   GEOM::ListOfGO_var aList = GEOM::GEOM_IShapesOperations::_narrow(
549     getOperation() )->MakeExplode( myObject, shapeType(), false );
550     
551   if ( !aList->length() )
552     return false;
553   
554   // Throw away sub-shapes not selected by user if not in preview mode 
555   // and manual selection is active
556   if ( !isAllSubShapes() )
557   {
558     if ( IObjectCount() == 1 ) {
559       Standard_Boolean aResult = Standard_False;
560       GEOM::GEOM_Object_var anObj =
561         GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
562
563       if ( aResult && !anObj->_is_nil() ) {
564         TColStd_IndexedMapOfInteger aMapIndex;
565         ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
566
567         GEOM::GEOM_ILocalOperations_var aLocOp = 
568           getGeomEngine()->GetILocalOperations( getStudyId() );
569
570         for ( int i = 0, n = aList->length(); i < n; i++ )
571           if ( aMapIndex.Contains( aLocOp->GetSubShapeIndex( myObject, aList[i] ) ) )
572             objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
573       }
574     }
575   }
576   else
577     for ( int i = 0, n = aList->length(); i < n; i++ )
578       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
579   
580   return objects.size();
581 }
582
583 //================================================================
584 // Function : getFather
585 // Purpose  : Get father object for object to be added in study
586 //            ( called with addInStudy method )
587 //================================================================
588 GEOM::GEOM_Object_ptr EntityGUI_SubShapeDlg::getFather( GEOM::GEOM_Object_ptr )
589 {
590   return myObject;
591 }
592
593 const char* EntityGUI_SubShapeDlg::getNewObjectName() const
594 {
595   return "";
596 }