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