Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / VVTK / VVTK_PrimitiveBox.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  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.
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : VVTK_PrimitiveBox.cxx
25 //  Author : Oleg UVAROV
26 //  Module : VISU
27
28 #include "VVTK_PrimitiveBox.h"
29
30 #include "VISU_OpenGLPointSpriteMapper.hxx"
31
32 #include "SUIT_ResourceMgr.h"
33 #include "SUIT_Session.h"
34
35 #include "QtxDblSpinBox.h"
36 #include "QtxIntSpinBox.h"
37
38 #include <qlayout.h>
39 #include <qlabel.h>
40 #include <qlineedit.h>
41 #include <qbuttongroup.h>
42 #include <qradiobutton.h>
43 #include <qpushbutton.h>
44 #include <qfiledialog.h>
45
46 #include <iostream.h>
47
48 using namespace std;
49
50 VVTK_PrimitiveBox::VVTK_PrimitiveBox( QWidget* parent ) :
51   QGroupBox( parent )
52 {
53   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
54
55   setTitle( tr( "PRIMITIVE_TITLE" ) );
56   setColumnLayout(0, Qt::Vertical );
57   layout()->setSpacing( 0 );
58   layout()->setMargin( 0 );
59
60   QGridLayout* aLayout = new QGridLayout( layout() );
61   aLayout->setAlignment(Qt::AlignTop | Qt::AlignCenter);
62   aLayout->setSpacing(6);
63   aLayout->setMargin(11);
64
65   myPrimitiveType = VISU_OpenGLPointSpriteMapper::PointSprite;
66
67   QString aRootDir = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/";
68   myMainTexture = aRootDir + "sprite_texture.bmp";
69   myAlphaTexture = aRootDir + "sprite_alpha.bmp";
70
71   // Primitive Type
72   QButtonGroup* aPrimitiveTypeGroup = new QButtonGroup( 3, Qt::Horizontal, this, "PrimitiveTypeGroup" );
73   aPrimitiveTypeGroup->setMinimumWidth( 450 );
74   aPrimitiveTypeGroup->setRadioButtonExclusive( true );
75   aPrimitiveTypeGroup->setFrameStyle( QFrame::NoFrame );
76   aPrimitiveTypeGroup->layout()->setMargin( 0 );
77
78   myPointSpriteButton = new QRadioButton( tr( "POINT_SPRITE" ), aPrimitiveTypeGroup );
79   myOpenGLPointButton = new QRadioButton( tr( "OPENGL_POINT" ), aPrimitiveTypeGroup );
80   myGeomSphereButton = new QRadioButton( tr( "GEOMETRICAL_SPHERE" ), aPrimitiveTypeGroup );
81
82   aLayout->addMultiCellWidget( aPrimitiveTypeGroup, 0, 0, 0, 2 );
83
84   // Clamp ( Point Sprite & OpenGL Point )
85   myClampLabel = new QLabel( tr( "CLAMP" ), this );
86   myClampSpinBox = new QtxDblSpinBox( 1.0, 512.0, 1.0, this );
87   myClampSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
88
89   aLayout->addWidget( myClampLabel, 1, 0 );
90   aLayout->addMultiCellWidget( myClampSpinBox, 1, 1, 1, 2 );
91
92   // Main Texture ( Point Sprite )
93   myMainTextureLabel = new QLabel( tr( "MAIN_TEXTURE" ), this );
94   myMainTextureLineEdit = new QLineEdit( this );
95   myMainTextureButton = new QPushButton( this );
96   myMainTextureButton->setAutoDefault( false );
97   myMainTextureButton->setPixmap( aResourceMgr->loadPixmap( "VISU", tr( "ICON_LOAD_TEXTURE" ) ) );
98   connect( myMainTextureButton, SIGNAL( clicked() ), this, SLOT( onBrowseMainTexture() ) );
99
100   aLayout->addWidget( myMainTextureLabel, 2, 0 );
101   aLayout->addWidget( myMainTextureLineEdit, 2, 1 );
102   aLayout->addWidget( myMainTextureButton, 2, 2 );
103
104   // Alpha Texture ( Point Sprite )
105   myAlphaTextureLabel = new QLabel( tr( "ALPHA_TEXTURE" ), this );
106   myAlphaTextureLineEdit = new QLineEdit( this );
107   myAlphaTextureButton = new QPushButton( this );
108   myAlphaTextureButton->setAutoDefault( false );
109   myAlphaTextureButton->setPixmap( aResourceMgr->loadPixmap( "VISU", tr( "ICON_LOAD_TEXTURE" ) ) );
110   connect( myAlphaTextureButton, SIGNAL( clicked() ), this, SLOT( onBrowseAlphaTexture() ) );
111
112   aLayout->addWidget( myAlphaTextureLabel, 3, 0 );
113   aLayout->addWidget( myAlphaTextureLineEdit, 3, 1 );
114   aLayout->addWidget( myAlphaTextureButton, 3, 2 );
115
116   // Alpha Threshold ( Point Sprite )
117   myAlphaThresholdLabel = new QLabel( tr( "ALPHA_THRESHOLD" ), this );
118   myAlphaThresholdSpinBox = new QtxDblSpinBox( 0.0, 1.0, 0.1, this );
119   myAlphaThresholdSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
120
121   aLayout->addWidget( myAlphaThresholdLabel, 4, 0 );
122   aLayout->addMultiCellWidget( myAlphaThresholdSpinBox, 4, 4, 1, 2 );
123
124   // Resolution ( Geometrical Sphere )
125   myResolutionLabel = new QLabel( tr( "RESOLUTION" ), this );
126   myResolutionSpinBox = new QtxIntSpinBox( 3, 100, 1, this );
127   myResolutionSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
128   connect( myResolutionSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( onResolutionChanged( int ) ) );
129
130   aLayout->addWidget( myResolutionLabel, 1, 0 );
131   aLayout->addMultiCellWidget( myResolutionSpinBox, 1, 1, 1, 2 );
132
133   // Number of faces ( Geometrical Sphere )
134   myFaceNumberLabel = new QLabel( tr( "FACE_NUMBER" ), this );
135   myFaceNumberLineEdit = new QLineEdit( this );
136   myFaceNumberLineEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
137   myFaceNumberLineEdit->setEnabled( false );
138   //myFaceNumberLineEdit->setReadOnly( true );
139
140   aLayout->addWidget( myFaceNumberLabel, 2, 0 );
141   aLayout->addMultiCellWidget( myFaceNumberLineEdit, 2, 2, 1, 2 );
142
143   // Notification ( Geometrical Sphere )
144   myFaceLimitLabel = new QLabel( tr( "FACE_LIMIT" ), this );
145   myFaceLimitSpinBox = new QtxIntSpinBox( 10, 1000000, 10, this );
146   myFaceLimitSpinBox->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
147
148   aLayout->addWidget( myFaceLimitLabel, 3, 0 );
149   aLayout->addMultiCellWidget( myFaceLimitSpinBox, 3, 3, 1, 2 );
150
151   connect( myPointSpriteButton, SIGNAL( clicked() ), this, SLOT( onTogglePointSprite() ) );
152   connect( myOpenGLPointButton, SIGNAL( clicked() ), this, SLOT( onToggleOpenGLPoint() ) );
153   connect( myGeomSphereButton,  SIGNAL( clicked() ), this, SLOT( onToggleGeomSphere() ) );
154 }
155
156 void VVTK_PrimitiveBox::onTogglePointSprite()
157 {
158   myPrimitiveType = VISU_OpenGLPointSpriteMapper::PointSprite;
159
160   myClampLabel->show();
161   myClampSpinBox->show();
162
163   myMainTextureLabel->show();
164   myMainTextureLineEdit->show();
165   myMainTextureButton->show();
166
167   myAlphaTextureLabel->show();
168   myAlphaTextureLineEdit->show();
169   myAlphaTextureButton->show();
170
171   myAlphaThresholdLabel->show();
172   myAlphaThresholdSpinBox->show();
173
174   myResolutionLabel->hide();
175   myResolutionSpinBox->hide();
176
177   myFaceNumberLabel->hide();
178   myFaceNumberLineEdit->hide();
179
180   myFaceLimitLabel->hide();
181   myFaceLimitSpinBox->hide();
182 }
183
184 void VVTK_PrimitiveBox::onToggleOpenGLPoint()
185 {
186   myPrimitiveType = VISU_OpenGLPointSpriteMapper::OpenGLPoint;
187
188   myClampLabel->show();
189   myClampSpinBox->show();
190
191   myMainTextureLabel->hide();
192   myMainTextureLineEdit->hide();
193   myMainTextureButton->hide();
194
195   myAlphaTextureLabel->hide();
196   myAlphaTextureLineEdit->hide();
197   myAlphaTextureButton->hide();
198
199   myAlphaThresholdLabel->hide();
200   myAlphaThresholdSpinBox->hide();
201
202   myResolutionLabel->hide();
203   myResolutionSpinBox->hide();
204
205   myFaceNumberLabel->hide();
206   myFaceNumberLineEdit->hide();
207
208   myFaceLimitLabel->hide();
209   myFaceLimitSpinBox->hide();
210 }
211
212 void VVTK_PrimitiveBox::onToggleGeomSphere()
213 {
214   myPrimitiveType = VISU_OpenGLPointSpriteMapper::GeomSphere;
215
216   myClampLabel->hide();
217   myClampSpinBox->hide();
218
219   myMainTextureLabel->hide();
220   myMainTextureLineEdit->hide();
221   myMainTextureButton->hide();
222
223   myAlphaTextureLabel->hide();
224   myAlphaTextureLineEdit->hide();
225   myAlphaTextureButton->hide();
226
227   myAlphaThresholdLabel->hide();
228   myAlphaThresholdSpinBox->hide();
229
230   myResolutionLabel->show();
231   myResolutionSpinBox->show();
232
233   myFaceNumberLabel->show();
234   myFaceNumberLineEdit->show();
235
236   myFaceLimitLabel->show();
237   myFaceLimitSpinBox->show();
238 }
239
240 void VVTK_PrimitiveBox::onResolutionChanged( int theResolution )
241 {
242   setFaceNumber( 2 * theResolution * ( theResolution - 2 ) );
243 }
244
245 void VVTK_PrimitiveBox::setPrimitiveType( int theType )
246 {
247   myPrimitiveType = theType;
248
249   switch( myPrimitiveType )
250   {
251     case VISU_OpenGLPointSpriteMapper::PointSprite :
252       myPointSpriteButton->setChecked( true );
253       onTogglePointSprite();
254       break;
255     case VISU_OpenGLPointSpriteMapper::OpenGLPoint :
256       myOpenGLPointButton->setChecked( true );
257       onToggleOpenGLPoint();
258       break;
259     case VISU_OpenGLPointSpriteMapper::GeomSphere :
260       myGeomSphereButton->setChecked( true );
261       onToggleGeomSphere();
262       break;
263     default : break;
264   }
265 }
266
267 float VVTK_PrimitiveBox::getClamp() const
268 {
269   return myClampSpinBox->value();
270 }
271
272 void VVTK_PrimitiveBox::setClamp( float theClamp )
273 {
274   myClampSpinBox->setValue( theClamp );
275 }
276
277 void VVTK_PrimitiveBox::setClampMaximum( float theClampMaximum )
278 {
279   myClampSpinBox->setMaxValue( theClampMaximum );
280 }
281
282 void VVTK_PrimitiveBox::setMainTexture( const QString& theMainTexture )
283 {
284   myMainTexture = theMainTexture;
285   myMainTextureLineEdit->setText( theMainTexture.section( '/', -1 ) );
286 }
287
288 void VVTK_PrimitiveBox::setAlphaTexture( const QString& theAlphaTexture )
289 {
290   myAlphaTexture = theAlphaTexture;
291   myAlphaTextureLineEdit->setText( theAlphaTexture.section( '/', -1 ) );
292 }
293
294 float VVTK_PrimitiveBox::getAlphaThreshold() const
295 {
296   return myAlphaThresholdSpinBox->value();
297 }
298
299 void VVTK_PrimitiveBox::setAlphaThreshold( float theAlphaThreshold )
300 {
301   myAlphaThresholdSpinBox->setValue( theAlphaThreshold );
302 }
303
304 int VVTK_PrimitiveBox::getResolution() const
305 {
306   return myResolutionSpinBox->value();
307 }
308
309 void VVTK_PrimitiveBox::setResolution( int theResolution )
310 {
311   myResolutionSpinBox->setValue( theResolution );
312 }
313
314 int VVTK_PrimitiveBox::getFaceNumber() const
315 {
316   int aResolution = getResolution();
317   return 2 * aResolution * ( aResolution - 2 );
318   //return myFaceNumberLineEdit->text().toInt();
319 }
320
321 void VVTK_PrimitiveBox::setFaceNumber( int theFaceNumber )
322 {
323   myFaceNumberLineEdit->setText( QString::number( theFaceNumber ) );
324 }
325
326 int VVTK_PrimitiveBox::getFaceLimit() const
327 {
328   return myFaceLimitSpinBox->value();
329 }
330
331 void VVTK_PrimitiveBox::setFaceLimit( int theFaceLimit )
332 {
333   myFaceLimitSpinBox->setValue( theFaceLimit );
334 }
335
336 void VVTK_PrimitiveBox::onBrowseMainTexture()
337 {
338   QString aRootDir = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/";
339   QString aFileName = QFileDialog::getOpenFileName( aRootDir, "Bitmap (*.bmp *.jpg *.png)", this );
340
341   if( aFileName.isNull() )
342     return;
343
344   myMainTexture = aFileName;
345   myMainTextureLineEdit->setText( aFileName.section( '/', -1 ) );
346 }
347
348 void VVTK_PrimitiveBox::onBrowseAlphaTexture()
349 {
350   QString aRootDir = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/";
351   QString aFileName = QFileDialog::getOpenFileName( aRootDir, "Bitmap (*.bmp *.jpg *.png)", this );
352
353   if( aFileName.isNull() )
354     return;
355
356   myAlphaTexture = aFileName;
357   myAlphaTextureLineEdit->setText( aFileName.section( '/', -1 ) );
358 }