]> SALOME platform Git repositories - modules/geom.git/blob - src/EntityGUI/EntityGUI_SubShapeDlg.cxx
Salome HOME
Fix for bug IPAL18899 : 4x REGRESS: Dump python on the study leads to fatal error...
[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("create_explode_page.html");
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(myGeomGUI->getApp()->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   bool isOk = onAccept();
179
180   // restore selection, corresponding to current selection mode
181   SubShapeToggled();
182
183   return isOk;
184 }
185
186
187 //=================================================================================
188 // function : SelectionIntoArgument()
189 // purpose  : Called when selection as changed or other case
190 //          : used only by SelectButtonC1A1 (LineEditC1A1)
191 //=================================================================================
192 void EntityGUI_SubShapeDlg::SelectionIntoArgument()
193 {
194   if ( !isAllSubShapes() )
195     return;
196
197   ResetStateOfDialog();
198
199   QString aString = ""; /* name of selection */
200
201   int nbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aString, true );
202   if( nbSel != 1 )
203     return;
204
205   TopoDS_Shape S;
206   Handle(SALOME_InteractiveObject) IO = firstIObject();
207   if ( !IO->hasEntry() )
208   {
209     SUIT_Session::session()->activeApplication()->putInfo( tr( "GEOM_PRP_SHAPE_IN_STUDY" ) );
210     updateButtonState();
211     return;
212   }
213
214   if ( !GEOMBase::GetTopoFromSelection( selectedIO(), S ) ||
215        S.IsNull() ||
216        S.ShapeType() == TopAbs_VERTEX )
217   {
218     myObject = GEOM::GEOM_Object::_nil();
219     updateButtonState();
220     return;
221   }
222
223
224   Standard_Boolean testResult;
225   myObject = GEOMBase::ConvertIOinGEOMObject( IO, testResult );
226   if ( !testResult || myObject->_is_nil() )
227   {
228     updateButtonState();
229     return;
230   }
231
232   myShape = S;
233   GroupPoints->LineEdit1->setText( aString );
234
235
236   int SelectedShapeType = GroupPoints->ComboBox1->currentItem();
237   int count = GroupPoints->ComboBox1->count();
238
239   if ( myWithShape )
240     count = count - 1;
241
242   int i = 0;
243   // Solving PAL5590
244   if ( myShape.ShapeType() == TopAbs_COMPOUND ) {
245     unsigned int nb = NumberOfSubShapes(myShape, TopAbs_COMPOUND);
246     if (nb > 0)
247       i++;
248   }
249   while ( i <= myShape.ShapeType())
250   {
251     GroupPoints->ComboBox1->removeItem( 0 );
252     i++;
253   }
254
255   if ( myShape.ShapeType() == TopAbs_COMPOUND )
256   {
257     if ( myWithShape == false )
258     {
259       GroupPoints->ComboBox1->insertItem( "Shape" );
260       myWithShape = true;
261     }
262   }
263   else
264   {
265     if ( myWithShape == true )
266     {
267       GroupPoints->ComboBox1->removeItem( GroupPoints->ComboBox1->count() - 1 );
268       myWithShape = false;
269     }
270   }
271
272   int count1 = GroupPoints->ComboBox1->count();
273   if ( myWithShape )
274     count1 = count1 - 1;
275
276   if ( SelectedShapeType > myShape.ShapeType() )
277   {
278     if ( SelectedShapeType == 8 )
279     {
280       if ( myShape.ShapeType() != TopAbs_COMPOUND )
281       {
282         GroupPoints->ComboBox1->setCurrentItem( 0 );
283         ComboTextChanged();
284       }
285     }
286     else
287       GroupPoints->ComboBox1->setCurrentItem(count1 - count + SelectedShapeType);
288   }
289   else
290   {
291     GroupPoints->ComboBox1->setCurrentItem( 0 );
292     ComboTextChanged();
293   }
294
295   updateButtonState();
296 }
297
298
299 //=================================================================================
300 // function : SetEditCurrentArgument()
301 // purpose  :
302 //=================================================================================
303 void EntityGUI_SubShapeDlg::SetEditCurrentArgument()
304 {
305   GroupPoints->LineEdit1->setFocus();
306   myEditCurrentArgument = GroupPoints->LineEdit1;
307
308   GroupPoints->CheckButton1->setChecked( FALSE );
309   SubShapeToggled();
310   SelectionIntoArgument();
311 }
312
313
314 //=================================================================================
315 // function : LineEditReturnPressed()
316 // purpose  :
317 //=================================================================================
318 void EntityGUI_SubShapeDlg::LineEditReturnPressed()
319 {
320   QLineEdit* send = (QLineEdit*)sender();
321   if(send == GroupPoints->LineEdit1)
322     SetEditCurrentArgument();
323   else
324     return;
325
326   GEOMBase_Skeleton::LineEditReturnPressed();
327 }
328
329
330 //=================================================================================
331 // function : DeactivateActiveDialog()
332 // purpose  :
333 //=================================================================================
334 void EntityGUI_SubShapeDlg::DeactivateActiveDialog()
335 {
336   if (GroupConstructors->isEnabled()) {
337     GEOMBase_Skeleton::DeactivateActiveDialog();
338   }
339 }
340
341
342 //=================================================================================
343 // function : ActivateThisDialog()
344 // purpose  :
345 //=================================================================================
346 void EntityGUI_SubShapeDlg::ActivateThisDialog()
347 {
348   GEOMBase_Skeleton::ActivateThisDialog();
349   connect(myGeomGUI->getApp()->selectionMgr(),
350           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
351   SubShapeToggled();
352   updateButtonState();
353 }
354
355
356 //=================================================================================
357 // function : enterEvent()
358 // purpose  :
359 //=================================================================================
360 void EntityGUI_SubShapeDlg::enterEvent(QEvent* e)
361 {
362   if (!GroupConstructors->isEnabled())
363     ActivateThisDialog();
364 }
365
366 //=================================================================================
367 // function : ResetStateOfDialog()
368 // purpose  : Completely reset the state of method including local context
369 //=================================================================================
370 void EntityGUI_SubShapeDlg::ResetStateOfDialog()
371 {
372   myObject = GEOM::GEOM_Object::_nil();
373   myShape.Nullify();
374   myEditCurrentArgument->setText("");
375
376   int SelectedShapeType = GroupPoints->ComboBox1->currentItem();
377   int count = GroupPoints->ComboBox1->count();
378   if ( myWithShape )
379     count = count - 1;
380
381   /* type for sub shape selection */
382   GroupPoints->ComboBox1->clear();
383   GroupPoints->ComboBox1->insertItem("Compound");
384   GroupPoints->ComboBox1->insertItem("Compsolid");
385   GroupPoints->ComboBox1->insertItem("Solid");
386   GroupPoints->ComboBox1->insertItem("Shell");
387   GroupPoints->ComboBox1->insertItem("Face");
388   GroupPoints->ComboBox1->insertItem("Wire");
389   GroupPoints->ComboBox1->insertItem("Edge");
390   GroupPoints->ComboBox1->insertItem("Vertex");
391   GroupPoints->ComboBox1->insertItem("Shape");
392
393   myWithShape = true;
394
395   GroupPoints->ComboBox1->setCurrentItem( 8 - count + SelectedShapeType );
396   ComboTextChanged();
397
398   updateButtonState();
399 }
400
401
402 //=================================================================================
403 // function : SubShapeToggled()
404 // purpose  : Allow user selection of all or only selected sub shapes
405 //          : Called when 'CheckButton1' state change
406 //=================================================================================
407 void EntityGUI_SubShapeDlg::SubShapeToggled()
408 {
409   globalSelection( GEOM_ALLSHAPES );
410
411   if ( !isAllSubShapes() )
412     localSelection( myObject, shapeType() );
413 }
414
415
416 //=================================================================================
417 // function : ComboTextChanged()
418 // purpose  :
419 //=================================================================================
420 void EntityGUI_SubShapeDlg::ComboTextChanged()
421 {
422   /* Select sub shapes mode not checked */
423   updateButtonState();
424   SubShapeToggled();
425 }
426
427
428 //=================================================================================
429 // function : NumberOfSubShapes()
430 // purpose  :
431 //=================================================================================
432 unsigned int EntityGUI_SubShapeDlg::NumberOfSubShapes (const TopoDS_Shape& S,
433                                                        const int shapeType) const
434 {
435   if (S.IsNull())
436     return 0;
437
438   unsigned int index = 0;
439   TopTools_MapOfShape M;
440
441   if (S.ShapeType() == TopAbs_COMPOUND &&
442       (TopAbs_ShapeEnum(shapeType) == TopAbs_SHAPE ||
443        TopAbs_ShapeEnum(shapeType) == TopAbs_COMPSOLID ||
444        TopAbs_ShapeEnum(shapeType) == TopAbs_COMPOUND)) {
445     TopoDS_Iterator It (S, Standard_True, Standard_True);
446     for (; It.More(); It.Next()) {
447       if (M.Add(It.Value())) {
448         if (TopAbs_ShapeEnum(shapeType) == TopAbs_SHAPE ||
449             TopAbs_ShapeEnum(shapeType) == It.Value().ShapeType()) {
450           index++;
451         }
452       }
453     }
454   } else {
455     TopExp_Explorer Exp (S, TopAbs_ShapeEnum(shapeType));
456     for (; Exp.More(); Exp.Next()) {
457       if (M.Add(Exp.Current())) {
458         index++;
459       }
460     }
461   }
462
463   M.Clear();
464   return index;
465 }
466
467 //=================================================================================
468 // function : updateButtonState
469 // purpose  :
470 //=================================================================================
471 void EntityGUI_SubShapeDlg::updateButtonState()
472 {
473   if ( SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() != OCCViewer_Viewer::Type() ||
474        myObject->_is_nil() || shapeType() == TopAbs_SHAPE || shapeType() == TopAbs_COMPOUND ) {
475     GroupPoints->CheckButton1->setChecked( FALSE );
476     GroupPoints->CheckButton1->setEnabled( FALSE );
477   }
478   else
479     GroupPoints->CheckButton1->setEnabled( TRUE );
480 }
481
482 //=================================================================================
483 // function : isAllSubShapes
484 // purpose  :
485 //=================================================================================
486 bool EntityGUI_SubShapeDlg::isAllSubShapes() const
487 {
488   return !GroupPoints->CheckButton1->isChecked() || !GroupPoints->CheckButton1->isEnabled();
489 }
490
491 //=================================================================================
492 // function : shapeType
493 // purpose  :
494 //=================================================================================
495 int EntityGUI_SubShapeDlg::shapeType() const
496 {
497   int type = GroupPoints->ComboBox1->currentItem();
498
499   if (myObject->_is_nil())
500     return type;
501
502   // Solving PAL5590
503   type += myShape.ShapeType() + 1;
504   if (myShape.ShapeType() == TopAbs_COMPOUND &&
505       NumberOfSubShapes(myShape, TopAbs_COMPOUND) > 0) {
506     type--;
507   }
508
509   return type;
510 }
511
512 //=================================================================================
513 // function : createOperation
514 // purpose  :
515 //=================================================================================
516 GEOM::GEOM_IOperations_ptr EntityGUI_SubShapeDlg::createOperation()
517 {
518   return getGeomEngine()->GetIShapesOperations( getStudyId() );
519 }
520
521 //=================================================================================
522 // function : isValid
523 // purpose  :
524 //=================================================================================
525 bool EntityGUI_SubShapeDlg::isValid( QString& msg )
526 {
527   bool isOk = false;
528   Standard_Boolean testResult;  
529   GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
530   if ( !testResult || myObject->_is_nil() )  {
531     updateButtonState();
532     return isOk;
533   }
534   if ( !myObject->_is_nil() ) {
535     if ( isAllSubShapes() )
536       isOk = true;
537     else if ( IObjectCount() == 1 ) {
538       if ( testResult && !anObj->_is_nil() ) {
539         TColStd_IndexedMapOfInteger aMapIndex;
540         myGeomGUI->getApp()->selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
541         isOk = aMapIndex.Extent() > 0;
542         if ( !isOk )
543           msg += tr( "NO_SUBSHAPES_SELECTED" );
544       }
545     }
546   }
547   return isOk;
548 }
549
550 //=================================================================================
551 // function : execute
552 // purpose  :
553 //=================================================================================
554 bool EntityGUI_SubShapeDlg::execute( ObjectList& objects )
555 {
556   GEOM::ListOfGO_var aList = GEOM::GEOM_IShapesOperations::_narrow(
557     getOperation() )->MakeExplode( myObject, shapeType(), true );
558
559   if ( !aList->length() )
560     return false;
561
562   // Throw away sub-shapes not selected by user if not in preview mode
563   // and manual selection is active
564   if ( !isAllSubShapes() )
565   {
566     if ( IObjectCount() == 1 ) {
567       Standard_Boolean aResult = Standard_False;
568       GEOM::GEOM_Object_var anObj =
569         GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
570
571       if ( aResult && !anObj->_is_nil() ) {
572         TColStd_IndexedMapOfInteger aMapIndex;
573         myGeomGUI->getApp()->selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
574
575         GEOM::GEOM_ILocalOperations_var aLocOp =
576           getGeomEngine()->GetILocalOperations( getStudyId() );
577
578         for ( int i = 0, n = aList->length(); i < n; i++ )
579           if ( aMapIndex.Contains( aLocOp->GetSubShapeIndex( myObject, aList[i] ) ) )
580             objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
581       }
582     }
583   }
584   else
585     for ( int i = 0, n = aList->length(); i < n; i++ )
586       objects.push_back( GEOM::GEOM_Object::_duplicate( aList[i] ) );
587
588   return objects.size();
589 }
590
591 //================================================================
592 // Function : getFather
593 // Purpose  : Get father object for object to be added in study
594 //            ( called with addInStudy method )
595 //================================================================
596 GEOM::GEOM_Object_ptr EntityGUI_SubShapeDlg::getFather( GEOM::GEOM_Object_ptr )
597 {
598   return myObject;
599 }
600
601 const char* EntityGUI_SubShapeDlg::getNewObjectName() const
602 {
603   return "";
604 }