Salome HOME
Remove debug information.
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_MaterialPropertiesDlg.cxx
1 // Copyright (C) 2007-2011  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 // File   : GEOMToolsGUI_MaterialPropertiesDlg.cxx
20 // Author : Margarita KARPUNINA, Open CASCADE S.A.S. (margarita.karpunina@opencascade.com)
21
22 #include "GEOMToolsGUI_MaterialPropertiesDlg.h"
23
24 #include "Material_Model.h"
25 #include "Material_ResourceMgr.h"
26
27 #include <GeometryGUI.h>
28 #include <GEOM_Constants.h>
29 #include <GEOM_Displayer.h>
30 #include <GEOM_Actor.h>
31
32 #include <GEOMBase.h>
33
34 #include <Basics_OCCTVersion.hxx>
35
36 #include <SALOME_ListIO.hxx>
37 #include <SALOME_ListIteratorOfListIO.hxx>
38
39 #include <SVTK_Functor.h>
40 #include <SVTK_Prs.h>
41 #include <SVTK_ViewModel.h>
42 #include <SVTK_ViewWindow.h>
43 #include <SVTK_View.h>
44
45 #include <VTKViewer_Algorithm.h>
46
47 #include <OCCViewer_ViewModel.h>
48
49 #include <SUIT_Desktop.h>
50 #include <SUIT_MessageBox.h>
51 #include <SUIT_OverrideCursor.h>
52 #include <SUIT_ResourceMgr.h>
53 #include <SUIT_Session.h>
54 #include <SUIT_ViewManager.h>
55
56 #include <SalomeApp_Application.h>
57 #include <SalomeApp_Study.h>
58
59 #include <LightApp_SelectionMgr.h>
60
61 #include <QtxColorButton.h>
62 #include <QtxDoubleSpinBox.h>
63
64 // OCCT Includes
65 #include <Graphic3d_AspectFillArea3d.hxx>
66
67 // VTK includes
68 #include <vtkRenderer.h>
69 #include <vtkProperty.h>
70
71 // QT Includes
72 #include <QApplication>
73 #include <QButtonGroup>
74 #include <QCheckBox>
75 #include <QGridLayout>
76 #include <QGroupBox>
77 #include <QHBoxLayout>
78 #include <QKeyEvent>
79 #include <QLabel>
80 #include <QListWidget>
81 #include <QMap>
82 #include <QMessageBox>
83 #include <QPushButton>
84 #include <QSpinBox>
85 #include <QTabWidget>
86 #include <QVBoxLayout>
87
88 #define MARGIN  9
89 #define SPACING 6
90
91 /*!
92   \class GEOMToolsGUI_MaterialPropertiesDlg
93   \brief GEOM material properties dialog box class.
94
95   The dialog box lists all GEOM materials available via the application and allows
96   user to create own materials.
97 */
98
99 /*!
100   \brief Constructor
101   \param parent parent widget
102 */
103 GEOMToolsGUI_MaterialPropertiesDlg::GEOMToolsGUI_MaterialPropertiesDlg( QWidget* parent )
104   : QtxDialog( parent, true, true, OK | Close | Apply | Help),
105     myResMgr( 0 )
106 {
107   // Set title
108   setWindowTitle( tr( "MATERIAL_PROPERTIES_TLT" ) );
109
110   // Set viewer type
111   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
112   if ( app ) {
113     SUIT_ViewWindow* window = app->desktop()->activeWindow();
114     if ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() )
115       myViewerType = OCC;
116     else if ( window && window->getViewManager()->getType() == SVTK_Viewer::Type() )
117       myViewerType = VTK;
118   }
119
120   // Create main layout
121   QVBoxLayout* main = new QVBoxLayout( mainFrame() );
122   main->setMargin( 0 ); main->setSpacing( SPACING );
123
124   // Create main widgets
125   myBackMaterialCheck = new QCheckBox( tr( "MATERIAL_BACK_CHK" ), this );
126   QFrame* fr = new QFrame( this );
127   fr->setFrameStyle( QFrame::Box | QFrame::Sunken );
128
129   main->addWidget( myBackMaterialCheck );
130   main->addWidget( fr );
131
132   // Create editor widgets
133   myMaterialList = new QListWidget( fr );
134   myMaterialTab  = new QTabWidget( fr );
135
136   QHBoxLayout* frLayout = new QHBoxLayout( fr );
137   frLayout->setMargin( MARGIN ); frLayout->setSpacing( SPACING );
138   frLayout->addWidget( myMaterialList );
139   frLayout->addWidget( myMaterialTab );
140   frLayout->setStretchFactor( myMaterialList, 1 );
141   frLayout->setStretchFactor( myMaterialTab, 2 );
142
143   // ======================= Create a tab for front material ======================= 
144   QWidget* w1 = new QWidget( myMaterialTab );
145   QVBoxLayout* vLayout1 = new QVBoxLayout( w1 );
146
147   QGridLayout* gLayout1 = new QGridLayout( w1 );
148   gLayout1->setMargin( MARGIN ); gLayout1->setSpacing( SPACING );
149
150   // ----------------- "Ambient" reflection type group box -----------------
151   myAmbientGroupF = new QGroupBox( tr( "AMBIENT_GRP" ), w1 );
152   myAmbientGroupF->setCheckable(true);
153   connect( myAmbientGroupF, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) ); 
154
155   // Ambient color
156   QLabel* ambColorLab1 = new QLabel( tr( "COLOR" ), myAmbientGroupF );
157   myAmbientColorF = new QtxColorButton( myAmbientGroupF );
158   myAmbientColorF->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
159   connect( myAmbientColorF, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
160
161   // Ambient coefficient
162   QLabel* ambCoefficientLab1 = new QLabel( tr( "COEFFICIENT" ), myAmbientGroupF );
163   myAmbientCoefntF = new QtxDoubleSpinBox( myAmbientGroupF );
164   myAmbientCoefntF->setMaximum(1);
165   myAmbientCoefntF->setSingleStep(0.05);
166   connect( myAmbientCoefntF, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
167
168   // Ambient group box layout
169   QGridLayout* ambientLayout1 = new QGridLayout( myAmbientGroupF );
170   ambientLayout1->setMargin( MARGIN ); ambientLayout1->setSpacing( SPACING );
171   ambientLayout1->addWidget( ambColorLab1,       0, 0 );
172   ambientLayout1->addWidget( myAmbientColorF,    0, 1 );
173   ambientLayout1->addWidget( ambCoefficientLab1, 1, 0 );
174   ambientLayout1->addWidget( myAmbientCoefntF,   1, 1 );
175
176   // ----------------- "Diffuse" reflection type group box -----------------
177   myDiffuseGroupF = new QGroupBox( tr( "DIFFUSE_GRP" ), w1 );
178   myDiffuseGroupF->setCheckable(true);
179   connect( myDiffuseGroupF, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) );
180
181   // Diffuse color
182   QLabel* difColorLab1 = new QLabel( tr( "COLOR" ), myDiffuseGroupF );
183   myDiffuseColorF = new QtxColorButton( myDiffuseGroupF );
184   myDiffuseColorF->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
185   connect( myDiffuseColorF, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
186
187   // Diffuse coefficient
188   QLabel* difCoefficientLab1 = new QLabel( tr( "COEFFICIENT" ), myDiffuseGroupF );
189   myDiffuseCoefntF = new QtxDoubleSpinBox( myDiffuseGroupF );
190   myDiffuseCoefntF->setMaximum(1);
191   myDiffuseCoefntF->setSingleStep(0.05);
192   connect( myDiffuseCoefntF, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
193
194   // Diffuse group box layout
195   QGridLayout* diffuseLayout1 = new QGridLayout( myDiffuseGroupF );
196   diffuseLayout1->setMargin( MARGIN ); diffuseLayout1->setSpacing( SPACING );
197   diffuseLayout1->addWidget( difColorLab1,       0, 0 );
198   diffuseLayout1->addWidget( myDiffuseColorF,    0, 1 );
199   diffuseLayout1->addWidget( difCoefficientLab1, 1, 0 );
200   diffuseLayout1->addWidget( myDiffuseCoefntF,   1, 1 );
201
202   // ----------------- "Specular" reflection type group box -----------------
203   mySpecularGroupF = new QGroupBox( tr( "SPECULAR_GRP" ), w1 );
204   mySpecularGroupF->setCheckable(true);
205   connect( mySpecularGroupF, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) );
206
207   // Specular color
208   QLabel* specColorLab1 = new QLabel( tr( "COLOR" ), mySpecularGroupF );
209   mySpecularColorF = new QtxColorButton( mySpecularGroupF );
210   mySpecularColorF->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
211   connect( mySpecularColorF, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
212
213   // Specular coefficient
214   QLabel* specCoefficientLab1 = new QLabel( tr( "COEFFICIENT" ), mySpecularGroupF );
215   mySpecularCoefntF = new QtxDoubleSpinBox( mySpecularGroupF );
216   mySpecularCoefntF->setMaximum(1);
217   mySpecularCoefntF->setSingleStep(0.05);
218   connect( mySpecularCoefntF, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
219
220   // Specular group box layout
221   QGridLayout* specularLayout1 = new QGridLayout( mySpecularGroupF );
222   specularLayout1->setMargin( MARGIN ); specularLayout1->setSpacing( SPACING );
223   specularLayout1->addWidget( specColorLab1,       0, 0 );
224   specularLayout1->addWidget( mySpecularColorF,    0, 1 );
225   specularLayout1->addWidget( specCoefficientLab1, 1, 0 );
226   specularLayout1->addWidget( mySpecularCoefntF,   1, 1 );
227
228   // ----------------- "Emission" reflection type group box -----------------
229   myEmissionGroupF = new QGroupBox( tr( "EMISSION_GRP" ), w1 );
230   myEmissionGroupF->setCheckable(true);
231   connect( myEmissionGroupF, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) );
232
233   // Emission color
234   QLabel* emisColorLab1 = new QLabel( tr( "COLOR" ), myEmissionGroupF );
235   myEmissionColorF = new QtxColorButton( myEmissionGroupF );
236   myEmissionColorF->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
237   connect( myEmissionColorF, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
238
239   // Emission coefficient
240   QLabel* emisCoefficientLab1 = new QLabel( tr( "COEFFICIENT" ), myEmissionGroupF );
241   myEmissionCoefntF = new QtxDoubleSpinBox( myEmissionGroupF );
242   myEmissionCoefntF->setMaximum(1);
243   myEmissionCoefntF->setSingleStep(0.05);
244   connect( myEmissionCoefntF, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
245
246   // Emission group box layout
247   QGridLayout* emissionLayout1 = new QGridLayout( myEmissionGroupF );
248   emissionLayout1->setMargin( MARGIN ); emissionLayout1->setSpacing( SPACING );
249   emissionLayout1->addWidget( emisColorLab1,       0, 0 );
250   emissionLayout1->addWidget( myEmissionColorF,    0, 1 );
251   emissionLayout1->addWidget( emisCoefficientLab1, 1, 0 );
252   emissionLayout1->addWidget( myEmissionCoefntF,   1, 1 );
253
254   // Erase emission group in case of VTK viewer
255   if ( myViewerType == VTK )
256     myEmissionGroupF->hide();
257
258   // Add group boxes to the main grid layout of the frame with material properties
259   gLayout1->addWidget( myAmbientGroupF,  0, 0 );
260   gLayout1->addWidget( myDiffuseGroupF,  0, 1 );
261   gLayout1->addWidget( mySpecularGroupF, 1, 0 );
262   gLayout1->addWidget( myEmissionGroupF, 1, 1 );
263
264   // Shininess
265   QLabel* shininessLab1 = new QLabel( tr( "SHININESS" ), w1 );
266   myShininessF = new QtxDoubleSpinBox( w1 );
267   myShininessF->setMaximum(1);
268   myShininessF->setSingleStep(0.05);
269   connect( myShininessF, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
270
271   // Shininess layout
272   QHBoxLayout* shLayout1 = new QHBoxLayout( w1 );
273   shLayout1->setMargin( MARGIN ); shLayout1->setSpacing( SPACING );
274   shLayout1->addWidget( shininessLab1 );
275   shLayout1->addWidget( myShininessF );
276
277   // Fill initial vertical layout of the reflection type group box
278   vLayout1->addLayout( gLayout1 );
279   vLayout1->addLayout( shLayout1 );
280   vLayout1->addStretch();
281
282   // ======================= Create a tab for back material ======================= 
283   myMaterialBWidget = new QWidget( myMaterialTab );
284   QVBoxLayout* vLayout2 = new QVBoxLayout( myMaterialBWidget );
285
286   QGridLayout* gLayout2 = new QGridLayout( myMaterialBWidget );
287   gLayout2->setMargin( MARGIN ); gLayout2->setSpacing( SPACING );
288
289   // ----------------- "Ambient" reflection type group box -----------------
290   myAmbientGroupB = new QGroupBox( tr( "AMBIENT_GRP" ), myMaterialBWidget );
291   myAmbientGroupB->setCheckable(true);
292   connect( myAmbientGroupB, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) ); 
293
294   // Ambient color
295   QLabel* ambColorLab2 = new QLabel( tr( "COLOR" ), myAmbientGroupB );
296   myAmbientColorB = new QtxColorButton( myAmbientGroupB );
297   myAmbientColorB->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
298   connect( myAmbientColorB, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
299
300   // Ambient coefficient
301   QLabel* ambCoefficientLab2 = new QLabel( tr( "COEFFICIENT" ), myAmbientGroupB );
302   myAmbientCoefntB = new QtxDoubleSpinBox( myAmbientGroupB );
303   myAmbientCoefntB->setMaximum(1);
304   myAmbientCoefntB->setSingleStep(0.05);
305   connect( myAmbientCoefntB, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
306
307   // Ambient group box layout
308   QGridLayout* ambientLayout2 = new QGridLayout( myAmbientGroupB );
309   ambientLayout2->setMargin( MARGIN ); ambientLayout2->setSpacing( SPACING );
310   ambientLayout2->addWidget( ambColorLab2,       0, 0 );
311   ambientLayout2->addWidget( myAmbientColorB,    0, 1 );
312   ambientLayout2->addWidget( ambCoefficientLab2, 1, 0 );
313   ambientLayout2->addWidget( myAmbientCoefntB,   1, 1 );
314
315   // ----------------- "Diffuse" reflection type group box -----------------
316   myDiffuseGroupB = new QGroupBox( tr( "DIFFUSE_GRP" ), myMaterialBWidget );
317   myDiffuseGroupB->setCheckable(true);
318   connect( myDiffuseGroupB, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) );
319
320   // Diffuse color
321   QLabel* difColorLab2 = new QLabel( tr( "COLOR" ), myDiffuseGroupB );
322   myDiffuseColorB = new QtxColorButton( myDiffuseGroupB );
323   myDiffuseColorB->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
324   connect( myDiffuseColorB, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
325
326   // Diffuse coefficient
327   QLabel* difCoefficientLab2 = new QLabel( tr( "COEFFICIENT" ), myDiffuseGroupB );
328   myDiffuseCoefntB = new QtxDoubleSpinBox( myDiffuseGroupB );
329   myDiffuseCoefntB->setMaximum(1);
330   myDiffuseCoefntB->setSingleStep(0.05);
331   connect( myDiffuseCoefntB, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
332
333   // Diffuse group box layout
334   QGridLayout* diffuseLayout2 = new QGridLayout( myDiffuseGroupB );
335   diffuseLayout2->setMargin( MARGIN ); diffuseLayout2->setSpacing( SPACING );
336   diffuseLayout2->addWidget( difColorLab2,       0, 0 );
337   diffuseLayout2->addWidget( myDiffuseColorB,    0, 1 );
338   diffuseLayout2->addWidget( difCoefficientLab2, 1, 0 );
339   diffuseLayout2->addWidget( myDiffuseCoefntB,   1, 1 );
340
341   // ----------------- "Specular" reflection type group box -----------------
342   mySpecularGroupB = new QGroupBox( tr( "SPECULAR_GRP" ), myMaterialBWidget );
343   mySpecularGroupB->setCheckable(true);
344   connect( mySpecularGroupB, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) );
345
346   // Specular color
347   QLabel* specColorLab2 = new QLabel( tr( "COLOR" ), mySpecularGroupB );
348   mySpecularColorB = new QtxColorButton( mySpecularGroupB );
349   mySpecularColorB->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
350   connect( mySpecularColorB, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
351
352   // Specular coefficient
353   QLabel* specCoefficientLab2 = new QLabel( tr( "COEFFICIENT" ), mySpecularGroupB );
354   mySpecularCoefntB = new QtxDoubleSpinBox( mySpecularGroupB );
355   mySpecularCoefntB->setMaximum(1);
356   mySpecularCoefntB->setSingleStep(0.05);
357   connect( mySpecularCoefntB, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
358
359   // Specular group box layout
360   QGridLayout* specularLayout2 = new QGridLayout( mySpecularGroupB );
361   specularLayout2->setMargin( MARGIN ); specularLayout2->setSpacing( SPACING );
362   specularLayout2->addWidget( specColorLab2,       0, 0 );
363   specularLayout2->addWidget( mySpecularColorB,    0, 1 );
364   specularLayout2->addWidget( specCoefficientLab2, 1, 0 );
365   specularLayout2->addWidget( mySpecularCoefntB,   1, 1 );
366
367   // ----------------- "Emission" reflection type group box -----------------
368   myEmissionGroupB = new QGroupBox( tr( "EMISSION_GRP" ), myMaterialBWidget );
369   myEmissionGroupB->setCheckable(true);
370   connect( myEmissionGroupB, SIGNAL( toggled( bool ) ), this, SLOT( onReflectionTypeToggled( bool ) ) );
371
372   // Emission color
373   QLabel* emisColorLab2 = new QLabel( tr( "COLOR" ), myEmissionGroupB );
374   myEmissionColorB = new QtxColorButton( myEmissionGroupB );
375   myEmissionColorB->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
376   connect( myEmissionColorB, SIGNAL( changed( QColor ) ), this, SIGNAL( changed() ) ); 
377
378   // Emission coefficient
379   QLabel* emisCoefficientLab2 = new QLabel( tr( "COEFFICIENT" ), myEmissionGroupB );
380   myEmissionCoefntB = new QtxDoubleSpinBox( myEmissionGroupB );
381   myEmissionCoefntB->setMaximum(1);
382   myEmissionCoefntB->setSingleStep(0.05);
383   connect( myEmissionCoefntB, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
384
385   // Emission group box layout
386   QGridLayout* emissionLayout2 = new QGridLayout( myEmissionGroupB );
387   emissionLayout2->setMargin( MARGIN ); emissionLayout2->setSpacing( SPACING );
388   emissionLayout2->addWidget( emisColorLab2,       0, 0 );
389   emissionLayout2->addWidget( myEmissionColorB,    0, 1 );
390   emissionLayout2->addWidget( emisCoefficientLab2, 1, 0 );
391   emissionLayout2->addWidget( myEmissionCoefntB,   1, 1 );
392
393   // Erase emission group in case of VTK viewer
394   if ( myViewerType == VTK )
395     myEmissionGroupB->hide();
396
397   // Add group boxes to the main grid layout of the frame with material properties
398   gLayout2->addWidget( myAmbientGroupB,  0, 0 );
399   gLayout2->addWidget( myDiffuseGroupB,  0, 1 );
400   gLayout2->addWidget( mySpecularGroupB, 1, 0 );
401   gLayout2->addWidget( myEmissionGroupB, 1, 1 );
402
403   // Shininess
404   QLabel* shininessLab2 = new QLabel( tr( "SHININESS" ), myMaterialBWidget );
405   myShininessB = new QtxDoubleSpinBox( myMaterialBWidget );
406   myShininessB->setMaximum(1);
407   myShininessB->setSingleStep(0.05);
408   connect( myShininessB, SIGNAL( valueChanged( double ) ), this, SIGNAL( materialChanged() ) );
409
410   // Shininess layout
411   QHBoxLayout* shLayout2 = new QHBoxLayout( myMaterialBWidget );
412   shLayout2->setMargin( MARGIN ); shLayout2->setSpacing( SPACING );
413   shLayout2->addWidget( shininessLab2 );
414   shLayout2->addWidget( myShininessB );
415
416   // Fill initial vertical layout of the reflection type group box
417   vLayout2->addLayout( gLayout2 );
418   vLayout2->addLayout( shLayout2 );
419   vLayout2->addStretch();
420
421   // Add tabs to material tab widget
422   myMaterialTab->addTab( w1, tr( "Front material" ) );
423   myMaterialTab->addTab( myMaterialBWidget, tr( "Back material" ) );
424
425   // Initialize dialog box
426   setFocusProxy( fr );
427   setButtonPosition( Right, Close );
428   setDialogFlags( AlignOnce );
429   myMaterialList->setEditTriggers( QAbstractItemView::EditKeyPressed );
430
431   // ! RESOURCES
432   QStringList globalMaterials = resourceMgr()->materials( Material_ResourceMgr::Global );
433   QStringList userMaterials = resourceMgr()->materials( Material_ResourceMgr::User );
434
435   QListWidgetItem* item;
436
437   // Current material
438   item = new QListWidgetItem( tr( "[ Current ]" ) );
439   item->setForeground( QColor( Qt::red ) );
440   item->setData( TypeRole, QVariant( Current ) );
441   myMaterialList->addItem( item );
442   // Default material
443   item = new QListWidgetItem( tr( "[ Default ]" ) );
444   item->setForeground( QColor( Qt::green ) );
445   item->setData( TypeRole, QVariant( Default ) );
446   myMaterialList->addItem( item );  
447   // ! RESOURCES
448   // Global materials
449   foreach ( QString sname, globalMaterials ) {
450     item = new QListWidgetItem( sname );
451     item->setForeground( QColor( Qt::blue ) );
452     item->setData( TypeRole, QVariant( Global ) );
453     item->setData( NameRole, QVariant( sname ) );
454     myMaterialList->addItem( item );
455   }
456   // ! RESOURCES
457   // User materials
458   foreach ( QString sname, userMaterials ) {
459     item = new QListWidgetItem( sname );
460     item->setData( TypeRole, QVariant( User ) );
461     item->setData( NameRole, QVariant( sname ) );
462     item->setFlags( item->flags() | Qt::ItemIsEditable );
463     myMaterialList->addItem( item );
464   }
465   
466   // Connect signals
467   connect( myMaterialTab,       SIGNAL( currentChanged( int ) ),
468            this,                SLOT( onCurrentTabChanged( int ) ) );
469   connect( myBackMaterialCheck, SIGNAL( toggled( bool ) ),
470            this,                SLOT( onBackMaterialChecked( bool ) ) );
471
472   connect( myMaterialList,      SIGNAL( itemSelectionChanged() ),
473            this,                SLOT( onMaterialChanged() ) );
474   connect( myMaterialList,      SIGNAL( itemChanged( QListWidgetItem* ) ),       
475            this,                SLOT( onItemChanged( QListWidgetItem* ) ) );
476   connect( myMaterialList,      SIGNAL( itemDoubleClicked( QListWidgetItem* ) ), 
477            this,                SLOT( onApply() ) );
478
479   connect( this, SIGNAL( changed() ),      this, SIGNAL( materialChanged() ) );
480   connect( this, SIGNAL( materialChanged() ), this, SLOT( onChanged() ) );
481   
482   connect( this, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
483   connect( this, SIGNAL( dlgHelp() ),  this, SLOT( onHelp() ) );
484
485   // Initialize current fornt and back material models of the selected shape
486   if ( app ) {
487     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
488     if ( aSelMgr ) {
489       SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
490       if( aStudy ) {
491         SALOME_ListIO selected;
492         aSelMgr->selectedObjects( selected );
493
494         Handle(SALOME_InteractiveObject) FirstIOS =  selected.First();
495         if ( !FirstIOS.IsNull() ) {
496           SUIT_ViewWindow* window = app->desktop()->activeWindow();
497           int aMgrId = window->getViewManager()->getGlobalId();
498           
499           QString aMaterialF;
500           QString aMaterialB;
501
502           for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
503
504             PropMap aPropMap = aStudy->getObjectPropMap( aMgrId, It.Value()->getEntry() );
505             aMaterialF = aPropMap.value(FRONT_MATERIAL_PROP).toString();            
506             aMaterialB = aPropMap.value(BACK_MATERIAL_PROP).toString();
507               
508             if ( !aMaterialF.isEmpty() ) {
509               
510               QStringList aPropsF = aMaterialF.split(DIGIT_SEPARATOR);
511               
512               myCurrentModelF = Material_Model::getMaterialModel( aPropsF );            
513               
514               if ( !aMaterialB.isEmpty() ) {            
515                 QStringList aPropsB = aMaterialB.split(DIGIT_SEPARATOR);                
516                 myCurrentModelB = Material_Model::getMaterialModel( aPropsB );
517                 
518                 myBackMaterialCheck->setChecked( true );
519               }
520               else {
521                 myCurrentModelB = Material_Model::getMaterialModel( aPropsF );
522                 
523                 myBackMaterialCheck->setChecked( false );
524                 myMaterialTab->removeTab( 1 );
525               }
526               
527               break;
528             }
529           }
530             
531           if ( aMaterialF.isEmpty() ) {
532             myCurrentModelF = new Material_Model();
533             myCurrentModelF->fromResources( SUIT_Session::session()->resourceMgr(), "Geometry" );
534
535             myCurrentModelB = new Material_Model();
536             myCurrentModelB->fromResources( SUIT_Session::session()->resourceMgr(), "Geometry", false );
537           }
538         }
539       }
540     }
541   }
542
543   myMaterialList->setCurrentRow( 0 );
544   myMaterialListFId = 0;
545   myMaterialListBId = 0;
546
547   myIsBTabWasActivated = false;
548
549   myHelpFileName = "material_page.html";
550 }
551
552 /*!
553   \brief Destructor
554 */
555 GEOMToolsGUI_MaterialPropertiesDlg::~GEOMToolsGUI_MaterialPropertiesDlg()
556 {
557   if ( myCurrentModelF )
558     delete myCurrentModelF;
559
560   if ( myCurrentModelB )
561     delete myCurrentModelB;
562 }
563
564 /*!
565   \brief Called when "OK" button is clicked
566 */
567 void GEOMToolsGUI_MaterialPropertiesDlg::accept()
568 {
569   onApply();
570   QtxDialog::accept();
571 }
572
573 /*!
574   \brief Process key press event
575   \param e key event
576 */
577 void GEOMToolsGUI_MaterialPropertiesDlg::keyPressEvent( QKeyEvent* e )
578 {
579   if ( e->key() == Qt::Key_Delete ) {
580     QListWidgetItem* item = myMaterialList->currentItem();
581     if ( item && item->data( TypeRole ).toInt() == User ) {
582       if ( QMessageBox::question( this,
583                                   tr( "Delete user material" ),
584                                   tr( "Remove material %1?" ).arg( item->text() ),
585                                   QMessageBox::Yes | QMessageBox::No,
586                                   QMessageBox::Yes ) == QMessageBox::Yes ) {
587         resourceMgr()->remove( item->data( NameRole ).toString() );
588         resourceMgr()->save();
589         delete item;
590       }
591     }
592   }
593   QtxDialog::keyPressEvent( e );
594 }
595
596 /*!
597   \brief Get GEOM materials resource manager
598   \return materials resource manager
599 */
600 Material_ResourceMgr* GEOMToolsGUI_MaterialPropertiesDlg::resourceMgr()
601 {
602   if ( !myResMgr )
603     myResMgr = new Material_ResourceMgr();
604   return myResMgr;
605 }
606
607 /*!
608   \brief Initialize dialog box fields from material model
609   \param model material model
610 */
611 void GEOMToolsGUI_MaterialPropertiesDlg::fromModel( Material_Model* model)
612 {
613   if ( !model ) return;
614
615   bool isReflectionTypeActive;
616
617   if ( isFrontTabActive() ) { // Fill in front material tab
618     
619     // Ambient reflection type
620     isReflectionTypeActive = model->hasAmbientReflection();
621     myAmbientGroupF->setChecked( isReflectionTypeActive );
622     if ( isReflectionTypeActive ) {
623       // Load ambient color
624       myAmbientColorF->setColor( model->color(Material_Model::Ambient) );
625       // Load ambient coefficient
626       myAmbientCoefntF->setValue( model->coefficient(Material_Model::Ambient) );
627     }
628     
629     // Diffuse reflection type
630     isReflectionTypeActive = model->hasDiffuseReflection();
631     myDiffuseGroupF->setChecked( isReflectionTypeActive );
632     if ( isReflectionTypeActive ) {
633       // Load diffuse color
634       myDiffuseColorF->setColor( model->color(Material_Model::Diffuse) );
635       // Load diffuse coefficient
636       myDiffuseCoefntF->setValue( model->coefficient(Material_Model::Diffuse) );
637     }
638     
639     // Specular reflection type
640     isReflectionTypeActive = model->hasSpecularReflection();
641     mySpecularGroupF->setChecked( isReflectionTypeActive );
642     if ( isReflectionTypeActive ) {
643       // Load specular color
644       mySpecularColorF->setColor( model->color(Material_Model::Specular) );
645       // Load specular coefficient
646       mySpecularCoefntF->setValue( model->coefficient(Material_Model::Specular) );
647     }
648     
649     // Emission reflection type
650     isReflectionTypeActive = model->hasEmissionReflection();
651     myEmissionGroupF->setChecked( isReflectionTypeActive );
652     if ( isReflectionTypeActive ) {
653       // Load emission color
654       myEmissionColorF->setColor( model->color(Material_Model::Emission) );
655       // Load emission coefficient
656       myEmissionCoefntF->setValue( model->coefficient(Material_Model::Emission) );
657     }
658     
659     // Shininess
660     myShininessF->setValue( model->shininess() );
661     
662   }
663   else { // Fill in back material tab
664     
665     // Ambient reflection type
666     isReflectionTypeActive = model->hasAmbientReflection();
667     myAmbientGroupB->setChecked( isReflectionTypeActive );
668     if ( isReflectionTypeActive ) {
669       // Load ambient color
670       myAmbientColorB->setColor( model->color(Material_Model::Ambient) );
671       // Load ambient coefficient
672       myAmbientCoefntB->setValue( model->coefficient(Material_Model::Ambient) );
673     }
674     
675     // Diffuse reflection type
676     isReflectionTypeActive = model->hasDiffuseReflection();
677     myDiffuseGroupB->setChecked( isReflectionTypeActive );
678     if ( isReflectionTypeActive ) {
679       // Load diffuse color
680       myDiffuseColorB->setColor( model->color(Material_Model::Diffuse) );
681       // Load diffuse coefficient
682       myDiffuseCoefntB->setValue( model->coefficient(Material_Model::Diffuse) );
683     }
684     
685     // Specular reflection type
686     isReflectionTypeActive = model->hasSpecularReflection();
687     mySpecularGroupB->setChecked( isReflectionTypeActive );
688     if ( isReflectionTypeActive ) {
689       // Load specular color
690       mySpecularColorB->setColor( model->color(Material_Model::Specular) );
691       // Load specular coefficient
692       mySpecularCoefntB->setValue( model->coefficient(Material_Model::Specular) );
693     }
694     
695     // Emission reflection type
696     isReflectionTypeActive = model->hasEmissionReflection();
697     myEmissionGroupB->setChecked( isReflectionTypeActive );
698     if ( isReflectionTypeActive ) {
699       // Load emission color
700       myEmissionColorB->setColor( model->color(Material_Model::Emission) );
701       // Load emission coefficient
702       myEmissionCoefntB->setValue( model->coefficient(Material_Model::Emission) );
703     }
704     
705     // Shininess
706     myShininessB->setValue( model->shininess() );
707     
708   }
709 }
710  
711 /*!
712   \brief Save values from dialog box fields to material model
713   \param model material model
714 */
715 void GEOMToolsGUI_MaterialPropertiesDlg::toModel( Material_Model* model ) const
716 {
717   if ( !model ) return;
718     
719   if ( isFrontTabActive() )
720     toFrontModel( model );
721   else
722     toBackModel( model );
723 }
724
725 /*!
726   \brief Save values from dialog box fields to front material model
727   \param model front material model to be filled
728 */
729 void GEOMToolsGUI_MaterialPropertiesDlg::toFrontModel( Material_Model* model ) const
730 {
731   if ( !model ) return;
732     
733   // "Ambient" reflection type
734   if ( myAmbientGroupF->isChecked() ) {
735     model->setColor( Material_Model::Ambient, myAmbientColorF->color() );
736     model->setCoefficient( Material_Model::Ambient, myAmbientCoefntF->value() );
737   }
738   else {
739     model->removeColor( Material_Model::Ambient );
740     model->removeCoefficient( Material_Model::Ambient );
741   }
742   
743   // "Diffuse" reflection type
744   if ( myDiffuseGroupF->isChecked() ) {
745     model->setColor( Material_Model::Diffuse, myDiffuseColorF->color() );
746     model->setCoefficient( Material_Model::Diffuse, myDiffuseCoefntF->value() );
747   }
748   else {
749     model->removeColor( Material_Model::Diffuse );
750     model->removeCoefficient( Material_Model::Diffuse );
751   }
752   
753   // "Specular" reflection type
754   if ( mySpecularGroupF->isChecked() ) {
755     model->setColor( Material_Model::Specular, mySpecularColorF->color() );
756     model->setCoefficient( Material_Model::Specular, mySpecularCoefntF->value() );
757   }
758   else {
759     model->removeColor( Material_Model::Specular );
760     model->removeCoefficient( Material_Model::Specular );
761   }
762   
763   // "Emission" reflection type
764   if ( myEmissionGroupF->isChecked() ) {
765     model->setColor( Material_Model::Emission, myEmissionColorF->color() );
766     model->setCoefficient( Material_Model::Emission, myEmissionCoefntF->value() );
767   }
768   else {
769     model->removeColor( Material_Model::Emission );
770     model->removeCoefficient( Material_Model::Emission );
771   }
772   
773   // Shininess
774   model->setShininess( myShininessF->value() );
775 }
776
777 /*!
778   \brief Save values from dialog box fields to back material model
779   \param model back material model to be filled
780 */
781 void GEOMToolsGUI_MaterialPropertiesDlg::toBackModel( Material_Model* model ) const
782 {
783   if ( !model )
784     return;
785
786   // "Ambient" reflection type
787   if ( myAmbientGroupB->isChecked() ) {
788     model->setColor( Material_Model::Ambient, myAmbientColorB->color() );
789     model->setCoefficient( Material_Model::Ambient, myAmbientCoefntB->value() );
790   }
791   else {
792     model->removeColor( Material_Model::Ambient );
793     model->removeCoefficient( Material_Model::Ambient );
794   }
795   
796   // "Diffuse" reflection type
797   if ( myDiffuseGroupB->isChecked() ) {
798     model->setColor( Material_Model::Diffuse, myDiffuseColorB->color() );
799     model->setCoefficient( Material_Model::Diffuse, myDiffuseCoefntB->value() );
800   }
801   else {
802     model->removeColor( Material_Model::Diffuse );
803     model->removeCoefficient( Material_Model::Diffuse );
804   }
805   
806   // "Specular" reflection type
807   if ( mySpecularGroupB->isChecked() ) {
808     model->setColor( Material_Model::Specular, mySpecularColorB->color() );
809     model->setCoefficient( Material_Model::Specular, mySpecularCoefntB->value() );
810   }
811   else {
812     model->removeColor( Material_Model::Specular );
813     model->removeCoefficient( Material_Model::Specular );
814   }
815   
816   // "Emission" reflection type
817   if ( myEmissionGroupB->isChecked() ) {
818     model->setColor( Material_Model::Emission, myEmissionColorB->color() );
819     model->setCoefficient( Material_Model::Emission, myEmissionCoefntB->value() );
820   }
821   else {
822     model->removeColor( Material_Model::Emission );
823     model->removeCoefficient( Material_Model::Emission );
824   }
825   
826   // Shininess
827   model->setShininess( myShininessB->value() );
828 }
829
830 /*!
831   \brief Find unique name for the material name
832   \param name material name template
833   \param item if not 0, used to be ignored when browsing through items list
834   \param addSuffix if \c true, the integrer suffix is always added to the material name (otherwise
835   suffix is added only if item name is not unique)
836   \return new unique material name
837 */
838 QString GEOMToolsGUI_MaterialPropertiesDlg::findUniqueName( const QString& name, QListWidgetItem* item, bool addSuffix )
839 {
840   bool found = false;
841   int idx = 0;
842   for( int i = 2; i < myMaterialList->count(); i++ ) {
843     if ( item == myMaterialList->item( i ) ) continue;
844     QString iname = myMaterialList->item( i )->text();
845     if ( iname == name ) {
846       found = true;
847     }
848     else {
849       iname = iname.mid( name.length() ).trimmed();
850       bool ok = false;
851       int nx = iname.toInt( &ok );
852       if ( ok ) idx = qMax( idx, nx );
853     }
854   }
855   return found || addSuffix ? QString( "%1 %2" ).arg( name ).arg( idx+1 ) : name;
856 }
857
858 /*!
859   \brief Check if tab with front material properties is currently active
860   \return true if front material tab is active
861 */
862 bool GEOMToolsGUI_MaterialPropertiesDlg::isFrontTabActive() const
863 {
864   return ( myMaterialTab->currentIndex() == 0 ? true : false );
865 }
866
867 /*!
868   \brief Called when "Apply" button is pressed
869 */
870 void GEOMToolsGUI_MaterialPropertiesDlg::onApply()
871 {  
872   // save user materials
873   resourceMgr()->save();
874
875   toFrontModel( myCurrentModelF );
876   if ( myBackMaterialCheck->isChecked() && myIsBTabWasActivated )
877     toBackModel( myCurrentModelB );
878
879   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
880   if ( !app )
881     return;
882   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
883   if ( !aSelMgr )
884     return;
885   
886   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
887   
888   if(!aStudy)
889     return;
890   
891   SALOME_ListIO selected;
892   aSelMgr->selectedObjects( selected );
893   if ( selected.IsEmpty() )
894     return;
895
896   Handle(SALOME_InteractiveObject) FirstIOS =  selected.First();
897   if ( FirstIOS.IsNull() )
898     return;
899         
900   SUIT_ViewWindow* window = app->desktop()->activeWindow();
901   int aMgrId = window->getViewManager()->getGlobalId();
902
903   // Parse material properties and form a string for persistent purpose
904   QString aMaterialF = myCurrentModelF->getMaterialProperty();
905   QString aMaterialB;
906   if ( myBackMaterialCheck->isChecked() )
907     aMaterialB = myCurrentModelB->getMaterialProperty();
908   
909   if ( myViewerType == VTK ) {    
910     // Get material properties from the current model
911     /*
912     vtkProperty* aPropertyF;
913     if ( !unsetMaterial )
914       aPropertyF = myCurrentModelF->getMaterialVTKProperty();
915     */
916     vtkProperty* aPropertyF = myCurrentModelF->getMaterialVTKProperty();
917     vtkProperty* aPropertyB = aPropertyF;
918     if ( myBackMaterialCheck->isChecked() )
919       aPropertyB = myCurrentModelB->getMaterialVTKProperty();
920
921     SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
922     if ( !vtkVW )
923       return;
924     SVTK_View* aView = vtkVW->getView();
925
926     SUIT_OverrideCursor();
927     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
928
929       /*
930       if ( unsetMaterial || aMaterialF.isEmpty() )
931         // Unset material for the selected shape
932         aisShape->UnsetMaterial();
933       else
934         // Set material for the selected shape
935       */
936         aView->SetMaterial( It.Value(), aPropertyF, aPropertyB );
937
938       // Persistent
939       aStudy->setObjectProperty( aMgrId, It.Value()->getEntry(), FRONT_MATERIAL_PROP, aMaterialF );
940       aStudy->setObjectProperty( aMgrId, It.Value()->getEntry(), BACK_MATERIAL_PROP, aMaterialB );
941     } // for...
942     aView->Repaint();
943     GeometryGUI::Modified();
944   } // if ( VTK )
945         
946   else if ( myViewerType == OCC ) {
947     // Get material properties from the current model
948     /*
949     Graphic3d_MaterialAspect aMatF;
950     if ( !unsetMaterial )
951       aMatF = myCurrentModelF->getMaterialOCCAspect();
952     */
953     Graphic3d_MaterialAspect aMatF = myCurrentModelF->getMaterialOCCAspect();
954     Graphic3d_MaterialAspect aMatB = aMatF;
955     if ( myBackMaterialCheck->isChecked() )
956       aMatB = myCurrentModelB->getMaterialOCCAspect();
957
958     Handle(GEOM_AISShape) aisShape;
959     
960     SUIT_OverrideCursor();
961     OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
962     if ( !vm )
963       return;
964
965     GEOMBase* gb = new GEOMBase();
966
967     Handle(AIS_InteractiveContext) ic = vm->getAISContext();
968     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
969       aisShape = gb->ConvertIOinGEOMAISShape( It.Value(), true );
970       if ( !aisShape.IsNull() ) {
971
972         if(!aisShape->HasInteractiveContext())
973           aisShape->SetContext(ic);
974
975         /*
976         if ( unsetMaterial || aMaterialF.isEmpty() )
977           // Unset material for the selected shape
978           aisShape->UnsetMaterial();
979         else
980         */
981           if ( myBackMaterialCheck->isChecked() ) {
982             // Set front material for the selected shape
983             aisShape->SetCurrentFacingModel(Aspect_TOFM_FRONT_SIDE);
984             aisShape->SetMaterial(aMatF);
985             // Set back material for the selected shape
986             aisShape->SetCurrentFacingModel(Aspect_TOFM_BACK_SIDE);
987             aisShape->SetMaterial(aMatB);
988             // Return to the default facing mode
989             aisShape->SetCurrentFacingModel(Aspect_TOFM_BOTH_SIDE);
990           }
991           else {
992             // Set the same front and back (is equal to front) materials for the selected shape
993             aisShape->SetMaterial(aMatF);
994           }
995
996         if (aisShape->DisplayMode() != AIS_Shaded/*aisShape->DisplayMode() == GEOM_AISShape::ShadingWithEdges*/)
997           ic->RecomputePrsOnly( aisShape, Standard_False );
998
999         // Persistent   
1000         aStudy->setObjectProperty( aMgrId, It.Value()->getEntry(), FRONT_MATERIAL_PROP, aMaterialF );
1001         aStudy->setObjectProperty( aMgrId, It.Value()->getEntry(), BACK_MATERIAL_PROP, aMaterialB );
1002       }
1003     } // for...    
1004     ic->UpdateCurrentViewer();
1005     GeometryGUI::Modified();
1006   } // if ( OCC )
1007 }
1008
1009 /*!
1010   \brief Called when "Help" button is pressed
1011 */
1012 void GEOMToolsGUI_MaterialPropertiesDlg::onHelp()
1013 {
1014   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
1015   if (app) {
1016     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
1017     app->onHelpContextModule(aGeomGUI ? app->moduleName(aGeomGUI->moduleName()) : QString(""), myHelpFileName);
1018   }
1019   else {
1020                 QString platform;
1021 #ifdef WIN32
1022                 platform = "winapplication";
1023 #else
1024                 platform = "application";
1025 #endif
1026     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
1027                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
1028                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
1029                              QObject::tr("BUT_OK"));
1030   }  
1031 }
1032
1033 /*!
1034   \brief Called when user check/uncheck "Enable back material" check box
1035   \param theIsChecked the check state of the check box
1036 */
1037 void GEOMToolsGUI_MaterialPropertiesDlg::onBackMaterialChecked( bool theIsChecked )
1038 {
1039   if ( theIsChecked ) {
1040     // Tab with back material properties is displayed
1041     myMaterialTab->addTab( myMaterialBWidget, tr( "Back material" ) );
1042
1043     // Create a current model of back material
1044     if ( !myCurrentModelB ) {
1045       myCurrentModelB = new Material_Model();
1046       myCurrentModelB->fromResources( SUIT_Session::session()->resourceMgr(), "Geometry", false );
1047     }
1048     
1049     myMaterialListBId = 0;
1050   }
1051   else {
1052     // Tab with back material properties is hidden
1053     myMaterialTab->removeTab( 1 );
1054     
1055     // Remove the current model for back material
1056     if ( myCurrentModelB ) {
1057       delete myCurrentModelB;
1058       myCurrentModelB = 0;
1059     }
1060   }
1061 }
1062
1063 /*!
1064   \brief Called when user activates material tab
1065   \param theIndex the index of the tab which was activated by the user
1066 */
1067 void GEOMToolsGUI_MaterialPropertiesDlg::onCurrentTabChanged(int theIndex)
1068 {
1069   blockSignals( true );
1070
1071   // Change selection in the list of materials
1072   if ( isFrontTabActive() )
1073     myMaterialList->setCurrentRow( myMaterialListFId );
1074   else if ( myBackMaterialCheck->isChecked() )
1075     myMaterialList->setCurrentRow( myMaterialListBId );    
1076
1077   if ( theIndex == 1 )
1078     myIsBTabWasActivated = true;
1079
1080   blockSignals( false );
1081
1082   onMaterialChanged();
1083 }
1084
1085 /*!
1086   \brief Called when user selects any material item in the materials list
1087 */
1088 void GEOMToolsGUI_MaterialPropertiesDlg::onMaterialChanged()
1089 {
1090   blockSignals( true );
1091
1092   QListWidgetItem* item = myMaterialList->currentItem();
1093   int type = item->data( TypeRole ).toInt();
1094   
1095   Material_Model* model = 0;
1096
1097   bool isFrontTab = isFrontTabActive();
1098
1099   if ( isFrontTab )
1100     myMaterialListFId = myMaterialList->currentRow();
1101   else
1102     myMaterialListBId = myMaterialList->currentRow();
1103
1104   switch ( type ) {    
1105   case Current:
1106     // current material
1107     model = ( isFrontTab ? myCurrentModelF : myCurrentModelB );
1108     break;    
1109   case Default:
1110     // default material
1111     model = new Material_Model();
1112     model->fromResources( SUIT_Session::session()->resourceMgr(), "Geometry", ( isFrontTab ? true : false ) );
1113     break;    
1114   case Global:
1115   case User:
1116     // global material, user material
1117     model = new Material_Model();
1118     model->fromResources( resourceMgr(), item->data( NameRole ).toString() );
1119     break;
1120   default:
1121     break;
1122   }
1123
1124   fromModel( model );
1125   if ( type != Current )
1126     delete model;
1127
1128   blockSignals( false );
1129 }
1130
1131 /*!
1132   \brief Called when any material parameter is changed by the user
1133 */
1134 void GEOMToolsGUI_MaterialPropertiesDlg::onChanged()
1135 {
1136   QListWidgetItem* item = myMaterialList->currentItem();
1137   int type = item->data( TypeRole ).toInt();
1138
1139   bool isFrontTab = isFrontTabActive();
1140
1141   // for the current and user schemas do not perform any actions
1142   if ( type == Current ) {    
1143     Material_Model model = ( isFrontTab ? *( myCurrentModelF ) : *( myCurrentModelB ) );
1144     toModel( &model );
1145     model.save( 0, QString(), isFrontTab );
1146     blockSignals( true );
1147     fromModel( &model );
1148     blockSignals( false );    
1149   }
1150   else if ( type == User ) {    
1151     Material_Model model;
1152     toModel( &model );
1153     QString oldName = item->data( NameRole ).toString(), newName = item->text();
1154     if ( oldName == newName ) {
1155       model.save( resourceMgr(), oldName, isFrontTab );
1156     }
1157     else {
1158       resourceMgr()->remove( oldName );
1159       model.save( resourceMgr(), newName, isFrontTab );
1160       item->setData( NameRole, newName );
1161     }
1162     blockSignals( true );
1163     fromModel( &model );
1164     blockSignals( false );    
1165   }
1166   else {
1167     // if user tries to change global (or default, or no material) material,
1168     // we create a new user material basing on selected one
1169     QString newName = findUniqueName( tr( "CUSTOM_MATERIAL" ), 0, true );
1170     item = new QListWidgetItem( newName );
1171     item->setData( TypeRole, QVariant( User ) );
1172     item->setData( NameRole, QVariant( newName ) );
1173     item->setFlags( item->flags() | Qt::ItemIsEditable );
1174     myMaterialList->addItem( item );
1175     
1176     Material_Model model;
1177     toModel( &model );
1178     model.save( resourceMgr(), newName, isFrontTab );
1179     
1180     myMaterialList->setCurrentItem( item );
1181
1182     if ( isFrontTab )
1183       myMaterialListFId = myMaterialList->currentRow();
1184     else
1185       myMaterialListBId = myMaterialList->currentRow();
1186
1187   }
1188 }
1189
1190 /*!
1191   \brief Called when user material is renamed by the user
1192 */
1193 void GEOMToolsGUI_MaterialPropertiesDlg::onItemChanged( QListWidgetItem* item )
1194 {
1195   QString newName = item->text();
1196   QString uniqueName = findUniqueName( newName, item );
1197   if ( uniqueName != newName ) {
1198     myMaterialList->blockSignals( true );
1199     item->setText( uniqueName );
1200     myMaterialList->blockSignals( false );
1201   }
1202   onChanged();
1203 }                               
1204
1205 /*!
1206   \brief Called when widget effect is changed
1207 */
1208 void GEOMToolsGUI_MaterialPropertiesDlg::onReflectionTypeToggled( bool theIsOn )
1209 {
1210   QGroupBox* anObj = (QGroupBox*)sender();
1211
1212   // Set an empty values for color and coefficient
1213   // of the checked/unchecked reflection type
1214   QColor c;
1215
1216
1217   // Make changes on front material tab
1218   if ( anObj == myAmbientGroupF ) {    
1219     myAmbientColorF->setColor( c );
1220     myAmbientCoefntF->setValue( 0.0 );
1221   }
1222   else if ( anObj == myDiffuseGroupF ) {
1223     myDiffuseColorF->setColor( c );
1224     myDiffuseCoefntF->setValue( 0.0 );
1225   }
1226   else if ( anObj == mySpecularGroupF ) {
1227     mySpecularColorF->setColor( c );
1228     mySpecularCoefntF->setValue( 0.0 );
1229   }
1230   else if ( anObj == myEmissionGroupF ) {
1231     myEmissionColorF->setColor( c );
1232     myEmissionCoefntF->setValue( 0.0 );
1233   }
1234
1235   // Make changes on back material tab    
1236   if ( anObj == myAmbientGroupB ) {    
1237     myAmbientColorB->setColor( c );
1238     myAmbientCoefntB->setValue( 0.0 );
1239   }
1240   else if ( anObj == myDiffuseGroupB ) {
1241     myDiffuseColorB->setColor( c );
1242     myDiffuseCoefntB->setValue( 0.0 );
1243   }
1244   else if ( anObj == mySpecularGroupB ) {
1245     mySpecularColorB->setColor( c );
1246     mySpecularCoefntB->setValue( 0.0 );
1247   }
1248   else if ( anObj == myEmissionGroupB ) {
1249     myEmissionColorB->setColor( c );
1250     myEmissionCoefntB->setValue( 0.0 );
1251   }
1252   
1253   emit( changed() );
1254 }