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 : TransformationGUI_TranslationDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
27 #include "TransformationGUI_TranslationDlg.h"
30 #include <GeometryGUI.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Desktop.h>
36 #include <SUIT_ViewWindow.h>
37 #include <SUIT_ViewManager.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40 #include <GEOM_AISVector.hxx>
42 #include <SOCC_ViewModel.h>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Vertex.hxx>
50 #include <TColStd_IndexedMapOfInteger.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <ShapeAnalysis_Edge.hxx>
53 #include <BRep_Tool.hxx>
54 #include <BRepBuilderAPI_MakeEdge.hxx>
55 #include <BRep_Builder.hxx>
57 #include <GEOMImpl_Types.hxx>
59 //=================================================================================
60 // class : TransformationGUI_TranslationDlg()
61 // purpose : Constructs a TransformationGUI_TranslationDlg which is a child of 'parent', with the
62 // name 'name' and widget flags set to 'f'.
63 // The dialog will by default be modeless, unless you set 'modal' to
64 // TRUE to construct a modal dialog.
65 //=================================================================================
66 TransformationGUI_TranslationDlg::TransformationGUI_TranslationDlg
67 (GeometryGUI* theGeometryGUI, QWidget* parent, bool modal, Qt::WindowFlags fl)
68 : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
71 SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
72 QPixmap image0 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TRANSLATION_DXYZ")));
73 QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TRANSLATION_POINTS")));
74 QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_TRANSLATION_VECTOR")));
75 QPixmap image3 (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
77 setWindowTitle(tr("GEOM_TRANSLATION_TITLE"));
79 /***************************************************************/
80 mainFrame()->GroupConstructors->setTitle(tr("GEOM_TRANSLATION"));
81 mainFrame()->RadioButton1->setIcon(image0);
82 mainFrame()->RadioButton2->setIcon(image1);
83 mainFrame()->RadioButton3->setIcon(image2);
84 mainFrame()->RadioButton1->setChecked(true);
86 GroupPoints = new DlgRef_3Sel3Spin2Check(centralWidget());
87 GroupPoints->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
88 GroupPoints->TextLabel1->setText(tr("GEOM_OBJECTS"));
89 GroupPoints->TextLabel3->setText(tr("GEOM_POINT_I").arg(2));
90 GroupPoints->TextLabel4->setText(tr("GEOM_DX"));
91 GroupPoints->TextLabel5->setText(tr("GEOM_DY"));
92 GroupPoints->TextLabel6->setText(tr("GEOM_DZ"));
93 GroupPoints->PushButton1->setIcon(image3);
94 GroupPoints->PushButton2->setIcon(image3);
95 GroupPoints->PushButton3->setIcon(image3);
96 GroupPoints->CheckBox1->setText(tr("Activate Distance"));
97 GroupPoints->CheckBox2->setText(tr("GEOM_CREATE_COPY"));
99 QVBoxLayout* layout = new QVBoxLayout(centralWidget());
100 layout->setMargin(0); layout->setSpacing(6);
101 layout->addWidget(GroupPoints);
102 /***************************************************************/
104 setHelpFileName("translation_operation_page.html");
106 // Activate Create a Copy mode
107 GroupPoints->CheckBox2->setChecked(true);
108 CreateCopyModeChanged();
113 //=================================================================================
114 // function : ~TransformationGUI_TranslationDlg()
115 // purpose : Destroys the object and frees any allocated resources
116 //=================================================================================
117 TransformationGUI_TranslationDlg::~TransformationGUI_TranslationDlg()
119 // no need to delete child widgets, Qt does it all for us
122 //=================================================================================
125 //=================================================================================
126 void TransformationGUI_TranslationDlg::Init()
128 // Get setting of step value from file configuration
129 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
130 double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
132 // min, max, step and decimals for spin boxes & initial values
133 initSpinBox(GroupPoints->SpinBox1, COORD_MIN, COORD_MAX, step, "length_precision" );
134 initSpinBox(GroupPoints->SpinBox2, COORD_MIN, COORD_MAX, step, "length_precision" );
135 initSpinBox(GroupPoints->SpinBox3, COORD_MIN, COORD_MAX, step, "length_precision" );
137 GroupPoints->SpinBox1->setValue(0.0);
138 GroupPoints->SpinBox2->setValue(0.0);
139 GroupPoints->SpinBox3->setValue(0.0);
142 GroupPoints->LineEdit1->setReadOnly(true);
143 GroupPoints->LineEdit2->setReadOnly(true);
144 GroupPoints->LineEdit3->setReadOnly(true);
146 GroupPoints->LineEdit1->setText("");
147 GroupPoints->LineEdit2->setText("");
148 GroupPoints->LineEdit3->setText("");
155 mainFrame()->GroupBoxPublish->show();
157 // signals and slots connections
158 connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
159 connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
161 connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
163 connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
164 connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
165 connect(GroupPoints->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
167 connect(GroupPoints->SpinBox1, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
168 connect(GroupPoints->SpinBox2, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
169 connect(GroupPoints->SpinBox3, SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox()));
171 connect(myGeomGUI, SIGNAL(SignalDefaultStepValueChanged(double)), this, SLOT(SetDoubleSpinBoxStep(double)));
173 connect(GroupPoints->CheckBox1, SIGNAL(toggled(bool)), this, SLOT(ActivateDistanceChanged()));
174 connect(GroupPoints->CheckBox2, SIGNAL(toggled(bool)), this, SLOT(CreateCopyModeChanged()));
176 initName(tr("GEOM_TRANSLATION"));
178 ConstructorsClicked(0);
181 //=================================================================================
182 // function : SetDoubleSpinBoxStep()
183 // purpose : Double spin box management
184 //=================================================================================
185 void TransformationGUI_TranslationDlg::SetDoubleSpinBoxStep (double step)
187 GroupPoints->SpinBox1->setSingleStep(step);
188 GroupPoints->SpinBox2->setSingleStep(step);
189 GroupPoints->SpinBox3->setSingleStep(step);
192 //=================================================================================
193 // function : ConstructorsClicked()
194 // purpose : Radio button management
195 //=================================================================================
196 void TransformationGUI_TranslationDlg::ConstructorsClicked (int constructorId)
198 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
200 switch (constructorId) {
201 case 0: // translation an object by dx, dy, dz
203 GroupPoints->ShowRows(1, 2, false);
204 GroupPoints->ShowRows(3, 5, true);
206 GroupPoints->TextLabel6->setText(tr("GEOM_DZ"));
207 GroupPoints->SpinBox3->setEnabled(true);
209 GroupPoints->CheckBox1->hide();
211 GroupPoints->PushButton1->click();
214 case 1: // translation an object by 2 points
216 GroupPoints->ShowRows(3, 5, false);
217 GroupPoints->ShowRows(0, 2, true);
219 GroupPoints->TextLabel6->setText(tr("GEOM_DZ"));
220 GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg(1));
221 GroupPoints->LineEdit2->clear();
222 GroupPoints->LineEdit3->clear();
223 GroupPoints->SpinBox3->setEnabled(true);
225 GroupPoints->CheckBox1->hide();
230 GroupPoints->PushButton1->click();
233 case 2: // translation an object by vector
235 GroupPoints->ShowRows(0, 1, true);
236 GroupPoints->ShowRows(2, 4, false);
237 GroupPoints->ShowRows(5, 5, true);
239 GroupPoints->TextLabel6->setText(tr("GEOM_DISTANCE"));
240 GroupPoints->SpinBox3->setValue(0.0);
241 GroupPoints->SpinBox3->setEnabled(false);
242 GroupPoints->TextLabel2->setText(tr("GEOM_VECTOR"));
243 GroupPoints->LineEdit2->clear();
245 GroupPoints->CheckBox1->show();
248 ActivateDistanceChanged();
250 GroupPoints->PushButton1->click();
255 qApp->processEvents();
257 resize(minimumSizeHint());
261 SelectionIntoArgument();
268 //=================================================================================
269 // function : ClickOnOk()
271 //=================================================================================
272 void TransformationGUI_TranslationDlg::ClickOnOk()
278 //=================================================================================
279 // function : ClickOnApply()
281 //=================================================================================
282 bool TransformationGUI_TranslationDlg::ClickOnApply()
284 if (!onAccept(GroupPoints->CheckBox2->isChecked()))
290 myEditCurrentArgument = GroupPoints->LineEdit1;
291 myEditCurrentArgument->setText("");
292 myGeomGUI->getApp()->selectionMgr()->clearSelected();
294 // activate selection and connect selection manager
295 ConstructorsClicked(getConstructorId());
299 //=================================================================================
300 // function : SelectionIntoArgument()
301 // purpose : Called when selection is changed or on dialog initialization or activation
302 //=================================================================================
303 void TransformationGUI_TranslationDlg::SelectionIntoArgument()
307 if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
308 myObjects = getSelected( TopAbs_SHAPE, -1 );
309 if ( !myObjects.isEmpty() ) {
310 QString aName = myObjects.count() > 1 ? QString( "%1_objects").arg( myObjects.count() ) : GEOMBase::GetName( myObjects[0].get() );
311 myEditCurrentArgument->setText( aName );
314 myEditCurrentArgument->setText("");
318 TopAbs_ShapeEnum aNeedType = ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 2 ) ?
319 TopAbs_EDGE : TopAbs_VERTEX;
320 GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
322 if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
323 QString aName = GEOMBase::GetName( aSelectedObject.get() );
324 myEditCurrentArgument->setText( aName );
325 if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
326 if ( getConstructorId() == 1 ) {
327 myPoint1 = aSelectedObject;
329 GroupPoints->PushButton3->click();
330 else if ( myObjects.isEmpty() )
331 GroupPoints->PushButton1->click();
333 else if ( getConstructorId() == 2 ) {
334 myVector = aSelectedObject;
335 if ( myObjects.isEmpty() )
336 GroupPoints->PushButton1->click();
339 else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) {
340 myPoint2 = aSelectedObject;
341 if ( myObjects.isEmpty() )
342 GroupPoints->PushButton1->click();
343 else if ( !myPoint1 )
344 GroupPoints->PushButton2->click();
348 if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
349 if ( getConstructorId() == 1 ) myPoint1.nullify();
350 else myVector.nullify();
352 else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) {
355 myEditCurrentArgument->setText("");
362 //=================================================================================
363 // function : SetEditCurrentArgument()
365 //=================================================================================
366 void TransformationGUI_TranslationDlg::SetEditCurrentArgument()
368 QPushButton* send = (QPushButton*)sender();
370 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
373 if (send == GroupPoints->PushButton1) {
374 myEditCurrentArgument = GroupPoints->LineEdit1;
376 GroupPoints->PushButton2->setDown(false);
377 GroupPoints->PushButton3->setDown(false);
378 GroupPoints->LineEdit2->setEnabled(false);
379 GroupPoints->LineEdit3->setEnabled(false);
381 else if (send == GroupPoints->PushButton2) {
382 myEditCurrentArgument = GroupPoints->LineEdit2;
384 GroupPoints->PushButton1->setDown(false);
385 GroupPoints->PushButton3->setDown(false);
386 GroupPoints->LineEdit1->setEnabled(false);
387 GroupPoints->LineEdit3->setEnabled(false);
389 if (getConstructorId() == 1)
390 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
392 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
394 else if (send == GroupPoints->PushButton3) {
395 myEditCurrentArgument = GroupPoints->LineEdit3;
397 GroupPoints->PushButton1->setDown(false);
398 GroupPoints->PushButton2->setDown(false);
399 GroupPoints->LineEdit1->setEnabled(false);
400 GroupPoints->LineEdit2->setEnabled(false);
402 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
404 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
405 this, SLOT(SelectionIntoArgument()));
408 myEditCurrentArgument->setEnabled(true);
409 myEditCurrentArgument->setFocus();
410 // after setFocus(), because it will be setDown(false) when loses focus
413 // seems we need it only to avoid preview disappearing, caused by selection mode change
417 //=================================================================================
418 // function : ActivateThisDialog()
420 //=================================================================================
421 void TransformationGUI_TranslationDlg::ActivateThisDialog()
423 GEOMBase_Skeleton::ActivateThisDialog();
424 connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
425 this, SLOT( SelectionIntoArgument() ) );
427 ConstructorsClicked( getConstructorId() );
430 //=================================================================================
431 // function : enterEvent()
433 //=================================================================================
434 void TransformationGUI_TranslationDlg::enterEvent (QEvent*)
436 if (!mainFrame()->GroupConstructors->isEnabled())
437 ActivateThisDialog();
440 //=================================================================================
441 // function : ValueChangedInSpinBox()
443 //=================================================================================
444 void TransformationGUI_TranslationDlg::ValueChangedInSpinBox()
449 //=================================================================================
450 // function : createOperation
452 //=================================================================================
453 GEOM::GEOM_IOperations_ptr TransformationGUI_TranslationDlg::createOperation()
455 return getGeomEngine()->GetITransformOperations(getStudyId());
458 //=================================================================================
459 // function : isValid
461 //=================================================================================
462 bool TransformationGUI_TranslationDlg::isValid (QString& msg)
465 switch (getConstructorId()) {
467 ok = GroupPoints->SpinBox1->isValid( msg, !IsPreview() ) &&
468 GroupPoints->SpinBox2->isValid( msg, !IsPreview() ) &&
469 GroupPoints->SpinBox3->isValid( msg, !IsPreview() ) &&
470 !myObjects.isEmpty();
473 ok = myPoint1 && myPoint2 && !myObjects.isEmpty();
476 ok = GroupPoints->SpinBox3->isValid( msg, !IsPreview() ) &&
477 myVector && !myObjects.isEmpty();
485 //=================================================================================
486 // function : execute
488 //=================================================================================
489 bool TransformationGUI_TranslationDlg::execute (ObjectList& objects)
492 bool toCreateCopy = IsPreview() || GroupPoints->CheckBox2->isChecked();
494 GEOM::GEOM_Object_var anObj;
496 GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
498 switch (getConstructorId()) {
501 double dx = GroupPoints->SpinBox1->value();
502 double dy = GroupPoints->SpinBox2->value();
503 double dz = GroupPoints->SpinBox3->value();
505 QStringList aParameters;
506 aParameters<<GroupPoints->SpinBox1->text();
507 aParameters<<GroupPoints->SpinBox2->text();
508 aParameters<<GroupPoints->SpinBox3->text();
511 for (int i = 0; i < myObjects.count(); i++) {
512 myCurrObject = myObjects[i];
513 anObj = anOper->TranslateDXDYDZCopy(myObjects[i].get(), dx, dy, dz);
514 if (!anObj->_is_nil()) {
516 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
517 objects.push_back(anObj._retn());
522 for (int i = 0; i < myObjects.count(); i++) {
523 myCurrObject = myObjects[i];
524 anObj = anOper->TranslateDXDYDZ(myObjects[i].get(), dx, dy, dz);
525 if (!anObj->_is_nil()) {
527 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
528 updateAttributes(anObj, aParameters);
530 objects.push_back(anObj._retn());
540 for (int i = 0; i < myObjects.count(); i++) {
541 myCurrObject = myObjects[i];
542 anObj = anOper->TranslateTwoPointsCopy(myObjects[i].get(), myPoint1.get(), myPoint2.get());
543 if (!anObj->_is_nil())
544 objects.push_back(anObj._retn());
548 for (int i = 0; i < myObjects.count(); i++) {
549 myCurrObject = myObjects[i];
550 anObj = anOper->TranslateTwoPoints(myObjects[i].get(), myPoint1.get(), myPoint2.get());
551 if (!anObj->_is_nil())
552 objects.push_back(anObj._retn());
560 QStringList aParameters;
561 aParameters<<GroupPoints->SpinBox3->text();
562 bool byDistance = GroupPoints->CheckBox1->isChecked();
563 createPathPreview( myVector.get() );
566 double aDistance = GroupPoints->SpinBox3->value();
567 for (int i = 0; i < myObjects.count(); i++) {
568 myCurrObject = myObjects[i];
569 anObj = anOper->TranslateVectorDistance(myObjects[i].get(), myVector.get(), aDistance, toCreateCopy);
570 if (!anObj->_is_nil()) {
572 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
574 updateAttributes(anObj, aParameters);
576 objects.push_back(anObj._retn());
582 for (int i = 0; i < myObjects.count(); i++) {
583 myCurrObject = myObjects[i];
584 anObj = anOper->TranslateVectorCopy(myObjects[i].get(), myVector.get());
585 if (!anObj->_is_nil())
586 objects.push_back(anObj._retn());
590 for (int i = 0; i < myObjects.count(); i++) {
591 myCurrObject = myObjects[i];
592 anObj = anOper->TranslateVector(myObjects[i].get(), myVector.get());
593 if (!anObj->_is_nil())
594 objects.push_back(anObj._retn());
605 //=================================================================================
606 // function : restoreSubShapes
608 //=================================================================================
609 void TransformationGUI_TranslationDlg::restoreSubShapes (SALOMEDS::Study_ptr theStudy,
610 SALOMEDS::SObject_ptr theSObject)
612 if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
613 // we pass here the first operation argument (object) through the list of arguments
614 // because the rotation operation place its arguments in the data structure in another order,
615 // and we need to point the first argument directly
616 GEOM::ListOfGO_var anArgs = new GEOM::ListOfGO;
618 anArgs[0] = myCurrObject.copy();
619 getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, anArgs,
620 /*theFindMethod=*/GEOM::FSM_Transformed,
621 /*theInheritFirstArg=*/true,
622 mainFrame()->CheckBoxAddPrefix->isChecked());
626 //=================================================================================
627 // function : CreateCopyModeChanged()
629 //=================================================================================
630 void TransformationGUI_TranslationDlg::CreateCopyModeChanged()
632 mainFrame()->GroupBoxName->setEnabled(GroupPoints->CheckBox2->isChecked());
635 //=================================================================================
636 // function : ActivateDistanceChanged()
638 //=================================================================================
639 void TransformationGUI_TranslationDlg::ActivateDistanceChanged()
641 GroupPoints->SpinBox3->setEnabled( GroupPoints->CheckBox1->isChecked() );
645 //=================================================================================
646 // function : addSubshapeToStudy
647 // purpose : virtual method to add new SubObjects if local selection
648 //=================================================================================
649 void TransformationGUI_TranslationDlg::addSubshapesToStudy()
651 bool toCreateCopy = IsPreview() || GroupPoints->CheckBox2->isChecked();
653 switch (getConstructorId()) {
655 GEOMBase::PublishSubObject( myPoint1.get() );
656 GEOMBase::PublishSubObject( myPoint2.get() );
659 GEOMBase::PublishSubObject( myVector.get() );
667 //=================================================================================
668 // function : createPathPreview
670 //=================================================================================
671 void TransformationGUI_TranslationDlg::createPathPreview ( GEOM::GEOM_Object_ptr thePath )
675 GEOMBase::GetShape( thePath, aShape, TopAbs_SHAPE );
676 TopoDS_Edge anEdge = TopoDS::Edge( aShape );
677 ShapeAnalysis_Edge aShapeAnal;
678 TopoDS_Vertex aFirst = aShapeAnal.FirstVertex( anEdge );
679 TopoDS_Vertex aLast = aShapeAnal.LastVertex( anEdge );
680 TopoDS_Shape aVector = BRepBuilderAPI_MakeEdge(BRep_Tool::Pnt(aFirst), BRep_Tool::Pnt(aLast)).Shape();
681 const char* aName = "tmpVector";
682 Handle(GEOM_AISVector) anIO = new GEOM_AISVector( aVector, aName );
684 // add Prs to preview
685 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
686 SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
688 aPrs->AddObject(anIO);
689 GEOMBase_Helper::displayPreview( aPrs, false, true );