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