Salome HOME
756d957c966ddf69e3b1582c4eae6d672226963a
[modules/geom.git] / src / TransformationGUI / TransformationGUI_ProjectionDlg.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 // File   : TransformationGUI_ProjectionDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25
26 #include "TransformationGUI.h"
27 #include "TransformationGUI_ProjectionDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32 #include <GEOMImpl_Types.hxx>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38
39 #include <TColStd_MapOfInteger.hxx>
40
41 namespace
42 {
43   enum EOperationType { PROJ_ON_FACE = 0, PROJ_ON_WIRE, PROJ_ON_EDGE };
44 }
45
46 //=================================================================================
47 // class    : TransformationGUI_ProjectionDlg()
48 // purpose  : Constructs a TransformationGUI_ProjectionDlg which is a child of 'parent', with the
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 TransformationGUI_ProjectionDlg::TransformationGUI_ProjectionDlg (GeometryGUI* theGeometryGUI,
54                                       QWidget* parent, bool modal, Qt::WindowFlags fl)
55   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl)
56 {
57   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
58   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_PROJECTION_ON_FACE")));
59   QPixmap image3 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_PROJECTION_ON_WIRE")));
60   QPixmap image4 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_PROJECTION_ON_EDGE")));
61
62   setWindowTitle(tr("GEOM_PROJECTION_TITLE"));
63
64   mainFrame()->RadioButton1->setIcon(image2);
65   mainFrame()->RadioButton2->setIcon(image3);
66   mainFrame()->RadioButton3->setIcon(image4);
67
68   myGroup = new DlgRef_2Sel (centralWidget());
69
70   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
71   myGroup->PushButton1->setIcon(image1);
72   myGroup->PushButton2->setIcon(image1);
73   myGroup->LineEdit1->setReadOnly(true);
74   myGroup->LineEdit2->setReadOnly(true);
75
76   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
77   layout->setMargin(0); layout->setSpacing(6);
78   layout->addWidget(myGroup);
79
80   setHelpFileName("projection_operation_page.html");
81
82   // Initialisation
83   Init();
84 }
85
86 //=================================================================================
87 // function : ~TransformationGUI_ProjectionDlg()
88 // purpose  : Destroys the object and frees any allocated resources
89 //=================================================================================
90 TransformationGUI_ProjectionDlg::~TransformationGUI_ProjectionDlg()
91 {
92 }
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void TransformationGUI_ProjectionDlg::Init()
99 {
100   mainFrame()->GroupBoxPublish->show();
101
102   // init variables
103   myEditCurrentArgument = myGroup->LineEdit1;
104
105   myGroup->LineEdit1->setText("");
106   myGroup->LineEdit2->setText("");
107   myObject1.nullify();
108   myObject2.nullify();
109
110   showOnlyPreviewControl();
111  
112   // signals and slots connections
113   connect(this, SIGNAL( constructorsClicked( int )),
114           this, SLOT  ( ConstructorsClicked( int )));
115   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
116   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
117
118   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
119   connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
120
121   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
122           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
123
124   initName( windowTitle() );
125
126   setTabOrder(mainFrame()->GroupConstructors, mainFrame()->GroupBoxName);
127   setTabOrder(mainFrame()->GroupBoxName, mainFrame()->GroupMedium);
128   setTabOrder(mainFrame()->GroupMedium, mainFrame()->GroupButtons);
129
130   //mainFrame()->RadioButton1->setFocus();
131   ConstructorsClicked( 0 );
132
133   resize(100,100);
134 }
135
136 //=================================================================================
137 // function : ClickOnOk()
138 // purpose  :
139 //=================================================================================
140 void TransformationGUI_ProjectionDlg::ClickOnOk()
141 {
142   setIsApplyAndClose(true);
143   if (ClickOnApply())
144     ClickOnCancel();
145 }
146
147 //=================================================================================
148 // function : ClickOnApply()
149 // purpose  :
150 //=================================================================================
151 bool TransformationGUI_ProjectionDlg::ClickOnApply()
152 {
153   if (!onAccept())
154     return false;
155
156   initName();
157   ConstructorsClicked( getConstructorId() );
158
159   return true;
160 }
161
162 //=================================================================================
163 // function : SelectionIntoArgument()
164 // purpose  : Called when selection is changed or on dialog initialization or activation
165 //=================================================================================
166 void TransformationGUI_ProjectionDlg::SelectionIntoArgument()
167 {
168   myEditCurrentArgument->setText("");
169
170   GEOM::GeomObjPtr aSelectedObject;
171   if ( myEditCurrentArgument == myGroup->LineEdit1 ) // what to project
172     switch(  getConstructorId() ) {
173     case PROJ_ON_FACE: aSelectedObject = getSelected(TopAbs_SHAPE); break;
174     case PROJ_ON_WIRE:
175     case PROJ_ON_EDGE: aSelectedObject = getSelected(TopAbs_VERTEX); break;
176     default:;
177     }
178   else
179     switch(  getConstructorId() ) { // where to project
180     case PROJ_ON_FACE: aSelectedObject = getSelected(TopAbs_FACE); break;
181     case PROJ_ON_WIRE: aSelectedObject = getSelected(TopAbs_WIRE); break;
182     case PROJ_ON_EDGE: aSelectedObject = getSelected(TopAbs_EDGE); break;
183     default:;
184     }
185
186   TopoDS_Shape aShape;
187   if (aSelectedObject && GEOMBase::GetShape(aSelectedObject.get(), aShape) && !aShape.IsNull())
188   {
189     QString aName = GEOMBase::GetName(aSelectedObject.get());
190     myEditCurrentArgument->setText(aName);
191
192     // clear selection
193     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
194     myGeomGUI->getApp()->selectionMgr()->clearSelected();
195     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
196             this, SLOT(SelectionIntoArgument()));
197
198     if (myEditCurrentArgument == myGroup->LineEdit1) {
199       myObject1 = aSelectedObject;
200       if (!myObject2)
201         myGroup->PushButton2->click();
202     }
203     else if (myEditCurrentArgument == myGroup->LineEdit2) {
204       myObject2 = aSelectedObject;
205       if (!myObject1)
206         myGroup->PushButton1->click();
207     }
208   }
209   else {
210     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
211     else if (myEditCurrentArgument == myGroup->LineEdit2) myObject2.nullify();
212   }
213   processPreview();
214 }
215
216 //=================================================================================
217 // function : SetEditCurrentArgument()
218 // purpose  :
219 //=================================================================================
220 void TransformationGUI_ProjectionDlg::SetEditCurrentArgument()
221 {
222   QPushButton* send = (QPushButton*)sender();
223
224   globalSelection(); // close local contexts, if any
225
226   if (send == myGroup->PushButton1) // what to project
227   {
228     myEditCurrentArgument = myGroup->LineEdit1;
229
230     myGroup->PushButton2->setDown(false);
231     myGroup->LineEdit2->setEnabled(false);
232
233     switch ( getConstructorId() ) {
234     case PROJ_ON_FACE: {
235       TColStd_MapOfInteger aMap;
236       aMap.Add( GEOM_POINT );
237       aMap.Add( GEOM_EDGE );
238       aMap.Add( GEOM_WIRE );
239       globalSelection( aMap );
240       std::list<int> needTypes;
241       needTypes.push_back( TopAbs_VERTEX ), needTypes.push_back( TopAbs_EDGE ), needTypes.push_back( TopAbs_WIRE );
242       localSelection(needTypes);
243       break;
244     }
245     case PROJ_ON_WIRE:
246     case PROJ_ON_EDGE: {
247       localSelection(TopAbs_VERTEX);
248       break;
249     }
250     default:;
251     }
252   }
253   else if (send == myGroup->PushButton2) // where to project
254   {
255     myEditCurrentArgument = myGroup->LineEdit2;
256
257     myGroup->PushButton1->setDown(false);
258     myGroup->LineEdit1->setEnabled(false);
259
260     switch ( getConstructorId() ) {
261     case PROJ_ON_FACE: localSelection(TopAbs_FACE); break;
262     case PROJ_ON_WIRE: localSelection(TopAbs_WIRE); break;
263     case PROJ_ON_EDGE: localSelection(TopAbs_EDGE); break;
264     default:;
265     }
266   }
267
268   // enable line edit
269   myEditCurrentArgument->setEnabled(true);
270   myEditCurrentArgument->setFocus();
271   // after setFocus(), because it will be setDown(false) when loses focus
272   send->setDown(true);
273
274   SelectionIntoArgument();
275 }
276
277 //=================================================================================
278 // function : ActivateThisDialog()
279 // purpose  :
280 //=================================================================================
281 void TransformationGUI_ProjectionDlg::ActivateThisDialog()
282 {
283   GEOMBase_Skeleton::ActivateThisDialog();
284
285   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
286           this, SLOT(SelectionIntoArgument()));
287   processPreview();
288 }
289
290 //=================================================================================
291 // function : enterEvent()
292 // purpose  : when mouse enter onto the QWidget
293 //=================================================================================
294 void TransformationGUI_ProjectionDlg::enterEvent (QEvent*)
295 {
296   if (!mainFrame()->GroupConstructors->isEnabled())
297     ActivateThisDialog();
298 }
299
300 //=================================================================================
301 // function : createOperation
302 // purpose  :
303 //=================================================================================
304 GEOM::GEOM_IOperations_ptr TransformationGUI_ProjectionDlg::createOperation()
305 {
306   return getGeomEngine()->GetITransformOperations();
307 }
308
309 //=================================================================================
310 // function : isValid
311 // purpose  :
312 //=================================================================================
313 bool TransformationGUI_ProjectionDlg::isValid (QString&)
314 {
315   return myObject1 && myObject2;
316 }
317
318 //=================================================================================
319 // function : execute
320 // purpose  :
321 //=================================================================================
322 bool TransformationGUI_ProjectionDlg::execute (ObjectList& objects)
323 {
324   GEOM::GEOM_Object_var anObj;
325
326   GEOM::GEOM_ITransformOperations_var anOper = GEOM::GEOM_ITransformOperations::_narrow(getOperation());
327   anObj = anOper->ProjectShapeCopy( myObject1.get(), myObject2.get() );
328   if (!anObj->_is_nil())
329     objects.push_back(anObj._retn());
330
331   return true;
332 }
333
334 //=================================================================================
335 // function : ConstructorsClicked()
336 // purpose  : Radio button management
337 //=================================================================================
338 void TransformationGUI_ProjectionDlg::ConstructorsClicked( int constructorId )
339 {
340   myObject1.nullify();
341   myObject2.nullify();
342   myGroup->LineEdit1->setText( "" );
343   myGroup->LineEdit2->setText( "" );
344
345   switch ( constructorId ) {
346   case PROJ_ON_FACE:
347     mainFrame()->GroupConstructors->setTitle(tr("GEOM_PROJECTION_ON_FACE"));
348     myGroup->TextLabel1->setText(tr("GEOM_PROJ_ON_FACE_SOURCE"));
349     myGroup->TextLabel2->setText(tr("GEOM_PROJ_ON_FACE_TARGET"));
350     break;
351   case PROJ_ON_EDGE:
352     mainFrame()->GroupConstructors->setTitle(tr("GEOM_PROJECTION_ON_EDGE"));
353     myGroup->TextLabel1->setText(tr("GEOM_PROJ_ON_EDGE_SOURCE"));
354     myGroup->TextLabel2->setText(tr("GEOM_PROJ_ON_EDGE_TARGET"));
355     break;
356   case PROJ_ON_WIRE:
357     mainFrame()->GroupConstructors->setTitle(tr("GEOM_PROJECTION_ON_WIRE"));
358     myGroup->TextLabel1->setText(tr("GEOM_PROJ_ON_WIRE_SOURCE"));
359     myGroup->TextLabel2->setText(tr("GEOM_PROJ_ON_WIRE_TARGET"));
360     break;
361   default:;
362   }
363   myGroup->PushButton1->click();
364 }
365
366 //=================================================================================
367 // function : addSubshapeToStudy
368 // purpose  : virtual method to add new SubObjects if local selection
369 //=================================================================================
370 void TransformationGUI_ProjectionDlg::addSubshapesToStudy()
371 {
372   GEOMBase::PublishSubObject( myObject1.get() );
373   GEOMBase::PublishSubObject( myObject2.get() );
374 }
375
376 //=================================================================================
377 // function : getSourceObjects
378 // purpose  : virtual method to get source objects
379 //=================================================================================
380 QList<GEOM::GeomObjPtr> TransformationGUI_ProjectionDlg::getSourceObjects()
381 {
382   QList<GEOM::GeomObjPtr> res;
383   res << myObject1 << myObject2;
384   return res;
385 }