Salome HOME
*** empty log message ***
[modules/geom.git] / src / RepairGUI / RepairGUI_CloseContourDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  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 // File   : RepairGUI_CloseContourDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "RepairGUI_CloseContourDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SalomeApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ResourceMgr.h>
36
37 #include <GEOMImpl_Types.hxx>
38
39 #include <TopAbs.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TColStd_MapOfInteger.hxx>
42
43 //=================================================================================
44 // class    : RepairGUI_CloseContourDlg()
45 // purpose  : Constructs a RepairGUI_CloseContourDlg  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 RepairGUI_CloseContourDlg::RepairGUI_CloseContourDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
51                                                       bool modal )
52   : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
53 {
54   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_CLOSECONTOUR" ) ) );
55   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
56
57   setWindowTitle( tr( "GEOM_CLOSECONTOUR_TITLE" ) );
58
59   /***************************************************************/
60   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_CLOSECONTOUR_TITLE" ) );
61   mainFrame()->RadioButton1->setIcon( image0 );
62   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
63   mainFrame()->RadioButton2->close();
64   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
65   mainFrame()->RadioButton3->close();
66
67   GroupPoints = new DlgRef_2SelExt( centralWidget() );
68   GroupPoints->GroupBox1->setTitle( tr( "Contour to close" ) );
69   GroupPoints->TextLabel1->setText( tr( "GEOM_SELECTED_SHAPE" ) );
70   GroupPoints->PushButton1->setIcon( image1 );
71   GroupPoints->LineEdit1->setReadOnly( true );
72
73   GroupPoints->TextLabel2->setText( tr( "Contour to close" ) );
74   GroupPoints->PushButton2->setIcon( image1 );
75   GroupPoints->LineEdit2->setReadOnly( true );
76
77   QRadioButton* rb1 = new QRadioButton( tr( "Close by common vertex" ), GroupPoints->Box );
78   QRadioButton* rb2 = new QRadioButton( tr( "Close by new edge" ),      GroupPoints->Box );
79
80   myIsVertexGr = new QButtonGroup( GroupPoints->Box );
81   myIsVertexGr->addButton( rb1, 0 );
82   myIsVertexGr->addButton( rb2, 1 );
83   rb1->setChecked( true );
84
85   QVBoxLayout* l = new QVBoxLayout( GroupPoints->Box );
86   l->setMargin( 0 ); l->setSpacing( 6 );
87   l->addWidget( rb1 );
88   l->addWidget( rb2 );
89
90   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
91   layout->setMargin( 0 ); layout->setSpacing( 6 );
92   layout->addWidget( GroupPoints );
93
94   /***************************************************************/
95
96   setHelpFileName( "close_contour.htm" );
97
98   Init();
99 }
100
101
102 //=================================================================================
103 // function : ~RepairGUI_CloseContourDlg()
104 // purpose  : Destroys the object and frees any allocated resources
105 //=================================================================================
106 RepairGUI_CloseContourDlg::~RepairGUI_CloseContourDlg()
107 {
108 }
109
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void RepairGUI_CloseContourDlg::Init()
116 {
117   /* init variables */
118   myEditCurrentArgument = GroupPoints->LineEdit1;
119
120   myObject = GEOM::GEOM_Object::_nil();
121   myWiresInd = new GEOM::short_array();
122   myWiresInd->length( 0 );
123   
124   //myGeomGUI->SetState( 0 );
125   initSelection();
126
127   /* signals and slots connections */
128   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
129   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
130
131   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
132   connect( GroupPoints->PushButton2, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
133   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
134   connect( GroupPoints->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
135
136   connect( myGeomGUI->getApp()->selectionMgr(), 
137            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
138
139   initName( tr( "CLOSE_CONTOUR_NEW_OBJ_NAME" ) );
140 }
141
142
143 //=================================================================================
144 // function : ClickOnOk()
145 // purpose  : Same than click on apply but close this dialog.
146 //=================================================================================
147 void RepairGUI_CloseContourDlg::ClickOnOk()
148 {
149   if ( ClickOnApply() )
150     ClickOnCancel();
151 }
152
153
154
155 //=================================================================================
156 // function : ClickOnApply()
157 // purpose  :
158 //=================================================================================
159 bool RepairGUI_CloseContourDlg::ClickOnApply()
160 {
161   if ( !onAccept() )
162     return false;
163
164   initName();
165
166   myEditCurrentArgument = GroupPoints->LineEdit1;
167   myEditCurrentArgument->setText( "" );
168   myObject = GEOM::GEOM_Object::_nil();
169   myWiresInd->length( 0 );
170
171   initSelection();
172
173   return true;
174 }
175
176 //=================================================================================
177 // function : SelectionIntoArgument()
178 // purpose  : Called when selection as changed or other case
179 //          : used only by SelectButtonC1A1 (LineEditC1A1)
180 //=================================================================================
181 void RepairGUI_CloseContourDlg::SelectionIntoArgument()
182 {
183   erasePreview();
184   myEditCurrentArgument->setText( "" );
185   GroupPoints->LineEdit2->setText( "" );
186   
187   if ( myEditCurrentArgument == GroupPoints->LineEdit1 )
188     myObject = GEOM::GEOM_Object::_nil();
189   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 )
190     myWiresInd->length( 0 );
191
192   if ( IObjectCount() == 1 ) {
193     Handle(SALOME_InteractiveObject) anIO = firstIObject();
194
195     if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) { // face selection
196       Standard_Boolean aRes;
197       myObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
198       if ( aRes && GEOMBase::IsShape( myObject ) ) {
199         myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
200         TopoDS_Shape aShape;
201         if ( GEOMBase::GetShape( myObject, aShape, TopAbs_WIRE ) )
202           GroupPoints->LineEdit2->setText( myEditCurrentArgument->text() );
203       }
204       else
205         myObject = GEOM::GEOM_Object::_nil();
206     }
207     else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
208       TColStd_IndexedMapOfInteger aMap;
209       myGeomGUI->getApp()->selectionMgr()->GetIndexes( anIO, aMap );
210       const int n = aMap.Extent();
211       myWiresInd->length( n );
212       for ( int i = 1; i <= n; i++ )
213         myWiresInd[ i-1 ] = aMap( i );
214       if ( n )
215         myEditCurrentArgument->setText( QString::number( n ) + "_" + tr( "GEOM_WIRE" ) + tr( "_S_" ) );
216     }
217   }
218 }
219
220 //=================================================================================
221 // function : SetEditCurrentArgument()
222 // purpose  :
223 //=================================================================================
224 void RepairGUI_CloseContourDlg::SetEditCurrentArgument()
225 {
226   const QObject* send = sender();
227   if ( send == GroupPoints->PushButton1 )
228     myEditCurrentArgument = GroupPoints->LineEdit1; 
229   else if ( send == GroupPoints->PushButton2 && !myObject->_is_nil() )
230     myEditCurrentArgument = GroupPoints->LineEdit2;
231   if ( myEditCurrentArgument ) {
232     initSelection();
233     myEditCurrentArgument->setFocus();
234     SelectionIntoArgument();
235   }
236 }
237
238
239 //=================================================================================
240 // function : LineEditReturnPressed()
241 // purpose  :
242 //=================================================================================
243 void RepairGUI_CloseContourDlg::LineEditReturnPressed()
244 {
245   const QObject* send = sender();
246   if ( send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2 ) {
247     myEditCurrentArgument = (QLineEdit*)send;
248     GEOMBase_Skeleton::LineEditReturnPressed();
249   }
250 }
251
252
253 //=================================================================================
254 // function : ActivateThisDialog()
255 // purpose  :
256 //=================================================================================
257 void RepairGUI_CloseContourDlg::ActivateThisDialog()
258 {
259   GEOMBase_Skeleton::ActivateThisDialog();
260   connect( myGeomGUI->getApp()->selectionMgr(), 
261            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
262
263   myEditCurrentArgument = GroupPoints->LineEdit1;
264   myEditCurrentArgument->setText( "" );
265   GroupPoints->LineEdit2->setText( "" );
266   myObject = GEOM::GEOM_Object::_nil();
267   myWiresInd->length( 0 );
268
269   //myGeomGUI->SetState( 0 );
270   initSelection();
271 }
272
273
274 //=================================================================================
275 // function : enterEvent()
276 // purpose  : Mouse enter onto the dialog to activate it
277 //=================================================================================
278 void RepairGUI_CloseContourDlg::enterEvent( QEvent* )
279 {
280   if ( !mainFrame()->GroupConstructors->isEnabled() )
281     ActivateThisDialog();
282 }
283
284
285 //=================================================================================
286 // function : closeEvent()
287 // purpose  :
288 //=================================================================================
289 void RepairGUI_CloseContourDlg::closeEvent( QCloseEvent* e )
290 {
291   //myGeomGUI->SetState( -1 );
292   GEOMBase_Skeleton::closeEvent( e );
293 }
294
295 //=================================================================================
296 // function : createOperation
297 // purpose  :
298 //=================================================================================
299 GEOM::GEOM_IOperations_ptr RepairGUI_CloseContourDlg::createOperation()
300 {
301   return getGeomEngine()->GetIHealingOperations( getStudyId() );
302 }
303
304 //=================================================================================
305 // function : isValid
306 // purpose  :
307 //=================================================================================
308 bool RepairGUI_CloseContourDlg::isValid( QString& )
309 {
310   TopoDS_Shape aTmpShape;
311   return !myObject->_is_nil() && ( myWiresInd->length() || GEOMBase::GetShape( myObject, aTmpShape, TopAbs_WIRE ) );
312 }
313
314 //=================================================================================
315 // function : execute
316 // purpose  :
317 //=================================================================================
318 bool RepairGUI_CloseContourDlg::execute( ObjectList& objects )
319 {
320   GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->CloseContour( myObject, myWiresInd, getIsByVertex() );
321   bool aResult = !anObj->_is_nil();
322   if ( aResult )
323     objects.push_back( anObj._retn() );
324     
325   return aResult;
326 }
327
328 //=================================================================================
329 // function : getIsByVertex
330 // purpose  :
331 //=================================================================================
332 bool RepairGUI_CloseContourDlg::getIsByVertex() const
333 {
334   return myIsVertexGr->button( 0 )->isChecked();
335 }
336
337 //=================================================================================
338 // function : initSelection
339 // purpose  :
340 //=================================================================================
341 void RepairGUI_CloseContourDlg::initSelection()
342 {
343   if ( myEditCurrentArgument == GroupPoints->LineEdit1 ) {
344     TColStd_MapOfInteger aTypes;
345     aTypes.Add( GEOM_COMPOUND );
346     aTypes.Add( GEOM_SOLID );
347     aTypes.Add( GEOM_SHELL );
348     aTypes.Add( GEOM_FACE );
349     aTypes.Add( GEOM_WIRE );
350     
351     globalSelection( aTypes );
352   }
353   else if ( myEditCurrentArgument == GroupPoints->LineEdit2 ) {
354     localSelection( myObject, TopAbs_EDGE );
355     localSelection( myObject, TopAbs_WIRE );
356   }
357 }