Salome HOME
76e0850a271e76982c8241aec71a487346770cec
[modules/geom.git] / src / OperationGUI / OperationGUI_GetSharedShapesDlg.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 //  File   : OperationGUI_GetSharedShapesDlg.cxx
23 //  Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24
25 #include "OperationGUI_GetSharedShapesDlg.h"
26
27 #include <DlgRef.h>
28 #include <GeometryGUI.h>
29 #include <GEOMBase.h>
30
31 #include <GEOMImpl_Types.hxx>
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_MessageBox.h>
37 #include <SalomeApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39
40 #include <TopoDS_Iterator.hxx>
41 #include <TopoDS_Shape.hxx>
42
43 namespace
44 {
45   GEOM::shape_type maxShapeType(const GEOM::ListOfGO& objs)
46   {
47     GEOM::shape_type r = GEOM::SHAPE;
48     for ( CORBA::ULong i = 0; i < objs.length(); i++ ) {
49       GEOM::shape_type t = objs[i]->GetShapeType();
50       if ( t == GEOM::COMPOUND || t == GEOM::COMPSOLID )
51         t = objs[i]->GetMaxShapeType();
52       else if ( t < GEOM::VERTEX )
53         t = GEOM::shape_type( (int)t+1 );
54       r = qMin( r, t );
55       if ( r <= GEOM::SOLID ) break;
56     }
57     return r;
58   }
59 }
60
61 //=================================================================================
62 // class    : OperationGUI_GetSharedShapesDlg()
63 // purpose  : Constructs a OperationGUI_GetSharedShapesDlg which is a child of 'parent', with the
64 //            name 'name' and widget flags set to 'f'.
65 //            The dialog will by default be modeless, unless you set 'modal' to
66 //            TRUE to construct a modal dialog.
67 //=================================================================================
68 OperationGUI_GetSharedShapesDlg::OperationGUI_GetSharedShapesDlg
69 (GeometryGUI* theGeometryGUI, QWidget* parent)
70   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
71 {
72   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
73   QPixmap image0(aResMgr->loadPixmap("GEOM", tr("ICON_DLG_SHARED_SHAPES")));
74   QPixmap image2(aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
75
76   setWindowTitle(tr("GEOM_SHARED_SHAPES_TITLE"));
77
78   /***************************************************************/
79   mainFrame()->GroupConstructors->setTitle(tr("GEOM_GET_SHARED_SHAPES"));
80   mainFrame()->RadioButton1->setIcon(image0);
81   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
82   mainFrame()->RadioButton2->close();
83   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
84   mainFrame()->RadioButton3->close();
85
86   GroupPoints = new DlgRef_2Sel1List1Check(centralWidget());
87   GroupPoints->GroupBox1->setTitle(tr("GEOM_SHARED_SHAPES_INPUT"));
88   GroupPoints->TextLabel1->setText(tr("GEOM_SHAPES"));
89   GroupPoints->TextLabel2->hide();
90   GroupPoints->TextLabel3->setText(tr("GEOM_SUBSHAPE_TYPE"));
91   GroupPoints->PushButton1->setIcon(image2);
92   GroupPoints->PushButton2->hide();
93   GroupPoints->LineEdit1->setReadOnly(true);
94   GroupPoints->LineEdit2->hide();
95   GroupPoints->LineEdit1->setEnabled(true);
96   GroupPoints->CheckButton1->setText(tr("GEOM_SHARED_SHAPES_MULTISHARE"));
97
98   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
99   layout->setMargin(0); layout->setSpacing(6);
100   layout->addWidget(GroupPoints);
101
102   /***************************************************************/
103
104   setHelpFileName("shared_shapes_page.html");
105
106   Init();
107 }
108
109 //=================================================================================
110 // function : ~OperationGUI_GetSharedShapesDlg()
111 // purpose  : Destroys the object and frees any allocated resources
112 //=================================================================================
113 OperationGUI_GetSharedShapesDlg::~OperationGUI_GetSharedShapesDlg()
114 {
115   // no need to delete child widgets, Qt does it all for us
116 }
117
118 //=================================================================================
119 // function : Init()
120 // purpose  :
121 //=================================================================================
122 void OperationGUI_GetSharedShapesDlg::Init()
123 {
124   initTypes();
125
126   showOnlyPreviewControl();
127
128   /* signals and slots connections */
129   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
130   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
131
132   connect(this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
133
134   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
135
136   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
137
138   connect(GroupPoints->ComboBox1, SIGNAL(activated(int)), this, SLOT(ComboTextChanged()));
139
140   connect(GroupPoints->CheckButton1, SIGNAL(clicked()), this, SLOT(processPreview()));
141
142   connect(myGeomGUI->getApp()->selectionMgr(),
143            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
144
145   //initName(getNewObjectName());
146   initName(getPrefixByType());
147
148   ConstructorsClicked(0);
149   GroupPoints->PushButton1->click();
150 }
151
152 void OperationGUI_GetSharedShapesDlg::initTypes()
153 {
154   GEOM::shape_type t = maxShapeType( myListShapes );
155
156   int idx = GroupPoints->ComboBox1->count() > 0 ? 
157     GroupPoints->ComboBox1->itemData( GroupPoints->ComboBox1->currentIndex() ).toInt() : -1;
158
159   GroupPoints->ComboBox1->clear();
160
161   if ( t == GEOM::SHAPE || t <= GEOM::SOLID )
162     GroupPoints->ComboBox1->addItem( tr( "GEOM_SOLID" ), int( GEOM::SOLID ) );
163   if ( t == GEOM::SHAPE || t <= GEOM::SHELL )
164     GroupPoints->ComboBox1->addItem( tr( "GEOM_SHELL" ), int( GEOM::SHELL ) );
165   if ( t == GEOM::SHAPE || t <= GEOM::FACE )
166     GroupPoints->ComboBox1->addItem( tr( "GEOM_FACE" ), int( GEOM::FACE ) );
167   if ( t == GEOM::SHAPE || t <= GEOM::WIRE )
168     GroupPoints->ComboBox1->addItem( tr( "GEOM_WIRE" ), int( GEOM::WIRE ) );
169   if ( t == GEOM::SHAPE || t <= GEOM::EDGE )
170     GroupPoints->ComboBox1->addItem( tr( "GEOM_EDGE" ), int( GEOM::EDGE ) );
171   if ( t == GEOM::SHAPE || t <= GEOM::VERTEX )
172     GroupPoints->ComboBox1->addItem( tr( "GEOM_VERTEX" ), int( GEOM::VERTEX ) );
173
174   idx = GroupPoints->ComboBox1->findData( idx );
175   GroupPoints->ComboBox1->setCurrentIndex( idx >= 0 ? idx : 0 );
176
177   if ( idx < 0 )
178     ComboTextChanged();
179 }
180
181 //=================================================================================
182 // function : ConstructorsClicked()
183 // purpose  : Radio button management
184 //=================================================================================
185 void OperationGUI_GetSharedShapesDlg::ConstructorsClicked (int /*constructorId*/)
186 {
187   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
188   globalSelection();
189
190   myListShapes.length(0);
191
192   GroupPoints->PushButton1->setDown(true);
193
194   myEditCurrentArgument = GroupPoints->LineEdit1;
195   GroupPoints->LineEdit1->clear();
196
197   qApp->processEvents();
198   updateGeometry();
199   resize(minimumSizeHint());
200
201   myEditCurrentArgument->setFocus();
202   connect(myGeomGUI->getApp()->selectionMgr(),
203           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
204   SelectionIntoArgument();
205 }
206
207 //=================================================================================
208 // function : ClickOnOk()
209 // purpose  :
210 //=================================================================================
211 void OperationGUI_GetSharedShapesDlg::ClickOnOk()
212 {
213   setIsApplyAndClose( true );
214   if (ClickOnApply())
215     ClickOnCancel();
216 }
217
218 //=================================================================================
219 // function : ClickOnApply()
220 // purpose  :
221 //=================================================================================
222 bool OperationGUI_GetSharedShapesDlg::ClickOnApply()
223 {
224   if (!onAccept())
225     return false;
226
227   //initName(getNewObjectName());
228   initName(getPrefixByType());
229
230   return true;
231 }
232
233 //=================================================================================
234 // function : SelectionIntoArgument()
235 // purpose  : Called when selection as changed or other case
236 //=================================================================================
237 void OperationGUI_GetSharedShapesDlg::SelectionIntoArgument()
238 {
239   myEditCurrentArgument->setText("");
240   QString aString = "";
241
242   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
243   SALOME_ListIO aSelList;
244   aSelMgr->selectedObjects(aSelList);
245
246   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
247
248   if (nbSel < 1) {
249     myListShapes.length(0);
250   }
251
252   GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListShapes, true);
253
254   initTypes();
255
256   if (!myListShapes.length())
257     return;
258
259   myEditCurrentArgument->setText(aString);
260   processPreview();
261 }
262
263 //=================================================================================
264 // function : SetEditCurrentArgument()
265 // purpose  :
266 //=================================================================================
267 void OperationGUI_GetSharedShapesDlg::SetEditCurrentArgument()
268 {
269   QPushButton* send = (QPushButton*)sender();
270
271   if (send == GroupPoints->PushButton1) {
272     myEditCurrentArgument = GroupPoints->LineEdit1;
273     GroupPoints->LineEdit1->setEnabled(true);
274   }
275
276   globalSelection(GEOM_ALLSHAPES);
277
278   myEditCurrentArgument->setFocus();
279   SelectionIntoArgument();
280   send->setDown(true);
281 }
282
283 //=================================================================================
284 // function : ActivateThisDialog()
285 // purpose  :
286 //=================================================================================
287 void OperationGUI_GetSharedShapesDlg::ActivateThisDialog()
288 {
289   GEOMBase_Skeleton::ActivateThisDialog();
290   connect(myGeomGUI->getApp()->selectionMgr(),
291           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
292
293   ConstructorsClicked(getConstructorId());
294   processPreview();
295 }
296
297 //=================================================================================
298 // function : enterEvent()
299 // purpose  :
300 //=================================================================================
301 void OperationGUI_GetSharedShapesDlg::enterEvent(QEvent*)
302 {
303   if (!mainFrame()->GroupConstructors->isEnabled())
304     this->ActivateThisDialog();
305 }
306
307 //=======================================================================
308 //function : ComboTextChanged
309 //purpose  :
310 //=======================================================================
311 void OperationGUI_GetSharedShapesDlg::ComboTextChanged()
312 {
313   //initName(getNewObjectName());
314   initName(getPrefixByType());
315   processPreview();
316 }
317
318 //=================================================================================
319 // function : GetType()
320 // purpose  :
321 //=================================================================================
322 int OperationGUI_GetSharedShapesDlg::GetType() const
323 {
324   return GroupPoints->ComboBox1->count() > 0 ?
325     GroupPoints->ComboBox1->itemData( GroupPoints->ComboBox1->currentIndex() ).toInt() : (int)GEOM::SHAPE;
326 }
327
328 //=================================================================================
329 // function : createOperation
330 // purpose  :
331 //=================================================================================
332 GEOM::GEOM_IOperations_ptr OperationGUI_GetSharedShapesDlg::createOperation()
333 {
334   return getGeomEngine()->GetIShapesOperations();
335 }
336
337 //=================================================================================
338 // function : isValid
339 // purpose  :
340 //=================================================================================
341 bool OperationGUI_GetSharedShapesDlg::isValid (QString& msg)
342 {
343   bool isOK = myListShapes.length() > 1 ||
344     ( myListShapes.length() > 0 && myListShapes[0]->GetShapeType() == GEOM::COMPOUND );
345
346   if ( !isOK )
347     msg = tr("MSG_SHARED_SHAPES_TOO_FEW_SHAPES");
348
349   return isOK;
350 }
351
352 //=================================================================================
353 // function : execute
354 // purpose  :
355 //=================================================================================
356 bool OperationGUI_GetSharedShapesDlg::execute (ObjectList& objects)
357 {
358   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow(getOperation());
359   GEOM::ListOfGO_var aList = anOper->GetSharedShapesMulti(myListShapes, GetType(), GroupPoints->CheckButton1->isChecked());
360
361   if (!aList->length())
362     return false;
363
364   if (aList->length() > 1)
365     myMainFrame->ResultName->setText(getPrefixByType());
366
367   for (int i = 0, n = aList->length(); i < n; i++)
368     objects.push_back(GEOM::GEOM_Object::_duplicate(aList[i]));
369
370   return objects.size();
371 }
372
373 //================================================================
374 // Function : getFather
375 // Purpose  : Get father object for object to be added in study
376 //            ( called with addInStudy method )
377 //================================================================
378 GEOM::GEOM_Object_ptr OperationGUI_GetSharedShapesDlg::getFather (GEOM::GEOM_Object_ptr)
379 {
380   if (myListShapes.length() > 0)
381     //return myListShapes[0]._retn();
382     return myListShapes[0];
383   return NULL;
384 }
385
386 //================================================================
387 // Function : getPrefixByType
388 // Purpose  :
389 //================================================================
390 QString OperationGUI_GetSharedShapesDlg::getPrefixByType () const
391 {
392   QString aPref;
393
394   switch ( GetType() ) {
395   case GEOM::SOLID:  aPref = tr("GEOM_SHARED_SHAPE").arg(tr("GEOM_SOLID")) ; break;
396   case GEOM::SHELL:  aPref = tr("GEOM_SHARED_SHAPE").arg(tr("GEOM_SHELL")) ; break;
397   case GEOM::FACE:   aPref = tr("GEOM_SHARED_SHAPE").arg(tr("GEOM_FACE"))  ; break;
398   case GEOM::WIRE:   aPref = tr("GEOM_SHARED_SHAPE").arg(tr("GEOM_WIRE"))  ; break;
399   case GEOM::EDGE:   aPref = tr("GEOM_SHARED_SHAPE").arg(tr("GEOM_EDGE"))  ; break;
400   case GEOM::VERTEX: aPref = tr("GEOM_SHARED_SHAPE").arg(tr("GEOM_VERTEX")); break;
401   default:           aPref = tr("GEOM_SHARED_SHAPE").arg(tr("GEOM_SHAPE")) ; break;
402   }
403
404   return aPref;
405 }
406
407 //=================================================================================
408 // function : getSourceObjects
409 // purpose  : virtual method to get source objects
410 //=================================================================================
411 QList<GEOM::GeomObjPtr> OperationGUI_GetSharedShapesDlg::getSourceObjects()
412 {
413   QList<GEOM::GeomObjPtr> res;
414   GEOM::ListOfGO aListPtr(myListShapes);
415   for (CORBA::ULong i = 0; i < aListPtr.length(); i++) {
416     GEOM::GeomObjPtr aGeomObjPtr(aListPtr[i]);
417     res << aGeomObjPtr;
418   }
419   return res;
420 }