Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / MeasureGUI / MeasureGUI_Skeleton.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : MeasureGUI_Skeleton.cxx
24 // Author : Damine COQUERET, Open CASCADE S.A.S.
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     LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
183     SALOME_ListIO aSelList;
184     aSelMgr->selectedObjects(aSelList);
185
186     if (GEOMBase::SelectionByNameInDialogs(thisWidget, objectUserName, aSelList))
187       mySelEdit->setText(objectUserName);
188   }
189 }
190
191
192 //=================================================================================
193 // function : DeactivateActiveDialog()
194 // purpose  :
195 //=================================================================================
196 void MeasureGUI_Skeleton::DeactivateActiveDialog()
197 {
198   setEnabled( false );
199   
200   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
201   if ( aSel )
202     disconnect( aSel, 0, this, 0 );
203   
204   myGeomGUI->SetActiveDialogBox( 0 );
205
206   globalSelection();
207 }
208
209
210 //=================================================================================
211 // function : ActivateThisDialog()
212 // purpose  :
213 //=================================================================================
214 void MeasureGUI_Skeleton::ActivateThisDialog()
215 {
216   myGeomGUI->EmitSignalDeactivateDialog();
217   
218   setEnabled( true );
219   
220   myGeomGUI->SetActiveDialogBox( ( QDialog* )this );
221
222   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
223   if ( aSel )
224     connect( aSel, SIGNAL( currentSelectionChanged() ), 
225              this, SLOT  ( SelectionIntoArgument() ) ) ;
226   
227   redisplayPreview();
228   activateSelection();
229 }
230
231 //=================================================================================
232 // function : SetEditCurrentArgument
233 // purpose  :
234 //=================================================================================
235 void MeasureGUI_Skeleton::SetEditCurrentArgument()
236 {
237   mySelEdit->setFocus();
238   SelectionIntoArgument();
239 }
240
241 //=================================================================================
242 // function : SelectionIntoArgument
243 // purpose  :
244 //=================================================================================
245 void MeasureGUI_Skeleton::SelectionIntoArgument()
246 {
247   myObj = GEOM::GEOM_Object::_nil();
248
249   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
250   SALOME_ListIO aSelList;
251   aSelMgr->selectedObjects(aSelList);
252
253   GEOM::GEOM_Object_var aSelectedObject = GEOM::GEOM_Object::_nil();
254
255   if (aSelList.Extent() > 0) {
256     Standard_Boolean testResult = Standard_False;
257     aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
258     if (!testResult)
259       aSelectedObject = GEOM::GEOM_Object::_nil();
260   }
261
262   if (aSelectedObject->_is_nil()) {
263     mySelEdit->setText("");
264     processObject();
265     erasePreview();
266     return;
267   }
268
269   myObj = aSelectedObject;
270   mySelEdit->setText(GEOMBase::GetName(myObj));
271   processObject();
272   redisplayPreview();
273 }
274
275 //=================================================================================
276 // function : processObject
277 // purpose  :
278 //=================================================================================
279 void MeasureGUI_Skeleton::processObject()
280 {  
281 }
282
283
284 //=================================================================================
285 // function : closeEvent
286 // purpose  :
287 //=================================================================================
288 void MeasureGUI_Skeleton::closeEvent( QCloseEvent* e )
289 {
290   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
291   if ( aSel )
292     disconnect( aSel, 0, this, 0 );
293   QDialog::closeEvent( e );
294 }
295
296 //=================================================================================
297 // function : enterEvent()
298 // purpose  :
299 //=================================================================================
300 void MeasureGUI_Skeleton::enterEvent( QEvent* )
301 {
302   if ( mainFrame()->GroupConstructors->isEnabled() )
303     return;
304     
305   ActivateThisDialog();
306 }
307
308 //=================================================================================
309 // function : buildPrs
310 // purpose  : Build presentation for "preview"
311 //=================================================================================
312 SALOME_Prs* MeasureGUI_Skeleton::buildPrs()
313 {
314   return 0;
315 }
316
317 //=================================================================================
318 // function : closeEvent()
319 // purpose  : Build presentation for "preview"
320 //=================================================================================
321 void MeasureGUI_Skeleton::redisplayPreview()
322 {
323   QString aMess;
324   if ( !isValid( aMess ) ) {
325     erasePreview( true );
326     return;
327   }
328
329   erasePreview( false );
330
331   try {
332     SUIT_OverrideCursor();
333
334     getDisplayer()->SetColor( Quantity_NOC_VIOLET );
335     getDisplayer()->SetToActivate( false );
336     
337     if ( SALOME_Prs* aPrs = buildPrs() )
338       displayPreview( aPrs );
339   }
340   catch ( const SALOME::SALOME_Exception& e ) {
341     SalomeApp_Tools::QtCatchCorbaException( e );
342   }
343   
344 }
345
346 //=================================================================================
347 // function : activateSelection
348 // purpose  : 
349 //=================================================================================
350 void MeasureGUI_Skeleton::activateSelection()
351 {
352   globalSelection( GEOM_ALLSHAPES );
353 }
354
355 //=================================================================================
356 // function : isValid
357 // purpose  :
358 //=================================================================================
359 bool MeasureGUI_Skeleton::isValid( QString& )
360 {
361   return !myObj->_is_nil();
362 }
363
364 //================================================================
365 // Function : getDisplayer
366 // Purpose  :
367 //================================================================
368 GEOM_Displayer* MeasureGUI_Skeleton::getDisplayer()
369 {
370   if ( !myDisplayer )
371     myDisplayer = new GEOM_Displayer( getStudy() );
372   return myDisplayer;
373 }
374
375 //=================================================================================
376 // function : createOperation
377 // purpose  :
378 //=================================================================================
379 GEOM::GEOM_IOperations_ptr MeasureGUI_Skeleton::createOperation()
380 {
381   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
382 }
383
384 //=================================================================================
385 // function : keyPressEvent()
386 // purpose  :
387 //=================================================================================
388 void MeasureGUI_Skeleton::keyPressEvent( QKeyEvent* e )
389 {
390   QDialog::keyPressEvent( e );
391   if ( e->isAccepted() )
392     return;
393
394   if ( e->key() == Qt::Key_F1 ) {
395     e->accept();
396     ClickOnHelp();
397   }
398 }
399
400 MeasureGUI_SkeletonBox* MeasureGUI_Skeleton::mainFrame() const
401 {
402   return myMainFrame;
403 }
404
405 QWidget* MeasureGUI_Skeleton::centralWidget() const
406 {
407   return mainFrame()->GroupMedium;
408 }
409
410 QPushButton* MeasureGUI_Skeleton::buttonClose() const
411 {
412   return mainFrame()->buttonClose;
413 }
414
415 QPushButton* MeasureGUI_Skeleton::buttonHelp() const
416 {
417   return mainFrame()->buttonHelp;
418 }