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