Salome HOME
Using files from package LightApp instead of SalomeApp
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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(QWidget* parent, const char* name, bool modal, WFlags fl)
56   :GEOMBase_Skeleton(parent, name, modal, fl)
57 {
58   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SUBSHAPE")));
59   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
60
61   setCaption(tr("GEOM_SUBSHAPE_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_SUB_SHAPE"));
65   RadioButton1->setPixmap(image0);
66   RadioButton2->close(TRUE);
67   RadioButton3->close(TRUE);
68
69   GroupPoints = new DlgRef_1Sel1Check1List_QTD(this, "GroupPoints");
70   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
71   GroupPoints->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
72   GroupPoints->TextLabel2->setText(tr("GEOM_SUBSHAPE_TYPE"));
73   GroupPoints->CheckButton1->setText(tr("GEOM_SUBSHAPE_SELECT"));
74   GroupPoints->PushButton1->setPixmap(image1);
75   GroupPoints->LineEdit1->setReadOnly( true );
76
77   Layout1->addWidget(GroupPoints, 1, 0);
78   /***************************************************************/
79
80   Init();
81 }
82
83
84 //=================================================================================
85 // function : ~EntityGUI_SubShapeDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 EntityGUI_SubShapeDlg::~EntityGUI_SubShapeDlg()
89 {
90 }
91
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void EntityGUI_SubShapeDlg::Init()
98 {
99   /* init variables */
100   myEditCurrentArgument = GroupPoints->LineEdit1;
101   myObject = GEOM::GEOM_Object::_nil();
102
103   myWithShape = true;
104
105   /* type for sub shape selection */
106   GroupPoints->ComboBox1->insertItem("Compound");
107   GroupPoints->ComboBox1->insertItem("Compsolid");
108   GroupPoints->ComboBox1->insertItem("Solid");
109   GroupPoints->ComboBox1->insertItem("Shell");
110   GroupPoints->ComboBox1->insertItem("Face");
111   GroupPoints->ComboBox1->insertItem("Wire");
112   GroupPoints->ComboBox1->insertItem("Edge");
113   GroupPoints->ComboBox1->insertItem("Vertex");
114   GroupPoints->ComboBox1->insertItem("Shape");
115
116   if (SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
117       != OCCViewer_Viewer::Type())
118     GroupPoints->CheckButton1->setEnabled(false);
119
120   /* signals and slots connections */
121   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
122   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
123   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
124
125   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
126   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
127
128   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
130
131   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
132   connect(GroupPoints->CheckButton1, SIGNAL(stateChanged(int)), this, SLOT(SubShapeToggled()));
133   
134   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
135           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
136   
137   updateButtonState();
138
139   SelectionIntoArgument();
140 }
141
142
143 //=================================================================================
144 // function : ClickOnOk()
145 // purpose  :
146 //=================================================================================
147 void EntityGUI_SubShapeDlg::ClickOnOk()
148 {
149   if ( ClickOnApply() )
150     ClickOnCancel();
151 }
152
153
154 //=================================================================================
155 // function : ClickOnApply()
156 // purpose  :
157 //=================================================================================
158 bool EntityGUI_SubShapeDlg::ClickOnApply()
159 {
160   SUIT_Session::session()->activeApplication()->putInfo(tr(""));
161     
162   /* Explode all sub shapes */
163   if( isAllSubShapes() ) {
164     /* More than 30 subshapes : ask confirmation */
165     unsigned int nb = NumberOfSubShapes(myShape, shapeType());
166     if(nb > 30) {
167       const QString caption = tr("GEOM_CONFIRM");
168       const QString text = tr("GEOM_CONFIRM_INFO").arg(nb);
169       const QString button0 = tr("GEOM_BUT_EXPLODE");
170       const QString button1 = tr("GEOM_BUT_CANCEL");
171
172       if(QMessageBox::warning(this, caption, text, button0, button1) != 0)
173         return false;  /* aborted */
174     }
175   }
176
177   return onAccept();
178 }
179
180
181 //=================================================================================
182 // function : SelectionIntoArgument()
183 // purpose  : Called when selection as changed or other case
184 //          : used only by SelectButtonC1A1 (LineEditC1A1)
185 //=================================================================================
186 void EntityGUI_SubShapeDlg::SelectionIntoArgument()
187 {
188   if ( !isAllSubShapes() )
189     return;
190
191   ResetStateOfDialog();
192
193   QString aString = ""; /* name of selection */
194
195   int nbSel = GEOMBase::GetNameOfSelectedIObjects( selectedIO(), aString, true );
196   if( nbSel != 1 )
197     return;
198
199   TopoDS_Shape S;
200   Handle(SALOME_InteractiveObject) IO = firstIObject();
201   if ( !IO->hasEntry() )
202   {
203     SUIT_Session::session()->activeApplication()->putInfo( tr( "GEOM_PRP_SHAPE_IN_STUDY" ) );
204     updateButtonState();
205     return;
206   }
207
208   if ( !myGeomBase->GetTopoFromSelection( selectedIO(), S ) ||
209        S.IsNull() ||
210        S.ShapeType() == TopAbs_VERTEX )
211   {
212     updateButtonState();
213     return;
214   }
215
216
217   Standard_Boolean testResult;
218   myObject = GEOMBase::ConvertIOinGEOMObject( IO, testResult );
219   if ( !testResult || myObject->_is_nil() )
220   {
221     updateButtonState();
222     return;
223   }
224
225   myShape = S;
226   GroupPoints->LineEdit1->setText( aString );
227
228
229   int SelectedShapeType = GroupPoints->ComboBox1->currentItem();
230   int count = GroupPoints->ComboBox1->count();
231
232   if ( myWithShape )
233     count = count - 1;
234
235   int i = 0;
236   // Solving PAL5590
237   if ( myShape.ShapeType() == TopAbs_COMPOUND ) {
238     unsigned int nb = NumberOfSubShapes(myShape, TopAbs_COMPOUND);
239     if (nb > 0)
240       i++;
241   }
242   while ( i <= myShape.ShapeType())
243   {
244     GroupPoints->ComboBox1->removeItem( 0 );
245     i++;
246   }
247
248   if ( myShape.ShapeType() == TopAbs_COMPOUND )
249   {
250     if ( myWithShape == false )
251     {
252       GroupPoints->ComboBox1->insertItem( "Shape" );
253       myWithShape = true;
254     }
255   }
256   else
257   {
258     if ( myWithShape == true )
259     {
260       GroupPoints->ComboBox1->removeItem( GroupPoints->ComboBox1->count() - 1 );
261       myWithShape = false;
262     }
263   }
264
265   int count1 = GroupPoints->ComboBox1->count();
266   if ( myWithShape )
267     count1 = count1 - 1;
268
269   if ( SelectedShapeType > myShape.ShapeType() )
270   {
271     if ( SelectedShapeType == 8 )
272     {
273       if ( myShape.ShapeType() != TopAbs_COMPOUND )
274       {
275         GroupPoints->ComboBox1->setCurrentItem( 0 );
276         ComboTextChanged();
277       }
278     }
279     else
280       GroupPoints->ComboBox1->setCurrentItem(count1 - count + SelectedShapeType);
281   }
282   else
283   {
284     GroupPoints->ComboBox1->setCurrentItem( 0 );
285     ComboTextChanged();
286   }
287
288   updateButtonState();
289 }
290
291
292 //=================================================================================
293 // function : SetEditCurrentArgument()
294 // purpose  :
295 //=================================================================================
296 void EntityGUI_SubShapeDlg::SetEditCurrentArgument()
297 {
298   GroupPoints->LineEdit1->setFocus();
299   myEditCurrentArgument = GroupPoints->LineEdit1;
300   
301   GroupPoints->CheckButton1->setChecked( FALSE );
302   SubShapeToggled();
303   SelectionIntoArgument();
304 }
305
306
307 //=================================================================================
308 // function : LineEditReturnPressed()
309 // purpose  :
310 //=================================================================================
311 void EntityGUI_SubShapeDlg::LineEditReturnPressed()
312 {  
313   QLineEdit* send = (QLineEdit*)sender();
314   if(send == GroupPoints->LineEdit1)
315     SetEditCurrentArgument();
316   else
317     return;
318
319   GEOMBase_Skeleton::LineEditReturnPressed();
320 }
321
322
323 //=================================================================================
324 // function : DeactivateActiveDialog()
325 // purpose  :
326 //=================================================================================
327 void EntityGUI_SubShapeDlg::DeactivateActiveDialog()
328 {
329   if(GroupConstructors->isEnabled()) {
330     GEOMBase_Skeleton::DeactivateActiveDialog();
331   }
332 }
333
334
335 //=================================================================================
336 // function : ActivateThisDialog()
337 // purpose  :
338 //=================================================================================
339 void EntityGUI_SubShapeDlg::ActivateThisDialog()
340 {
341   GEOMBase_Skeleton::ActivateThisDialog();
342   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
343           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
344   SubShapeToggled();
345   updateButtonState();
346 }
347
348
349 //=================================================================================
350 // function : enterEvent()
351 // purpose  :
352 //=================================================================================
353 void EntityGUI_SubShapeDlg::enterEvent(QEvent* e)
354 {
355   if(GroupConstructors->isEnabled())
356     return;
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 }