Salome HOME
Issue 0019855 (Cannot delete objects with dependencies) : improve 'Delete objects...
[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   mySelBtn = 0;
65   mySelEdit = 0;
66   myDisplayer = 0;
67   myHelpFileName = "";
68
69   if ( !name )
70     setName( "MeasureGUI_Skeleton" );
71
72   buttonClose->setText( tr( "GEOM_BUT_CLOSE" ) );
73   buttonHelp->setText(tr("GEOM_BUT_HELP"));
74
75   buttonClose->setAutoDefault( false );
76
77   resize( 350, 0 );
78 }
79
80
81 //=================================================================================
82 // function : ~MeasureGUI_Skeleton()
83 // purpose  : Destroys the object and frees any allocated resources
84 //=================================================================================
85 MeasureGUI_Skeleton::~MeasureGUI_Skeleton()
86 {
87   myGeomGUI->SetActiveDialogBox( 0 );
88   delete myDisplayer;
89 }
90
91
92 //=================================================================================
93 // function : Init()
94 // purpose  :
95 //=================================================================================
96 void MeasureGUI_Skeleton::Init()
97 {
98   /* init variables */
99   myGeomGUI->SetActiveDialogBox((QDialog*)this);
100
101   /* signals and slots connections */
102   
103   connect( buttonClose, SIGNAL( clicked() ),
104           this,         SLOT( ClickOnCancel() ) );
105
106   connect( buttonHelp,  SIGNAL( clicked() ), 
107            this,        SLOT( ClickOnHelp() ) );
108   
109   connect( myGeomGUI,   SIGNAL( SignalDeactivateActiveDialog() ),
110            this,        SLOT  ( DeactivateActiveDialog() ) );
111            
112   connect( myGeomGUI,   SIGNAL( SignalCloseAllDialogs() ),
113            this,        SLOT  ( ClickOnCancel() ) );
114
115   connect( mySelEdit,   SIGNAL( returnPressed() ),
116            this,        SLOT( LineEditReturnPressed() ) );
117            
118   connect( mySelBtn,    SIGNAL( clicked() ),
119            this,        SLOT  ( SetEditCurrentArgument() ) );
120
121   LightApp_SelectionMgr* aSel = ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
122   if ( aSel )
123     connect( aSel, SIGNAL( currentSelectionChanged() ), 
124              this, SLOT  ( SelectionIntoArgument() ) ) ;
125
126   /* displays Dialog */
127   RadioButton1->setChecked( TRUE );
128
129   activateSelection();
130   SelectionIntoArgument();
131   show();
132 }
133
134
135 //=================================================================================
136 // function : ClickOnCancel()
137 // purpose  :
138 //=================================================================================
139 void MeasureGUI_Skeleton::ClickOnCancel()
140 {
141   close();
142 }
143
144 //=================================================================================
145 // function : ClickOnHelp()
146 // purpose  :
147 //=================================================================================
148 void MeasureGUI_Skeleton::ClickOnHelp()
149 {
150   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
151   if (app) 
152     app->onHelpContextModule(myGeomGUI ? app->moduleName(myGeomGUI->moduleName()) : QString(""), myHelpFileName);
153   else {
154                 QString platform;
155 #ifdef WIN32
156                 platform = "winapplication";
157 #else
158                 platform = "application";
159 #endif
160     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
161                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
162                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
163                            QObject::tr("BUT_OK"));
164   }
165 }
166
167 //=================================================================================
168 // function : LineEditReturnPressed()
169 // purpose  :
170 //=================================================================================
171 void MeasureGUI_Skeleton::LineEditReturnPressed()
172 {
173   /* User name of object input management                          */
174   /* If successfull the selection is changed and signal emitted... */
175   /* so SelectionIntoArgument() is automatically called.           */
176   
177   const QString objectUserName = mySelEdit->text();
178   QWidget* thisWidget = ( QWidget* )this;
179   
180   if ( GEOMBase::SelectionByNameInDialogs( thisWidget, objectUserName, selectedIO() ) )
181     mySelEdit->setText( objectUserName );
182 }
183
184
185 //=================================================================================
186 // function : DeactivateActiveDialog()
187 // purpose  :
188 //=================================================================================
189 void MeasureGUI_Skeleton::DeactivateActiveDialog()
190 {
191   setEnabled( false );
192   
193   LightApp_SelectionMgr* aSel = ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
194   if ( aSel )
195     disconnect( aSel, 0, this, 0 );
196   
197   myGeomGUI->SetActiveDialogBox( 0 );
198
199   globalSelection();
200 }
201
202
203 //=================================================================================
204 // function : ActivateThisDialog()
205 // purpose  :
206 //=================================================================================
207 void MeasureGUI_Skeleton::ActivateThisDialog()
208 {
209   myGeomGUI->EmitSignalDeactivateDialog();
210   
211   setEnabled( true );
212   
213   myGeomGUI->SetActiveDialogBox( ( QDialog* )this );
214
215   LightApp_SelectionMgr* aSel = ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
216   if ( aSel )
217     connect( aSel, SIGNAL( currentSelectionChanged() ), 
218              this, SLOT  ( SelectionIntoArgument() ) ) ;
219   
220   redisplayPreview();
221   activateSelection();
222 }
223
224 //=================================================================================
225 // function : SetEditCurrentArgument
226 // purpose  :
227 //=================================================================================
228 void MeasureGUI_Skeleton::SetEditCurrentArgument()
229 {
230   mySelEdit->setFocus();
231   SelectionIntoArgument();
232 }
233
234 //=================================================================================
235 // function : SelectionIntoArgument
236 // purpose  :
237 //=================================================================================
238 void MeasureGUI_Skeleton::SelectionIntoArgument()
239 {
240   myObj = GEOM::GEOM_Object::_nil();
241   
242   Standard_Boolean testResult = Standard_False;
243   GEOM::GEOM_Object_var aSelectedObject =
244     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
245   
246   if( !testResult || aSelectedObject->_is_nil() )
247   {
248     mySelEdit->setText( "" );
249     processObject();
250     erasePreview();
251     return;
252   }
253
254   myObj = aSelectedObject;
255   mySelEdit->setText( GEOMBase::GetName( myObj ) );
256   processObject();
257   redisplayPreview();
258 }
259
260 //=================================================================================
261 // function : processObject
262 // purpose  :
263 //=================================================================================
264 void MeasureGUI_Skeleton::processObject()
265 {  
266 }
267
268
269 //=================================================================================
270 // function : closeEvent
271 // purpose  :
272 //=================================================================================
273 void MeasureGUI_Skeleton::closeEvent( QCloseEvent* e )
274 {
275   LightApp_SelectionMgr* aSel = ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
276   if ( aSel )
277     disconnect( aSel, 0, this, 0 );
278   QDialog::closeEvent( e );
279 }
280
281 //=================================================================================
282 // function : enterEvent()
283 // purpose  :
284 //=================================================================================
285 void MeasureGUI_Skeleton::enterEvent(QEvent* e)
286 {
287   if ( GroupConstructors->isEnabled() )
288     return;
289     
290   ActivateThisDialog();
291 }
292
293 //=================================================================================
294 // function : buildPrs
295 // purpose  : Build presentation for "preview"
296 //=================================================================================
297 SALOME_Prs* MeasureGUI_Skeleton::buildPrs()
298 {
299   return 0;
300 }
301
302 //=================================================================================
303 // function : closeEvent()
304 // purpose  : Build presentation for "preview"
305 //=================================================================================
306 void MeasureGUI_Skeleton::redisplayPreview()
307 {
308   QString aMess;
309   if ( !isValid( aMess ) )
310   {
311     erasePreview( true );
312     return;
313   }
314
315   erasePreview( false );
316
317   try
318   {
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   {
329     SalomeApp_Tools::QtCatchCorbaException( e );
330   }
331   
332 }
333
334 //=================================================================================
335 // function : activateSelection
336 // purpose  : 
337 //=================================================================================
338 void MeasureGUI_Skeleton::activateSelection()
339 {
340   globalSelection( GEOM_ALLSHAPES );
341 }
342
343 //=================================================================================
344 // function : isValid
345 // purpose  :
346 //=================================================================================
347 bool MeasureGUI_Skeleton::isValid( QString& )
348 {
349   return !myObj->_is_nil();
350 }
351
352 //================================================================
353 // Function : getDisplayer
354 // Purpose  :
355 //================================================================
356 GEOM_Displayer* MeasureGUI_Skeleton::getDisplayer()
357 {
358   if ( !myDisplayer )
359     myDisplayer = new GEOM_Displayer( getStudy() );
360   return myDisplayer;
361 }
362
363 //=================================================================================
364 // function : createOperation
365 // purpose  :
366 //=================================================================================
367 GEOM::GEOM_IOperations_ptr MeasureGUI_Skeleton::createOperation()
368 {
369   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
370 }
371
372 //=================================================================================
373 // function : keyPressEvent()
374 // purpose  :
375 //=================================================================================
376 void MeasureGUI_Skeleton::keyPressEvent( QKeyEvent* e )
377 {
378   QDialog::keyPressEvent( e );
379   if ( e->isAccepted() )
380     return;
381
382   if ( e->key() == Key_F1 )
383     {
384       e->accept();
385       ClickOnHelp();
386     }
387 }