1 // Copyright (C) 2015-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "OCCViewer_LightSourceDlg.h"
22 #include "OCCViewer.h"
23 #include "OCCViewer_ViewWindow.h"
24 #include "OCCViewer_ViewModel.h"
27 #include <SUIT_Session.h>
28 #include <QtxDoubleSpinBox.h>
29 #include <QtxColorButton.h>
33 #include <QVBoxLayout>
34 #include <QHBoxLayout>
36 #include <QPushButton>
38 #include <QStackedLayout>
39 #include <QRadioButton>
42 \class OCCViewer_LightSourceDlg
43 \brief Dialog allowing to assign parameters of light source
48 \param view - parent widget
51 OCCViewer_LightSourceDlg::OCCViewer_LightSourceDlg( OCCViewer_ViewWindow* view, OCCViewer_Viewer* model )
55 setObjectName( "OCCViewer_LightSourceDlg" );
56 setWindowTitle( tr( "LIGHT_SOURCE" ) );
59 setAttribute( Qt::WA_DeleteOnClose, true );
61 // Create layout for this dialog
62 QVBoxLayout* dlglayout = new QVBoxLayout( this );
63 dlglayout->setSpacing( 6 ); dlglayout->setMargin( 11 );
65 QGroupBox* typeGroup = new QGroupBox( tr( "TYPE" ), this );
66 QHBoxLayout* typeLayout = new QHBoxLayout( typeGroup );
67 typeLayout->setSpacing( 6 ); typeLayout->setMargin( 11 );
69 myDirType = new QRadioButton( tr( "DIRECTIONAL" ), typeGroup );
70 myPosType = new QRadioButton( tr( "POSITIONAL" ), typeGroup );
72 typeLayout->addWidget( myDirType );
73 typeLayout->addWidget( myPosType );
75 myStackedLayout = new QStackedLayout();
77 const double min = -RealLast();
78 const double max = RealLast();
79 const int precision = 3;
81 /********************** Directional light **********************/
82 /* Controls for directional light:
83 Dx, Dy, Dz - direction
84 Headlight - headlight flag
85 Color - the color of a light source
88 QWidget* dirWidget = new QWidget( this );
89 QVBoxLayout* dirLayout = new QVBoxLayout( dirWidget );
90 dirLayout->setSpacing( 6 ); dirLayout->setMargin( 11 );
92 // Create "Direction" group
94 const double dir_step = 0.1;
96 QGroupBox* dirCoordGroup = new QGroupBox( tr( "DIRECTION" ), this );
97 QHBoxLayout* dirCoordLayout = new QHBoxLayout( dirCoordGroup );
98 dirCoordLayout->setSpacing( 6 ); dirCoordLayout->setMargin( 11 );
100 QLabel* dxLabel = new QLabel( tr("Dx:"), dirCoordGroup );
101 myDx = new QtxDoubleSpinBox( min, max, dir_step, dirCoordGroup );
102 myDx->setValue( 0.0 );
103 myDx->setMinimumWidth( 80 );
105 QLabel* dyLabel = new QLabel( tr("Dy:"), dirCoordGroup );
106 myDy = new QtxDoubleSpinBox( min, max, dir_step, dirCoordGroup );
107 myDy->setValue( 0.0 );
108 myDy->setMinimumWidth( 80 );
110 QLabel* dzLabel = new QLabel( tr("Dz:"), dirCoordGroup );
111 myDz = new QtxDoubleSpinBox( min, max, dir_step, dirCoordGroup );
112 myDz->setValue( -1.0 );
113 myDz->setMinimumWidth( 80 );
115 dirCoordLayout->addWidget( dxLabel );
116 dirCoordLayout->addWidget( myDx );
117 dirCoordLayout->addWidget( dyLabel );
118 dirCoordLayout->addWidget( myDy );
119 dirCoordLayout->addWidget( dzLabel );
120 dirCoordLayout->addWidget( myDz );
122 // Create "Parameters" group
124 QGroupBox* dirParamGroup = new QGroupBox( dirWidget );
125 QHBoxLayout* dirParamLayout = new QHBoxLayout( dirParamGroup );
126 dirParamLayout->setSpacing( 6 ); dirParamLayout->setMargin( 11 );
128 myDirHeadLight = new QCheckBox( tr("HEADLIGHT"), dirParamGroup );
129 myDirHeadLight->setChecked( false );
131 QLabel* aColorLabel = new QLabel( tr( "COLOR" ), dirParamGroup );
132 myDirColor = new QtxColorButton( dirParamGroup );
133 myDirColor->setColor( Qt::white );
135 dirParamLayout->addWidget( myDirHeadLight );
136 dirParamLayout->addWidget( aColorLabel );
137 dirParamLayout->addWidget( myDirColor );
138 dirParamLayout->addStretch();
140 dirLayout->addWidget( dirCoordGroup );
141 dirLayout->addWidget( dirParamGroup );
143 /********************** Positional light **********************/
144 /* Controls for positional light:
146 Headlight - headlight flag
147 Color - the color of a light source
150 QWidget* posWidget = new QWidget( this );
151 QVBoxLayout* posLayout = new QVBoxLayout( posWidget );
152 posLayout->setSpacing( 6 ); posLayout->setMargin( 11 );
154 // Create "Position" group
156 const double pos_step = 1.0;
158 QGroupBox* posCoordGroup = new QGroupBox( tr( "POSITION" ), posWidget );
159 QHBoxLayout* posCoordLayout = new QHBoxLayout( posCoordGroup );
160 posCoordLayout->setSpacing( 6 ); posCoordLayout->setMargin( 11 );
162 QLabel* xLabel = new QLabel( tr("X:"), posCoordGroup );
163 myX = new QtxDoubleSpinBox( min, max, pos_step, posCoordGroup );
164 myX->setValue( 0.0 );
165 myX->setMinimumWidth( 80 );
167 QLabel* yLabel = new QLabel( tr("Y:"), posCoordGroup );
168 myY = new QtxDoubleSpinBox( min, max, pos_step, posCoordGroup );
169 myY->setValue( 0.0 );
170 myY->setMinimumWidth( 80 );
172 QLabel* zLabel = new QLabel( tr("Z:"), posCoordGroup );
173 myZ = new QtxDoubleSpinBox( min, max, pos_step, posCoordGroup );
174 myZ->setValue( 0.0 );
175 myZ->setMinimumWidth( 80 );
177 posCoordLayout->addWidget( xLabel );
178 posCoordLayout->addWidget( myX );
179 posCoordLayout->addWidget( yLabel );
180 posCoordLayout->addWidget( myY );
181 posCoordLayout->addWidget( zLabel );
182 posCoordLayout->addWidget( myZ );
184 // Create "Parameters" group
186 QGroupBox* posParamGroup = new QGroupBox( posWidget );
187 QHBoxLayout* posParamLayout = new QHBoxLayout( posParamGroup );
188 posParamLayout->setSpacing( 6 ); posParamLayout->setMargin( 11 );
190 myPosHeadLight = new QCheckBox( tr("HEADLIGHT"), posParamGroup );
191 myPosHeadLight->setChecked( false );
193 aColorLabel = new QLabel( tr( "COLOR" ), posParamGroup );
194 myPosColor = new QtxColorButton( posParamGroup );
195 myPosColor->setColor( Qt::white );
197 posParamLayout->addWidget( myPosHeadLight );
198 posParamLayout->addWidget( aColorLabel );
199 posParamLayout->addWidget( myPosColor );
200 posParamLayout->addStretch();
202 posLayout->addWidget( posCoordGroup );
203 posLayout->addWidget( posParamGroup );
205 // add widgets in the stacked layout
206 myStackedLayout->addWidget( dirWidget );
207 myStackedLayout->addWidget( posWidget );
209 // Create "Buttons" group
211 QGroupBox* buttonGroup = new QGroupBox( this );
212 QHBoxLayout* buttonLayout = new QHBoxLayout( buttonGroup );
213 buttonLayout->setSpacing( 6 );
214 buttonLayout->setMargin( 11 );
216 QPushButton* okButton = new QPushButton( tr( "BUT_APPLY_AND_CLOSE" ), buttonGroup );
217 okButton->setDefault( true );
218 QPushButton* defaultButton = new QPushButton( tr( "BUT_DEFAULT" ), buttonGroup );
219 QPushButton* closeButton = new QPushButton( tr( "BUT_CLOSE" ), buttonGroup );
220 QPushButton* helpButton = new QPushButton( tr( "GEOM_BUT_HELP" ), buttonGroup );
222 buttonLayout->addWidget( okButton );
223 buttonLayout->addWidget( defaultButton );
224 buttonLayout->addStretch();
225 buttonLayout->addWidget( closeButton );
226 buttonLayout->addWidget( helpButton );
228 dlglayout->addWidget( typeGroup );
229 dlglayout->addLayout( myStackedLayout );
230 dlglayout->addWidget( buttonGroup );
232 this->setLayout( dlglayout );
238 // Signals and slots connections
239 connect( myDirType, SIGNAL( clicked( bool ) ), this, SLOT( onTypeChanged() ) );
240 connect( myPosType, SIGNAL( clicked( bool ) ), this, SLOT( onTypeChanged() ) );
242 connect( myDx, SIGNAL( valueChanged( double ) ), this, SLOT( onDirChanged() ) );
243 connect( myDy, SIGNAL( valueChanged( double ) ), this, SLOT( onDirChanged() ) );
244 connect( myDz, SIGNAL( valueChanged( double ) ), this, SLOT( onDirChanged() ) );
245 connect( myDirHeadLight, SIGNAL( clicked( bool ) ), this, SLOT( onDirChanged() ) );
246 connect( myDirColor, SIGNAL( changed( QColor ) ), this, SLOT( onDirChanged() ) );
248 connect( myX, SIGNAL( valueChanged( double ) ), this, SLOT( onPosChanged() ) );
249 connect( myY, SIGNAL( valueChanged( double ) ), this, SLOT( onPosChanged() ) );
250 connect( myZ, SIGNAL( valueChanged( double ) ), this, SLOT( onPosChanged() ) );
251 connect( myPosHeadLight, SIGNAL( clicked( bool ) ), this, SLOT( onPosChanged() ) );
252 connect( myPosColor, SIGNAL( changed( QColor ) ), this, SLOT( onPosChanged() ) );
254 connect( okButton, SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
255 connect( defaultButton, SIGNAL( clicked() ), this, SLOT( ClickOnDefault() ) );
256 connect( closeButton, SIGNAL( clicked() ), this, SLOT( ClickOnClose() ) );
257 connect( helpButton, SIGNAL( clicked() ), this, SLOT( ClickOnHelp() ) );
259 resize( minimumSizeHint() );
265 OCCViewer_LightSourceDlg::~OCCViewer_LightSourceDlg()
267 ( myInType == V3d_DIRECTIONAL ) ? myModel->getViewer3d()->DelLight( myPosLight ) :
268 myModel->getViewer3d()->DelLight( myDirLight );
271 QString OCCViewer_LightSourceDlg::getName()
273 // return the name of object
274 return QString( "OCCViewer_LightSourceDlg" );
278 Initialization of initial values of widgets
280 void OCCViewer_LightSourceDlg::initParam( bool theIsDefault )
282 Handle(V3d_Light) aLight;
283 myModel->getViewer3d()->InitDefinedLights();
284 while ( myModel->getViewer3d()->MoreDefinedLights() )
286 aLight = myModel->getViewer3d()->DefinedLight();
287 if ( aLight->Type() == V3d_DIRECTIONAL ) {
288 myDirLight = Handle(V3d_DirectionalLight)::DownCast( aLight );
289 myPosLight = new V3d_PositionalLight( myModel->getViewer3d(), 0, 0, 0 );
290 myDirType->setChecked( true );
291 myStackedLayout->setCurrentIndex(0);
294 if ( aLight->Type() == V3d_POSITIONAL ) {
295 myPosLight = Handle(V3d_PositionalLight)::DownCast( aLight );
296 myDirLight = new V3d_DirectionalLight( myModel->getViewer3d() );
297 myPosType->setChecked( true );
298 myStackedLayout->setCurrentIndex(1);
301 myModel->getViewer3d()->NextDefinedLights();
305 Quantity_Color aColor = aLight->Color();
306 if( myDirType->isChecked() ) {
307 myDirColor->setColor( OCCViewer::color( aColor ) );
308 myDirLight->Direction( aX, aY, aZ );
309 myDx->setValue( aX );
310 myDy->setValue( aY );
311 myDz->setValue( aZ );
312 myDirHeadLight->setChecked( myDirLight->Headlight() );
314 else if( myPosType->isChecked() ) {
315 myPosColor->setColor( OCCViewer::color( aColor ) );
316 myPosLight->Position( aX, aY, aZ );
320 myPosHeadLight->setChecked( myPosLight->Headlight() );
323 if ( !theIsDefault ) {
324 myInX = aX; myInY = aY; myInZ = aZ;
326 myInHeadLight = aLight->Headlight();
327 myInType = aLight->Type();
332 SLOT: called on type of light source changed
334 void OCCViewer_LightSourceDlg::onTypeChanged()
338 myStackedLayout->setCurrentIndex( myPosType->isChecked() );
339 if( myPosType->isChecked() ) {
340 myModel->getViewer3d()->SetLightOff( myDirLight );
343 else if( myDirType->isChecked() ) {
344 myModel->getViewer3d()->SetLightOff( myPosLight );
350 SLOT: called on value of directional light source changed
352 void OCCViewer_LightSourceDlg::onDirChanged()
356 myModel->getViewer3d()->SetLightOff( myDirLight );
357 if ( !( myDx->value() == 0 && myDy->value() == 0 && myDz->value() == 0 ) ) {
358 myDirLight->SetDirection( myDx->value(), myDy->value(), myDz->value() );
359 myDirLight->SetColor( OCCViewer::color( myDirColor->color() ) );
360 myDirLight->SetHeadlight( myDirHeadLight->isChecked() );
361 myModel->getViewer3d()->SetLightOn( myDirLight );
363 myModel->getViewer3d()->UpdateLights();
367 SLOT: called on value of positional light source changed
369 void OCCViewer_LightSourceDlg::onPosChanged()
373 myModel->getViewer3d()->SetLightOff( myPosLight );
374 myPosLight->SetPosition( myX->value(), myY->value(), myZ->value() );
375 myPosLight->SetColor( OCCViewer::color( myPosColor->color() ) );
376 myPosLight->SetHeadlight( myPosHeadLight->isChecked() );
377 myModel->getViewer3d()->SetLightOn( myPosLight );
378 myModel->getViewer3d()->UpdateLights();
382 \brief SLOT on "Apply and Close" button click: sets current light source and closes dialog
384 void OCCViewer_LightSourceDlg::ClickOnOk()
386 // need save a current type for deleting other light when dialog will be closed
387 myInType = myDirType->isChecked() ? V3d_DIRECTIONAL : V3d_POSITIONAL;
392 \brief SLOT on "Default" button click: sets default light source
394 void OCCViewer_LightSourceDlg::ClickOnDefault()
397 myModel->setDefaultLights();
399 myModel->getViewer3d()->UpdateLights();
404 \brief SLOT on "Close" button click: sets initial light source and closes dialog
406 void OCCViewer_LightSourceDlg::ClickOnClose()
408 if( myInType == V3d_DIRECTIONAL ) {
409 myDirLight->SetDirection( myInX, myInY, myInZ );
410 myDirLight->SetColor( myInColor );
411 myDirLight->SetHeadlight( myInHeadLight );
412 myModel->getViewer3d()->SetLightOn( myDirLight );
415 myPosLight->SetPosition( myInX, myInY, myInZ );
416 myPosLight->SetColor( myInColor );
417 myPosLight->SetHeadlight( myInHeadLight );
418 myModel->getViewer3d()->SetLightOn( myPosLight );
424 \brief SLOT on help button click: opens a help page
426 void OCCViewer_LightSourceDlg::ClickOnHelp()
428 SUIT_Application* app = SUIT_Session::session()->activeApplication();
430 app->onHelpContextModule( "GUI", "occ_3d_viewer_page.html", "light_source" );