Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / MeasureGUI / MeasureGUI_Skeleton.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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     aSelectedObject = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
258   }
259
260   if (aSelectedObject->_is_nil()) {
261     mySelEdit->setText("");
262     processObject();
263     erasePreview();
264     return;
265   }
266
267   myObj = aSelectedObject;
268   mySelEdit->setText(GEOMBase::GetName(myObj));
269   processObject();
270   redisplayPreview();
271 }
272
273 //=================================================================================
274 // function : processObject
275 // purpose  :
276 //=================================================================================
277 void MeasureGUI_Skeleton::processObject()
278 {  
279 }
280
281
282 //=================================================================================
283 // function : closeEvent
284 // purpose  :
285 //=================================================================================
286 void MeasureGUI_Skeleton::closeEvent( QCloseEvent* e )
287 {
288   LightApp_SelectionMgr* aSel = ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr();
289   if ( aSel )
290     disconnect( aSel, 0, this, 0 );
291   QDialog::closeEvent( e );
292 }
293
294 //=================================================================================
295 // function : enterEvent()
296 // purpose  :
297 //=================================================================================
298 void MeasureGUI_Skeleton::enterEvent( QEvent* )
299 {
300   if ( mainFrame()->GroupConstructors->isEnabled() )
301     return;
302     
303   ActivateThisDialog();
304 }
305
306 //=================================================================================
307 // function : buildPrs
308 // purpose  : Build presentation for "preview"
309 //=================================================================================
310 SALOME_Prs* MeasureGUI_Skeleton::buildPrs()
311 {
312   return 0;
313 }
314
315 //=================================================================================
316 // function : closeEvent()
317 // purpose  : Build presentation for "preview"
318 //=================================================================================
319 void MeasureGUI_Skeleton::redisplayPreview()
320 {
321   QString aMess;
322   if ( !isValid( aMess ) ) {
323     erasePreview( true );
324     return;
325   }
326
327   erasePreview( false );
328
329   try {
330     SUIT_OverrideCursor();
331
332     getDisplayer()->SetColor( Quantity_NOC_VIOLET );
333     getDisplayer()->SetToActivate( false );
334     
335     if ( SALOME_Prs* aPrs = buildPrs() )
336       displayPreview( aPrs );
337   }
338   catch ( const SALOME::SALOME_Exception& e ) {
339     SalomeApp_Tools::QtCatchCorbaException( e );
340   }
341   
342 }
343
344 //=================================================================================
345 // function : activateSelection
346 // purpose  : 
347 //=================================================================================
348 void MeasureGUI_Skeleton::activateSelection()
349 {
350   globalSelection( GEOM_ALLSHAPES );
351 }
352
353 //=================================================================================
354 // function : isValid
355 // purpose  :
356 //=================================================================================
357 bool MeasureGUI_Skeleton::isValid( QString& )
358 {
359   return !myObj->_is_nil();
360 }
361
362 //================================================================
363 // Function : getDisplayer
364 // Purpose  :
365 //================================================================
366 GEOM_Displayer* MeasureGUI_Skeleton::getDisplayer()
367 {
368   if ( !myDisplayer )
369     myDisplayer = new GEOM_Displayer( getStudy() );
370   return myDisplayer;
371 }
372
373 //=================================================================================
374 // function : createOperation
375 // purpose  :
376 //=================================================================================
377 GEOM::GEOM_IOperations_ptr MeasureGUI_Skeleton::createOperation()
378 {
379   return getGeomEngine()->GetIMeasureOperations( getStudyId() );
380 }
381
382 //=================================================================================
383 // function : keyPressEvent()
384 // purpose  :
385 //=================================================================================
386 void MeasureGUI_Skeleton::keyPressEvent( QKeyEvent* e )
387 {
388   QDialog::keyPressEvent( e );
389   if ( e->isAccepted() )
390     return;
391
392   if ( e->key() == Qt::Key_F1 ) {
393     e->accept();
394     ClickOnHelp();
395   }
396 }
397
398 MeasureGUI_SkeletonBox* MeasureGUI_Skeleton::mainFrame() const
399 {
400   return myMainFrame;
401 }
402
403 QWidget* MeasureGUI_Skeleton::centralWidget() const
404 {
405   return mainFrame()->GroupMedium;
406 }
407
408 QPushButton* MeasureGUI_Skeleton::buttonClose() const
409 {
410   return mainFrame()->buttonClose;
411 }
412
413 QPushButton* MeasureGUI_Skeleton::buttonHelp() const
414 {
415   return mainFrame()->buttonHelp;
416 }