Salome HOME
Update copyrights
[modules/geom.git] / src / GroupGUI / GroupGUI_BooleanDlg.cxx
1 // Copyright (C) 2007-2019  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   : GroupGUI_BooleanDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26
27 #include "GroupGUI_BooleanDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <GEOMImpl_Types.hxx>
39
40 //=================================================================================
41 // class    : GroupGUI_BooleanDlg()
42 // purpose  : Constructs a GroupGUI_BooleanDlg which is a child of 'parent', with the
43 //            name 'name' and widget flags set to 'f'.
44 //            The dialog will by default be modeless, unless you set 'modal' to
45 //            TRUE to construct a modal dialog.
46 //=================================================================================
47 GroupGUI_BooleanDlg::GroupGUI_BooleanDlg (const int theOperation, GeometryGUI* theGeometryGUI,
48                                           QWidget* parent, bool modal, Qt::WindowFlags fl)
49   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
50     myOperation(theOperation)
51 {
52   QPixmap image0;
53   QString aTitle, aCaption;
54   switch (myOperation) {
55   case UNION:
56     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FUSE")));
57     aTitle = tr("GEOM_UNION");
58     aCaption = tr("GEOM_UNION_TITLE");
59     setHelpFileName("work_with_groups_page.html#union_groups_anchor");
60     break;
61   case INTERSECT:
62     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_COMMON")));
63     aTitle = tr("GEOM_INTERSECT");
64     aCaption = tr("GEOM_INTERSECT_TITLE");
65     setHelpFileName("work_with_groups_page.html#intersect_groups_anchor");
66     break;
67   case CUT:
68     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CUT")));
69     aTitle = tr("GEOM_CUT");
70     aCaption = tr("GEOM_CUT_TITLE");
71     setHelpFileName("work_with_groups_page.html#cut_groups_anchor");
72     break;
73   }
74   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
75
76   setWindowTitle(aCaption);
77
78   /***************************************************************/
79   mainFrame()->GroupConstructors->setTitle(aTitle);
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   myGroup = new DlgRef_2Sel (centralWidget());
87
88   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
89   if (myOperation == CUT) {
90     myGroup->TextLabel1->setText(tr("GEOM_MAIN_GROUPS"));
91     myGroup->TextLabel2->setText(tr("GEOM_TOOL_GROUPS"));
92   }
93   else {
94     myGroup->TextLabel1->setText(tr("GEOM_GROUPS").arg(1));
95     myGroup->TextLabel2->hide();
96     myGroup->PushButton2->hide();
97     myGroup->LineEdit2->hide();
98   }
99
100   myGroup->PushButton1->setIcon(image1);
101   myGroup->PushButton2->setIcon(image1);
102   myGroup->LineEdit1->setReadOnly(true);
103   myGroup->LineEdit2->setReadOnly(true);
104
105   QVBoxLayout* layout = new QVBoxLayout (centralWidget());
106   layout->setMargin(0); layout->setSpacing(6);
107   layout->addWidget(myGroup);
108   /***************************************************************/
109
110   // Initialisation
111   Init();
112 }
113
114 //=================================================================================
115 // function : ~GroupGUI_BooleanDlg()
116 // purpose  : Destroys the object and frees any allocated resources
117 //=================================================================================
118 GroupGUI_BooleanDlg::~GroupGUI_BooleanDlg()
119 {
120 }
121
122 //=================================================================================
123 // function : Init()
124 // purpose  :
125 //=================================================================================
126 void GroupGUI_BooleanDlg::Init()
127 {
128   // init variables
129   myEditCurrentArgument = myGroup->LineEdit1;
130
131   myGroup->LineEdit1->setText("");
132   myGroup->LineEdit2->setText("");
133   myListShapes.length( 0 );
134   myListTools.length( 0 );
135
136   // signals and slots connections
137   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
138   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
139
140   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
141   connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
142
143   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
144            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
145
146   initName(mainFrame()->GroupConstructors->title());
147
148   setTabOrder(mainFrame()->GroupConstructors, mainFrame()->GroupBoxName);
149   setTabOrder(mainFrame()->GroupBoxName, mainFrame()->GroupMedium);
150   setTabOrder(mainFrame()->GroupMedium, mainFrame()->GroupButtons);
151
152   mainFrame()->RadioButton1->setFocus();
153
154   globalSelection(GEOM_GROUP);
155
156   myGroup->PushButton1->click();
157   SelectionIntoArgument();
158   resize(100,100);
159 }
160
161 //=================================================================================
162 // function : ClickOnOk()
163 // purpose  :
164 //=================================================================================
165 void GroupGUI_BooleanDlg::ClickOnOk()
166 {
167   setIsApplyAndClose(true);
168   if (ClickOnApply())
169     ClickOnCancel();
170 }
171
172 //=================================================================================
173 // function : ClickOnApply()
174 // purpose  :
175 //=================================================================================
176 bool GroupGUI_BooleanDlg::ClickOnApply()
177 {
178   if (!onAccept())
179     return false;
180
181   initName();
182   // activate selection and connect selection manager
183   myGroup->PushButton1->click();
184   return true;
185 }
186
187 //=================================================================================
188 // function : SelectionIntoArgument()
189 // purpose  : Called when selection is changed or on dialog initialization or activation
190 //=================================================================================
191 void GroupGUI_BooleanDlg::SelectionIntoArgument()
192 {
193   myEditCurrentArgument->setText("");
194
195   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
196   SALOME_ListIO aSelList;
197   aSelMgr->selectedObjects(aSelList);
198
199   QString aString = "";
200   GEOMBase::GetNameOfSelectedIObjects(aSelList, aString, true);
201
202   if (myEditCurrentArgument == myGroup->LineEdit1) {
203     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListShapes, true);
204   }
205   else if ( myEditCurrentArgument == myGroup->LineEdit2 ) {
206     GEOMBase::ConvertListOfIOInListOfGO(aSelList, myListTools, true);
207   }
208
209   myEditCurrentArgument->setText(aString);
210 }
211
212 //=================================================================================
213 // function : SetEditCurrentArgument()
214 // purpose  :
215 //=================================================================================
216 void GroupGUI_BooleanDlg::SetEditCurrentArgument()
217 {
218   QPushButton* send = (QPushButton*)sender();
219
220   if (send == myGroup->PushButton1) {
221     myEditCurrentArgument = myGroup->LineEdit1;
222
223     myGroup->PushButton2->setDown(false);
224     myGroup->LineEdit2->setEnabled(false);
225   }
226   else if (send == myGroup->PushButton2) {
227     myEditCurrentArgument = myGroup->LineEdit2;
228
229     myGroup->PushButton1->setDown(false);
230     myGroup->LineEdit1->setEnabled(false);
231   }
232
233   // enable line edit
234   myEditCurrentArgument->setEnabled(true);
235   myEditCurrentArgument->setFocus();
236   // after setFocus(), because it will be setDown(false) when loses focus
237   send->setDown(true);
238 }
239
240 //=================================================================================
241 // function : ActivateThisDialog()
242 // purpose  :
243 //=================================================================================
244 void GroupGUI_BooleanDlg::ActivateThisDialog()
245 {
246   GEOMBase_Skeleton::ActivateThisDialog();
247
248   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
249            this, SLOT(SelectionIntoArgument()));
250 }
251
252 //=================================================================================
253 // function : enterEvent()
254 // purpose  : when mouse enter onto the QWidget
255 //=================================================================================
256 void GroupGUI_BooleanDlg::enterEvent (QEvent*)
257 {
258   if (!mainFrame()->GroupConstructors->isEnabled())
259     ActivateThisDialog();
260 }
261
262 //=================================================================================
263 // function : createOperation
264 // purpose  :
265 //=================================================================================
266 GEOM::GEOM_IOperations_ptr GroupGUI_BooleanDlg::createOperation()
267 {
268   return getGeomEngine()->GetIGroupOperations();
269 }
270
271 //=================================================================================
272 // function : isValid
273 // purpose  :
274 //=================================================================================
275 bool GroupGUI_BooleanDlg::isValid (QString&)
276 {
277   return (myListShapes.length() > 0);
278 }
279
280 //=================================================================================
281 // function : execute
282 // purpose  :
283 //=================================================================================
284 bool GroupGUI_BooleanDlg::execute (ObjectList& objects)
285 {
286   GEOM::GEOM_Object_var anObj;
287
288   GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
289   switch (myOperation) {
290   case UNION:
291     anObj = anOper->UnionListOfGroups(myListShapes);
292     break;
293   case INTERSECT:
294     anObj = anOper->IntersectListOfGroups(myListShapes);
295     break;
296   case CUT:
297     anObj = anOper->CutListOfGroups(myListShapes, myListTools);
298     break;
299   default:
300     ;
301   }
302   if (!anObj->_is_nil())
303     objects.push_back(anObj._retn());
304
305   return true;
306 }
307
308 //================================================================
309 // Function : getFather
310 // Purpose  : Get father object for object to be added in study
311 //            (called with addInStudy method)
312 //================================================================
313 GEOM::GEOM_Object_ptr GroupGUI_BooleanDlg::getFather(GEOM::GEOM_Object_ptr theObj)
314 {
315   GEOM::GEOM_Object_var aFatherObj;
316   if (theObj->GetType() == GEOM_GROUP) {
317     GEOM::GEOM_IGroupOperations_var anOper = GEOM::GEOM_IGroupOperations::_narrow(getOperation());
318     aFatherObj = anOper->GetMainShape(theObj);
319   }
320   return aFatherObj._retn();
321 }
322
323 //=================================================================================
324 // function : getSourceObjects
325 // purpose  : virtual method to get source objects
326 //=================================================================================
327 QList<GEOM::GeomObjPtr> GroupGUI_BooleanDlg::getSourceObjects()
328 {
329   QList<GEOM::GeomObjPtr> res;
330   for ( CORBA::ULong i = 0; i < myListShapes.length(); i++) {
331     GEOM::GeomObjPtr aGeomObjPtr(myListShapes[i]);
332     res << aGeomObjPtr;
333   }
334   for ( CORBA::ULong i = 0; i < myListTools.length(); i++) {
335     GEOM::GeomObjPtr aGeomObjPtr(myListTools[i]);
336     res << aGeomObjPtr;
337   }
338   return res;
339 }