Salome HOME
e19115fee5ae9ad83d8adfcfc191fb8012b37ebc
[modules/geom.git] / src / RepairGUI / RepairGUI_RemoveIntWiresDlg.cxx
1 // Copyright (C) 2007-2013  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   : RepairGUI_RemoveIntWiresDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "RepairGUI_RemoveIntWiresDlg.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 // OCCT Includes
39 #include <TopAbs.hxx>
40 #include <TColStd_MapOfInteger.hxx>
41 #include <TColStd_IndexedMapOfInteger.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44
45 //=================================================================================
46 // class    : RepairGUI_RemoveIntWiresDlg()
47 // purpose  : Constructs a RepairGUI_RemoveIntWiresDlg  which is a child of 'parent', with the
48 //            name 'name' and widget flags set to 'f'.
49 //            The dialog will by default be modeless, unless you set 'modal' to
50 //            TRUE to construct a modal dialog.
51 //=================================================================================
52 RepairGUI_RemoveIntWiresDlg::RepairGUI_RemoveIntWiresDlg (GeometryGUI* theGeometryGUI, QWidget* parent,
53                                                           bool modal)
54   : GEOMBase_Skeleton(theGeometryGUI, parent, modal)
55 {
56   QPixmap image0 (myGeomGUI->getApp()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SUPPRESS_INT_WIRES")));
57   QPixmap image1 (myGeomGUI->getApp()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
58
59   setWindowTitle(tr("GEOM_REMOVE_INTERNAL_WIRES_TITLE"));
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle(tr("GEOM_REMOVE_INTERNAL_WIRES_TITLE"));
63   mainFrame()->RadioButton1->setIcon(image0);
64   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
65   mainFrame()->RadioButton2->close();
66   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
67   mainFrame()->RadioButton3->close();
68
69   GroupPoints = new DlgRef_1Sel1Check1Sel(centralWidget());
70   GroupPoints->GroupBox1->setTitle(tr("GEOM_INTERNAL_WIRES"));
71   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_FACE"));
72   GroupPoints->PushButton1->setIcon(image1);
73   GroupPoints->LineEdit1->setReadOnly(true);
74   GroupPoints->CheckButton1->setText(tr("GEOM_REMOVE_ALL_INT_WIRES"));
75   GroupPoints->TextLabel2->setText(tr("GEOM_WIRES_TO_REMOVE"));
76   GroupPoints->PushButton2->setIcon(image1);
77   GroupPoints->LineEdit2->setReadOnly(true);
78
79   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
80   layout->setMargin(0); layout->setSpacing(6);
81   layout->addWidget(GroupPoints);
82   /***************************************************************/
83
84   setHelpFileName("suppress_internal_wires_operation_page.html");
85
86   Init();
87 }
88
89 //=================================================================================
90 // function : ~RepairGUI_RemoveIntWiresDlg()
91 // purpose  : Destroys the object and frees any allocated resources
92 //=================================================================================
93 RepairGUI_RemoveIntWiresDlg::~RepairGUI_RemoveIntWiresDlg()
94 {
95 }
96
97 //=================================================================================
98 // function : Init()
99 // purpose  :
100 //=================================================================================
101 void RepairGUI_RemoveIntWiresDlg::Init()
102 {
103   // init variables
104   GroupPoints->LineEdit1->clear();
105   GroupPoints->LineEdit2->clear();
106   myObject = GEOM::GEOM_Object::_nil();
107   myWiresInd = new GEOM::short_array();
108   myWiresInd->length(0);
109
110   // signals and slots connections
111   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
112   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
113
114   connect(GroupPoints->PushButton1, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
115   connect(GroupPoints->PushButton2, SIGNAL(clicked()),       this, SLOT(SetEditCurrentArgument()));
116
117   connect(GroupPoints->LineEdit1,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
118   connect(GroupPoints->LineEdit2,   SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
119
120   connect(GroupPoints->CheckButton1, SIGNAL(clicked()), this, SLOT(onRemoveAllClicked()));
121
122   initName(tr("REMOVE_INT_WIRES_NEW_OBJ_NAME"));
123
124   GroupPoints->PushButton1->click();
125   SelectionIntoArgument();
126   resize(100,100);
127 }
128
129 //=================================================================================
130 // function : ClickOnOk()
131 // purpose  : Same than click on apply but close this dialog.
132 //=================================================================================
133 void RepairGUI_RemoveIntWiresDlg::ClickOnOk()
134 {
135   setIsApplyAndClose( true );
136   if (ClickOnApply())
137     ClickOnCancel();
138 }
139
140 //=================================================================================
141 // function : ClickOnApply()
142 // purpose  :
143 //=================================================================================
144 bool RepairGUI_RemoveIntWiresDlg::ClickOnApply()
145 {
146   if (!onAccept())
147     return false;
148
149   initName();
150   // activate first line edit
151   GroupPoints->PushButton1->click();
152   return true;
153 }
154
155 //=================================================================================
156 // function : SelectionIntoArgument()
157 // purpose  : Called when selection is changed or on dialog initialization or activation
158 //=================================================================================
159 void RepairGUI_RemoveIntWiresDlg::SelectionIntoArgument()
160 {
161   myEditCurrentArgument->setText("");
162   // the second argument depends on the first one
163   GroupPoints->LineEdit2->setText("");
164   myWiresInd->length(0);
165
166   if (myEditCurrentArgument == GroupPoints->LineEdit1)
167     myObject = GEOM::GEOM_Object::_nil();
168
169   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
170   SALOME_ListIO aSelList;
171   aSelMgr->selectedObjects(aSelList);
172
173   if (aSelList.Extent() == 1) {
174     Handle(SALOME_InteractiveObject) anIO = aSelList.First();
175
176     if (myEditCurrentArgument == GroupPoints->LineEdit1) { // face selection
177       myObject = GEOMBase::ConvertIOinGEOMObject( anIO );
178       if ( GEOMBase::IsShape(myObject) ) {
179         myEditCurrentArgument->setText(GEOMBase::GetName(myObject));
180
181         // clear selection
182         disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
183         myGeomGUI->getApp()->selectionMgr()->clearSelected();
184         connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
185                 this, SLOT(SelectionIntoArgument()));
186
187         if (!GroupPoints->CheckButton1->isChecked())
188           GroupPoints->PushButton2->click();
189       }
190       else
191         myObject = GEOM::GEOM_Object::_nil();
192     }
193     else if (myEditCurrentArgument == GroupPoints->LineEdit2 &&
194              !GroupPoints->CheckButton1->isChecked()) {
195       TColStd_IndexedMapOfInteger aMap;
196       aSelMgr->GetIndexes(anIO, aMap);
197       const int n = aMap.Extent();
198       myWiresInd->length(n);
199       for (int i = 1; i <= n; i++)
200         myWiresInd[i-1] = aMap(i);
201       if (n)
202         myEditCurrentArgument->setText(QString::number(n) + "_" + tr("GEOM_WIRE") + tr("_S_"));
203     }
204   }
205 }
206
207 //=================================================================================
208 // function : SetEditCurrentArgument()
209 // purpose  :
210 //=================================================================================
211 void RepairGUI_RemoveIntWiresDlg::SetEditCurrentArgument()
212 {
213   QPushButton* send = (QPushButton*)sender();
214
215   bool isEffective = false;
216
217   if (send == GroupPoints->PushButton1) {
218     isEffective = true;
219     myEditCurrentArgument = GroupPoints->LineEdit1;
220
221     GroupPoints->PushButton2->setDown(false);
222     GroupPoints->LineEdit2->setEnabled(false);
223   }
224   else if (send == GroupPoints->PushButton2 && !myObject->_is_nil()) {
225     isEffective = true;
226     myEditCurrentArgument = GroupPoints->LineEdit2;
227
228     GroupPoints->PushButton1->setDown(false);
229     GroupPoints->LineEdit1->setEnabled(false);
230   }
231
232   if (isEffective) {
233     initSelection();
234
235     // enable line edit
236     myEditCurrentArgument->setEnabled(true);
237     myEditCurrentArgument->setFocus();
238     // after setFocus(), because it will be setDown(false) when loses focus
239     send->setDown(true);
240   }
241 }
242
243 //=================================================================================
244 // function : LineEditReturnPressed()
245 // purpose  :
246 //=================================================================================
247 void RepairGUI_RemoveIntWiresDlg::LineEditReturnPressed()
248 {
249   const QObject* send = sender();
250   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2) {
251     myEditCurrentArgument = (QLineEdit*)send;
252     GEOMBase_Skeleton::LineEditReturnPressed();
253   }
254 }
255
256 //=================================================================================
257 // function : ActivateThisDialog()
258 // purpose  :
259 //=================================================================================
260 void RepairGUI_RemoveIntWiresDlg::ActivateThisDialog()
261 {
262   GEOMBase_Skeleton::ActivateThisDialog();
263   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
264            this, SLOT( SelectionIntoArgument() ) );
265
266   myEditCurrentArgument = GroupPoints->LineEdit1;
267   myEditCurrentArgument->setText( "" );
268   GroupPoints->LineEdit2->setText( "" );
269   myObject = GEOM::GEOM_Object::_nil();
270   myWiresInd->length( 0 );
271
272   //myGeomGUI->SetState( 0 );
273   initSelection();
274 }
275
276 //=================================================================================
277 // function : enterEvent()
278 // purpose  : Mouse enter onto the dialog to activate it
279 //=================================================================================
280 void RepairGUI_RemoveIntWiresDlg::enterEvent (QEvent*)
281 {
282   if (!mainFrame()->GroupConstructors->isEnabled())
283     ActivateThisDialog();
284 }
285
286 //=================================================================================
287 // function : createOperation
288 // purpose  :
289 //=================================================================================
290 GEOM::GEOM_IOperations_ptr RepairGUI_RemoveIntWiresDlg::createOperation()
291 {
292   return getGeomEngine()->GetIHealingOperations(getStudyId());
293 }
294
295 //=================================================================================
296 // function : isValid
297 // purpose  :
298 //=================================================================================
299 bool RepairGUI_RemoveIntWiresDlg::isValid (QString&)
300 {
301   return !myObject->_is_nil() && (GroupPoints->CheckButton1->isChecked() || myWiresInd->length());
302 }
303
304 //=================================================================================
305 // function : execute
306 // purpose  :
307 //=================================================================================
308 bool RepairGUI_RemoveIntWiresDlg::execute (ObjectList& objects)
309 {
310   GEOM::GEOM_IHealingOperations_var anOper = GEOM::GEOM_IHealingOperations::_narrow(getOperation());
311   GEOM::GEOM_Object_var anObj = anOper->RemoveIntWires(myObject, myWiresInd);
312
313   bool aResult = !anObj->_is_nil();
314   if (aResult)
315     objects.push_back(anObj._retn());
316
317   return aResult;
318 }
319
320 //=================================================================================
321 // function : onRemoveAllClicked
322 // purpose  :
323 //=================================================================================
324 void RepairGUI_RemoveIntWiresDlg::onRemoveAllClicked()
325 {
326   bool b = GroupPoints->CheckButton1->isChecked();
327   GroupPoints->TextLabel2->setEnabled(!b);
328   GroupPoints->PushButton2->setEnabled(!b);
329   GroupPoints->LineEdit2->setEnabled(!b);
330   if (b) {
331     GroupPoints->LineEdit2->setText("");
332     myWiresInd->length(0);
333   }
334 }
335
336 //=================================================================================
337 // function : initSelection
338 // purpose  :
339 //=================================================================================
340 void RepairGUI_RemoveIntWiresDlg::initSelection()
341 {
342   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
343
344   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
345     TColStd_MapOfInteger aTypes;
346     aTypes.Add(GEOM_COMPOUND);
347     aTypes.Add(GEOM_SOLID);
348     aTypes.Add(GEOM_SHELL);
349     aTypes.Add(GEOM_FACE);
350
351     globalSelection(aTypes);
352   }
353   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
354     //localSelection(myObject, TopAbs_EDGE);
355     localSelection(myObject, TopAbs_WIRE);
356   }
357
358   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
359           this, SLOT(SelectionIntoArgument()));
360 }