Salome HOME
Update copyright
[modules/geom.git] / src / BooleanGUI / BooleanGUI_Dialog.cxx
1 // Copyright (C) 2007-2011  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BooleanGUI_Dialog.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BooleanGUI.h"
28 #include "BooleanGUI_Dialog.h"
29
30 #include <DlgRef.h>
31 #include <GeometryGUI.h>
32 #include <GEOMBase.h>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38
39 //=================================================================================
40 // class    : BooleanGUI_Dialog()
41 // purpose  : Constructs a BooleanGUI_Dialog which is a child of 'parent', with the
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 BooleanGUI_Dialog::BooleanGUI_Dialog (const int theOperation, GeometryGUI* theGeometryGUI,
47                                       QWidget* parent, bool modal, Qt::WindowFlags fl)
48   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl),
49     myOperation(theOperation)
50 {
51   QPixmap image0;
52   QString aTitle, aCaption;
53   switch (myOperation) {
54   case BooleanGUI::COMMON:
55     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_COMMON")));
56     aTitle = tr("GEOM_COMMON");
57     aCaption = tr("GEOM_COMMON_TITLE");
58     setHelpFileName("common_operation_page.html");
59     break;
60   case BooleanGUI::CUT:
61     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_CUT")));
62     aTitle = tr("GEOM_CUT");
63     aCaption = tr("GEOM_CUT_TITLE");
64     setHelpFileName("cut_operation_page.html");
65     break;
66   case BooleanGUI::FUSE:
67     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_FUSE")));
68     aTitle = tr("GEOM_FUSE");
69     aCaption = tr("GEOM_FUSE_TITLE");
70     setHelpFileName("fuse_operation_page.html");
71     break;
72   case BooleanGUI::SECTION:
73     image0 = QPixmap(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SECTION")));
74     aTitle = tr("GEOM_SECTION");
75     aCaption = tr("GEOM_SECTION_TITLE");
76     setHelpFileName("section_opeartion_page.html");
77     break;
78   }
79   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
80
81   setWindowTitle(aCaption);
82
83   /***************************************************************/
84   mainFrame()->GroupConstructors->setTitle(aTitle);
85   mainFrame()->RadioButton1->setIcon(image0);
86   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
87   mainFrame()->RadioButton2->close();
88   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
89   mainFrame()->RadioButton3->close();
90
91   myGroup = new DlgRef_2Sel(centralWidget());
92
93   myGroup->GroupBox1->setTitle(tr("GEOM_ARGUMENTS"));
94   if (myOperation != BooleanGUI::CUT) {
95     myGroup->TextLabel1->setText(tr("GEOM_OBJECT_I").arg(1));
96     myGroup->TextLabel2->setText(tr("GEOM_OBJECT_I").arg(2));
97   }
98   else {
99     myGroup->TextLabel1->setText(tr("GEOM_MAIN_OBJECT"));
100     myGroup->TextLabel2->setText(tr("GEOM_TOOL_OBJECT"));
101   }
102
103   myGroup->PushButton1->setIcon(image1);
104   myGroup->PushButton2->setIcon(image1);
105   myGroup->LineEdit1->setReadOnly(true);
106   myGroup->LineEdit2->setReadOnly(true);
107
108   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
109   layout->setMargin(0); layout->setSpacing(6);
110   layout->addWidget(myGroup);
111   /***************************************************************/
112
113   // Initialisation
114   Init();
115 }
116
117 //=================================================================================
118 // function : ~BooleanGUI_Dialog()
119 // purpose  : Destroys the object and frees any allocated resources
120 //=================================================================================
121 BooleanGUI_Dialog::~BooleanGUI_Dialog()
122 {
123 }
124
125 //=================================================================================
126 // function : Init()
127 // purpose  :
128 //=================================================================================
129 void BooleanGUI_Dialog::Init()
130 {
131   mainFrame()->GroupBoxPublish->show();
132
133   // init variables
134   myEditCurrentArgument = myGroup->LineEdit1;
135
136   myGroup->LineEdit1->setText("");
137   myGroup->LineEdit2->setText("");
138   myObject1.nullify();
139   myObject2.nullify();
140  
141   // signals and slots connections
142   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
143   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
144
145   connect(myGroup->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
146   connect(myGroup->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
147
148   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
149            SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
150
151   initName(mainFrame()->GroupConstructors->title());
152
153   setTabOrder(mainFrame()->GroupConstructors, mainFrame()->GroupBoxName);
154   setTabOrder(mainFrame()->GroupBoxName, mainFrame()->GroupMedium);
155   setTabOrder(mainFrame()->GroupMedium, mainFrame()->GroupButtons);
156
157   mainFrame()->RadioButton1->setFocus();
158
159   globalSelection(GEOM_ALLSHAPES);
160
161   myGroup->PushButton1->click();
162   SelectionIntoArgument();
163   resize(100,100);
164 }
165
166 //=================================================================================
167 // function : ClickOnOk()
168 // purpose  :
169 //=================================================================================
170 void BooleanGUI_Dialog::ClickOnOk()
171 {
172   setIsApplyAndClose( true );
173   if (ClickOnApply())
174     ClickOnCancel();
175 }
176
177 //=================================================================================
178 // function : ClickOnApply()
179 // purpose  :
180 //=================================================================================
181 bool BooleanGUI_Dialog::ClickOnApply()
182 {
183   if (!onAccept())
184     return false;
185
186   initName();
187   // activate selection and connect selection manager
188   myGroup->PushButton1->click();
189   return true;
190 }
191
192 //=================================================================================
193 // function : SelectionIntoArgument()
194 // purpose  : Called when selection is changed or on dialog initialization or activation
195 //=================================================================================
196 void BooleanGUI_Dialog::SelectionIntoArgument()
197 {
198   myEditCurrentArgument->setText("");
199
200   GEOM::GeomObjPtr aSelectedObject = getSelected( TopAbs_SHAPE );
201   TopoDS_Shape aShape;
202   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
203     QString aName = GEOMBase::GetName( aSelectedObject.get() );
204     myEditCurrentArgument->setText( aName );
205
206     // clear selection
207     disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
208     myGeomGUI->getApp()->selectionMgr()->clearSelected();
209     connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
210             this, SLOT(SelectionIntoArgument()));
211
212     if (myEditCurrentArgument == myGroup->LineEdit1) {
213       myObject1 = aSelectedObject;
214       if (!myObject2)
215         myGroup->PushButton2->click();
216     }
217     else if (myEditCurrentArgument == myGroup->LineEdit2) {
218       myObject2 = aSelectedObject;
219       if (!myObject1)
220         myGroup->PushButton1->click();
221     }
222   }
223   else {
224     if      (myEditCurrentArgument == myGroup->LineEdit1) myObject1.nullify();
225     else if (myEditCurrentArgument == myGroup->LineEdit2) myObject2.nullify();
226   }
227 }
228
229 //=================================================================================
230 // function : SetEditCurrentArgument()
231 // purpose  :
232 //=================================================================================
233 void BooleanGUI_Dialog::SetEditCurrentArgument()
234 {
235   QPushButton* send = (QPushButton*)sender();
236
237   if (send == myGroup->PushButton1) {
238     myEditCurrentArgument = myGroup->LineEdit1;
239
240     myGroup->PushButton2->setDown(false);
241     myGroup->LineEdit2->setEnabled(false);
242   }
243   else if (send == myGroup->PushButton2) {
244     myEditCurrentArgument = myGroup->LineEdit2;
245
246     myGroup->PushButton1->setDown(false);
247     myGroup->LineEdit1->setEnabled(false);
248   }
249
250   // enable line edit
251   myEditCurrentArgument->setEnabled(true);
252   myEditCurrentArgument->setFocus();
253   // after setFocus(), because it will be setDown(false) when loses focus
254   send->setDown(true);
255 }
256
257 //=================================================================================
258 // function : ActivateThisDialog()
259 // purpose  :
260 //=================================================================================
261 void BooleanGUI_Dialog::ActivateThisDialog()
262 {
263   GEOMBase_Skeleton::ActivateThisDialog();
264
265   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
266            this, SLOT( SelectionIntoArgument() ) );
267   processPreview();
268 }
269
270 //=================================================================================
271 // function : enterEvent()
272 // purpose  : when mouse enter onto the QWidget
273 //=================================================================================
274 void BooleanGUI_Dialog::enterEvent (QEvent*)
275 {
276   if (!mainFrame()->GroupConstructors->isEnabled())
277     ActivateThisDialog();
278 }
279
280 //=================================================================================
281 // function : createOperation
282 // purpose  :
283 //=================================================================================
284 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
285 {
286   return getGeomEngine()->GetIBooleanOperations(getStudyId());
287 }
288
289 //=================================================================================
290 // function : isValid
291 // purpose  :
292 //=================================================================================
293 bool BooleanGUI_Dialog::isValid (QString&)
294 {
295   return myObject1 && myObject2;
296 }
297
298 //=================================================================================
299 // function : execute
300 // purpose  :
301 //=================================================================================
302 bool BooleanGUI_Dialog::execute (ObjectList& objects)
303 {
304   GEOM::GEOM_Object_var anObj;
305
306   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
307   anObj = anOper->MakeBoolean(myObject1.get(), myObject2.get(), myOperation);
308   if (!anObj->_is_nil())
309     objects.push_back(anObj._retn());
310
311   return true;
312 }
313
314 //=================================================================================
315 // function : restoreSubShapes
316 // purpose  :
317 //=================================================================================
318 void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
319                                           SALOMEDS::SObject_ptr theSObject)
320 {
321   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
322     // empty list of arguments means that all arguments should be restored
323     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
324                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
325                                          /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT,
326                                          mainFrame()->CheckBoxAddPrefix->isChecked()); // ? false
327   }
328 }