Salome HOME
0dfba6b9e2c5a87e0846a8562c83cc7ed92c4b3b
[modules/geom.git] / src / TransformationGUI / TransformationGUI_OffsetDlg.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : TransformationGUI_OffsetDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "TransformationGUI_OffsetDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 //=================================================================================
39 // class    : TransformationGUI_OffsetDlg()
40 // purpose  : Constructs a TransformationGUI_OffsetDlg which is a child of 'parent', with the
41 //            name 'name' and widget flags set to 'f'.
42 //            The dialog will by default be modeless, unless you set 'modal' to
43 //            TRUE to construct a modal dialog.
44 //=================================================================================
45 TransformationGUI_OffsetDlg::TransformationGUI_OffsetDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
46                                                           bool modal, Qt::WindowFlags fl )
47   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
48 {
49   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_OFFSET" ) ) );
50   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
51
52   setWindowTitle( tr( "GEOM_OFFSET_TITLE" ) );
53
54   /***************************************************************/
55   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_OFFSET" ) );
56   mainFrame()->RadioButton1->setIcon( image0 );
57   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
58   mainFrame()->RadioButton2->close();
59   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
60   mainFrame()->RadioButton3->close();
61
62   GroupPoints = new DlgRef_1Sel1Spin1Check( centralWidget() );
63   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
64   GroupPoints->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
65   GroupPoints->TextLabel2->setText( tr( "GEOM_OFFSET" ) );
66   GroupPoints->CheckButton1->setText( tr( "GEOM_JOIN_BY_PIPES" ) );
67   GroupPoints->CheckButton1->setChecked( true );
68
69   GroupPoints->PushButton1->setIcon( image1 );
70
71   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
72   layout->setMargin( 0 ); layout->setSpacing( 6 );
73   layout->addWidget( GroupPoints );
74
75   /***************************************************************/
76
77   setHelpFileName( "offset_operation_page.html" );
78
79   Init();
80 }
81
82
83 //=================================================================================
84 // function : ~TransformationGUI_OffsetDlg()
85 // purpose  : Destroys the object and frees any allocated resources
86 //=================================================================================
87 TransformationGUI_OffsetDlg::~TransformationGUI_OffsetDlg()
88 {
89   /* no need to delete child widgets, Qt does it all for us */
90 }
91
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void TransformationGUI_OffsetDlg::Init()
98 {
99   /* init variables */
100   myEditCurrentArgument = GroupPoints->LineEdit1;
101   GroupPoints->LineEdit1->setReadOnly( true );
102
103   myObjects.clear();
104
105   /* Get setting of step value from file configuration */
106   double step = 1;
107
108   /* min, max, step and decimals for spin boxes & initial values */
109   initSpinBox( GroupPoints->SpinBox_DX, COORD_MIN, COORD_MAX, step, "length_precision" );
110   GroupPoints->SpinBox_DX->setValue( 1e-05 );
111
112   mainFrame()->GroupBoxPublish->show();
113
114   /* signals and slots connections */
115   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
116   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
117
118   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
119   connect( myGeomGUI->getApp()->selectionMgr(),
120            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
121
122   connect( GroupPoints->SpinBox_DX,   SIGNAL( valueChanged( double ) ), this, SLOT( ValueChangedInSpinBox() ) );
123   connect( GroupPoints->CheckButton1, SIGNAL( toggled( bool ) ),        this, SLOT( JoinModeChanged() ) );
124
125   initName( tr( "GEOM_OFFSET" ) );
126
127   globalSelection( GEOM_ALLSHAPES );
128   resize(100,100);
129   SelectionIntoArgument();
130 }
131
132
133 //=================================================================================
134 // function : ClickOnOk()
135 // purpose  :
136 //=================================================================================
137 void TransformationGUI_OffsetDlg::ClickOnOk()
138 {
139   setIsApplyAndClose( true );
140   if ( ClickOnApply() )
141     ClickOnCancel();
142 }
143
144
145 //=================================================================================
146 // function : ClickOnApply()
147 // purpose  :
148 //=================================================================================
149 bool TransformationGUI_OffsetDlg::ClickOnApply()
150 {
151   if ( !onAccept( /*copy=*/true ))
152     return false;
153
154   initName();
155   return true;
156 }
157
158
159 //=================================================================================
160 // function : SelectionIntoArgument()
161 // purpose  : Called when selection has changed
162 //=================================================================================
163 void TransformationGUI_OffsetDlg::SelectionIntoArgument()
164 {
165   myObjects = getSelected( TopAbs_SHAPE, -1 );
166   if ( !myObjects.isEmpty() ) {
167     QString aName = myObjects.count() > 1 ? QString( "%1_objects").arg( myObjects.count() ) : GEOMBase::GetName( myObjects[0].get() );
168     myEditCurrentArgument->setText( aName );
169   }
170   else {
171     myEditCurrentArgument->setText("");
172   }
173
174   processPreview();
175 }
176
177
178 //=================================================================================
179 // function : SetEditCurrentArgument()
180 // purpose  :
181 //=================================================================================
182 void TransformationGUI_OffsetDlg::SetEditCurrentArgument()
183 {
184   QPushButton* send = (QPushButton*)sender();
185
186   if ( send == GroupPoints->PushButton1 ) {
187     GroupPoints->PushButton1->setDown(true);
188     myEditCurrentArgument = GroupPoints->LineEdit1;
189     myEditCurrentArgument->setFocus();
190     SelectionIntoArgument();
191   }
192 }
193
194
195 //=================================================================================
196 // function : enterEvent()
197 // purpose  : when mouse enter onto the QWidget
198 //=================================================================================
199 void TransformationGUI_OffsetDlg::enterEvent( QEvent* )
200 {
201   if ( !mainFrame()->GroupConstructors->isEnabled() )
202     ActivateThisDialog();
203 }
204
205
206 //=================================================================================
207 // function : ActivateThisDialog()
208 // purpose  :
209 //=================================================================================
210 void TransformationGUI_OffsetDlg::ActivateThisDialog()
211 {
212   GEOMBase_Skeleton::ActivateThisDialog();
213   connect( myGeomGUI->getApp()->selectionMgr(),
214            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
215   globalSelection( GEOM_ALLSHAPES );
216   myEditCurrentArgument = GroupPoints->LineEdit1;
217   myEditCurrentArgument->setFocus();
218 }
219
220
221 //=================================================================================
222 // function : ValueChangedInSpinBox()
223 // purpose  :
224 //=================================================================================
225 void TransformationGUI_OffsetDlg::ValueChangedInSpinBox()
226 {
227   processPreview();
228 }
229
230
231 //=================================================================================
232 // function : createOperation
233 // purpose  :
234 //=================================================================================
235 GEOM::GEOM_IOperations_ptr TransformationGUI_OffsetDlg::createOperation()
236 {
237   return getGeomEngine()->GetITransformOperations();
238 }
239
240 //=================================================================================
241 // function : isValid
242 // purpose  :
243 //=================================================================================
244 bool TransformationGUI_OffsetDlg::isValid( QString& msg )
245 {
246   bool ok = GroupPoints->SpinBox_DX->isValid( msg, !IsPreview() ) && !myObjects.isEmpty();
247   for ( int i = 0; i < myObjects.count() && ok; i++ ) {
248     GEOM::shape_type aType = myObjects[i]->GetShapeType();
249     ok = aType == GEOM::FACE || aType == GEOM::SHELL || aType == GEOM::SOLID;
250     if ( !ok )
251       msg = tr( "ERROR_SHAPE_TYPE" );
252   }
253   return ok;
254 }
255
256 //=================================================================================
257 // function : execute
258 // purpose  :
259 //=================================================================================
260 bool TransformationGUI_OffsetDlg::execute( ObjectList& objects )
261 {
262   bool res = false;
263
264   GEOM::GEOM_Object_var anObj;
265
266   GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
267
268   if ( true /*GroupPoints->CheckButton1->isChecked() || IsPreview()*/ ) {
269     for ( int i = 0; i < myObjects.count(); i++ ) {
270
271       anObj = anOper->OffsetShapeCopy( myObjects[i].get(), GetOffset(), GetIsJoinByPipes() );
272       if ( !anObj->_is_nil() ) {
273         if(!IsPreview()) {
274           anObj->SetParameters(GroupPoints->SpinBox_DX->text().toUtf8().constData());
275         }
276         objects.push_back( anObj._retn() );
277       }
278     }
279   }
280   else {
281     for ( int i = 0; i < myObjects.count(); i++ ) {
282       anObj = anOper->OffsetShape( myObjects[i].get(), GetOffset(), GetIsJoinByPipes()  );
283       if ( !anObj->_is_nil() )
284         objects.push_back( anObj._retn() );
285     }
286   }
287   res = true;
288
289   return res;
290 }
291
292 //=================================================================================
293 // function : restoreSubShapes
294 // purpose  :
295 //=================================================================================
296 void TransformationGUI_OffsetDlg::restoreSubShapes( SALOMEDS::SObject_ptr theSObject )
297 {
298   if ( mainFrame()->CheckBoxRestoreSS->isChecked() ) {
299     // empty list of arguments means that all arguments should be restored
300     getGeomEngine()->RestoreSubShapesSO( theSObject, GEOM::ListOfGO(),
301                                          /*theFindMethod=*/GEOM::FSM_Transformed,
302                                          /*theInheritFirstArg=*/true,
303                                          mainFrame()->CheckBoxAddPrefix->isChecked() );
304   }
305 }
306
307 //=================================================================================
308 // function : GetOffset()
309 // purpose  :
310 //=================================================================================
311 double TransformationGUI_OffsetDlg::GetOffset() const
312 {
313   return GroupPoints->SpinBox_DX->value();
314 }
315
316 //=======================================================================
317 //function : GetIsJoinByPipes
318 //purpose  :
319 //=======================================================================
320
321 bool TransformationGUI_OffsetDlg::GetIsJoinByPipes() const
322 {
323   return GroupPoints->CheckButton1->isChecked();
324 }
325
326 //=================================================================================
327 // function :  JoinModeChanged()
328 // purpose  :
329 //=================================================================================
330 void TransformationGUI_OffsetDlg::JoinModeChanged()
331 {
332   processPreview();
333   //mainFrame()->GroupBoxName->setEnabled( GroupPoints->CheckButton1->isChecked() );
334 }
335
336 //=================================================================================
337 // function : getSourceObjects
338 // purpose  : virtual method to get source objects
339 //=================================================================================
340 QList<GEOM::GeomObjPtr> TransformationGUI_OffsetDlg::getSourceObjects()
341 {
342   QList<GEOM::GeomObjPtr> res;
343   if ( GroupPoints->CheckButton1->isChecked() )
344     res.append( myObjects );
345   return res;
346 }