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