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