Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / BlocksGUI / BlocksGUI_ExplodeDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  CEA
4 //
5 //  This library is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU Lesser General Public
7 //  License as published by the Free Software Foundation; either
8 //  version 2.1 of the License.
9 //
10 //  This library is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with this library; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 //
19 //  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org
20 //
21 //
22 //
23 //  File   : BlocksGUI_ExplodeDlg.cxx
24 //  Author : Julia DOROVSKIKH
25 //  Module : GEOM
26 //  $Header$
27
28 #include "BlocksGUI_ExplodeDlg.h"
29
30 #include "DlgRef_SpinBox.h"
31
32 #include "GEOM_Displayer.h"
33
34 #include "QAD_Desktop.h"
35 #include "SALOME_ListIteratorOfListIO.hxx"
36
37 #include "utilities.h"
38
39 #include <TColStd_IndexedMapOfInteger.hxx>
40
41 #include <qmessagebox.h>
42 #include <qtextedit.h>
43 #include <qcheckbox.h>
44
45 //=================================================================================
46 // class    : BlocksGUI_ExplodeDlg()
47 // purpose  : Constructs a BlocksGUI_ExplodeDlg which is a child of 'parent'.
48 //=================================================================================
49 BlocksGUI_ExplodeDlg::BlocksGUI_ExplodeDlg (QWidget* parent,
50                                             SALOME_Selection* Sel,
51                                             bool modal)
52      : GEOMBase_Skeleton(parent, "ExplodeDlg", Sel, modal,
53                          WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
54 {
55   QPixmap image1 (QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_BLOCK_EXPLODE")));
56   QPixmap imageS (QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
57
58   setCaption(tr("GEOM_BLOCK_EXPLODE_TITLE"));
59
60   /***************************************************************/
61   GroupConstructors->setTitle(tr("GEOM_BLOCK_EXPLODE"));
62
63   RadioButton1->setPixmap(image1);
64   RadioButton2->close(TRUE);
65   RadioButton3->close(TRUE);
66
67   // Create first group
68   myGrp1 = new QGroupBox(1, Qt::Horizontal, tr("GEOM_ARGUMENTS"), this);
69
70   QGroupBox* aSelGrp = new QGroupBox(3, Qt::Horizontal, myGrp1);
71   aSelGrp->setFrameStyle(QFrame::NoFrame);
72   aSelGrp->setInsideMargin(0);
73
74   new QLabel(tr("GEOM_MAIN_OBJECT"), aSelGrp);
75   mySelBtn = new QPushButton(aSelGrp);
76   mySelBtn->setPixmap(imageS);
77   mySelName = new QLineEdit(aSelGrp);
78   mySelName->setReadOnly(true);
79
80   QGroupBox* aSpinGrp = new QGroupBox(2, Qt::Horizontal, myGrp1);
81   aSpinGrp->setFrameStyle(QFrame::NoFrame);
82   aSpinGrp->setInsideMargin(0);
83
84   new QLabel(tr("NB_FACES_MIN"), aSpinGrp);
85   mySpinBoxMin = new DlgRef_SpinBox(aSpinGrp);
86
87   new QLabel(tr("NB_FACES_MAX"), aSpinGrp);
88   mySpinBoxMax = new DlgRef_SpinBox(aSpinGrp);
89
90   QGroupBox* anInfoGrp = new QGroupBox(2, Qt::Horizontal, myGrp1);
91   anInfoGrp->setFrameStyle(QFrame::NoFrame);
92   anInfoGrp->setInsideMargin(0);
93
94   myBlocksNb = new QTextEdit(anInfoGrp);
95   myBlocksNb->setReadOnly(true);
96
97   QGroupBox* aCheckGrp = new QGroupBox(3, Qt::Horizontal, myGrp1);
98   aCheckGrp->setFrameStyle(QFrame::NoFrame);
99   aCheckGrp->setInsideMargin(0);
100
101   myCheckBtn = new QCheckBox(aCheckGrp, "CheckButton1");
102   myCheckBtn->setText(tr("GEOM_SUBSHAPE_SELECT"));
103
104   // Add groups to layout
105   Layout1->addWidget(myGrp1, 1, 0);
106   /***************************************************************/
107
108   Init();
109 }
110
111 //=================================================================================
112 // function : ~BlocksGUI_ExplodeDlg()
113 // purpose  : Destroys the object and frees any allocated resources
114 //=================================================================================
115 BlocksGUI_ExplodeDlg::~BlocksGUI_ExplodeDlg()
116 {
117   // no need to delete child widgets, Qt does it all for us
118   clearTemporary();
119 }
120
121 //=================================================================================
122 // function : Init()
123 // purpose  :
124 //=================================================================================
125 void BlocksGUI_ExplodeDlg::Init()
126 {
127   // Set range of spinboxes
128   double SpecificStep = 1.0;
129   mySpinBoxMin->RangeStepAndValidator(0.0, 999.0, SpecificStep, 3);
130   mySpinBoxMax->RangeStepAndValidator(0.0, 999.0, SpecificStep, 3);
131
132   if (QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC)
133     myCheckBtn->setEnabled(false);
134
135   // signals and slots connections
136   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
137   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
138
139   connect(mySelBtn, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140
141   connect(mySpinBoxMin, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
142   connect(mySpinBoxMax, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
143
144   connect(myCheckBtn, SIGNAL(stateChanged(int)), this, SLOT(SubShapeToggled()));
145
146   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
147
148   myConstructorId = -1;
149   ConstructorsClicked(0);
150 }
151
152 //=================================================================================
153 // function : ConstructorsClicked()
154 // purpose  : Radio button management
155 //=================================================================================
156 void BlocksGUI_ExplodeDlg::ConstructorsClicked (int constructorId)
157 {
158   if (myConstructorId == constructorId)
159     return;
160
161   myConstructorId = constructorId;
162
163   switch (constructorId) {
164   case 0:
165     myGrp1->show();
166     mySpinBoxMin->SetValue(6.0);
167     mySpinBoxMax->SetValue(6.0);
168     myCheckBtn->setChecked(FALSE);
169     break;
170   default:
171     break;
172   }
173
174   // init fields
175   myEditCurrentArgument = mySelName;
176   myObject = GEOM::GEOM_Object::_nil();
177
178   activateSelection();
179 }
180
181 //=================================================================================
182 // function : ClickOnOk()
183 // purpose  :
184 //=================================================================================
185 void BlocksGUI_ExplodeDlg::ClickOnOk()
186 {
187   if (ClickOnApply())
188     ClickOnCancel();
189 }
190
191 //=================================================================================
192 // function : ClickOnApply()
193 // purpose  :
194 //=================================================================================
195 bool BlocksGUI_ExplodeDlg::ClickOnApply()
196 {
197   QAD_Application::getDesktop()->putInfo(tr(""));
198
199   // Explode all sub shapes
200   if (isAllSubShapes()) {
201     // More than 30 subshapes : ask confirmation
202     if (myNbBlocks > 30) {
203       const QString caption = tr("GEOM_CONFIRM");
204       const QString text = tr("GEOM_CONFIRM_INFO").arg(myNbBlocks);
205       const QString button0 = tr("GEOM_BUT_EXPLODE");
206       const QString button1 = tr("GEOM_BUT_CANCEL");
207
208       if (QMessageBox::warning(this, caption, text, button0, button1) != 0)
209         return false;  /* aborted */
210     }
211   }
212
213   if (!onAccept())
214     return false;
215
216   activateSelection();
217
218   return true;
219 }
220
221 //=================================================================================
222 // function : SelectionIntoArgument()
223 // purpose  : Called when selection has changed
224 //=================================================================================
225 void BlocksGUI_ExplodeDlg::SelectionIntoArgument()
226 {
227   if (!isAllSubShapes())
228     return;
229
230   myObject = GEOM::GEOM_Object::_nil();
231   mySelName->setText("");
232
233   if (mySelection->IObjectCount() == 1) {
234     Standard_Boolean aResult = Standard_False;
235     GEOM::GEOM_Object_var anObj =
236       GEOMBase::ConvertIOinGEOMObject(mySelection->firstIObject(), aResult);
237
238     if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
239       myObject = anObj;
240       mySelName->setText(GEOMBase::GetName(anObj));
241     }
242   }
243
244   updateButtonState();
245 }
246
247 //=================================================================================
248 // function : SetEditCurrentArgument()
249 // purpose  :
250 //=================================================================================
251 void BlocksGUI_ExplodeDlg::SetEditCurrentArgument()
252 {
253   QPushButton* aSender = (QPushButton*)sender();
254
255   if (mySelBtn == aSender) {
256     mySelName->setFocus();
257     myEditCurrentArgument = mySelName;
258     myCheckBtn->setChecked(FALSE);
259   }
260
261   activateSelection();
262 }
263
264 //=================================================================================
265 // function : ActivateThisDialog()
266 // purpose  :
267 //=================================================================================
268 void BlocksGUI_ExplodeDlg::ActivateThisDialog()
269 {
270   GEOMBase_Skeleton::ActivateThisDialog();
271   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
272
273   activateSelection();
274 }
275
276 //=================================================================================
277 // function : enterEvent()
278 // purpose  :
279 //=================================================================================
280 void BlocksGUI_ExplodeDlg::enterEvent (QEvent* e)
281 {
282   if (!GroupConstructors->isEnabled())
283     this->ActivateThisDialog();
284 }
285
286 //=================================================================================
287 // function : ValueChangedInSpinBox()
288 // purpose  :
289 //=================================================================================
290 void BlocksGUI_ExplodeDlg::ValueChangedInSpinBox (double newValue)
291 {
292   if (!isAllSubShapes())
293     activateSelection();
294   else
295     updateButtonState();
296 }
297
298 //=================================================================================
299 // function : SubShapeToggled()
300 // purpose  : Allow user selection of all or only selected sub shapes
301 //          : Called when 'myCheckBtn' state change
302 //=================================================================================
303 void BlocksGUI_ExplodeDlg::SubShapeToggled()
304 {
305   activateSelection();
306 }
307
308 //=================================================================================
309 // function : activateSelection
310 // purpose  : Redisplay preview and Activate selection
311 //=================================================================================
312 void BlocksGUI_ExplodeDlg::activateSelection()
313 {
314   clearTemporary();
315   erasePreview(true);
316
317   if (isAllSubShapes()) { // Sub-shapes selection disabled
318     globalSelection( GEOM_ALLSHAPES );
319     SelectionIntoArgument();
320
321   } else {
322     displayPreview(true, true, false);
323     globalSelection(GEOM_PREVIEW);
324   }
325 }
326
327 //=================================================================================
328 // function : updateButtonState
329 // purpose  :
330 //=================================================================================
331 void BlocksGUI_ExplodeDlg::updateButtonState()
332 {
333   if (QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC ||
334       myObject->_is_nil()) {
335     myCheckBtn->setChecked(FALSE);
336     myCheckBtn->setEnabled(FALSE);
337   } else {
338     myCheckBtn->setEnabled(TRUE);
339   }
340
341   myNbBlocks = 0;
342
343   if (myObject->_is_nil()) {
344     myBlocksNb->setText("");
345   } else {
346     bool isOnlyBlocks = GEOM::GEOM_IBlocksOperations::_narrow
347       (getOperation())->IsCompoundOfBlocks(myObject,
348                                            (int)mySpinBoxMin->GetValue(),
349                                            (int)mySpinBoxMax->GetValue(),
350                                            myNbBlocks);
351     if (isOnlyBlocks)
352       myBlocksNb->setText(tr("GEOM_NB_BLOCKS_NO_OTHERS").arg(myNbBlocks));
353     else
354       myBlocksNb->setText(tr("GEOM_NB_BLOCKS_SOME_OTHERS").arg(myNbBlocks));
355   }
356 }
357
358 //=================================================================================
359 // function : isAllSubShapes
360 // purpose  :
361 //=================================================================================
362 bool BlocksGUI_ExplodeDlg::isAllSubShapes() const
363 {
364   return !myCheckBtn->isChecked() || !myCheckBtn->isEnabled();
365 }
366
367 //=================================================================================
368 // function : createOperation
369 // purpose  :
370 //=================================================================================
371 GEOM::GEOM_IOperations_ptr BlocksGUI_ExplodeDlg::createOperation()
372 {
373   return getGeomEngine()->GetIBlocksOperations(getStudyId());
374 }
375
376 //=================================================================================
377 // function : isValid()
378 // purpose  : Verify validity of input data
379 //=================================================================================
380 bool BlocksGUI_ExplodeDlg::isValid (QString&)
381 {
382   switch (getConstructorId()) {
383   case 0:
384     if (IsPreview())
385       return !myObject->_is_nil();
386     else
387       return !myObject->_is_nil() && (isAllSubShapes() || mySelection->IObjectCount());
388   default:
389     return false;
390   }
391
392   return false;
393 }
394
395 //=================================================================================
396 // function : execute
397 // purpose  :
398 //=================================================================================
399 bool BlocksGUI_ExplodeDlg::execute (ObjectList& objects)
400 {
401   GEOM::ListOfGO_var aList;
402
403   switch (getConstructorId()) {
404     case 0:
405       aList = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->ExplodeCompoundOfBlocks
406         (myObject,
407          (int)mySpinBoxMin->GetValue(),
408          (int)mySpinBoxMax->GetValue());
409       break;
410   }
411
412   if (!aList->length())
413     return false;
414
415   if (IsPreview()) {
416     clearTemporary();
417
418     // Store objects. They will be put in study when "Apply" is pressed
419     for (int i = 0, n = aList->length(); i < n; i++) {
420       objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
421       myTmpObjs.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
422     }
423
424     return objects.size() ? true : false;
425   }
426
427   // Throw away sub-shapes not selected by user if not in preview mode
428   // and manual selection is active
429   if (!isAllSubShapes())
430   {
431     QMap<QString, char> selected;
432
433     // Get names of selected objects
434     SALOME_ListIteratorOfListIO it (mySelection->StoredIObjects());
435     for (; it.More(); it.Next()) {
436       selected.insert(it.Value()->getName(), 0);
437     }
438
439     // Iterate through result and select objects with names from selection
440     ObjectList toRemoveFromEnggine;
441     ObjectList::iterator anIter;
442     for (anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter) {
443       if (selected.contains(GeometryGUI::GetORB()->object_to_string(*anIter)))
444         objects.push_back(*anIter);
445       else
446         toRemoveFromEnggine.push_back(*anIter);
447     }
448
449     // Remove from engine useless objects
450     ObjectList::iterator anIter2 = toRemoveFromEnggine.begin();
451     for (; anIter2 != toRemoveFromEnggine.end(); ++anIter2)
452       getGeomEngine()->RemoveObject(*anIter2);
453
454     myTmpObjs.clear();
455
456   } else {
457     for (int i = 0, n = aList->length(); i < n; i++)
458       objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
459   }
460
461   return objects.size();
462 }
463
464 //=================================================================================
465 // function : clearTemporary
466 // purpose  : Remove temporary objects from engine
467 //=================================================================================
468 void BlocksGUI_ExplodeDlg::clearTemporary()
469 {
470   ObjectList::iterator anIter;
471   for (anIter = myTmpObjs.begin(); anIter != myTmpObjs.end(); ++anIter)
472     getGeomEngine()->RemoveObject(*anIter);
473
474   myTmpObjs.clear();
475 }
476
477 //================================================================
478 // Function : getFather
479 // Purpose  : Get father object for object to be added in study
480 //            ( called with addInStudy method )
481 //================================================================
482 GEOM::GEOM_Object_ptr BlocksGUI_ExplodeDlg::getFather (GEOM::GEOM_Object_ptr)
483 {
484   return myObject;
485 }