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