]> SALOME platform Git repositories - modules/visu.git/blob - src/VVTK/VVTK_PickingDlg.cxx
Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/visu.git] / src / VVTK / VVTK_PickingDlg.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : VVTK_PickingDlg.cxx
8 //  Author : Oleg Uvarov
9 //  Module : VISU
10
11 #include "VVTK_PickingDlg.h"
12
13 #include "VISU_GaussPtsAct.h"
14 #include "VISU_GaussPtsSettings.h"
15
16 #include "SUIT_MessageBox.h"
17 #include "SUIT_ResourceMgr.h"
18 #include "SUIT_Session.h"
19
20 #include "SVTK_MainWindow.h"
21 #include "SVTK_RenderWindowInteractor.h"
22
23 #include <vtkActorCollection.h>
24 #include <vtkCallbackCommand.h>
25 #include <vtkObjectFactory.h>
26 #include <vtkRenderer.h>
27 #include <vtkGenericRenderWindowInteractor.h>
28 #include <vtkSmartPointer.h>
29
30 #include "utilities.h"
31
32 #include <qcheckbox.h>
33 #include <qcolordialog.h>
34 #include <qcombobox.h>
35 #include <qgroupbox.h>
36 #include <qlabel.h>
37 #include <qlayout.h>
38 #include <qpushbutton.h>
39 #include <qvbox.h>
40
41 #include "QtxAction.h"
42 #include "QtxDblSpinBox.h"
43 #include "QtxIntSpinBox.h"
44
45 #include "LightApp_Application.h"
46
47 using namespace std;
48
49 VVTK_PickingDlg::VVTK_PickingDlg(QtxAction* theAction,
50                                  SVTK_MainWindow* theParent,
51                                  const char* theName):
52   SVTK_DialogBase(theAction,
53                   theParent, 
54                   theName),
55   myEventCallbackCommand( vtkCallbackCommand::New() ),
56   myPickingSettings( VISU_PickingSettings::New() )
57 {
58   myPriority = 0.0;
59   myEventCallbackCommand->Delete();
60   myEventCallbackCommand->SetClientData(this); 
61   myEventCallbackCommand->SetCallback(VVTK_PickingDlg::ProcessEvents);
62
63   setCaption( tr( "PICKING_DLG_TITLE" ) );
64   setSizeGripEnabled(TRUE);
65
66   QVBoxLayout* TopLayout = new QVBoxLayout( this );
67   TopLayout->setSpacing(6);
68   TopLayout->setMargin(11);
69
70   QVBox* aBox = new QVBox( this );
71   aBox->setMargin(0);
72   aBox->setSpacing(6);
73
74   // Cursor
75   QGroupBox* CursorGroup = new QGroupBox( tr( "CURSOR_TITLE" ), aBox, "CursorGroup" );
76   CursorGroup->setColumnLayout(0, Qt::Vertical );
77   CursorGroup->layout()->setSpacing( 0 );
78   CursorGroup->layout()->setMargin( 0 );
79
80   QGridLayout* CursorGroupLayout = new QGridLayout (CursorGroup->layout());
81   CursorGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
82   CursorGroupLayout->setSpacing(6);
83   CursorGroupLayout->setMargin(11);
84
85   QLabel* CursorSizeLabel = new QLabel( tr( "CURSOR_SIZE" ), CursorGroup );
86   myCursorSizeSpinBox = new QtxDblSpinBox( 0, 1, 0.1, CursorGroup );
87   myCursorSizeSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
88
89   QLabel* PyramidHeightLabel = new QLabel( tr( "PYRAMID_HEIGHT" ), CursorGroup );
90   double aHeightMin=1.e-7;
91   double aHeightMax=10.;
92   double aHeightStep=0.1;
93   myPyramidHeightSpinBox = new QtxDblSpinBox(aHeightMin, aHeightMax, aHeightStep, CursorGroup );
94   myPyramidHeightSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
95   
96   QLabel* SelectionColorLabel = new QLabel( tr( "SELECTION_COLOR" ), CursorGroup );
97   mySelectionColorButton = new QPushButton( CursorGroup );
98   mySelectionColorButton->setPaletteBackgroundColor( Qt::blue );
99   mySelectionColorButton->setPaletteForegroundColor( Qt::blue );
100   connect( mySelectionColorButton, SIGNAL( clicked() ), this, SLOT( onColorButtonPressed() ) );
101
102   CursorGroupLayout->addWidget( CursorSizeLabel, 0, 0 );
103   CursorGroupLayout->addWidget( myCursorSizeSpinBox, 0, 1 );
104   CursorGroupLayout->addWidget( PyramidHeightLabel, 1, 0 );
105   CursorGroupLayout->addWidget( myPyramidHeightSpinBox, 1, 1 );
106   CursorGroupLayout->addWidget( SelectionColorLabel, 2, 0 );
107   CursorGroupLayout->addWidget( mySelectionColorButton, 2, 1 );
108
109   // Tolerance
110   QGroupBox* ToleranceGroup = new QGroupBox( tr( "TOLERANCE_TITLE" ), aBox, "ToleranceGroup" );
111   ToleranceGroup->setColumnLayout(0, Qt::Vertical );
112   ToleranceGroup->layout()->setSpacing( 0 );
113   ToleranceGroup->layout()->setMargin( 0 );
114
115   QGridLayout* ToleranceGroupLayout = new QGridLayout (ToleranceGroup->layout());
116   ToleranceGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
117   ToleranceGroupLayout->setSpacing(6);
118   ToleranceGroupLayout->setMargin(11);
119
120   QLabel* PointToleranceLabel = new QLabel( tr( "POINT_TOLERANCE" ), ToleranceGroup );
121   myPointToleranceSpinBox = new QtxDblSpinBox( 0.001, 10.0, 0.01, ToleranceGroup );
122   myPointToleranceSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
123
124   ToleranceGroupLayout->addWidget( PointToleranceLabel, 0, 0 );
125   ToleranceGroupLayout->addWidget( myPointToleranceSpinBox, 0, 1 );
126
127   // Information window
128   QGroupBox* InfoWindowGroup = new QGroupBox( tr( "INFO_WINDOW_TITLE" ), aBox, "InfoWindowGroup" );
129   InfoWindowGroup->setColumnLayout(0, Qt::Vertical );
130   InfoWindowGroup->layout()->setSpacing( 0 );
131   InfoWindowGroup->layout()->setMargin( 0 );
132
133   QGridLayout* InfoWindowGroupLayout = new QGridLayout (InfoWindowGroup->layout());
134   InfoWindowGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
135   InfoWindowGroupLayout->setSpacing(6);
136   InfoWindowGroupLayout->setMargin(11);
137
138   QLabel* TransparencyLabel = new QLabel( tr( "TRANSPARENCY" ), InfoWindowGroup );
139   myTransparencySpinBox = new QtxIntSpinBox( 0, 100, 10, InfoWindowGroup );
140   myTransparencySpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
141
142   InfoWindowGroupLayout->addWidget( TransparencyLabel, 0, 0 );
143   InfoWindowGroupLayout->addWidget( myTransparencySpinBox, 0, 1 );
144
145   QLabel* PositionLabel = new QLabel( tr( "POSITION" ), InfoWindowGroup );
146   myPositionComboBox = new QComboBox( InfoWindowGroup );
147   myPositionComboBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
148
149   myPositionComboBox->insertItem( tr( "BELOW_POINT" ) );
150   myPositionComboBox->insertItem( tr( "TOP_LEFT_CORNER" ) );
151
152   InfoWindowGroupLayout->addWidget( TransparencyLabel, 0, 0 );
153   InfoWindowGroupLayout->addWidget( myTransparencySpinBox, 0, 1 );
154   InfoWindowGroupLayout->addWidget( PositionLabel, 1, 0 );
155   InfoWindowGroupLayout->addWidget( myPositionComboBox, 1, 1 );
156
157   // Movement of the camera
158   QGroupBox* CameraGroup = new QGroupBox( tr( "CAMERA_TITLE" ), aBox, "CameraGroup" );
159   CameraGroup->setColumnLayout(0, Qt::Vertical );
160   CameraGroup->layout()->setSpacing( 0 );
161   CameraGroup->layout()->setMargin( 0 );
162
163   QGridLayout* CameraGroupLayout = new QGridLayout (CameraGroup->layout());
164   CameraGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
165   CameraGroupLayout->setSpacing(6);
166   CameraGroupLayout->setMargin(11);
167
168   QLabel* ZoomFactorLabel = new QLabel( tr( "ZOOM_FACTOR" ), CameraGroup );
169   myZoomFactorSpinBox = new QtxDblSpinBox( 0.1, 10.0, 0.1, CameraGroup );
170   myZoomFactorSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
171
172   QLabel* StepNumberLabel = new QLabel( tr( "STEP_NUMBER" ), CameraGroup );
173   myStepNumberSpinBox = new QtxIntSpinBox( 1, 100, 1, CameraGroup );
174   myStepNumberSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
175
176   CameraGroupLayout->addWidget( ZoomFactorLabel, 0, 0 );
177   CameraGroupLayout->addWidget( myZoomFactorSpinBox, 0, 1 );
178   CameraGroupLayout->addWidget( StepNumberLabel, 1, 0 );
179   CameraGroupLayout->addWidget( myStepNumberSpinBox, 1, 1 );
180
181   // Display parent mesh element
182   QGroupBox* ParentMeshGroup = new QGroupBox( tr( "PARENT_MESH_TITLE" ), aBox, "ParentMeshGroup" );
183   ParentMeshGroup->setColumnLayout(0, Qt::Vertical );
184   ParentMeshGroup->layout()->setSpacing( 0 );
185   ParentMeshGroup->layout()->setMargin( 0 );
186
187   QGridLayout* ParentMeshGroupLayout = new QGridLayout (ParentMeshGroup->layout());
188   ParentMeshGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
189   ParentMeshGroupLayout->setSpacing(6);
190   ParentMeshGroupLayout->setMargin(11);
191
192   myDisplayParentMeshCheckBox = new QCheckBox( tr( "DISPLAY_PARENT_MESH" ), ParentMeshGroup );
193   ParentMeshGroupLayout->addWidget( myDisplayParentMeshCheckBox, 0, 0 );
194
195   // Common buttons ===========================================================
196   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
197   GroupButtons->setColumnLayout(0, Qt::Vertical );
198   GroupButtons->layout()->setSpacing( 0 );
199   GroupButtons->layout()->setMargin( 0 );
200   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
201   GroupButtonsLayout->setAlignment( Qt::AlignTop );
202   GroupButtonsLayout->setSpacing( 6 );
203   GroupButtonsLayout->setMargin( 11 );
204
205   QPushButton* buttonApply = new QPushButton( tr( "&Apply" ), GroupButtons, "buttonApply" );
206   buttonApply->setAutoDefault( TRUE );
207   buttonApply->setDefault( TRUE );
208   GroupButtonsLayout->addWidget( buttonApply, 0, 0 );
209   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
210
211   QPushButton* buttonClose = new QPushButton( tr( "&Close" ) , GroupButtons, "buttonClose" );
212   buttonClose->setAutoDefault( TRUE );
213   GroupButtonsLayout->addWidget( buttonClose, 0, 2 );
214
215   QPushButton* buttonHelp = new QPushButton( tr( "&Help" ) , GroupButtons, "buttonHelp" );
216   buttonHelp->setAutoDefault( TRUE );
217   GroupButtonsLayout->addWidget( buttonHelp, 0, 3 );
218
219   TopLayout->addWidget( aBox );
220   TopLayout->addWidget( GroupButtons );
221
222   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( onClickApply() ) );
223   connect( buttonClose, SIGNAL( clicked() ), this, SLOT( onClickClose() ) );
224   connect( buttonHelp,  SIGNAL( clicked() ), this, SLOT( onClickHelp() ) );
225 }
226
227 VVTK_PickingDlg::~VVTK_PickingDlg()
228 {
229 }
230
231 void VVTK_PickingDlg::AddActor( VISU_GaussPtsAct* theActor )
232 {
233   theActor->SetPickingSettings( myPickingSettings.GetPointer() );
234 }
235
236 void VVTK_PickingDlg::RemoveActor( VISU_GaussPtsAct* theActor )
237 {
238   theActor->SetPickingSettings( NULL );
239 }
240
241 void VVTK_PickingDlg::Update()
242 {
243   float aCursorSize = 0.5;
244   float aPyramidHeight = 10.0;
245   float aPointTolerance = 0.1;
246   QColor aColor = Qt::yellow;
247   int anInfoWindowTransparency = 50;
248   int anInfoWindowPosition = VISU_PickingSettings::BelowPoint;
249   float aZoomFactor = 1.5;
250   int aStepNumber = 10;
251   bool aDisplayParentMesh = false;
252
253   if( !myPickingSettings->GetInitial() )
254   {
255     myCursorSizeSpinBox->setValue( myPickingSettings->GetCursorSize() );
256     myPyramidHeightSpinBox->setValue( myPickingSettings->GetPyramidHeight() );
257     myPointToleranceSpinBox->setValue( myPickingSettings->GetPointTolerance() );
258     myTransparencySpinBox->setValue( int(myPickingSettings->GetInfoWindowTransparency() * 100.0) );
259     myPositionComboBox->setCurrentItem( myPickingSettings->GetInfoWindowPosition() );
260     myZoomFactorSpinBox->setValue( myPickingSettings->GetZoomFactor() );
261     myStepNumberSpinBox->setValue( myPickingSettings->GetStepNumber() );
262     myDisplayParentMeshCheckBox->setChecked( myPickingSettings->GetDisplayParentMesh() );
263
264     vtkFloatingPointType* aColor = myPickingSettings->GetColor();
265     mySelectionColorButton->setPaletteBackgroundColor( QColor( ( int )( aColor[0] * 255.0 ),
266                                                                ( int )( aColor[1] * 255.0 ),
267                                                                ( int )( aColor[2] * 255.0 ) ) );
268
269     return;
270   }
271
272   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
273
274   aCursorSize = aResourceMgr->doubleValue( "VISU", "picking_cursor_size", aCursorSize );
275   myCursorSizeSpinBox->setValue( aCursorSize );
276
277   aPyramidHeight = aResourceMgr->doubleValue( "VISU", "picking_pyramid_height", aPyramidHeight );
278   myPyramidHeightSpinBox->setValue( aPyramidHeight );
279
280   aPointTolerance = aResourceMgr->doubleValue( "VISU", "picking_point_tolerance", aPointTolerance );
281   myPointToleranceSpinBox->setValue( aPointTolerance );
282
283   aColor = aResourceMgr->colorValue( "VISU", "picking_selection_color", aColor );
284   mySelectionColorButton->setPaletteBackgroundColor( aColor );
285
286   anInfoWindowTransparency = aResourceMgr->integerValue( "VISU", "picking_transparency", anInfoWindowTransparency );
287   myTransparencySpinBox->setValue( anInfoWindowTransparency );
288
289   anInfoWindowPosition = aResourceMgr->integerValue( "VISU", "picking_position", anInfoWindowPosition );
290   myPositionComboBox->setCurrentItem( anInfoWindowPosition );
291
292   aZoomFactor = aResourceMgr->doubleValue( "VISU", "picking_zoom_factor", aZoomFactor );
293   myZoomFactorSpinBox->setValue( aZoomFactor );
294
295   aStepNumber = aResourceMgr->integerValue( "VISU", "picking_step_number", aStepNumber );
296   myStepNumberSpinBox->setValue( aStepNumber );
297
298   aDisplayParentMesh = aResourceMgr->booleanValue( "VISU", "picking_display_parent_mesh", aDisplayParentMesh );
299   myDisplayParentMeshCheckBox->setChecked( aDisplayParentMesh );
300
301   onClickApply();
302 }
303
304
305 VISU_PickingSettings* 
306 VVTK_PickingDlg
307 ::GetPickingSettings()
308 {
309   return myPickingSettings.GetPointer();
310 }
311
312 void VVTK_PickingDlg::SetInteractor( SVTK_RenderWindowInteractor* theInteractor )
313 {
314   myInteractor = theInteractor;
315
316   theInteractor->GetDevice()->AddObserver(vtkCommand::KeyPressEvent, 
317                                           myEventCallbackCommand.GetPointer(), 
318                                           myPriority);
319 }
320
321
322 void VVTK_PickingDlg::ProcessEvents(vtkObject* vtkNotUsed(theObject), 
323                                     unsigned long theEvent,
324                                     void* theClientData, 
325                                     void* vtkNotUsed(theCallData))
326 {
327   VVTK_PickingDlg* self = reinterpret_cast<VVTK_PickingDlg*>(theClientData);
328
329   switch(theEvent){
330   case vtkCommand::KeyPressEvent:
331     self->KeyPressed();
332     break;
333   }
334 }
335
336 void VVTK_PickingDlg::KeyPressed()
337 {
338   if( myInteractor->GetDevice()->GetKeyCode() == 'P' )
339   {
340     bool aDisplayParentMesh = !myPickingSettings->GetDisplayParentMesh();
341     myPickingSettings->SetDisplayParentMesh( aDisplayParentMesh );
342
343     myDisplayParentMeshCheckBox->setChecked( aDisplayParentMesh );
344
345     myPickingSettings->InvokeEvent(VISU::UpdatePickingSettingsEvent,NULL);
346   }
347 }
348
349 void VVTK_PickingDlg::onClickApply()
350 {
351   myPickingSettings->SetInitial( false );
352
353   myPickingSettings->SetCursorSize( myCursorSizeSpinBox->value() );
354   myPickingSettings->SetPyramidHeight( myPyramidHeightSpinBox->value() );
355   myPickingSettings->SetPointTolerance( myPointToleranceSpinBox->value() );
356   myPickingSettings->SetInfoWindowTransparency( myTransparencySpinBox->value() / 100.0 );
357   myPickingSettings->SetInfoWindowPosition( myPositionComboBox->currentItem() );
358   myPickingSettings->SetZoomFactor( myZoomFactorSpinBox->value() );
359   myPickingSettings->SetStepNumber( myStepNumberSpinBox->value() );
360   myPickingSettings->SetDisplayParentMesh( myDisplayParentMeshCheckBox->isChecked() );
361
362   QColor aButtonColor = mySelectionColorButton->paletteBackgroundColor();
363   vtkFloatingPointType aColor[3];
364   aColor[0] = aButtonColor.red() / 255.0;
365   aColor[1] = aButtonColor.green() / 255.0;
366   aColor[2] = aButtonColor.blue() / 255.0;
367   myPickingSettings->SetColor( aColor );
368
369   myPickingSettings->InvokeEvent(VISU::UpdatePickingSettingsEvent,NULL);
370 }
371
372 void VVTK_PickingDlg::onClickClose()
373 {
374   reject();
375 }
376
377 void VVTK_PickingDlg::onClickHelp()
378 {
379   QString aHelpFileName = "picking.htm";
380   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
381   if (app)
382     app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
383   else {
384     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
385                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
386                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(aHelpFileName),
387                            QObject::tr("BUT_OK"));
388   }
389 }
390
391 void VVTK_PickingDlg::onColorButtonPressed()
392 {
393   QColor aColor = QColorDialog::getColor( mySelectionColorButton->paletteBackgroundColor(), this );
394   if( aColor.isValid() )
395     mySelectionColorButton->setPaletteBackgroundColor( aColor );
396 }