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