1 // Copyright (C) 2007-2012 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()
274 setIsApplyAndClose( true );
279 //=================================================================================
280 // function : ClickOnApply()
282 //=================================================================================
283 bool TransformationGUI_TranslationDlg::ClickOnApply()
285 if (!onAccept(GroupPoints->CheckBox2->isChecked()))
290 // activate selection and connect selection manager
291 ConstructorsClicked(getConstructorId());
292 SelectionIntoArgument();
297 //=================================================================================
298 // function : SelectionIntoArgument()
299 // purpose : Called when selection is changed or on dialog initialization or activation
300 //=================================================================================
301 void TransformationGUI_TranslationDlg::SelectionIntoArgument()
305 if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
306 myObjects = getSelected( TopAbs_SHAPE, -1 );
307 if ( !myObjects.isEmpty() ) {
308 QString aName = myObjects.count() > 1 ? QString( "%1_objects").arg( myObjects.count() ) : GEOMBase::GetName( myObjects[0].get() );
309 myEditCurrentArgument->setText( aName );
312 myEditCurrentArgument->setText("");
316 TopAbs_ShapeEnum aNeedType = ( myEditCurrentArgument == GroupPoints->LineEdit2 && getConstructorId() == 2 ) ?
317 TopAbs_EDGE : TopAbs_VERTEX;
318 GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
320 if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
321 QString aName = GEOMBase::GetName( aSelectedObject.get() );
322 myEditCurrentArgument->setText( aName );
323 if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
324 if ( getConstructorId() == 1 ) {
325 myPoint1 = aSelectedObject;
327 GroupPoints->PushButton3->click();
328 else if ( myObjects.isEmpty() )
329 GroupPoints->PushButton1->click();
331 else if ( getConstructorId() == 2 ) {
332 myVector = aSelectedObject;
333 if ( myObjects.isEmpty() )
334 GroupPoints->PushButton1->click();
337 else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) {
338 myPoint2 = aSelectedObject;
339 if ( myObjects.isEmpty() )
340 GroupPoints->PushButton1->click();
341 else if ( !myPoint1 )
342 GroupPoints->PushButton2->click();
346 if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
347 if ( getConstructorId() == 1 ) myPoint1.nullify();
348 else myVector.nullify();
350 else if ( myEditCurrentArgument == GroupPoints->LineEdit3 ) {
353 myEditCurrentArgument->setText("");
360 //=================================================================================
361 // function : SetEditCurrentArgument()
363 //=================================================================================
364 void TransformationGUI_TranslationDlg::SetEditCurrentArgument()
366 QPushButton* send = (QPushButton*)sender();
368 disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
371 if (send == GroupPoints->PushButton1) {
372 myEditCurrentArgument = GroupPoints->LineEdit1;
374 GroupPoints->PushButton2->setDown(false);
375 GroupPoints->PushButton3->setDown(false);
376 GroupPoints->LineEdit2->setEnabled(false);
377 GroupPoints->LineEdit3->setEnabled(false);
379 else if (send == GroupPoints->PushButton2) {
380 myEditCurrentArgument = GroupPoints->LineEdit2;
382 GroupPoints->PushButton1->setDown(false);
383 GroupPoints->PushButton3->setDown(false);
384 GroupPoints->LineEdit1->setEnabled(false);
385 GroupPoints->LineEdit3->setEnabled(false);
387 if (getConstructorId() == 1)
388 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
390 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_EDGE);
392 else if (send == GroupPoints->PushButton3) {
393 myEditCurrentArgument = GroupPoints->LineEdit3;
395 GroupPoints->PushButton1->setDown(false);
396 GroupPoints->PushButton2->setDown(false);
397 GroupPoints->LineEdit1->setEnabled(false);
398 GroupPoints->LineEdit2->setEnabled(false);
400 localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
402 connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
403 this, SLOT(SelectionIntoArgument()));
406 myEditCurrentArgument->setEnabled(true);
407 myEditCurrentArgument->setFocus();
408 // after setFocus(), because it will be setDown(false) when loses focus
411 // seems we need it only to avoid preview disappearing, caused by selection mode change
415 //=================================================================================
416 // function : ActivateThisDialog()
418 //=================================================================================
419 void TransformationGUI_TranslationDlg::ActivateThisDialog()
421 GEOMBase_Skeleton::ActivateThisDialog();
422 connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
423 this, SLOT( SelectionIntoArgument() ) );
425 ConstructorsClicked( getConstructorId() );
428 //=================================================================================
429 // function : enterEvent()
431 //=================================================================================
432 void TransformationGUI_TranslationDlg::enterEvent (QEvent*)
434 if (!mainFrame()->GroupConstructors->isEnabled())
435 ActivateThisDialog();
438 //=================================================================================
439 // function : ValueChangedInSpinBox()
441 //=================================================================================
442 void TransformationGUI_TranslationDlg::ValueChangedInSpinBox()
447 //=================================================================================
448 // function : createOperation
450 //=================================================================================
451 GEOM::GEOM_IOperations_ptr TransformationGUI_TranslationDlg::createOperation()
453 return getGeomEngine()->GetITransformOperations(getStudyId());
456 //=================================================================================
457 // function : isValid
459 //=================================================================================
460 bool TransformationGUI_TranslationDlg::isValid (QString& msg)
463 switch (getConstructorId()) {
465 ok = GroupPoints->SpinBox1->isValid( msg, !IsPreview() ) &&
466 GroupPoints->SpinBox2->isValid( msg, !IsPreview() ) &&
467 GroupPoints->SpinBox3->isValid( msg, !IsPreview() ) &&
468 !myObjects.isEmpty();
471 ok = myPoint1 && myPoint2 && !myObjects.isEmpty();
474 ok = GroupPoints->SpinBox3->isValid( msg, !IsPreview() ) &&
475 myVector && !myObjects.isEmpty();
483 //=================================================================================
484 // function : execute
486 //=================================================================================
487 bool TransformationGUI_TranslationDlg::execute (ObjectList& objects)
490 bool toCreateCopy = IsPreview() || GroupPoints->CheckBox2->isChecked();
492 GEOM::GEOM_Object_var anObj;
494 GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
496 switch (getConstructorId()) {
499 double dx = GroupPoints->SpinBox1->value();
500 double dy = GroupPoints->SpinBox2->value();
501 double dz = GroupPoints->SpinBox3->value();
503 QStringList aParameters;
504 aParameters<<GroupPoints->SpinBox1->text();
505 aParameters<<GroupPoints->SpinBox2->text();
506 aParameters<<GroupPoints->SpinBox3->text();
509 for (int i = 0; i < myObjects.count(); i++) {
510 myCurrObject = myObjects[i];
511 anObj = anOper->TranslateDXDYDZCopy(myObjects[i].get(), dx, dy, dz);
512 if (!anObj->_is_nil()) {
514 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
515 objects.push_back(anObj._retn());
520 for (int i = 0; i < myObjects.count(); i++) {
521 myCurrObject = myObjects[i];
522 anObj = anOper->TranslateDXDYDZ(myObjects[i].get(), dx, dy, dz);
523 if (!anObj->_is_nil()) {
525 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
526 updateAttributes(anObj, aParameters);
528 objects.push_back(anObj._retn());
538 for (int i = 0; i < myObjects.count(); i++) {
539 myCurrObject = myObjects[i];
540 anObj = anOper->TranslateTwoPointsCopy(myObjects[i].get(), myPoint1.get(), myPoint2.get());
541 if (!anObj->_is_nil())
542 objects.push_back(anObj._retn());
546 for (int i = 0; i < myObjects.count(); i++) {
547 myCurrObject = myObjects[i];
548 anObj = anOper->TranslateTwoPoints(myObjects[i].get(), myPoint1.get(), myPoint2.get());
549 if (!anObj->_is_nil())
550 objects.push_back(anObj._retn());
558 QStringList aParameters;
559 aParameters<<GroupPoints->SpinBox3->text();
560 bool byDistance = GroupPoints->CheckBox1->isChecked();
561 createPathPreview( myVector.get() );
564 double aDistance = GroupPoints->SpinBox3->value();
565 for (int i = 0; i < myObjects.count(); i++) {
566 myCurrObject = myObjects[i];
567 anObj = anOper->TranslateVectorDistance(myObjects[i].get(), myVector.get(), aDistance, toCreateCopy);
568 if (!anObj->_is_nil()) {
570 anObj->SetParameters(aParameters.join(":").toLatin1().constData());
572 updateAttributes(anObj, aParameters);
574 objects.push_back(anObj._retn());
580 for (int i = 0; i < myObjects.count(); i++) {
581 myCurrObject = myObjects[i];
582 anObj = anOper->TranslateVectorCopy(myObjects[i].get(), myVector.get());
583 if (!anObj->_is_nil())
584 objects.push_back(anObj._retn());
588 for (int i = 0; i < myObjects.count(); i++) {
589 myCurrObject = myObjects[i];
590 anObj = anOper->TranslateVector(myObjects[i].get(), myVector.get());
591 if (!anObj->_is_nil())
592 objects.push_back(anObj._retn());
603 //=================================================================================
604 // function : restoreSubShapes
606 //=================================================================================
607 void TransformationGUI_TranslationDlg::restoreSubShapes (SALOMEDS::Study_ptr theStudy,
608 SALOMEDS::SObject_ptr theSObject)
610 if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
611 // we pass here the first operation argument (object) through the list of arguments
612 // because the rotation operation place its arguments in the data structure in another order,
613 // and we need to point the first argument directly
614 GEOM::ListOfGO_var anArgs = new GEOM::ListOfGO;
616 anArgs[0] = myCurrObject.copy();
617 getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, anArgs,
618 /*theFindMethod=*/GEOM::FSM_Transformed,
619 /*theInheritFirstArg=*/true,
620 mainFrame()->CheckBoxAddPrefix->isChecked());
624 //=================================================================================
625 // function : CreateCopyModeChanged()
627 //=================================================================================
628 void TransformationGUI_TranslationDlg::CreateCopyModeChanged()
630 mainFrame()->GroupBoxName->setEnabled(GroupPoints->CheckBox2->isChecked());
633 //=================================================================================
634 // function : ActivateDistanceChanged()
636 //=================================================================================
637 void TransformationGUI_TranslationDlg::ActivateDistanceChanged()
639 GroupPoints->SpinBox3->setEnabled( GroupPoints->CheckBox1->isChecked() );
643 //=================================================================================
644 // function : addSubshapeToStudy
645 // purpose : virtual method to add new SubObjects if local selection
646 //=================================================================================
647 void TransformationGUI_TranslationDlg::addSubshapesToStudy()
649 bool toCreateCopy = IsPreview() || GroupPoints->CheckBox2->isChecked();
651 switch (getConstructorId()) {
653 GEOMBase::PublishSubObject( myPoint1.get() );
654 GEOMBase::PublishSubObject( myPoint2.get() );
657 GEOMBase::PublishSubObject( myVector.get() );
665 //=================================================================================
666 // function : createPathPreview
668 //=================================================================================
669 void TransformationGUI_TranslationDlg::createPathPreview ( GEOM::GEOM_Object_ptr thePath )
673 GEOMBase::GetShape( thePath, aShape, TopAbs_SHAPE );
674 TopoDS_Edge anEdge = TopoDS::Edge( aShape );
675 ShapeAnalysis_Edge aShapeAnal;
676 TopoDS_Vertex aFirst = aShapeAnal.FirstVertex( anEdge );
677 TopoDS_Vertex aLast = aShapeAnal.LastVertex( anEdge );
678 TopoDS_Shape aVector = BRepBuilderAPI_MakeEdge(BRep_Tool::Pnt(aFirst), BRep_Tool::Pnt(aLast)).Shape();
679 const char* aName = "tmpVector";
680 Handle(GEOM_AISVector) anIO = new GEOM_AISVector( aVector, aName );
682 // add Prs to preview
683 SUIT_ViewWindow* vw = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
684 SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>(((SOCC_Viewer*)(vw->getViewManager()->getViewModel()))->CreatePrs(0));
686 aPrs->AddObject(anIO);
687 GEOMBase_Helper::displayPreview( aPrs, false, true );