]> SALOME platform Git repositories - modules/geom.git/blob - src/BooleanGUI/BooleanGUI_Dialog.cxx
Salome HOME
Fix for the issue "0021788: [CEA 622] preview on boolean operations : no preview".
[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 //=================================================================================
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   processPreview();
228 }
229
230 //=================================================================================
231 // function : SetEditCurrentArgument()
232 // purpose  :
233 //=================================================================================
234 void BooleanGUI_Dialog::SetEditCurrentArgument()
235 {
236   QPushButton* send = (QPushButton*)sender();
237
238   if (send == myGroup->PushButton1) {
239     myEditCurrentArgument = myGroup->LineEdit1;
240
241     myGroup->PushButton2->setDown(false);
242     myGroup->LineEdit2->setEnabled(false);
243   }
244   else if (send == myGroup->PushButton2) {
245     myEditCurrentArgument = myGroup->LineEdit2;
246
247     myGroup->PushButton1->setDown(false);
248     myGroup->LineEdit1->setEnabled(false);
249   }
250
251   // enable line edit
252   myEditCurrentArgument->setEnabled(true);
253   myEditCurrentArgument->setFocus();
254   // after setFocus(), because it will be setDown(false) when loses focus
255   send->setDown(true);
256 }
257
258 //=================================================================================
259 // function : ActivateThisDialog()
260 // purpose  :
261 //=================================================================================
262 void BooleanGUI_Dialog::ActivateThisDialog()
263 {
264   GEOMBase_Skeleton::ActivateThisDialog();
265
266   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
267            this, SLOT( SelectionIntoArgument() ) );
268   processPreview();
269 }
270
271 //=================================================================================
272 // function : enterEvent()
273 // purpose  : when mouse enter onto the QWidget
274 //=================================================================================
275 void BooleanGUI_Dialog::enterEvent (QEvent*)
276 {
277   if (!mainFrame()->GroupConstructors->isEnabled())
278     ActivateThisDialog();
279 }
280
281 //=================================================================================
282 // function : createOperation
283 // purpose  :
284 //=================================================================================
285 GEOM::GEOM_IOperations_ptr BooleanGUI_Dialog::createOperation()
286 {
287   return getGeomEngine()->GetIBooleanOperations(getStudyId());
288 }
289
290 //=================================================================================
291 // function : isValid
292 // purpose  :
293 //=================================================================================
294 bool BooleanGUI_Dialog::isValid (QString&)
295 {
296   return myObject1 && myObject2;
297 }
298
299 //=================================================================================
300 // function : execute
301 // purpose  :
302 //=================================================================================
303 bool BooleanGUI_Dialog::execute (ObjectList& objects)
304 {
305   GEOM::GEOM_Object_var anObj;
306
307   GEOM::GEOM_IBooleanOperations_var anOper = GEOM::GEOM_IBooleanOperations::_narrow(getOperation());
308   anObj = anOper->MakeBoolean(myObject1.get(), myObject2.get(), myOperation);
309   if (!anObj->_is_nil())
310     objects.push_back(anObj._retn());
311
312   return true;
313 }
314
315 //=================================================================================
316 // function : restoreSubShapes
317 // purpose  :
318 //=================================================================================
319 void BooleanGUI_Dialog::restoreSubShapes (SALOMEDS::Study_ptr   theStudy,
320                                           SALOMEDS::SObject_ptr theSObject)
321 {
322   if (mainFrame()->CheckBoxRestoreSS->isChecked()) {
323     // empty list of arguments means that all arguments should be restored
324     getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, GEOM::ListOfGO(),
325                                          /*theFindMethod=*/GEOM::FSM_GetInPlace, // ? GEOM::FSM_GetSame
326                                          /*theInheritFirstArg=*/myOperation == BooleanGUI::CUT,
327                                          mainFrame()->CheckBoxAddPrefix->isChecked()); // ? false
328   }
329 }