1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : PrimitiveGUI_BoxDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
27 #include "PrimitiveGUI_BoxDlg.h"
30 #include <GeometryGUI.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
39 #include <TopoDS_Shape.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <GEOMImpl_Types.hxx>
48 //=================================================================================
49 // class : PrimitiveGUI_BoxDlg()
50 // purpose : Constructs a PrimitiveGUI_BoxDlg 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 PrimitiveGUI_BoxDlg::PrimitiveGUI_BoxDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
56 bool modal, Qt::WindowFlags fl)
57 : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
59 QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_2P")));
60 QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BOX_DXYZ")));
61 QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
63 setWindowTitle(tr("GEOM_BOX_TITLE"));
65 /***************************************************************/
66 mainFrame()->GroupConstructors->setTitle(tr("GEOM_BOX"));
67 mainFrame()->RadioButton1->setIcon(image0);
68 mainFrame()->RadioButton2->setIcon(image1);
69 mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
70 mainFrame()->RadioButton3->close();
72 GroupPoints = new DlgRef_2Sel(centralWidget());
73 GroupPoints->GroupBox1->setTitle(tr("GEOM_DIAGONAL_POINTS"));
74 GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg(1));
75 GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg(2));
76 GroupPoints->PushButton1->setIcon(image2);
77 GroupPoints->PushButton2->setIcon(image2);
79 GroupDimensions = new DlgRef_3Spin(centralWidget());
80 GroupDimensions->GroupBox1->setTitle(tr("GEOM_BOX_OBJ"));
81 GroupDimensions->TextLabel1->setText(tr("GEOM_DX"));
82 GroupDimensions->TextLabel2->setText(tr("GEOM_DY"));
83 GroupDimensions->TextLabel3->setText(tr("GEOM_DZ"));
85 QVBoxLayout* layout = new QVBoxLayout(centralWidget());
86 layout->setMargin(0); layout->setSpacing(6);
87 layout->addWidget(GroupPoints);
88 layout->addWidget(GroupDimensions);
89 /***************************************************************/
91 setHelpFileName("create_box_page.html");
96 //=================================================================================
97 // function : ~DialogBox_Box()
98 // purpose : Destroys the object and frees any allocated resources
99 //=================================================================================
100 PrimitiveGUI_BoxDlg::~PrimitiveGUI_BoxDlg()
102 // no need to delete child widgets, Qt does it all for us
105 //=================================================================================
108 //=================================================================================
109 void PrimitiveGUI_BoxDlg::Init()
111 // Get setting of step value from file configuration
112 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
113 double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
115 // min, max, step and decimals for spin boxes
116 initSpinBox(GroupDimensions->SpinBox_DX, 0.000001, COORD_MAX, step, "length_precision" );
117 initSpinBox(GroupDimensions->SpinBox_DY, 0.000001, COORD_MAX, step, "length_precision" );
118 initSpinBox(GroupDimensions->SpinBox_DZ, 0.000001, COORD_MAX, step, "length_precision" );
121 GroupPoints->LineEdit1->setReadOnly(true);
122 GroupPoints->LineEdit2->setReadOnly(true);
124 GroupPoints->LineEdit1->setText("");
125 GroupPoints->LineEdit2->setText("");
126 myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
128 double initValue = 200.0;
129 GroupDimensions->SpinBox_DX->setValue(initValue);
130 GroupDimensions->SpinBox_DY->setValue(initValue);
131 GroupDimensions->SpinBox_DZ->setValue(initValue);
133 // signals and slots connections
134 connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
135 connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
137 connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
139 connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
140 connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142 connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
143 connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
145 connect(GroupDimensions->SpinBox_DX, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
146 connect(GroupDimensions->SpinBox_DY, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
147 connect(GroupDimensions->SpinBox_DZ, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
149 connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
151 initName(tr("GEOM_BOX"));
153 setConstructorId(1); // simplest constructor
154 ConstructorsClicked(1);
157 //=================================================================================
158 // function : SetDoubleSpinBoxStep()
159 // purpose : Double spin box management
160 //=================================================================================
161 void PrimitiveGUI_BoxDlg::SetDoubleSpinBoxStep (double step)
163 GroupDimensions->SpinBox_DX->setSingleStep(step);
164 GroupDimensions->SpinBox_DY->setSingleStep(step);
165 GroupDimensions->SpinBox_DZ->setSingleStep(step);
168 //=================================================================================
169 // function : ConstructorsClicked()
170 // purpose : Radio button management
171 //=================================================================================
172 void PrimitiveGUI_BoxDlg::ConstructorsClicked (int constructorId)
174 switch (constructorId) {
177 GroupDimensions->hide();
180 GroupPoints->PushButton1->click();
186 GroupDimensions->show();
187 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
188 globalSelection(); // close local contexts, if any
195 qApp->processEvents();
197 resize(minimumSizeHint());
198 SelectionIntoArgument();
203 //=================================================================================
204 // function : ClickOnOk()
206 //=================================================================================
207 void PrimitiveGUI_BoxDlg::ClickOnOk()
213 //=================================================================================
214 // function : ClickOnApply()
216 //=================================================================================
217 bool PrimitiveGUI_BoxDlg::ClickOnApply()
223 // activate selection and connect selection manager
224 ConstructorsClicked(getConstructorId());
228 //=================================================================================
229 // function : SelectionIntoArgument()
230 // purpose : Called when selection is changed or on dialog initialization or activation
231 //=================================================================================
232 void PrimitiveGUI_BoxDlg::SelectionIntoArgument()
234 if (getConstructorId() != 0)
238 myEditCurrentArgument->setText("");
240 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
241 SALOME_ListIO aSelList;
242 aSelMgr->selectedObjects(aSelList);
244 if (aSelList.Extent() != 1) {
245 if (myEditCurrentArgument == GroupPoints->LineEdit1) myPoint1 = GEOM::GEOM_Object::_nil();
246 else if (myEditCurrentArgument == GroupPoints->LineEdit2) myPoint2 = GEOM::GEOM_Object::_nil();
251 Standard_Boolean testResult = Standard_False;
252 GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
254 if (!testResult || CORBA::is_nil(aSelectedObject))
257 QString aName = GEOMBase::GetName(aSelectedObject);
259 // Get Selected object if selected subshape
261 if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull())
263 TColStd_IndexedMapOfInteger aMap;
264 aSelMgr->GetIndexes(aSelList.First(), aMap);
265 if (aMap.Extent() == 1) // Local Selection
267 int anIndex = aMap(1);
268 aName.append(":vertex_" + QString::number(anIndex));
270 //Find SubShape Object in Father
271 GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather(aSelectedObject, aName);
273 if (aFindedObject == GEOM::GEOM_Object::_nil()) { // Object not found in study
274 GEOM::GEOM_IShapesOperations_var aShapesOp =
275 getGeomEngine()->GetIShapesOperations(getStudyId());
276 aSelectedObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
279 aSelectedObject = aFindedObject; // get Object from study
282 else // Global Selection
284 if (aShape.ShapeType() != TopAbs_VERTEX) {
285 aSelectedObject = GEOM::GEOM_Object::_nil();
291 myEditCurrentArgument->setText(aName);
294 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
295 myGeomGUI->getApp()->selectionMgr()->clearSelected();
296 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
297 this, SLOT(SelectionIntoArgument()));
299 if (myEditCurrentArgument == GroupPoints->LineEdit1) {
300 myPoint1 = aSelectedObject;
301 if (!myPoint1->_is_nil() && myPoint2->_is_nil())
302 GroupPoints->PushButton2->click();
304 else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
305 myPoint2 = aSelectedObject;
306 if (!myPoint2->_is_nil() && myPoint1->_is_nil())
307 GroupPoints->PushButton1->click();
313 //=================================================================================
314 // function : SetEditCurrentArgument()
316 //=================================================================================
317 void PrimitiveGUI_BoxDlg::SetEditCurrentArgument()
319 QPushButton* send = (QPushButton*)sender();
321 // ?? Commented, because we need this flag in ConstructorsClicked, because
322 // SelectionIntoArgument must be called only on dialog creation, and must not be called on
323 // simple switch between constructors (as we need to keep old values in fields in this case)
326 //disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
328 if (send == GroupPoints->PushButton1) {
329 myEditCurrentArgument = GroupPoints->LineEdit1;
330 GroupPoints->PushButton2->setDown(false);
331 GroupPoints->LineEdit2->setEnabled(false);
333 else if (send == GroupPoints->PushButton2) {
334 myEditCurrentArgument = GroupPoints->LineEdit2;
335 GroupPoints->PushButton1->setDown(false);
336 GroupPoints->LineEdit1->setEnabled(false);
340 myEditCurrentArgument->setEnabled(true);
341 myEditCurrentArgument->setFocus();
342 // after setFocus(), because it will be setDown(false) when loses focus
345 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
346 globalSelection(); // close local contexts, if any
347 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
348 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
349 this, SLOT(SelectionIntoArgument()));
351 // seems we need it only to avoid preview disappearing, caused by selection mode change
355 //=================================================================================
356 // function : LineEditReturnPressed()
358 //=================================================================================
359 void PrimitiveGUI_BoxDlg::LineEditReturnPressed()
361 QLineEdit* send = (QLineEdit*)sender();
362 if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2) {
363 myEditCurrentArgument = send;
364 GEOMBase_Skeleton::LineEditReturnPressed();
368 //=================================================================================
369 // function : ActivateThisDialog()
371 //=================================================================================
372 void PrimitiveGUI_BoxDlg::ActivateThisDialog()
374 GEOMBase_Skeleton::ActivateThisDialog();
375 if (getConstructorId() == 0) {
376 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
377 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
378 this, SLOT(SelectionIntoArgument()));
383 //=================================================================================
384 // function : enterEvent [REDEFINED]
386 //=================================================================================
387 void PrimitiveGUI_BoxDlg::enterEvent (QEvent*)
389 if (!mainFrame()->GroupConstructors->isEnabled())
390 ActivateThisDialog();
393 //=================================================================================
394 // function : ValueChangedInSpinBox()
396 //=================================================================================
397 void PrimitiveGUI_BoxDlg::ValueChangedInSpinBox()
402 //=================================================================================
403 // function : createOperation
405 //=================================================================================
406 GEOM::GEOM_IOperations_ptr PrimitiveGUI_BoxDlg::createOperation()
408 return getGeomEngine()->GetI3DPrimOperations(getStudyId());
411 //=================================================================================
412 // function : isValid
414 //=================================================================================
415 bool PrimitiveGUI_BoxDlg::isValid (QString& msg)
418 if( getConstructorId() == 1 )
420 ok = GroupDimensions->SpinBox_DX->isValid( msg, !IsPreview() ) && ok;
421 ok = GroupDimensions->SpinBox_DY->isValid( msg, !IsPreview() ) && ok;
422 ok = GroupDimensions->SpinBox_DZ->isValid( msg, !IsPreview() ) && ok;
424 ok = fabs( GroupDimensions->SpinBox_DX->value() ) > Precision::Confusion() && ok;
425 ok = fabs( GroupDimensions->SpinBox_DY->value() ) > Precision::Confusion() && ok;
426 ok = fabs( GroupDimensions->SpinBox_DZ->value() ) > Precision::Confusion() && ok;
428 return getConstructorId() == 0 ? !(myPoint1->_is_nil() || myPoint2->_is_nil()) : ok;
431 //=================================================================================
432 // function : execute
434 //=================================================================================
435 bool PrimitiveGUI_BoxDlg::execute (ObjectList& objects)
439 GEOM::GEOM_Object_var anObj;
441 GEOM::GEOM_I3DPrimOperations_var anOper = GEOM::GEOM_I3DPrimOperations::_narrow(getOperation());
443 switch (getConstructorId()) {
446 if (!CORBA::is_nil(myPoint1) && !CORBA::is_nil(myPoint2)) {
447 anObj = anOper->MakeBoxTwoPnt(myPoint1, myPoint2);
454 double x = GroupDimensions->SpinBox_DX->value();
455 double y = GroupDimensions->SpinBox_DY->value();
456 double z = GroupDimensions->SpinBox_DZ->value();
458 anObj = anOper->MakeBoxDXDYDZ(x, y, z);
459 if (!anObj->_is_nil() && !IsPreview())
461 QStringList aParameters;
462 aParameters << GroupDimensions->SpinBox_DX->text();
463 aParameters << GroupDimensions->SpinBox_DY->text();
464 aParameters << GroupDimensions->SpinBox_DZ->text();
465 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
472 if (!anObj->_is_nil())
473 objects.push_back(anObj._retn());
478 //=================================================================================
479 // function : addSubshapesToStudy
480 // purpose : virtual method to add new SubObjects if local selection
481 //=================================================================================
482 void PrimitiveGUI_BoxDlg::addSubshapesToStudy()
484 if (getConstructorId() == 0) {
485 QMap<QString, GEOM::GEOM_Object_var> objMap;
486 objMap[GroupPoints->LineEdit1->text()] = myPoint1;
487 objMap[GroupPoints->LineEdit2->text()] = myPoint2;
488 addSubshapesToFather(objMap);