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