Back to index

salome-geom  6.5.0
GEOMToolsGUI_MaterialPropertiesDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File   : GEOMToolsGUI_MaterialPropertiesDlg.cxx
00021 // Author : Margarita KARPUNINA, Open CASCADE S.A.S. (margarita.karpunina@opencascade.com)
00022 
00023 #include "GEOMToolsGUI_MaterialPropertiesDlg.h"
00024 
00025 #include "GeometryGUI.h"
00026 #include "GEOM_Constants.h"
00027 #include "GEOM_VTKPropertyMaterial.hxx"
00028 #include "GEOMBase.h"
00029 
00030 #include <QtxColorButton.h>
00031 #include <QtxDoubleSpinBox.h>
00032 #include <SUIT_Desktop.h>
00033 #include <SUIT_MessageBox.h>
00034 #include <SUIT_OverrideCursor.h>
00035 #include <SUIT_ResourceMgr.h>
00036 #include <SUIT_Session.h>
00037 #include <SUIT_ViewManager.h>
00038 #include <SALOME_ListIteratorOfListIO.hxx>
00039 #include <OCCViewer_ViewModel.h>
00040 #include <SVTK_ViewModel.h>
00041 #include <SVTK_ViewWindow.h>
00042 #include <SVTK_View.h>
00043 #include <LightApp_SelectionMgr.h>
00044 #include <SalomeApp_Application.h>
00045 #include <SalomeApp_Study.h>
00046 #include <SalomeApp_Tools.h>
00047 
00048 #include <QCheckBox>
00049 #include <QGridLayout>
00050 #include <QHBoxLayout>
00051 #include <QKeyEvent>
00052 #include <QLabel>
00053 #include <QMap>
00054 #include <QMenu>
00055 #include <QPushButton>
00056 #include <QVBoxLayout>
00057 
00058 #define MARGIN  9  // layout spacing
00059 #define SPACING 6  // layout margin
00060 
00070 GEOMToolsGUI_MaterialList::GEOMToolsGUI_MaterialList( QWidget* parent )
00071   : QListWidget( parent )
00072 {}
00073 
00077 void GEOMToolsGUI_MaterialList::contextMenuEvent( QContextMenuEvent* e )
00078 {
00079   emit contextMenu( e );
00080 }
00081 
00093 GEOMToolsGUI_MaterialPropertiesDlg::GEOMToolsGUI_MaterialPropertiesDlg( QWidget* parent )
00094   : QtxDialog( parent, true, true, OK | Close | Apply | Help )
00095 {
00096   // Set title
00097   setWindowTitle( tr( "MATERIAL_PROPERTIES_TLT" ) );
00098 
00099   // main layout
00100   QVBoxLayout* main = new QVBoxLayout( mainFrame() );
00101   main->setMargin( 0 );
00102   main->setSpacing( SPACING );
00103 
00104   // add top-level frame box to enclose all main widgets (excluding buttons)
00105   QFrame* fr = new QFrame( mainFrame() );
00106   fr->setFrameStyle( QFrame::Box | QFrame::Sunken );
00107   main->addWidget( fr );
00108 
00109   // materials list widget
00110   myMaterials = new GEOMToolsGUI_MaterialList( fr );
00111   // properties widget
00112   QWidget* propWidget = new QWidget( fr );
00113 
00114   // layout main widgets
00115   QHBoxLayout* frLayout = new QHBoxLayout( fr );
00116   frLayout->setMargin( MARGIN );
00117   frLayout->setSpacing( SPACING );
00118   frLayout->addWidget( myMaterials );
00119   frLayout->addWidget( propWidget );
00120 
00121   // layout for material properties widgets
00122   QGridLayout* propLayout = new QGridLayout( propWidget );
00123   propLayout->setMargin( 0 );
00124   propLayout->setSpacing( SPACING );
00125 
00126   // current color widgets
00127   myColorLab = new QLabel( tr( "CURRENT_COLOR" ), propWidget );
00128   myColor = new QtxColorButton( propWidget );
00129 
00130   // "physical" material type widgets
00131   myPhysical = new QCheckBox( tr( "PHYSICAL" ), propWidget );
00132 
00133   // reflection components widgets
00134   for ( int i = Material_Model::Ambient; i <= Material_Model::Emissive; i++ )
00135   {
00136     Reflection refl;
00137 
00138     refl.label = new QLabel( tr( QString( "REFLECTION_%1" ).arg( i ).toLatin1().data() ), propWidget );
00139 
00140     refl.color = new QtxColorButton( propWidget );
00141     //refl.color->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
00142 
00143     refl.coef = new QtxDoubleSpinBox( propWidget );
00144     refl.coef->setPrecision( 4 );
00145     refl.coef->setDecimals( 4 ); 
00146     refl.coef->setRange( 0., 1. );
00147     refl.coef->setSingleStep( 0.05 );
00148     refl.coef->setMinimumWidth( 80 );
00149 
00150     refl.enabled = new QCheckBox( tr( "ENABLED" ), propWidget );
00151 
00152     myReflection << refl;
00153   }
00154 
00155   // shininess widgets
00156   QLabel* shininessLab = new QLabel( tr( "SHININESS" ), propWidget );
00157   myShininess = new QtxDoubleSpinBox( propWidget );
00158   myShininess->setPrecision( 4 );
00159   myShininess->setDecimals( 4 ); 
00160   myShininess->setRange( 0., 1. );
00161   myShininess->setSingleStep( 0.05 );
00162 
00163   // separator widgets
00164   QFrame* line1 = new QFrame( propWidget );
00165   line1->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00166   QFrame* line2 = new QFrame( propWidget );
00167   line2->setFrameStyle( QFrame::HLine | QFrame::Sunken );
00168 
00169   // add / remove material buttons
00170   myAddButton = new QPushButton( tr( "ADD_MATERIAL" ),    propWidget );
00171   myDelButton = new QPushButton( tr( "DELETE_MATERIAL" ), propWidget );
00172 
00173   // buttons layout
00174   QHBoxLayout* btnLayout = new QHBoxLayout;
00175   btnLayout->setMargin( 0 );
00176   btnLayout->setSpacing( SPACING );
00177   btnLayout->addWidget( myAddButton );
00178   btnLayout->addWidget( myDelButton );
00179 
00180   // layout all properties widgets together
00181   propLayout->addWidget( myColorLab, 0, 0 );
00182   propLayout->addWidget( myColor,    0, 1 );
00183   propLayout->addWidget( line1, 1, 0, 1, 4 );
00184   propLayout->addWidget( myPhysical,     2, 0, 1, 2 );
00185   for ( int i = Material_Model::Ambient; i <= Material_Model::Emissive; i++ ) {
00186     propLayout->addWidget( myReflection[i].label,   i+3, 0 );
00187     propLayout->addWidget( myReflection[i].color,   i+3, 1 );
00188     propLayout->addWidget( myReflection[i].coef,    i+3, 2 );
00189     propLayout->addWidget( myReflection[i].enabled, i+3, 3 );
00190   }
00191   propLayout->addWidget( shininessLab,   7, 0 );
00192   propLayout->addWidget( myShininess,    7, 2 );
00193   propLayout->addWidget( line2, 8, 0, 1, 4 );
00194   propLayout->setRowStretch( 9, 5 );
00195   propLayout->addLayout( btnLayout, 10, 0, 1, 4 );
00196 
00197   // initialize dialog box
00198   setButtonPosition( Right, Close );
00199 
00200   // fill in the list of materials
00201   QStringList globalMaterials = myResourceMgr.materials( Material_ResourceMgr::Global );
00202   QStringList userMaterials   = myResourceMgr.materials( Material_ResourceMgr::User );
00203 
00204   QListWidgetItem* item;
00205 
00206   // - current material
00207   item = new QListWidgetItem( tr( "CURRENT_MATERIAL" ) );
00208   item->setForeground( QColor( Qt::red ) );
00209   item->setData( TypeRole, QVariant( Current ) );
00210   myMaterials->addItem( item );
00211   // - global materials
00212   foreach( QString material, globalMaterials ) {
00213     item = new QListWidgetItem( material );
00214     item->setForeground( QColor( Qt::blue ) );
00215     item->setData( TypeRole, QVariant( Global ) );
00216     item->setData( NameRole, QVariant( material ) );
00217     myMaterials->addItem( item );
00218   }
00219   // - user materials
00220   foreach ( QString material, userMaterials ) {
00221     item = new QListWidgetItem( material );
00222     item->setData( TypeRole, QVariant( User ) );
00223     item->setData( NameRole, QVariant( material ) );
00224     item->setFlags( item->flags() | Qt::ItemIsEditable );
00225     myMaterials->addItem( item );
00226   }
00227   // install event filter to the materials list to process key press events
00228   myMaterials->installEventFilter( this );
00229 
00230   // connect signals
00231   // note: all widgets, that change material properties, are connected to the common signal
00232   // changed(), instead of connecting directly to the slot - this allows easy temporary blocking 
00233   // of the change signal
00234   connect( myPhysical, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) ); 
00235   for ( int i = Material_Model::Ambient; i <= Material_Model::Emissive; i++ ) {
00236     connect( myReflection[i].color,   SIGNAL( changed( QColor ) ),   this, SIGNAL( changed() ) );
00237     connect( myReflection[i].coef,    SIGNAL( valueChanged( double ) ), this, SIGNAL( changed() ) );
00238     connect( myReflection[i].enabled, SIGNAL( toggled( bool ) ),     this, SIGNAL( changed() ) );
00239   }
00240   connect( myShininess, SIGNAL( valueChanged( double ) ), this, SIGNAL( changed() ) );
00241   connect( myMaterials, SIGNAL( itemSelectionChanged() ),
00242            this,        SLOT( onMaterialChanged() ) );
00243   connect( myMaterials, SIGNAL( itemChanged( QListWidgetItem* ) ),
00244            this,        SLOT( onItemChanged( QListWidgetItem* ) ) );
00245   connect( myMaterials, SIGNAL( contextMenu( QContextMenuEvent* ) ),
00246            this,        SLOT( onContextMenu( QContextMenuEvent* ) ) );
00247   connect( myAddButton, SIGNAL( clicked() ), this, SLOT( onAddMaterial() ) );
00248   connect( myDelButton, SIGNAL( clicked() ), this, SLOT( onDeleteMaterial() ) );
00249   connect( this, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
00250   connect( this, SIGNAL( dlgHelp() ),  this, SLOT( onHelp() ) );
00251   connect( this, SIGNAL( changed() ),  this, SLOT( onChanged() ) );
00252 
00253   // initialize current material model according to the selection
00254   myColor->setColor( SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) ) );
00255   myCurrentModel.fromResources( SUIT_Session::session()->resourceMgr()->stringValue( "Geometry", "material", "Plastic" ) );
00256   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
00257   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00258   LightApp_SelectionMgr* selMgr = app->selectionMgr();
00259   if ( study ) {
00260     SALOME_ListIO selected;
00261     selMgr->selectedObjects( selected );
00262     if ( selected.Extent() > 0 ) {
00263       Handle(SALOME_InteractiveObject) io = selected.First();
00264       if ( !io.IsNull() ) {
00265         SUIT_ViewWindow* window = app->desktop()->activeWindow();
00266         if ( window ) {
00267           int mgrId = window->getViewManager()->getGlobalId();
00268           PropMap propMap = study->getObjectPropMap( mgrId, io->getEntry() );
00269           QString matProp = propMap.value(MATERIAL_PROP).toString();      
00270           if ( !matProp.isEmpty() )
00271             myCurrentModel.fromProperties( matProp );
00272           QColor c = propMap.value(COLOR_PROP).value<QColor>();
00273           if ( c.isValid() )
00274             myColor->setColor( c );
00275         }
00276       }
00277     }
00278   }
00279   
00280   // finally activate current material properties
00281   myMaterials->setCurrentRow( 0 );
00282 }
00283 
00287 GEOMToolsGUI_MaterialPropertiesDlg::~GEOMToolsGUI_MaterialPropertiesDlg()
00288 {
00289 }
00290 
00294 void GEOMToolsGUI_MaterialPropertiesDlg::accept()
00295 {
00296   onApply();
00297   QtxDialog::accept();
00298 }
00299 
00305 bool GEOMToolsGUI_MaterialPropertiesDlg::eventFilter( QObject* o, QEvent* e )
00306 {
00307   // process key press event from materials list
00308   if ( o == myMaterials && e->type() == QEvent::KeyPress ) {
00309     QKeyEvent* ke = (QKeyEvent*)e;
00310     if ( ke->key() == Qt::Key_Delete ) {
00311       onDeleteMaterial();
00312     }
00313   }
00314   return QtxDialog::eventFilter( o, e );
00315 }
00316 
00321 void GEOMToolsGUI_MaterialPropertiesDlg::fromModel( const Material_Model& model )
00322 {
00323   // reflection components
00324   for ( int i = Material_Model::Ambient; i <= Material_Model::Emissive; i++ )
00325   {
00326     myReflection[i].color->setColor( model.color( (Material_Model::ReflectionType)i ) );
00327     myReflection[i].coef->setValue( model.reflection( (Material_Model::ReflectionType)i ) );
00328     myReflection[i].enabled->setChecked( model.hasReflection( (Material_Model::ReflectionType)i ) );
00329   }
00330   
00331   // shininess
00332   myShininess->setValue( model.shininess() );
00333 
00334   // type (physical or no)
00335   myPhysical->setChecked( model.isPhysical() );
00336 }
00337  
00342 void GEOMToolsGUI_MaterialPropertiesDlg::toModel( Material_Model& model ) const
00343 {
00344   // type (physical or no)
00345   model.setPhysical( myPhysical->isChecked() );
00346 
00347   // shininess
00348   model.setShininess( myShininess->value() );
00349 
00350   // reflection components
00351   for ( int i = Material_Model::Ambient; i <= Material_Model::Emissive; i++ )
00352   {
00353     model.setColor     ( (Material_Model::ReflectionType)i, myReflection[i].color->color() );
00354     model.setReflection( (Material_Model::ReflectionType)i, myReflection[i].coef->value() );
00355     model.setReflection( (Material_Model::ReflectionType)i, myReflection[i].enabled->isChecked() );
00356   }
00357 }
00358 
00367 QString GEOMToolsGUI_MaterialPropertiesDlg::findUniqueName( const QString& name, QListWidgetItem* item, bool addSuffix )
00368 {
00369   bool found = false;
00370   int idx = 0;
00371   for( int i = 1; i < myMaterials->count(); i++ ) {
00372     if ( item == myMaterials->item( i ) ) continue;
00373     QString iname = myMaterials->item( i )->text();
00374     if ( iname == name ) {
00375       found = true;
00376     }
00377     else if ( iname.startsWith( name ) ) {
00378       iname = iname.mid( name.length() ).trimmed();
00379       bool ok = false;
00380       int nx = iname.toInt( &ok );
00381       if ( ok ) idx = qMax( idx, nx );
00382     }
00383   }
00384   return found || addSuffix ? QString( "%1 %2" ).arg( name ).arg( idx+1 ) : name;
00385 }
00386 
00390 void GEOMToolsGUI_MaterialPropertiesDlg::onApply()
00391 {  
00392   // save user materials
00393   myResourceMgr.save();
00394 
00395   // store selected material properties in the current model
00396   toModel( myCurrentModel );
00397 
00398   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
00399   LightApp_SelectionMgr* selMgr = app->selectionMgr();
00400   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00401   
00402   if ( !study ) return;
00403 
00404   // get selection
00405   SALOME_ListIO selected;
00406   selMgr->selectedObjects( selected );
00407   if ( selected.IsEmpty() ) return;
00408 
00409   SUIT_ViewWindow* window = app->desktop()->activeWindow();
00410   int mgrId = window->getViewManager()->getGlobalId();
00411 
00412   // convert current material properties to the string representation
00413   QString prop = myCurrentModel.toProperties();
00414  
00415   if ( window && window->getViewManager()->getType() == SVTK_Viewer::Type() ) {
00416     // for VTK viewer
00417     SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
00418     if ( !vtkVW )
00419       return;
00420 
00421     SVTK_View* aView = vtkVW->getView();
00422 
00423     // get VTK material properties from the current model
00424     GEOM_VTKPropertyMaterial* vtkProp = myCurrentModel.getMaterialVTKProperty();
00425 
00426     SUIT_OverrideCursor wc();
00427 
00428     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
00429       // set material property to the presentation
00430       aView->SetMaterial( It.Value(), vtkProp );
00431       // store chosen material in the property map
00432       study->setObjectProperty( mgrId, It.Value()->getEntry(), MATERIAL_PROP, prop );
00433       // set correct color for the non-physical material
00434       if ( !myCurrentModel.isPhysical() ) {
00435         aView->SetColor( It.Value(), myColor->color() );
00436        study->setObjectProperty( mgrId, It.Value()->getEntry(), COLOR_PROP, myColor->color() );
00437       }
00438     }
00439     aView->Repaint();
00440     GeometryGUI::Modified();
00441   }
00442   else if ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {    
00443     // for OCC viewer
00444     OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
00445     if ( !vm )
00446       return;
00447 
00448     Handle(AIS_InteractiveContext) ic = vm->getAISContext();
00449 
00450     // get OCC material aspect from the current model
00451     Graphic3d_MaterialAspect occAspect = myCurrentModel.getMaterialOCCAspect();
00452 
00453     SUIT_OverrideCursor wc();
00454 
00455     for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
00456       Handle(GEOM_AISShape) aisShape = GEOMBase::ConvertIOinGEOMAISShape( It.Value(), true );
00457       if ( !aisShape.IsNull() ) {
00458        // set material property to the presentation
00459        aisShape->SetMaterial( occAspect );
00460        // store chosen material in the property map
00461        study->setObjectProperty( mgrId, It.Value()->getEntry(), MATERIAL_PROP, prop );
00462        // set correct color for the non-physical material
00463        if ( !myCurrentModel.isPhysical() ) {
00464          aisShape->SetShadingColor( SalomeApp_Tools::color( myColor->color() ) );
00465          study->setObjectProperty( mgrId, It.Value()->getEntry(), COLOR_PROP, myColor->color() );
00466          ic->RecomputePrsOnly( aisShape, Standard_False );
00467        }
00468        //if ( aisShape->DisplayMode() != AIS_Shaded/*aisShape->DisplayMode() == GEOM_AISShape::ShadingWithEdges*/)
00469        ic->Redisplay( aisShape, Standard_False );
00470       }
00471     }
00472     ic->UpdateCurrentViewer();
00473     GeometryGUI::Modified();
00474   }
00475 }
00476 
00480 void GEOMToolsGUI_MaterialPropertiesDlg::onHelp()
00481 {
00482   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
00483   app->onHelpContextModule( "GEOM", "material_page.html" );
00484 }
00485 
00489 void GEOMToolsGUI_MaterialPropertiesDlg::onMaterialChanged()
00490 {
00491   // get currently selected item
00492   QListWidgetItem* item = myMaterials->currentItem();
00493   if ( !item ) return;
00494 
00495   bool blocked = blockSignals( true );
00496 
00497   int type = item->data( TypeRole ).toInt();
00498   if ( type == Current ) {
00499     // initialize widgets from current material model
00500     fromModel( myCurrentModel );
00501   }
00502   else {
00503     // initialize widgets from chosen material model (using resources manager)
00504     Material_Model model;
00505     model.fromResources( item->data( NameRole ).toString(), &myResourceMgr );
00506     fromModel( model );
00507   }
00508 
00509   blockSignals( blocked );
00510 
00511   // update buttons state
00512   updateState();
00513 }
00514 
00518 void GEOMToolsGUI_MaterialPropertiesDlg::onChanged()
00519 {
00520   // get currently selected item
00521   QListWidgetItem* item = myMaterials->currentItem();
00522   int type = item->data( TypeRole ).toInt();
00523   if ( !item ) return;
00524 
00525   if ( type == Current ) {    
00526     // for the current model do not perform any actions except store changed values
00527     toModel( myCurrentModel );
00528   }
00529   else if ( type == User ) {    
00530     // for the user model, simply store the changes in the resource manager
00531     Material_Model model;
00532     toModel( model );
00533     // check if the user model is renamed
00534     QString oldName = item->data( NameRole ).toString(), newName = item->text();
00535     if ( oldName == newName ) {
00536       // model is not renamed: store data using current name
00537       model.toResources( oldName, &myResourceMgr );
00538     }
00539     else {
00540       // model is renamed: remove model with old name and store model using new name
00541       myResourceMgr.remove( oldName );
00542       model.toResources( newName, &myResourceMgr );
00543       item->setData( NameRole, newName );
00544     }
00545   }
00546   else {
00547     // it is no allowed to change global material
00548     // user is asked about creating of a new user material model based on the currently selected one
00549     if ( SUIT_MessageBox::question( this,
00550                                 tr( "GEOM_WRN_WARNING" ),
00551                                 tr( "QUE_CREATE_NEW_MATERIAL" ),
00552                                 QMessageBox::Yes | QMessageBox::No,
00553                                 QMessageBox::Yes ) == QMessageBox::Yes ) {
00554       // user has chosen creation of new user model
00555       onAddMaterial();
00556     }
00557     else {
00558       // user has rejected creation of new user model: revert changes
00559       bool blocked = blockSignals( true );
00560       Material_Model model;
00561       model.fromResources( item->data( NameRole ).toString(), &myResourceMgr );
00562       fromModel( model );
00563       blockSignals( blocked );
00564     }
00565   }
00566 
00567   // update buttons state
00568   updateState();
00569 }
00570 
00574 void GEOMToolsGUI_MaterialPropertiesDlg::onItemChanged( QListWidgetItem* item )
00575 {
00576   // check new name to be unique (add suffix if necessary)
00577   QString newName = item->text();
00578   QString uniqueName = findUniqueName( newName, item );
00579   if ( uniqueName != newName ) {
00580     bool blocked = myMaterials->blockSignals( true );
00581     item->setText( uniqueName );
00582     myMaterials->blockSignals( blocked );
00583   }
00584   onChanged();
00585 }                               
00586 
00590 void GEOMToolsGUI_MaterialPropertiesDlg::onContextMenu( QContextMenuEvent* e )
00591 {
00592   QListWidgetItem* item = myMaterials->itemAt( e->pos() );
00593   QMap<QAction*, int> actionMap;
00594   QMenu m;
00595   // rename
00596   if ( item && item->data( TypeRole ).toInt() == User ) {
00597     actionMap[ m.addAction( tr( "RENAME_MATERIAL" ) ) ] = 0;
00598     m.addSeparator();
00599   }
00600   // add user material
00601   actionMap[ m.addAction( tr( "ADD_MATERIAL" ) ) ] = 1;
00602   // delete user material
00603   if ( item && item->data( TypeRole ).toInt() == User ) {
00604     actionMap[ m.addAction( tr( "DELETE_MATERIAL" ) ) ] = 2;
00605   }
00606   QAction* a = m.exec( e->globalPos() );
00607   switch( actionMap[ a ] ) {
00608   case 0:
00609     // rename
00610     myMaterials->editItem( item );
00611     break;
00612   case 1:
00613     // add user material
00614     onAddMaterial();
00615     break;
00616   case 2:
00617     // delete user material
00618     onDeleteMaterial();
00619     break;
00620   default:
00621     break;
00622   }
00623 }
00624 
00628 void GEOMToolsGUI_MaterialPropertiesDlg::onDeleteMaterial()
00629 {
00630   QListWidgetItem* item = myMaterials->currentItem();
00631   if ( item && item->data( TypeRole ).toInt() == User ) {
00632     if ( SUIT_MessageBox::question( this,
00633                                 tr( "GEOM_WRN_WARNING" ),
00634                                 tr( "QUE_REMOVE_MATERIAL" ).arg( item->text() ),
00635                                 QMessageBox::Yes | QMessageBox::No,
00636                                 QMessageBox::Yes ) == QMessageBox::Yes ) {
00637       myResourceMgr.remove( item->data( NameRole ).toString() );
00638       delete item;
00639     }
00640   }
00641 }
00642 
00646 void GEOMToolsGUI_MaterialPropertiesDlg::onAddMaterial()
00647 {
00648   QString newName = findUniqueName( tr( "CUSTOM_MATERIAL" ), 0, true );
00649   QListWidgetItem* item = new QListWidgetItem( newName );
00650   item->setData( TypeRole, QVariant( User ) );
00651   item->setData( NameRole, QVariant( newName ) );
00652   item->setFlags( item->flags() | Qt::ItemIsEditable );
00653   myMaterials->addItem( item );
00654   
00655   Material_Model model;
00656   toModel( model );
00657   model.toResources( newName, &myResourceMgr );
00658   myMaterials->setCurrentItem( item );
00659   myMaterials->editItem( item );
00660 }
00661 
00665 void GEOMToolsGUI_MaterialPropertiesDlg::updateState()
00666 {
00667   QListWidgetItem* item = myMaterials->currentItem();
00668   myDelButton->setEnabled( item && item->data( TypeRole ).toInt() == User );
00669   myColorLab->setEnabled( !myPhysical->isChecked() );
00670   myColor->setEnabled( !myPhysical->isChecked() );
00671   myReflection[0].color->setEnabled( myPhysical->isChecked() );
00672 }