]> SALOME platform Git repositories - modules/geom.git/blob - src/EntityGUI/EntityGUI_SubShapeDlg.cxx
Salome HOME
b782a2bd2ed4a8e3caadaa6cc08b1f6f44bedfad
[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 using namespace std;
30 #include "EntityGUI_SubShapeDlg.h"
31
32 #include "DisplayGUI.h"
33 #include "QAD_RightFrame.h"
34 #include "OCCViewer_Viewer3d.h"
35
36 #include <TopExp_Explorer.hxx>
37 #include <TopTools_MapOfShape.hxx>
38
39 #include <qmessagebox.h>
40
41 //=================================================================================
42 // class    : EntityGUI_SubShapeDlg()
43 // purpose  : Constructs a EntityGUI_SubShapeDlg which is a child of 'parent', with the
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 EntityGUI_SubShapeDlg::EntityGUI_SubShapeDlg(QWidget* parent, const char* name, EntityGUI* theEntityGUI, SALOME_Selection* Sel, Handle(AIS_InteractiveContext) ic, bool modal, WFlags fl)
49   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
50 {
51   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_SUBSHAPE")));
52   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
53
54   setCaption(tr("GEOM_SUBSHAPE_TITLE"));
55
56   /***************************************************************/
57   GroupConstructors->setTitle(tr("GEOM_SUB_SHAPE"));
58   RadioButton1->setPixmap(image0);
59   RadioButton2->close(TRUE);
60   RadioButton3->close(TRUE);
61
62   GroupPoints = new DlgRef_1Sel1Check1List_QTD(this, "GroupPoints");
63   GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
64   GroupPoints->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
65   GroupPoints->TextLabel2->setText(tr("GEOM_SUBSHAPE_TYPE"));
66   GroupPoints->CheckButton1->setText(tr("GEOM_SUBSHAPE_SELECT"));
67   GroupPoints->PushButton1->setPixmap(image1);
68
69   Layout1->addWidget(GroupPoints, 1, 0);
70   /***************************************************************/
71
72   /* Initialisations */
73   myEntityGUI = theEntityGUI;
74   Init(ic);
75 }
76
77
78 //=================================================================================
79 // function : ~EntityGUI_SubShapeDlg()
80 // purpose  : Destroys the object and frees any allocated resources
81 //=================================================================================
82 EntityGUI_SubShapeDlg::~EntityGUI_SubShapeDlg()
83 {
84   // no need to delete child widgets, Qt does it all for us
85 }
86
87
88 //=================================================================================
89 // function : Init()
90 // purpose  :
91 //=================================================================================
92 void EntityGUI_SubShapeDlg::Init(Handle(AIS_InteractiveContext) ic)
93 {
94   /* init variables */
95   myEditCurrentArgument = GroupPoints->LineEdit1;
96
97   myWithShape = true;
98   myAbort = myOkShape = myUseLocalContext = false;
99   myIC = ic;
100   myLocalContextId = -1;
101
102   /* type for sub shape selection */
103   GroupPoints->ComboBox1->insertItem("Compound");
104   GroupPoints->ComboBox1->insertItem("Compsolid");
105   GroupPoints->ComboBox1->insertItem("Solid");
106   GroupPoints->ComboBox1->insertItem("Shell");
107   GroupPoints->ComboBox1->insertItem("Face");
108   GroupPoints->ComboBox1->insertItem("Wire");
109   GroupPoints->ComboBox1->insertItem("Edge");
110   GroupPoints->ComboBox1->insertItem("Vertex");
111   GroupPoints->ComboBox1->insertItem("Shape");
112
113   myShapeType = GroupPoints->ComboBox1->currentItem();
114   myOkSelectSubMode = GroupPoints->CheckButton1->isChecked();
115
116   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC)
117     GroupPoints->CheckButton1->setEnabled(false);
118
119   /* signals and slots connections */
120   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
121   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
122   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
123
124   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
125   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
126
127   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
128   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
129
130   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
131   connect(GroupPoints->CheckButton1, SIGNAL(stateChanged(int)), this, SLOT(AllOrNotAll()));
132   
133   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
134
135   /* displays Dialog */
136   GroupPoints->show();
137   this->show();
138
139   return;
140 }
141
142
143 //=================================================================================
144 // function : ClickOnOk()
145 // purpose  :
146 //=================================================================================
147 void EntityGUI_SubShapeDlg::ClickOnOk()
148 {
149   this->ClickOnApply();
150
151   /* User has aborted or not operation of explode all with many sub shapes */
152   if(myAbort == false)
153     this->ClickOnCancel();
154   else
155     myAbort = false;
156
157   return;
158 }
159
160
161 //=================================================================================
162 // function : ClickOnApply()
163 // purpose  :
164 //=================================================================================
165 void EntityGUI_SubShapeDlg::ClickOnApply()
166 {
167   QAD_Application::getDesktop()->putInfo(tr(""));
168   bool testResult = false;
169   myAbort = false; /* Not aborted by default */
170     
171   /* Explode all sub shapes */
172   if(myOkShape && !myOkSelectSubMode) {
173     /* More than 30 subshapes : ask confirmation */
174     unsigned int nb = NumberOfSubShapes(myShape, myShapeType);
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         testResult = myEntityGUI->OnSubShapeGetAll(myShape, myShapeIOR, myShapeType);
183       else
184         myAbort = true;  /* aborted */
185     }
186     else
187       testResult = myEntityGUI->OnSubShapeGetAll(myShape, myShapeIOR, myShapeType);
188   }
189   /* explode only selected sub shapes */
190   else if(myOkShape && myOkSelectSubMode)
191     testResult = myEntityGUI->OnSubShapeGetSelected(myShape, myShapeIOR, myShapeType, myLocalContextId, myUseLocalContext); 
192
193   if(!testResult) {
194     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_ABORT"));
195     myAbort = true;
196   }
197   else
198     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_DONE"));
199
200   /* Reset all arguments and local context to allow user a new selection ...*/
201   //this->ResetStateOfDialog();
202   return;
203 }
204
205
206 //=================================================================================
207 // function : ClickOnCancel()
208 // purpose  :
209 //=================================================================================
210 void EntityGUI_SubShapeDlg::ClickOnCancel()
211 {
212   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
213     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
214     myIC = v3d->getAISContext();
215     if(myUseLocalContext) {
216       myIC->CloseLocalContext(myLocalContextId);
217       myUseLocalContext = false;
218     }
219     DisplayGUI* myDisplayGUI = new DisplayGUI();
220     myDisplayGUI->OnDisplayAll(true);
221   }
222   GEOMBase_Skeleton::ClickOnCancel();
223   return;
224 }
225
226
227 //=================================================================================
228 // function : SelectionIntoArgument()
229 // purpose  : Called when selection as changed or other case
230 //          : used only by SelectButtonC1A1 (LineEditC1A1)
231 //=================================================================================
232 void EntityGUI_SubShapeDlg::SelectionIntoArgument()
233 {
234   myEditCurrentArgument->setText("");
235   this->ResetStateOfDialog();
236   QString aString = ""; /* name of selection */
237
238   int nbSel = myGeomBase->GetNameOfSelectedIObjects(mySelection, aString);
239   if(nbSel != 1) {
240     myOkShape = false;
241     return;
242   }
243   
244   /* nbSel == 1 */
245   TopoDS_Shape S;
246   Handle(SALOME_InteractiveObject) IO = mySelection->firstIObject();
247   if(!myGeomBase->GetTopoFromSelection(mySelection, S))
248     return;
249   
250   if(!IO->hasEntry()) {
251     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SHAPE_IN_STUDY"));
252     return;
253   }
254
255   if(!S.IsNull() && S.ShapeType() != TopAbs_VERTEX) {
256     if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
257       Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast( IO );
258       myShapeIOR = GIObject->getIOR(); /* the Geom IOR string of selection */
259       myEditCurrentArgument->setText(aString);
260       myShape = S;
261       myOkShape = true;
262     }
263     else {
264       SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
265       SALOMEDS::SObject_var obj = aStudy->FindObjectID(IO->getEntry());
266       SALOMEDS::GenericAttribute_var anAttr;
267       SALOMEDS::AttributeIOR_var anIOR;
268       if(!obj->_is_nil()) {
269         if(obj->FindAttribute(anAttr, "AttributeIOR")) {
270           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
271           myShapeIOR = anIOR->Value();
272           myOkShape = true;
273           myShape = S;
274           GroupPoints->LineEdit1->setText(aString);
275         }
276       }
277     }
278     
279     int SelectedShapeType = GroupPoints->ComboBox1->currentItem();
280     int count = GroupPoints->ComboBox1->count();
281
282     if(myWithShape) 
283       count = count - 1;
284
285     int i = 0;
286     while(i <= myShape.ShapeType()) {
287       GroupPoints->ComboBox1->removeItem(0);
288       i++;
289     }
290
291     if(myShape.ShapeType()==TopAbs_COMPOUND) {
292       if(myWithShape == false) {
293         GroupPoints->ComboBox1->insertItem("Shape");
294         myWithShape = true;
295       }
296     }
297     else {
298       if(myWithShape == true) {
299         GroupPoints->ComboBox1->removeItem(GroupPoints->ComboBox1->count() - 1);
300         myWithShape = false;
301       }
302     }
303     
304     int count1 = GroupPoints->ComboBox1->count();
305     if(myWithShape)
306       count1 = count1 - 1;
307     
308     if(SelectedShapeType > myShape.ShapeType()) {
309       if(SelectedShapeType == 8) {
310         if(myShape.ShapeType() != TopAbs_COMPOUND) {
311           GroupPoints->ComboBox1->setCurrentItem(0);
312           myShapeType = 8 - count1;
313         }
314       }
315       else {
316         GroupPoints->ComboBox1->setCurrentItem(count1 - count + SelectedShapeType);
317         myShapeType = 8 - count1 + GroupPoints->ComboBox1->currentItem();
318       }
319     } 
320     else {
321       GroupPoints->ComboBox1->setCurrentItem(0);
322       myShapeType = 8 - count1;
323     }    
324   }
325   return;
326 }
327
328
329 //=================================================================================
330 // function : SetEditCurrentArgument()
331 // purpose  :
332 //=================================================================================
333 void EntityGUI_SubShapeDlg::SetEditCurrentArgument()
334 {
335   QPushButton* send = (QPushButton*)sender();
336
337   if(send == GroupPoints->PushButton1) {
338     GroupPoints->LineEdit1->setFocus();
339     myEditCurrentArgument = GroupPoints->LineEdit1;
340   }
341   this->SelectionIntoArgument();
342
343   return;
344 }
345
346
347 //=================================================================================
348 // function : LineEditReturnPressed()
349 // purpose  :
350 //=================================================================================
351 void EntityGUI_SubShapeDlg::LineEditReturnPressed()
352 {  
353   QLineEdit* send = (QLineEdit*)sender();
354   if(send == GroupPoints->LineEdit1)
355     myEditCurrentArgument = GroupPoints->LineEdit1;
356   else
357     return;
358
359   GEOMBase_Skeleton::LineEditReturnPressed();
360   return;
361 }
362
363
364 //=================================================================================
365 // function : DeactivateActiveDialog()
366 // purpose  :
367 //=================================================================================
368 void EntityGUI_SubShapeDlg::DeactivateActiveDialog()
369 {
370   if(GroupConstructors->isEnabled()) {
371     this->ResetStateOfDialog();
372     GEOMBase_Skeleton::DeactivateActiveDialog();
373   }
374   return;
375 }
376
377
378 //=================================================================================
379 // function : ActivateThisDialog()
380 // purpose  :
381 //=================================================================================
382 void EntityGUI_SubShapeDlg::ActivateThisDialog()
383 {
384   GEOMBase_Skeleton::ActivateThisDialog();
385   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
386   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC)
387     GroupPoints->CheckButton1->setEnabled(true);
388   else
389     GroupPoints->CheckButton1->setEnabled(false);
390   return;
391 }
392
393
394 //=================================================================================
395 // function : enterEvent()
396 // purpose  :
397 //=================================================================================
398 void EntityGUI_SubShapeDlg::enterEvent(QEvent* e)
399 {
400   if(GroupConstructors->isEnabled())
401     return;
402   this->ActivateThisDialog();
403   return;
404 }
405
406
407 //=================================================================================
408 // function : closeEvent()
409 // purpose  :
410 //=================================================================================
411 void EntityGUI_SubShapeDlg::closeEvent(QCloseEvent* e)
412 {
413   /* same than click on cancel button */
414   this->ClickOnCancel();
415   return;
416 }
417
418
419 //=================================================================================
420 // function : ResetStateOfDialog()
421 // purpose  : Completely reset the state of method including local context
422 //=================================================================================
423 void EntityGUI_SubShapeDlg::ResetStateOfDialog()
424 {
425   if(myAbort == true) {
426     myOkShape = false;
427     myEditCurrentArgument->setText("");
428   }
429
430   int SelectedShapeType = GroupPoints->ComboBox1->currentItem();
431   int count = GroupPoints->ComboBox1->count();
432   if(myWithShape)
433     count = count - 1;
434
435   /* type for sub shape selection */
436   GroupPoints->ComboBox1->clear();
437   GroupPoints->ComboBox1->insertItem("Compound");
438   GroupPoints->ComboBox1->insertItem("Compsolid");
439   GroupPoints->ComboBox1->insertItem("Solid");
440   GroupPoints->ComboBox1->insertItem("Shell");
441   GroupPoints->ComboBox1->insertItem("Face");
442   GroupPoints->ComboBox1->insertItem("Wire");
443   GroupPoints->ComboBox1->insertItem("Edge");
444   GroupPoints->ComboBox1->insertItem("Vertex");
445   GroupPoints->ComboBox1->insertItem("Shape");
446   myWithShape=true;
447   GroupPoints->ComboBox1->setCurrentItem(8 - count + SelectedShapeType);
448   myOkSelectSubMode = false;
449
450   /* unpress buttons : due to abort box*/
451   buttonApply->setDown(FALSE);
452   buttonOk->setDown(FALSE);
453   GroupPoints->CheckButton1->setChecked(FALSE);
454
455   /* Close its local contact if opened */
456   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
457     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
458     myIC = v3d->getAISContext();
459     if(myUseLocalContext) {
460       myIC->CloseLocalContext(myLocalContextId);
461       myUseLocalContext = false;
462     }
463     DisplayGUI* myDisplayGUI = new DisplayGUI();
464     myDisplayGUI->OnDisplayAll(true);
465   }
466   return;
467 }
468
469
470 //=================================================================================
471 // function : AllOrNotAll()
472 // purpose  : Allow user selection of all or only selected sub shapes
473 //          : Called when 'CheckButton1' state change
474 //=================================================================================
475 void EntityGUI_SubShapeDlg::AllOrNotAll()
476 {
477   /* No sub shape selection if main shape not selected */
478   if(!myOkShape) {
479     this->ResetStateOfDialog();
480     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SELECT_FIRST"));
481     return;
482   }
483   
484   if(myShapeType ==TopAbs_SHAPE && myShape.ShapeType()==TopAbs_COMPOUND) {
485     /* Select sub shapes mode not checked */
486     myOkSelectSubMode = false;    
487     GroupPoints->CheckButton1->setChecked( FALSE );
488     //no meaning to allow user selection for type = shape
489     //TODO - add another message
490     //QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SELECT_FIRST")) ;
491     return;
492   }
493
494   myOkSelectSubMode = GroupPoints->CheckButton1->isChecked();
495
496   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
497     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
498     myIC = v3d->getAISContext();
499
500     if(myUseLocalContext) {
501       myIC->CloseLocalContext(myLocalContextId);
502       myUseLocalContext = false;
503     }
504     DisplayGUI* myDisplayGUI = new DisplayGUI();
505     myDisplayGUI->OnDisplayAll(true);
506   }
507   else {
508     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_NOT_FOR_VTK_VIEWER"));
509     return;
510   }
511
512   if(myOkShape && myOkSelectSubMode) {
513     /* local context is defined into the method */
514     DisplayGUI* myDisplayGUI = new DisplayGUI();
515     myDisplayGUI->PrepareSubShapeSelection(myShapeType, myLocalContextId);    
516     myUseLocalContext = true;
517     QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_SELECT_FACE"));
518   }
519   return;
520 }
521
522
523 //=================================================================================
524 // function : ComboTextChanged()
525 // purpose  : 
526 //=================================================================================
527 void EntityGUI_SubShapeDlg::ComboTextChanged()
528 {
529   if(myOkShape)
530     myShapeType = GroupPoints->ComboBox1->currentItem() + myShape.ShapeType() + 1;
531   else
532     myShapeType = GroupPoints->ComboBox1->currentItem();
533
534   /* Select sub shapes mode not checked */
535   GroupPoints->CheckButton1->setChecked(FALSE);
536   myOkSelectSubMode = FALSE;
537
538   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
539     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
540     myIC = v3d->getAISContext();
541
542     if(myUseLocalContext) {
543       myIC->CloseLocalContext(myLocalContextId);
544       myUseLocalContext = false;
545     }
546     DisplayGUI* myDisplayGUI = new DisplayGUI();
547     myDisplayGUI->OnDisplayAll(true);
548   }
549   return;
550 }
551
552
553 //=================================================================================
554 // function : NumberOfSubShapes()
555 // purpose  :
556 //=================================================================================
557 unsigned int EntityGUI_SubShapeDlg::NumberOfSubShapes(const TopoDS_Shape& S, const int shapeType)
558 {
559   if(S.IsNull())
560     return 0;
561   
562   unsigned int index = 0;
563   TopExp_Explorer Exp(S, TopAbs_ShapeEnum(shapeType));
564   TopTools_MapOfShape M;
565   while(Exp.More()) {
566     if(M.Add(Exp.Current()))
567       index++;
568     Exp.Next();
569   }
570   M.Clear();
571   return index;
572 }