Salome HOME
Merge from BR_Dev_For_4_0 branch (from tag mergeto_BR_QT4_Dev_17Jan08)
[modules/geom.git] / src / MeasureGUI / MeasureGUI_NormaleDlg.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   : MeasureGUI_NormaleDlg.cxx
23 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
24 //
25
26 #include "MeasureGUI_NormaleDlg.h"
27
28 #include <DlgRef.h>
29 #include <GEOMBase.h>
30 #include <GeometryGUI.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include "GEOMImpl_Types.hxx"
38
39 //=================================================================================
40 // class    : MeasureGUI_NormaleDlg()
41 // purpose  : Constructs a MeasureGUI_NormaleDlg which is a child of 'parent', with the
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 MeasureGUI_NormaleDlg::MeasureGUI_NormaleDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
47   : GEOMBase_Skeleton( theGeometryGUI, parent, false )
48 {
49   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
50   QPixmap image0( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_NORMALE" ) ) );
51   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
52
53   setWindowTitle( tr( "GEOM_NORMALE_TITLE" ) );
54
55   /***************************************************************/
56   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_NORMALE" ) );
57   mainFrame()->RadioButton1->setIcon( image0 );
58   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
59   mainFrame()->RadioButton2->close();
60   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->RadioButton3->close();
62
63   GroupArgs = new DlgRef_2Sel ( centralWidget() );
64   GroupArgs->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
65
66   GroupArgs->TextLabel1->setText( tr( "GEOM_FACE" ) );
67   GroupArgs->TextLabel2->setText( tr( "GEOM_POINT" ) );
68
69   GroupArgs->PushButton1->setIcon( image1 );
70   GroupArgs->PushButton2->setIcon( image1 );
71
72   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
73   layout->setMargin( 0 ); layout->setSpacing( 6 );
74   layout->addWidget( GroupArgs );
75
76   /***************************************************************/
77
78   setHelpFileName("using_measurement_tools_page.html#normale_anchor");
79
80   Init();
81 }
82
83 //=================================================================================
84 // function : ~MeasureGUI_NormaleDlg()
85 // purpose  : Destroys the object and frees any allocated resources
86 //=================================================================================
87 MeasureGUI_NormaleDlg::~MeasureGUI_NormaleDlg()
88 {
89 }
90
91 //=================================================================================
92 // function : Init()
93 // purpose  :
94 //=================================================================================
95 void MeasureGUI_NormaleDlg::Init()
96 {
97   /* init variables */
98   GroupArgs->LineEdit1->setReadOnly( true );
99   GroupArgs->LineEdit2->setReadOnly( true );
100
101   myFace = GEOM::GEOM_Object::_nil();
102   myPoint = GEOM::GEOM_Object::_nil();
103
104   myEditCurrentArgument = GroupArgs->LineEdit1;
105   globalSelection( GEOM_FACE );
106
107   /* signals and slots connections */
108   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
109   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
110
111   connect( GroupArgs->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
112   connect( GroupArgs->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
113
114   connect( GroupArgs->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
115   connect( GroupArgs->LineEdit2,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
116
117   connect( myGeomGUI->getApp()->selectionMgr(),
118            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
119
120   initName(tr("GEOM_VECTOR_NORMALE"));
121
122   SelectionIntoArgument();
123 }
124
125 //=================================================================================
126 // function : ClickOnOk()
127 // purpose  :
128 //=================================================================================
129 void MeasureGUI_NormaleDlg::ClickOnOk()
130 {
131   if ( ClickOnApply() )
132     ClickOnCancel();
133 }
134
135 //=================================================================================
136 // function : ClickOnApply()
137 // purpose  :
138 //=================================================================================
139 bool MeasureGUI_NormaleDlg::ClickOnApply()
140 {
141   if ( !onAccept() )
142     return false;
143
144   initName();
145   return true;
146 }
147
148 //=================================================================================
149 // function : SelectionIntoArgument()
150 // purpose  : Called when selection as changed or other case
151 //=================================================================================
152 void MeasureGUI_NormaleDlg::SelectionIntoArgument()
153 {
154   erasePreview();
155   myEditCurrentArgument->setText( "" );
156
157   if ( myEditCurrentArgument == GroupArgs->LineEdit1 ) {
158     myFace = GEOM::GEOM_Object::_nil();
159   }
160   else if ( myEditCurrentArgument == GroupArgs->LineEdit2 ) {
161     myPoint = GEOM::GEOM_Object::_nil();
162   }
163   
164   if ( IObjectCount() != 1 )
165     return;
166
167   // nbSel == 1
168   Standard_Boolean testResult = Standard_False;
169   GEOM::GEOM_Object_var aSelectedObject =
170     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
171
172   if ( !testResult )
173     return;
174
175   if ( myEditCurrentArgument == GroupArgs->LineEdit1 ) {
176     myFace = aSelectedObject;
177   }
178   else if ( myEditCurrentArgument == GroupArgs->LineEdit2 ) {
179     myPoint = aSelectedObject;
180   }
181
182   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
183
184   displayPreview();
185 }
186
187 //=================================================================================
188 // function : LineEditReturnPressed()
189 // purpose  :
190 //=================================================================================
191 void MeasureGUI_NormaleDlg::LineEditReturnPressed()
192 {
193   QLineEdit* send = (QLineEdit*)sender();
194   if ( send == GroupArgs->LineEdit1 ||
195        send == GroupArgs->LineEdit2 ) {
196     myEditCurrentArgument = send;
197     GEOMBase_Skeleton::LineEditReturnPressed();
198   }
199 }
200
201 //=================================================================================
202 // function : SetEditCurrentArgument()
203 // purpose  :
204 //=================================================================================
205 void MeasureGUI_NormaleDlg::SetEditCurrentArgument()
206 {
207   QPushButton* send = (QPushButton*)sender();
208
209   if ( send == GroupArgs->PushButton1 ) {
210     myEditCurrentArgument = GroupArgs->LineEdit1;
211     globalSelection( GEOM_FACE );
212   }
213   else if ( send == GroupArgs->PushButton2 ) {
214     myEditCurrentArgument = GroupArgs->LineEdit2;
215     globalSelection( GEOM_POINT );
216   }
217
218   myEditCurrentArgument->setFocus();
219   SelectionIntoArgument();
220 }
221
222 //=================================================================================
223 // function : ActivateThisDialog()
224 // purpose  :
225 //=================================================================================
226 void MeasureGUI_NormaleDlg::ActivateThisDialog()
227 {
228   GEOMBase_Skeleton::ActivateThisDialog();
229
230   SelectionIntoArgument();
231 }
232
233 //=================================================================================
234 // function : enterEvent()
235 // purpose  :
236 //=================================================================================
237 void MeasureGUI_NormaleDlg::enterEvent( QEvent* e )
238 {
239   if ( !mainFrame()->GroupConstructors->isEnabled() )
240     ActivateThisDialog();
241 }
242
243 //=================================================================================
244 // function : createOperation
245 // purpose  :
246 //=================================================================================
247 GEOM::GEOM_IOperations_ptr MeasureGUI_NormaleDlg::createOperation()
248 {
249   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
250 }
251
252 //=================================================================================
253 // function : isValid
254 // purpose  :
255 //=================================================================================
256 bool MeasureGUI_NormaleDlg::isValid( QString& )
257 {
258   //return !CORBA::is_nil(myFace) && !CORBA::is_nil(myPoint);
259   return !CORBA::is_nil( myFace );
260 }
261
262 //=================================================================================
263 // function : execute
264 // purpose  :
265 //=================================================================================
266 bool MeasureGUI_NormaleDlg::execute( ObjectList& objects )
267 {
268   GEOM::GEOM_Object_var anObj =
269     GEOM::GEOM_IMeasureOperations::_narrow( getOperation() )->GetNormal( myFace, myPoint );
270
271   if ( !anObj->_is_nil() )
272     objects.push_back( anObj._retn() );
273
274   return true;
275 }