Salome HOME
*** empty log message ***
[modules/geom.git] / src / RepairGUI / RepairGUI_DivideEdgeDlg.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_DivideEdgeDlg.cxx
23 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
24 //
25
26 #include "RepairGUI_DivideEdgeDlg.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
42 //=================================================================================
43 // class    : RepairGUI_DivideEdgeDlg()
44 // purpose  : Constructs a RepairGUI_DivideEdgeDlg  which is a child of 'parent', with the
45 //            name 'name' and widget flags set to 'f'.
46 //            The dialog will by default be modeless, unless you set 'modal' to
47 //            TRUE to construct a modal dialog.
48 //=================================================================================
49 RepairGUI_DivideEdgeDlg::RepairGUI_DivideEdgeDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
50                                                   bool modal )
51   : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
52 {
53   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_DIVIDE_EDGE" ) ) );
54   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
55
56   setWindowTitle( tr( "GEOM_DIVIDE_EDGE_TITLE" ) );
57
58   /***************************************************************/
59   mainFrame()->GroupConstructors->setTitle(tr("GEOM_DIVIDE_EDGE_TITLE"));
60   mainFrame()->RadioButton1->setIcon( image0 );
61   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
62   mainFrame()->RadioButton2->close();
63   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
64   mainFrame()->RadioButton3->close();
65
66   GroupPoints = new DlgRef_1SelExt( centralWidget() );
67   GroupPoints->GroupBox1->setTitle( tr( "GEOM_ADD_POINT" ) );
68   GroupPoints->TextLabel1->setText( tr( "GEOM_EDGE" ) );
69   GroupPoints->PushButton1->setIcon( image1 );
70   GroupPoints->LineEdit1->setReadOnly( true );
71
72   QRadioButton* rb1 = new QRadioButton( tr( "GEOM_BY_PARAMETER" ), GroupPoints->Box );
73   QRadioButton* rb2 = new QRadioButton( tr( "GEOM_BY_LENGTH" ),    GroupPoints->Box );
74
75   myIsParameterGr = new QButtonGroup( GroupPoints->Box );
76   myIsParameterGr->addButton( rb1, 0 );
77   myIsParameterGr->addButton( rb2, 1 );
78   rb1->setChecked( true );
79
80   myValEdt = new QDoubleSpinBox( GroupPoints->Box );
81   initSpinBox( myValEdt, 0., 1., 0.1, 3 );
82   myValEdt->setValue( 0.5 );
83   QLabel* aLbl1 = new QLabel( tr( "GEOM_VALUE" ), GroupPoints->Box );
84
85   QGridLayout* l = new QGridLayout( GroupPoints->Box );
86   l->setMargin( 0 ); l->setSpacing( 6 );
87   l->addWidget( rb1, 0, 0, 1, 2 );
88   l->addWidget( rb2, 1, 0, 1, 2 );
89   l->addWidget( aLbl1, 2, 0 );
90   l->addWidget( myValEdt, 2, 1 );
91
92   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
93   layout->setMargin( 0 ); layout->setSpacing( 6 );
94   layout->addWidget( GroupPoints );
95
96   /***************************************************************/
97
98   setHelpFileName( "add_point_on_edge.htm" );
99
100   Init();
101 }
102
103 //=================================================================================
104 // function : ~RepairGUI_DivideEdgeDlg()
105 // purpose  : Destroys the object and frees any allocated resources
106 //=================================================================================
107 RepairGUI_DivideEdgeDlg::~RepairGUI_DivideEdgeDlg()
108 {
109 }
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void RepairGUI_DivideEdgeDlg::Init()
116 {
117   /* init variables */
118   myEditCurrentArgument = GroupPoints->LineEdit1;
119
120   myObject = GEOM::GEOM_Object::_nil();
121   myIndex = -1;
122
123   //myGeomGUI->SetState( 0 );
124   initSelection();
125
126   /* signals and slots connections */
127   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
128   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
129
130   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
131   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
132
133   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
134            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
135
136   initName( tr( "DEVIDE_EDGE_NEW_OBJECT_NAME" ) );
137 }
138
139
140 //=================================================================================
141 // function : ClickOnOk()
142 // purpose  : Same than click on apply but close this dialog.
143 //=================================================================================
144 void RepairGUI_DivideEdgeDlg::ClickOnOk()
145 {
146   if ( ClickOnApply() )
147     ClickOnCancel();
148 }
149
150
151 //=================================================================================
152 // function : ClickOnApply()
153 // purpose  :
154 //=================================================================================
155 bool RepairGUI_DivideEdgeDlg::ClickOnApply()
156 {
157   if ( !onAccept() )
158     return false;
159
160   initName();
161
162   myEditCurrentArgument->setText( "" );
163   myObject = GEOM::GEOM_Object::_nil();
164   myIndex = -1;
165
166   initSelection();
167
168   return true;
169 }
170
171
172 //=================================================================================
173 // function : SelectionIntoArgument()
174 // purpose  : Called when selection as changed or other case
175 //          : used only by SelectButtonC1A1 (LineEditC1A1)
176 //=================================================================================
177 void RepairGUI_DivideEdgeDlg::SelectionIntoArgument()
178 {
179   myEditCurrentArgument->setText( "" );
180
181   myObject = GEOM::GEOM_Object::_nil();
182   myIndex = -1;
183
184   if ( IObjectCount() == 1 ) {
185     Handle(SALOME_InteractiveObject) anIO = firstIObject();
186     Standard_Boolean aRes;
187     GEOM::GEOM_Object_var aSelectedObj = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
188     if ( !CORBA::is_nil( aSelectedObj ) && aRes ) {
189       TopoDS_Shape aShape;
190       if ( GEOMBase::GetShape( aSelectedObj, aShape, TopAbs_SHAPE ) ) {
191         const int aType = aShape.ShapeType();
192         if ( aType <= TopAbs_EDGE ) {
193           // edge, wire, face, shell, solid, compound
194           GEOM::short_array anIndexes;
195
196           TColStd_IndexedMapOfInteger aMap;
197           SalomeApp_Application* anApp =
198             (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
199           anApp->selectionMgr()->GetIndexes( anIO, aMap );
200
201           if ( !aMap.IsEmpty() ) { 
202             // subshape selection
203             myIndex = aMap( 1 );
204             myObject = aSelectedObj;
205             myEditCurrentArgument->setText( tr( "GEOM_EDGE" ) + "_1" );
206           }
207           else if ( aType == TopAbs_EDGE ) { 
208             // single shape selection
209             myIndex = -1;
210             myObject = aSelectedObj;
211             myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
212           }
213           else {
214             // face, shell, solid or compound was selected, and NOT its subshape.
215             myIndex = -1;
216             myObject = GEOM::GEOM_Object::_nil();
217           }
218         }
219       }
220     }
221   }
222 }
223
224 //=================================================================================
225 // function : SetEditCurrentArgument()
226 // purpose  :
227 //=================================================================================
228 void RepairGUI_DivideEdgeDlg::SetEditCurrentArgument()
229 {
230   if ( sender() == GroupPoints->PushButton1 ) {
231     GroupPoints->LineEdit1->setFocus();
232     myEditCurrentArgument = GroupPoints->LineEdit1;
233   }
234   SelectionIntoArgument();
235 }
236
237
238 //=================================================================================
239 // function : LineEditReturnPressed()
240 // purpose  :
241 //=================================================================================
242 void RepairGUI_DivideEdgeDlg::LineEditReturnPressed()
243 {
244   if ( sender() == GroupPoints->LineEdit1 ) {
245     myEditCurrentArgument = GroupPoints->LineEdit1;
246     GEOMBase_Skeleton::LineEditReturnPressed();
247   }
248 }
249
250
251 //=================================================================================
252 // function : ActivateThisDialog()
253 // purpose  :
254 //=================================================================================
255 void RepairGUI_DivideEdgeDlg::ActivateThisDialog()
256 {
257   GEOMBase_Skeleton::ActivateThisDialog();
258   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
259            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
260
261   myObject = GEOM::GEOM_Object::_nil();
262   myIndex = -1;
263
264   //myGeomGUI->SetState( 0 );
265   initSelection();
266 }
267
268 //=================================================================================
269 // function : enterEvent()
270 // purpose  : Mouse enter onto the dialog to activate it
271 //=================================================================================
272 void RepairGUI_DivideEdgeDlg::enterEvent( QEvent* )
273 {
274   if ( !mainFrame()->GroupConstructors->isEnabled() )
275     ActivateThisDialog();
276 }
277
278 //=================================================================================
279 // function : closeEvent()
280 // purpose  :
281 //=================================================================================
282 void RepairGUI_DivideEdgeDlg::closeEvent( QCloseEvent* e )
283 {
284   //myGeomGUI->SetState( -1 );
285   GEOMBase_Skeleton::closeEvent( e );
286 }
287
288
289 //=================================================================================
290 // function : createOperation
291 // purpose  :
292 //=================================================================================
293 GEOM::GEOM_IOperations_ptr RepairGUI_DivideEdgeDlg::createOperation()
294 {
295   return getGeomEngine()->GetIHealingOperations( getStudyId() );
296 }
297
298 //=================================================================================
299 // function : isValid
300 // purpose  :
301 //=================================================================================
302 bool RepairGUI_DivideEdgeDlg::isValid( QString& )
303 {
304   return !myObject->_is_nil();
305 }
306
307 //=================================================================================
308 // function : execute
309 // purpose  :
310 //=================================================================================
311 bool RepairGUI_DivideEdgeDlg::execute( ObjectList& objects )
312 {
313   GEOM::GEOM_Object_var anObj = GEOM::GEOM_IHealingOperations::_narrow( getOperation() )->DivideEdge
314     ( myObject, myIndex, myValEdt->value(), getIsByParameter() );
315   bool aResult = !anObj->_is_nil();
316   if ( aResult )
317     objects.push_back( anObj._retn() );
318
319   return aResult;
320 }
321
322 //=================================================================================
323 // function : getIsByParameter
324 // purpose  :
325 //=================================================================================
326 bool RepairGUI_DivideEdgeDlg::getIsByParameter() const
327 {
328   return myIsParameterGr->button( 0 )->isChecked();
329 }
330
331 //=================================================================================
332 // function : initSelection
333 // purpose  :
334 //=================================================================================
335 void RepairGUI_DivideEdgeDlg::initSelection()
336 {
337   GEOM::GEOM_Object_var aNullGeomObject;
338   localSelection( aNullGeomObject, TopAbs_EDGE ); // load local selection on ALL objects
339 }