Salome HOME
Bug IPAL19097 NPAL16768 is not implemented for "Normal to a face"
[modules/geom.git] / src / MeasureGUI / MeasureGUI_Skeleton.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_Skeleton.cxx
23 // Author : Damine COQUERET, Open CASCADE S.A.S.
24 //
25
26 #include "MeasureGUI_Skeleton.h"
27 #include "MeasureGUI_Widgets.h"
28
29 #include <GEOMBase.h>
30 #include <GEOM_Displayer.h>
31 #include <GeometryGUI.h>
32
33 #include <LightApp_SelectionMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <SalomeApp_Tools.h>
36 #include <SUIT_Desktop.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Session.h>
40 #include <SUIT_OverrideCursor.h>
41
42 #include <QKeyEvent>
43
44 //=================================================================================
45 // class    : MeasureGUI_Skeleton()
46 // purpose  : Constructs a MeasureGUI_Skeleton which is a child of 'parent', with the 
47 //            name 'name' and widget flags set to 'f'.
48 //            The dialog will by default be modeless, unless you set 'modal' to
49 //            TRUE to construct a modal dialog.
50 //=================================================================================
51 MeasureGUI_Skeleton::MeasureGUI_Skeleton( GeometryGUI* GUI, QWidget* parent,
52                                           bool modal, Qt::WindowFlags f )
53   : QDialog( parent, f ),
54     GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) ),
55     myGeomGUI( GUI )
56 {
57   myMainFrame = new MeasureGUI_SkeletonBox( this );
58   QVBoxLayout* topLayout = new QVBoxLayout( this );
59   topLayout->setMargin( 9 ); topLayout->setSpacing( 0 );
60   topLayout->addWidget( myMainFrame );
61
62   setAttribute( Qt::WA_DeleteOnClose );
63   setModal( modal );
64   mySelBtn = 0;
65   mySelEdit = 0;
66   myDisplayer = 0;
67   myHelpFileName = "";
68   
69   buttonClose()->setText( tr( "GEOM_BUT_CLOSE" ) );
70   buttonHelp()->setText( tr( "GEOM_BUT_HELP" ) );
71 }
72
73
74 //=================================================================================
75 // function : ~MeasureGUI_Skeleton()
76 // purpose  : Destroys the object and frees any allocated resources
77 //=================================================================================
78 MeasureGUI_Skeleton::~MeasureGUI_Skeleton()
79 {
80   if ( myGeomGUI )
81     myGeomGUI->SetActiveDialogBox( 0 );
82   delete myDisplayer;
83 }
84
85
86 //=================================================================================
87 // function : Init()
88 // purpose  :
89 //=================================================================================
90 void MeasureGUI_Skeleton::Init()
91 {
92   /* init variables */
93   if ( myGeomGUI )
94     myGeomGUI->SetActiveDialogBox( (QDialog*)this );
95
96   /* signals and slots connections */
97   
98   connect( buttonClose(), SIGNAL( clicked() ),
99            this,          SLOT( ClickOnCancel() ) );
100
101   connect( buttonHelp(),  SIGNAL( clicked() ), 
102            this,          SLOT( ClickOnHelp() ) );
103   
104   if ( myGeomGUI ) {
105     connect( myGeomGUI,   SIGNAL( SignalDeactivateActiveDialog() ),
106              this,        SLOT  ( DeactivateActiveDialog() ) );
107     
108     connect( myGeomGUI,   SIGNAL( SignalCloseAllDialogs() ),
109              this,        SLOT  ( ClickOnCancel() ) );
110   }
111   
112   if ( mySelEdit ) {
113     connect( mySelEdit,   SIGNAL( returnPressed() ),
114              this,        SLOT( LineEditReturnPressed() ) );
115   }
116    
117   if ( mySelBtn ) {
118     connect( mySelBtn,    SIGNAL( clicked() ),
119              this,        SLOT  ( SetEditCurrentArgument() ) );
120   }
121
122   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
123   if ( aSel ) {
124     connect( aSel, SIGNAL( currentSelectionChanged() ), 
125              this, SLOT  ( SelectionIntoArgument() ) ) ;
126   }
127
128   /* displays Dialog */
129   mainFrame()->RadioButton1->setChecked( true );
130
131   activateSelection();
132   SelectionIntoArgument();
133 }
134
135
136 //=================================================================================
137 // function : ClickOnCancel()
138 // purpose  :
139 //=================================================================================
140 void MeasureGUI_Skeleton::ClickOnCancel()
141 {
142   close();
143 }
144
145 //=================================================================================
146 // function : ClickOnHelp()
147 // purpose  :
148 //=================================================================================
149 void MeasureGUI_Skeleton::ClickOnHelp()
150 {
151   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
152   if ( app ) {
153     app->onHelpContextModule( myGeomGUI ? app->moduleName( myGeomGUI->moduleName() ) : QString( "" ), myHelpFileName );
154   }
155   else {
156     QString platform;
157 #ifdef WIN32
158     platform = "winapplication";
159 #else
160     platform = "application";
161 #endif
162     SUIT_MessageBox::warning( this, tr( "WRN_WARNING" ),
163                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
164                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( myHelpFileName ) );
165   }
166 }
167
168 //=================================================================================
169 // function : LineEditReturnPressed()
170 // purpose  :
171 //=================================================================================
172 void MeasureGUI_Skeleton::LineEditReturnPressed()
173 {
174   /* User name of object input management                          */
175   /* If successfull the selection is changed and signal emitted... */
176   /* so SelectionIntoArgument() is automatically called.           */
177   
178   if ( mySelEdit ) {
179     const QString objectUserName = mySelEdit->text();
180     QWidget* thisWidget = ( QWidget* )this;
181     
182     if ( GEOMBase::SelectionByNameInDialogs( thisWidget, objectUserName, selectedIO() ) )
183       mySelEdit->setText( objectUserName );
184   }
185 }
186
187
188 //=================================================================================
189 // function : DeactivateActiveDialog()
190 // purpose  :
191 //=================================================================================
192 void MeasureGUI_Skeleton::DeactivateActiveDialog()
193 {
194   setEnabled( false );
195   
196   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
197   if ( aSel )
198     disconnect( aSel, 0, this, 0 );
199   
200   myGeomGUI->SetActiveDialogBox( 0 );
201
202   globalSelection();
203 }
204
205
206 //=================================================================================
207 // function : ActivateThisDialog()
208 // purpose  :
209 //=================================================================================
210 void MeasureGUI_Skeleton::ActivateThisDialog()
211 {
212   myGeomGUI->EmitSignalDeactivateDialog();
213   
214   setEnabled( true );
215   
216   myGeomGUI->SetActiveDialogBox( ( QDialog* )this );
217
218   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
219   if ( aSel )
220     connect( aSel, SIGNAL( currentSelectionChanged() ), 
221              this, SLOT  ( SelectionIntoArgument() ) ) ;
222   
223   redisplayPreview();
224   activateSelection();
225 }
226
227 //=================================================================================
228 // function : SetEditCurrentArgument
229 // purpose  :
230 //=================================================================================
231 void MeasureGUI_Skeleton::SetEditCurrentArgument()
232 {
233   mySelEdit->setFocus();
234   SelectionIntoArgument();
235 }
236
237 //=================================================================================
238 // function : SelectionIntoArgument
239 // purpose  :
240 //=================================================================================
241 void MeasureGUI_Skeleton::SelectionIntoArgument()
242 {
243   myObj = GEOM::GEOM_Object::_nil();
244   
245   Standard_Boolean testResult = Standard_False;
246   GEOM::GEOM_Object_var aSelectedObject =
247     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
248   
249   if ( !testResult || aSelectedObject->_is_nil() ) {
250     mySelEdit->setText( "" );
251     processObject();
252     erasePreview();
253     return;
254   }
255
256   myObj = aSelectedObject;
257   mySelEdit->setText( GEOMBase::GetName( myObj ) );
258   processObject();
259   redisplayPreview();
260 }
261
262 //=================================================================================
263 // function : processObject
264 // purpose  :
265 //=================================================================================
266 void MeasureGUI_Skeleton::processObject()
267 {  
268 }
269
270
271 //=================================================================================
272 // function : closeEvent
273 // purpose  :
274 //=================================================================================
275 void MeasureGUI_Skeleton::closeEvent( QCloseEvent* e )
276 {
277   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
278   if ( aSel )
279     disconnect( aSel, 0, this, 0 );
280   QDialog::closeEvent( e );
281 }
282
283 //=================================================================================
284 // function : enterEvent()
285 // purpose  :
286 //=================================================================================
287 void MeasureGUI_Skeleton::enterEvent( QEvent* )
288 {
289   if ( mainFrame()->GroupConstructors->isEnabled() )
290     return;
291     
292   ActivateThisDialog();
293 }
294
295 //=================================================================================
296 // function : buildPrs
297 // purpose  : Build presentation for "preview"
298 //=================================================================================
299 SALOME_Prs* MeasureGUI_Skeleton::buildPrs()
300 {
301   return 0;
302 }
303
304 //=================================================================================
305 // function : closeEvent()
306 // purpose  : Build presentation for "preview"
307 //=================================================================================
308 void MeasureGUI_Skeleton::redisplayPreview()
309 {
310   QString aMess;
311   if ( !isValid( aMess ) ) {
312     erasePreview( true );
313     return;
314   }
315
316   erasePreview( false );
317
318   try {
319     SUIT_OverrideCursor();
320
321     getDisplayer()->SetColor( Quantity_NOC_VIOLET );
322     getDisplayer()->SetToActivate( false );
323     
324     if ( SALOME_Prs* aPrs = buildPrs() )
325       displayPreview( aPrs );
326   }
327   catch ( const SALOME::SALOME_Exception& e ) {
328     SalomeApp_Tools::QtCatchCorbaException( e );
329   }
330   
331 }
332
333 //=================================================================================
334 // function : activateSelection
335 // purpose  : 
336 //=================================================================================
337 void MeasureGUI_Skeleton::activateSelection()
338 {
339   globalSelection( GEOM_ALLSHAPES );
340 }
341
342 //=================================================================================
343 // function : isValid
344 // purpose  :
345 //=================================================================================
346 bool MeasureGUI_Skeleton::isValid( QString& )
347 {
348   return !myObj->_is_nil();
349 }
350
351 //================================================================
352 // Function : getDisplayer
353 // Purpose  :
354 //================================================================
355 GEOM_Displayer* MeasureGUI_Skeleton::getDisplayer()
356 {
357   if ( !myDisplayer )
358     myDisplayer = new GEOM_Displayer( getStudy() );
359   return myDisplayer;
360 }
361
362 //=================================================================================
363 // function : createOperation
364 // purpose  :
365 //=================================================================================
366 GEOM::GEOM_IOperations_ptr MeasureGUI_Skeleton::createOperation()
367 {
368   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
369 }
370
371 //=================================================================================
372 // function : keyPressEvent()
373 // purpose  :
374 //=================================================================================
375 void MeasureGUI_Skeleton::keyPressEvent( QKeyEvent* e )
376 {
377   QDialog::keyPressEvent( e );
378   if ( e->isAccepted() )
379     return;
380
381   if ( e->key() == Qt::Key_F1 ) {
382     e->accept();
383     ClickOnHelp();
384   }
385 }
386
387 MeasureGUI_SkeletonBox* MeasureGUI_Skeleton::mainFrame() const
388 {
389   return myMainFrame;
390 }
391
392 QWidget* MeasureGUI_Skeleton::centralWidget() const
393 {
394   return mainFrame()->GroupMedium;
395 }
396
397 QPushButton* MeasureGUI_Skeleton::buttonClose() const
398 {
399   return mainFrame()->buttonClose;
400 }
401
402 QPushButton* MeasureGUI_Skeleton::buttonHelp() const
403 {
404   return mainFrame()->buttonHelp;
405 }