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