Salome HOME
Redesign SALOME documentation
[modules/gui.git] / src / OCCViewer / OCCViewer_EnvTextureDlg.cxx
1 // Copyright (C) 2015-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "OCCViewer_EnvTextureDlg.h"
22 #include "OCCViewer_ViewWindow.h"
23 #include "OCCViewer_ViewPort3d.h"
24 #include "OCCViewer_ViewFrame.h"
25
26 // GUI includes
27 #include <SUIT_Session.h>
28
29 // OCCT includes
30 #include <Graphic3d_TextureEnv.hxx>
31
32 // QT Includes
33 #include <QGroupBox>
34 #include <QVBoxLayout>
35 #include <QPushButton>
36 #include <QComboBox>
37 #include <QFileDialog>
38 #include <QLineEdit>
39
40 /*!
41   \class OCCViewer_EnvTextureDlg
42   \brief Dialog allowing to assign parameters of environment texture
43 */
44
45 /*!
46   \brief Constructor
47   \param view - parent widget
48 */
49 OCCViewer_EnvTextureDlg::OCCViewer_EnvTextureDlg( OCCViewer_ViewWindow* view )
50   :QDialog( view )
51 {
52
53   // get current view frame (OCCViewer_ViewWindow->QFrame->OCCViewer_ViewFrame)
54   myViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( view->parent()->parent() );
55   myView3d = view->getViewPort()->getView();
56
57   setObjectName( "OCCViewer_EnvTextureDlg" );
58   setWindowTitle( tr( "ENV_TEXTURE" ) );
59   setModal( false );
60
61   setAttribute( Qt::WA_DeleteOnClose, true );
62
63   // Create layout for this dialog
64   QVBoxLayout* dlglayout = new QVBoxLayout( this );
65   dlglayout->setSpacing( 6 );
66   dlglayout->setMargin( 11 );
67
68   // Create "Environment texture" group
69
70   myEnvTextureGroup = new QGroupBox( tr( "ENV_TEXTURE" ) );
71   myEnvTextureGroup->setCheckable( true );
72
73   QGridLayout* envTextureLayout = new QGridLayout( myEnvTextureGroup );
74   envTextureLayout->setSpacing( 6 );
75   envTextureLayout->setMargin( 11 );
76
77   myEnvTextureId = new QComboBox();
78   myEnvTextureId->addItem( tr( "ENV_CLOUDS" ) );
79   myEnvTextureId->addItem( tr( "ENV_CV" ) );
80   myEnvTextureId->addItem( tr( "ENV_MEDIT" ) );
81   myEnvTextureId->addItem( tr( "ENV_PEARL" ) );
82   myEnvTextureId->addItem( tr( "ENV_SKY1" ) );
83   myEnvTextureId->addItem( tr( "ENV_SKY2" ) );
84   myEnvTextureId->addItem( tr( "ENV_LINES" ) );
85   myEnvTextureId->addItem( tr( "ENV_ROAD" ) );
86   myEnvTextureId->addItem( tr( "ENV_CUSTOM" ) );
87   myEnvTextureId->setMinimumWidth( 300 );
88
89   myEnvTextureName = new QLineEdit();
90   myEnvTextureName->setVisible( false );
91   myFileButton = new QPushButton();
92   myFileButton->setText("...");
93   myFileButton->setVisible( false );
94
95   envTextureLayout->addWidget( myEnvTextureId,   0, 0, 1, 2 );
96   envTextureLayout->addWidget( myEnvTextureName, 1, 0 );
97   envTextureLayout->addWidget( myFileButton,     1, 1 );
98
99   // Create "Buttons" group
100
101   QGroupBox* groupButtons = new QGroupBox( this );
102   QHBoxLayout* groupButtonsLayout = new QHBoxLayout( groupButtons );
103   groupButtonsLayout->setSpacing( 6 );
104   groupButtonsLayout->setMargin( 11 );
105
106   QPushButton* buttonClose = new QPushButton( tr( "BUT_CLOSE" ) );
107   buttonClose->setDefault( true );
108
109   QPushButton* buttonHelp = new QPushButton( tr( "GEOM_BUT_HELP" ) );
110
111   groupButtonsLayout->addStretch();
112   groupButtonsLayout->addWidget( buttonClose );
113   groupButtonsLayout->addWidget( buttonHelp );
114
115   dlglayout->addWidget( myEnvTextureGroup );
116   dlglayout->addWidget( groupButtons );
117
118   // Initializations
119   initParam();
120
121   // Signals and slots connections
122   connect( myEnvTextureGroup,    SIGNAL( toggled(bool) ),            this, SLOT( onEnvTexture(bool) ) );
123   connect( myEnvTextureId,       SIGNAL( currentIndexChanged(int) ), this, SLOT( onTextureChanged() ) );
124   connect( myFileButton,         SIGNAL( clicked() ),                this, SLOT( onFileSelectionClicked() ) );
125   connect( buttonClose,          SIGNAL( clicked() ),                this, SLOT( close() ) );
126   connect( buttonHelp,           SIGNAL( clicked() ),                this, SLOT( ClickOnHelp() ) );
127 }
128
129 /*!
130   \brief Destructor
131 */
132 OCCViewer_EnvTextureDlg::~OCCViewer_EnvTextureDlg()
133 {
134 }
135
136 QString OCCViewer_EnvTextureDlg::getName()
137 {
138   // return the name of object
139   return QString( "OCCViewer_EnvTextureDlg" );
140 }
141
142 /*!
143   Initialization of initial values of widgets
144 */
145 void OCCViewer_EnvTextureDlg::initParam()
146 {
147   Handle(Graphic3d_TextureEnv) aTexture = myView3d->TextureEnv();
148   bool anIsTexture = !aTexture.IsNull();
149   myEnvTextureGroup->setChecked( anIsTexture );
150   if ( anIsTexture ) {
151     int aTextureId = myView3d->TextureEnv()->Name();
152     myEnvTextureId->setCurrentIndex( aTextureId );
153     if ( aTextureId == myEnvTextureId->count() - 1 ) {
154       TCollection_AsciiString aFileName;
155       aTexture->Path().SystemName( aFileName );
156       myEnvTextureName->setText( QString( aFileName.ToCString() ) );
157       myFileButton->setVisible( true );
158       myEnvTextureName->setVisible( true );
159     }
160   }
161 }
162
163 /*!
164   SLOT on "Environment texture" group click
165 */
166 void OCCViewer_EnvTextureDlg::onEnvTexture( bool theIsChecked )
167 {
168   if ( theIsChecked )
169     onTextureChanged();
170   else {
171     Handle(Graphic3d_TextureEnv) aTexture;
172     setEnvTexture( aTexture );
173   }
174 }
175
176 /*!
177   SLOT on when user changed the texture
178 */
179 void OCCViewer_EnvTextureDlg::onTextureChanged()
180 {
181   Handle(Graphic3d_TextureEnv) aTexture;
182   bool isCustom = myEnvTextureId->currentIndex() == myEnvTextureId->count()-1;
183   myEnvTextureName->setVisible( isCustom );
184   myFileButton->setVisible( isCustom );
185   if( isCustom ) {
186     if( myEnvTextureName->text().isEmpty() )
187       onFileSelectionClicked();
188     aTexture = new Graphic3d_TextureEnv( TCollection_AsciiString( myEnvTextureName->text().toStdString().c_str() ) );
189   }
190   else {
191     myEnvTextureName->setText("");
192     Graphic3d_NameOfTextureEnv aStandardTexture;
193     QList<Graphic3d_NameOfTextureEnv> aTextures;
194     aTextures << Graphic3d_NOT_ENV_CLOUDS << Graphic3d_NOT_ENV_CV   << Graphic3d_NOT_ENV_MEDIT
195                   << Graphic3d_NOT_ENV_PEARL  << Graphic3d_NOT_ENV_SKY1 << Graphic3d_NOT_ENV_SKY2
196                   << Graphic3d_NOT_ENV_LINES  << Graphic3d_NOT_ENV_ROAD;
197     aTexture = new Graphic3d_TextureEnv( aTextures.at( myEnvTextureId->currentIndex() ) );
198   }
199   setEnvTexture( aTexture );
200 }
201
202 /*!
203   SLOT on file selection button click
204 */
205 void OCCViewer_EnvTextureDlg::onFileSelectionClicked()
206 {
207   QString selFile = QFileDialog::getOpenFileName( this,tr( "GEOM_SELECT_IMAGE" ),QString(), tr( "OCC_TEXTURE_FILES" ) );
208   if ( !selFile.isEmpty() ) {
209     myEnvTextureName->setText( selFile );
210     onTextureChanged();
211   }
212 }
213
214 /*!
215   SLOT on help button click: opens a help page
216 */
217 void OCCViewer_EnvTextureDlg::ClickOnHelp()
218 {
219   SUIT_Application* app = SUIT_Session::session()->activeApplication();
220   if ( app )
221     app->onHelpContextModule( "GUI", "occ_3d_viewer.html", "env-texture" );
222 }
223
224 /*!
225   Sets current texture environment for all view in the viewer
226 */
227 void OCCViewer_EnvTextureDlg::setEnvTexture( Handle(Graphic3d_TextureEnv) theTexture)
228 {
229   for ( int i = OCCViewer_ViewFrame::BOTTOM_RIGHT; i <= OCCViewer_ViewFrame::TOP_RIGHT; i++ ) {
230     if ( OCCViewer_ViewWindow* aViewWindow = myViewFrame->getView(i) ) {
231       Handle(V3d_View) aView = aViewWindow->getViewPort()->getView();
232       aView->SetTextureEnv( theTexture );
233       aView->Redraw();
234     }
235   }
236 }