Salome HOME
a769ce1c1ea6d2b1cf35353607b4cf13c150c315
[modules/geom.git] / src / BuildGUI / BuildGUI_WireDlg.cxx
1 //  Copyright (C) 2007-2010  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   : BuildGUI_WireDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BuildGUI_WireDlg.h"
28
29 #include <GEOMImpl_Types.hxx>
30
31 #include <DlgRef.h>
32 #include <GeometryGUI.h>
33 #include <GEOMBase.h>
34
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_Session.h>
37 #include <SalomeApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39 #include <SALOME_ListIteratorOfListIO.hxx>
40
41 #include <TColStd_MapOfInteger.hxx>
42 #include <TColStd_IndexedMapOfInteger.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44 #include <Precision.hxx>
45
46 //=================================================================================
47 // class    : BuildGUI_WireDlg()
48 // purpose  : Constructs a BuildGUI_WireDlg 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 BuildGUI_WireDlg::BuildGUI_WireDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
54   : GEOMBase_Skeleton( theGeometryGUI, parent )
55 {
56   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BUILD_WIRE" ) ) );
57   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
58
59   setWindowTitle( tr( "GEOM_WIRE_TITLE" ) );
60
61   /***************************************************************/
62   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_WIRE" ) );
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   GroupType = new DlgRef_3Radio( centralWidget() );
70   GroupType->GroupBox1->setTitle( tr( "GEOM_OBJECT_TYPE" ) );
71   GroupType->RadioButton1->setText( tr( "GEOM_EDGE" ) );
72   GroupType->RadioButton2->setText( tr( "GEOM_WIRE" ) );
73   GroupType->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
74   GroupType->RadioButton3->close();
75
76   GroupArgs = new DlgRef_1Sel1Spin( centralWidget() );
77   GroupArgs->GroupBox1->setTitle( tr( "GEOM_WIRE_CONNECT" ) );
78   GroupArgs->TextLabel1->setText( tr( "GEOM_OBJECTS" ) );
79   GroupArgs->PushButton1->setIcon( image1 );
80   GroupArgs->LineEdit1->setReadOnly( true );
81
82   GroupArgs->TextLabel2->setText( tr( "GEOM_TOLERANCE" ) );
83   double SpecificStep = 0.0001;
84   double prec = Precision::Confusion();
85   initSpinBox(GroupArgs->SpinBox_DX, prec, MAX_NUMBER, SpecificStep, "len_tol_precision" );
86   GroupArgs->SpinBox_DX->setValue(prec);
87
88   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
89   layout->setMargin( 0 ); layout->setSpacing( 6 );
90   layout->addWidget( GroupType );
91   layout->addWidget( GroupArgs );
92   /***************************************************************/
93
94   setHelpFileName( "create_wire_page.html" );
95
96   /* Initialisations */
97   Init();
98 }
99
100
101 //=================================================================================
102 // function : ~BuildGUI_WireDlg()
103 // purpose  : Destroys the object and frees any allocated resources
104 //=================================================================================
105 BuildGUI_WireDlg::~BuildGUI_WireDlg()
106 {
107   // no need to delete child widgets, Qt does it all for us
108 }
109
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void BuildGUI_WireDlg::Init()
116 {
117   /* init variables */
118   myEditCurrentArgument = GroupArgs->LineEdit1;
119   GroupArgs->LineEdit1->setReadOnly( true );
120   GroupType->RadioButton1->setChecked(true);
121   
122   myOkEdgesAndWires = false;
123   
124   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
125
126   /* signals and slots connections */
127   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
128   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
129   connect( GroupArgs->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
130   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
131            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
132
133   connect( GroupType->RadioButton1, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
134   connect( GroupType->RadioButton2, SIGNAL( clicked() ), this, SLOT( TypeButtonClicked() ) );
135
136   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
137   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
138   
139   initName( tr( "GEOM_WIRE" ) );
140   SelectionIntoArgument();
141 }
142
143
144 //=================================================================================
145 // function : ClickOnOk()
146 // purpose  :
147 //=================================================================================
148 void BuildGUI_WireDlg::ClickOnOk()
149 {
150   if ( ClickOnApply() )
151     ClickOnCancel();
152 }
153
154
155 //=================================================================================
156 // function : ClickOnApply()
157 // purpose  :
158 //=================================================================================
159 bool BuildGUI_WireDlg::ClickOnApply()
160 {
161   if ( !onAccept() || !myOkEdgesAndWires )
162     return false;
163
164   initName();
165   TypeButtonClicked();
166   myMapToStudy.clear();
167   myEdgesAndWires.length(0);
168   myOkEdgesAndWires = false;
169   myEditCurrentArgument->setText( "" );
170   return true;
171 }
172
173 //=================================================================================
174 // function : TypeBittonClicked()
175 // purpose  : Radio button management
176 //=================================================================================
177 void BuildGUI_WireDlg::TypeButtonClicked()
178 {
179   if ( GroupType->RadioButton1->isChecked() ) {
180     globalSelection(); // close local contexts, if any
181     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_EDGE );
182     GroupArgs->TextLabel1->setText( tr( "GEOM_EDGE" ) );
183   }
184   else if ( GroupType->RadioButton2->isChecked() ) {
185     globalSelection(); // close local contexts, if any
186     localSelection( GEOM::GEOM_Object::_nil(), TopAbs_WIRE );
187     GroupArgs->TextLabel1->setText( tr( "GEOM_WIRE" ) );
188   }
189   SelectionIntoArgument();
190 }
191
192 //=================================================================================
193 // function : SelectionIntoArgument()
194 // purpose  : Called when selection as changed or other case
195 //=================================================================================
196 void BuildGUI_WireDlg::SelectionIntoArgument()
197 {
198   myEditCurrentArgument->setText( "" );
199   QString aString = ""; /* name of selection */
200
201   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
202   SALOME_ListIO aSelList;
203   aSelMgr->selectedObjects(aSelList);
204
205   myOkEdgesAndWires = false;
206   int nbSel = GEOMBase::GetNameOfSelectedIObjects(aSelList, aString);
207
208   if ( nbSel == 0 ) {
209     myMapToStudy.clear();
210     return;
211   }
212
213   TopAbs_ShapeEnum aNeedType = TopAbs_EDGE;
214   if (GroupType->RadioButton2->isChecked())
215     aNeedType = TopAbs_WIRE;
216
217   std::list<GEOM::GEOM_Object_var> aList; // subshapes list
218   TopoDS_Shape aShape;
219   Standard_Boolean aRes = Standard_False;
220   for (SALOME_ListIteratorOfListIO anIt (aSelList); anIt.More(); anIt.Next()) {
221     TColStd_IndexedMapOfInteger aMap;
222     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
223     if ( !CORBA::is_nil(aSelectedObject) && aRes && GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
224       aSelMgr->GetIndexes( anIt.Value(), aMap );
225
226       if ( aMap.Extent() > 0 ) { // local selection
227         for (int ind = 1; ind <= aMap.Extent(); ind++) {
228           aString = aSelectedObject->GetName();
229           int anIndex = aMap(ind);
230           if ( aNeedType == TopAbs_EDGE )
231             aString += QString( ":edge_%1" ).arg( anIndex );
232           else
233             aString += QString( ":wire_%1" ).arg( anIndex );
234           
235           //Find SubShape Object in Father
236           GEOM::GEOM_Object_var aFindedObject = GEOMBase_Helper::findObjectInFather( aSelectedObject, aString );
237           
238           if ( aFindedObject == GEOM::GEOM_Object::_nil() ) { // Object not found in study
239             GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
240             aList.push_back( aShapesOp->GetSubShape( aSelectedObject, anIndex ) );
241             myMapToStudy[aString] = aShapesOp->GetSubShape( aSelectedObject, anIndex );
242           }
243           else {
244             aList.push_back( aFindedObject ); // get Object from study
245           }
246         }
247       } else { // global selection
248         if ( aShape.ShapeType() == aNeedType ) {
249           GEOMBase::ConvertListOfIOInListOfGO(aSelList,  myEdgesAndWires);
250         } else {
251           aList.clear();
252           myEdgesAndWires.length(0);
253         }
254       }
255     }
256   }
257
258   // convert aList in listofgo
259   if ( aList.size() ) {
260     myEdgesAndWires.length( aList.size()  );
261     int k = 0;
262     for ( std::list<GEOM::GEOM_Object_var>::iterator j = aList.begin(); j != aList.end(); j++ )
263       myEdgesAndWires[k++] = *j;
264   }
265     
266   if ( myEdgesAndWires.length() > 1 )
267     aString = tr( "%1_objects" ).arg( myEdgesAndWires.length() );
268
269   if ( !myEdgesAndWires.length() ) {
270     aString = "";
271     myMapToStudy.clear();
272   }
273
274   myEditCurrentArgument->setText( aString );
275   myOkEdgesAndWires = true;
276 }
277
278
279 //=================================================================================
280 // function : SetEditCurrentArgument()
281 // purpose  :
282 //=================================================================================
283 void BuildGUI_WireDlg::SetEditCurrentArgument()
284 {
285   QPushButton* send = (QPushButton*)sender();
286   if ( send != GroupArgs->PushButton1 )
287     return;
288
289   TypeButtonClicked();
290   myEditCurrentArgument = GroupArgs->LineEdit1;
291
292   myEditCurrentArgument->setFocus();
293   SelectionIntoArgument();
294 }
295
296
297 //=================================================================================
298 // function : ActivateThisDialog()
299 // purpose  :
300 //=================================================================================
301 void BuildGUI_WireDlg::ActivateThisDialog()
302 {
303   GEOMBase_Skeleton::ActivateThisDialog();
304   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
305            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
306
307   TypeButtonClicked();
308 }
309
310 //=================================================================================
311 // function : DeactivateActiveDialog()
312 // purpose  : public slot to deactivate if active
313 //=================================================================================
314 void BuildGUI_WireDlg::DeactivateActiveDialog()
315 {
316   GEOMBase_Skeleton::DeactivateActiveDialog();
317 }
318
319 //=================================================================================
320 // function : enterEvent()
321 // purpose  :
322 //=================================================================================
323 void BuildGUI_WireDlg::enterEvent( QEvent* )
324 {
325   if ( !mainFrame()->GroupConstructors->isEnabled() )
326     ActivateThisDialog();
327 }
328
329 //=================================================================================
330 // function : createOperation
331 // purpose  :
332 //=================================================================================
333 GEOM::GEOM_IOperations_ptr BuildGUI_WireDlg::createOperation()
334 {
335   return getGeomEngine()->GetIShapesOperations( getStudyId() );
336 }
337
338 //=================================================================================
339 // function : isValid
340 // purpose  :
341 //=================================================================================
342 bool BuildGUI_WireDlg::isValid (QString& msg)
343 {
344   bool ok = GroupArgs->SpinBox_DX->isValid(msg, !IsPreview());
345   return myOkEdgesAndWires && ok;
346 }
347
348 //=================================================================================
349 // function : execute
350 // purpose  :
351 //=================================================================================
352 bool BuildGUI_WireDlg::execute (ObjectList& objects)
353 {
354   GEOM::GEOM_IShapesOperations_var anOper = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
355   GEOM::GEOM_Object_var anObj = anOper->MakeWire(myEdgesAndWires, GroupArgs->SpinBox_DX->value());
356
357   if (!anObj->_is_nil())
358     objects.push_back(anObj._retn());
359
360   return true;
361 }
362
363 //=================================================================================
364 // function : addSubshapeToStudy
365 // purpose  : virtual method to add new SubObjects if local selection
366 //=================================================================================
367 void BuildGUI_WireDlg::addSubshapesToStudy()
368 {
369   addSubshapesToFather( myMapToStudy );
370 }