Improve material management (performance issues)

This commit is contained in:
vsr 2013-02-04 12:22:30 +00:00
parent 52f5e304e4
commit a81cc8e71d
8 changed files with 328 additions and 221 deletions

View File

@ -213,28 +213,21 @@ int GEOMGUI_Selection::typeId( const int index ) const
bool GEOMGUI_Selection::isVisible( const int index ) const bool GEOMGUI_Selection::isVisible( const int index ) const
{ {
#ifdef USE_VISUAL_PROP_MAP
QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::Visibility ) );
if ( v.canConvert( QVariant::Bool ) )
return v.toBool();
#endif
bool res = false; bool res = false;
#ifdef USE_VISUAL_PROP_MAP GEOM::GEOM_Object_var obj = getObject( index );
bool found = false; SALOME_View* view = GEOM_Displayer::GetActiveView();
QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::Visibility ) ); if ( !CORBA::is_nil( obj ) && view ) {
if ( v.canConvert( QVariant::Bool ) ) { Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
res = v.toBool(); res = view->isVisible( io );
found = true;
} }
if ( !found ) {
#endif
GEOM::GEOM_Object_var obj = getObject( index );
SALOME_View* view = GEOM_Displayer::GetActiveView();
if ( !CORBA::is_nil( obj ) && view ) {
Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
res = view->isVisible( io );
}
#ifdef USE_VISUAL_PROP_MAP
}
#endif
return res; return res;
} }
@ -288,6 +281,7 @@ QString GEOMGUI_Selection::displayMode( const int index ) const
{ {
QString res; QString res;
QString viewType = activeViewType(); QString viewType = activeViewType();
#ifdef USE_VISUAL_PROP_MAP #ifdef USE_VISUAL_PROP_MAP
QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::DisplayMode ) ); QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::DisplayMode ) );
if ( v.canConvert( QVariant::Int ) ) { if ( v.canConvert( QVariant::Int ) ) {
@ -297,113 +291,103 @@ QString GEOMGUI_Selection::displayMode( const int index ) const
} else if ( viewType == SVTK_Viewer::Type() ) { } else if ( viewType == SVTK_Viewer::Type() ) {
VTK_DISPLAY_MODE_TO_STRING( res, dm ); VTK_DISPLAY_MODE_TO_STRING( res, dm );
} }
return res;
} }
if ( res.isEmpty() ) {
#endif #endif
SALOME_View* view = GEOM_Displayer::GetActiveView();
if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) { SALOME_View* view = GEOM_Displayer::GetActiveView();
SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
if ( prs ) { SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC if ( prs ) {
SOCC_Prs* occPrs = (SOCC_Prs*) prs; if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
AIS_ListOfInteractive lst; SOCC_Prs* occPrs = (SOCC_Prs*) prs;
occPrs->GetObjects( lst ); AIS_ListOfInteractive lst;
if ( lst.Extent() ) { occPrs->GetObjects( lst );
Handle(AIS_InteractiveObject) io = lst.First(); if ( lst.Extent() ) {
if ( !io.IsNull() ) { Handle(AIS_InteractiveObject) io = lst.First();
int dm; if ( !io.IsNull() ) {
Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io); int dm;
if(!aSh.IsNull()) { Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode(); if(!aSh.IsNull()) {
} else { dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode();
dm = io->DisplayMode(); } else {
} dm = io->DisplayMode();
}
OCC_DISPLAY_MODE_TO_STRING( res, dm );
if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
desktop()->activeWindow()->getViewManager()->getViewModel();
Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
dm = ic->DisplayMode();
OCC_DISPLAY_MODE_TO_STRING( res, dm ); OCC_DISPLAY_MODE_TO_STRING( res, dm );
if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
desktop()->activeWindow()->getViewManager()->getViewModel();
Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
dm = ic->DisplayMode();
OCC_DISPLAY_MODE_TO_STRING( res, dm );
}
} }
} }
} }
else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
if ( lst ) {
lst->InitTraversal();
vtkActor* actor = lst->GetNextActor();
if ( actor ) {
SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
if ( salActor ) {
int dm = salActor->getDisplayMode();
VTK_DISPLAY_MODE_TO_STRING( res, dm );
} // if ( salome actor )
} // if ( actor )
} // if ( lst == vtkPrs->GetObjects() )
} // if VTK
} }
else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
if ( lst ) {
lst->InitTraversal();
vtkActor* actor = lst->GetNextActor();
if ( actor ) {
SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
if ( salActor ) {
int dm = salActor->getDisplayMode();
VTK_DISPLAY_MODE_TO_STRING( res, dm );
} // if ( salome actor )
} // if ( actor )
} // if ( lst == vtkPrs->GetObjects() )
} // if VTK
} }
#ifdef USE_VISUAL_PROP_MAP
} }
#endif
return res; return res;
} }
bool GEOMGUI_Selection::isVectorsMode( const int index ) const bool GEOMGUI_Selection::isVectorsMode( const int index ) const
{ {
bool res = false;
#ifdef USE_VISUAL_PROP_MAP #ifdef USE_VISUAL_PROP_MAP
bool found = false;
QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::EdgesDirection ) ); QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::EdgesDirection ) );
if ( v.canConvert( QVariant::Bool ) ) { if ( v.canConvert( QVariant::Bool ) )
res = v.toBool(); return v.toBool();
found = true;
}
if ( !found ) {
#endif #endif
SALOME_View* view = GEOM_Displayer::GetActiveView();
QString viewType = activeViewType(); bool res = false;
if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); SALOME_View* view = GEOM_Displayer::GetActiveView();
if ( prs ) { QString viewType = activeViewType();
if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
SOCC_Prs* occPrs = (SOCC_Prs*) prs; SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
AIS_ListOfInteractive lst; if ( prs ) {
occPrs->GetObjects( lst ); if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
if ( lst.Extent() ) { SOCC_Prs* occPrs = (SOCC_Prs*) prs;
Handle(AIS_InteractiveObject) io = lst.First(); AIS_ListOfInteractive lst;
if ( !io.IsNull() ) { occPrs->GetObjects( lst );
Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io); if ( lst.Extent() ) {
if ( !aSh.IsNull() ) Handle(AIS_InteractiveObject) io = lst.First();
res = aSh->isShowVectors(); if ( !io.IsNull() ) {
} Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
} if ( !aSh.IsNull() )
} else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK res = aSh->isShowVectors();
SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
if ( lst ) {
lst->InitTraversal();
vtkActor* actor = lst->GetNextActor();
if ( actor ) {
GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
if ( aGeomActor )
res = aGeomActor->GetVectorMode();
}
} }
} }
} }
else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
if ( lst ) {
lst->InitTraversal();
vtkActor* actor = lst->GetNextActor();
if ( actor ) {
GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
if ( aGeomActor )
res = aGeomActor->GetVectorMode();
}
}
}
} }
#ifdef USE_VISUAL_PROP_MAP
} }
#endif
return res; return res;
} }
@ -548,35 +532,31 @@ QString GEOMGUI_Selection::selectionMode() const
return ""; return "";
} }
bool GEOMGUI_Selection::topLevel( const int index ) const { bool GEOMGUI_Selection::topLevel( const int index ) const
{
#ifdef USE_VISUAL_PROP_MAP
QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::TopLevel ) );
if ( v.canConvert<bool>() )
return v.toBool();
#endif
bool res = false; bool res = false;
#ifdef USE_VISUAL_PROP_MAP SALOME_View* view = GEOM_Displayer::GetActiveView();
bool found = false; QString viewType = activeViewType();
QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::TopLevel ) ); if ( view && viewType == OCCViewer_Viewer::Type() ) {
if ( v.canConvert<bool>() ) { SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
res = v.toBool(); if ( prs ) {
found = true; if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
} SOCC_Prs* occPrs = (SOCC_Prs*) prs;
AIS_ListOfInteractive lst;
if ( !found ) { occPrs->GetObjects( lst );
#endif if ( lst.Extent() ) {
SALOME_View* view = GEOM_Displayer::GetActiveView(); Handle(AIS_InteractiveObject) io = lst.First();
QString viewType = activeViewType(); if ( !io.IsNull() ) {
if ( view && viewType == OCCViewer_Viewer::Type() ) { Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); if ( !aSh.IsNull() )
if ( prs ) { res = (bool)aSh->isTopLevel();
if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
SOCC_Prs* occPrs = (SOCC_Prs*) prs;
AIS_ListOfInteractive lst;
occPrs->GetObjects( lst );
if ( lst.Extent() ) {
Handle(AIS_InteractiveObject) io = lst.First();
if ( !io.IsNull() ) {
Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
if ( !aSh.IsNull() )
res = (bool)aSh->isTopLevel();
}
} }
} }
} }
@ -585,57 +565,55 @@ bool GEOMGUI_Selection::topLevel( const int index ) const {
return res; return res;
} }
bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const{ bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const
bool res = false; {
#ifdef USE_VISUAL_PROP_MAP #ifdef USE_VISUAL_PROP_MAP
bool found = false; QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) );
QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) ); if ( v.canConvert<QString>() ) {
if ( v.canConvert<QString>() ) { Material_Model material;
Material_Model material; material.fromProperties( v.toString() );
material.fromProperties( v.toString() ); return material.isPhysical();
res = material.isPhysical(); }
found = true;
}
if ( !found ) {
#endif #endif
SALOME_View* view = GEOM_Displayer::GetActiveView();
QString viewType = activeViewType(); bool res = false;
if ( view ) {
SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() ); SALOME_View* view = GEOM_Displayer::GetActiveView();
if ( prs ) { QString viewType = activeViewType();
if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC if ( view ) {
SOCC_Prs* occPrs = (SOCC_Prs*) prs; SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() );
AIS_ListOfInteractive lst; if ( prs ) {
occPrs->GetObjects( lst ); if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
if ( lst.Extent() ) { SOCC_Prs* occPrs = (SOCC_Prs*) prs;
Handle(AIS_InteractiveObject) io = lst.First(); AIS_ListOfInteractive lst;
if ( !io.IsNull() ) { occPrs->GetObjects( lst );
Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io); if ( lst.Extent() ) {
if ( !aSh.IsNull() ) Handle(AIS_InteractiveObject) io = lst.First();
res = (bool) aSh->Attributes()->ShadingAspect()-> if ( !io.IsNull() ) {
Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC ); Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
} if ( !aSh.IsNull() )
} res = (bool) aSh->Attributes()->ShadingAspect()->
} Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC );
else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK }
SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs ); }
vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0; }
if ( lst ) { else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
lst->InitTraversal(); SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
vtkActor* actor = lst->GetNextActor(); vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
if ( actor ) { if ( lst ) {
GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor ); lst->InitTraversal();
if ( aGeomGActor ) { vtkActor* actor = lst->GetNextActor();
GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty()); if ( actor ) {
res = mat->GetPhysical(); GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor );
} // if ( salome actor ) if ( aGeomGActor ) {
} // if ( actor ) GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty());
} // if ( lst == vtkPrs->GetObjects() ) res = mat->GetPhysical();
} } // if ( salome actor )
} } // if ( actor )
} } // if ( lst == vtkPrs->GetObjects() )
} }
return res; }
}
return res;
} }

View File

@ -207,6 +207,8 @@ GeometryGUI::GeometryGUI() :
myDisplayer = 0; myDisplayer = 0;
myLocalSelectionMode = GEOM_ALLOBJECTS; myLocalSelectionMode = GEOM_ALLOBJECTS;
connect( Material_ResourceMgr::resourceMgr(), SIGNAL( changed() ), this, SLOT( updateMaterials() ) );
} }
//======================================================================= //=======================================================================
@ -603,16 +605,6 @@ void GeometryGUI::OnGUIEvent( int id, const QVariant& theParam )
library->OnGUIEvent( id, desk ); library->OnGUIEvent( id, desk );
else else
library->OnGUIEvent( id, desk, theParam); library->OnGUIEvent( id, desk, theParam);
// Update a list of materials for "Preferences" dialog
if ( id == GEOMOp::OpMaterialProperties ) {
LightApp_Preferences* pref = preferences();
if ( pref ) {
Material_ResourceMgr aMatResMgr;
setPreferenceProperty( pref->rootItem()->findItem( tr( "PREF_MATERIAL" ), true )->id(),
"strings",
aMatResMgr.materials() );
}
}
} }
else else
SUIT_MessageBox::critical( desk, tr( "GEOM_ERROR" ), tr( "GEOM_ERR_LIB_NOT_FOUND" ), tr( "GEOM_BUT_OK" ) ); SUIT_MessageBox::critical( desk, tr( "GEOM_ERROR" ), tr( "GEOM_ERR_LIB_NOT_FOUND" ), tr( "GEOM_BUT_OK" ) );
@ -1734,18 +1726,21 @@ void GeometryGUI::contextMenuPopup( const QString& client, QMenu* menu, QString&
QString curModel = ""; QString curModel = "";
if ( v.canConvert<QString>() ) curModel = v.toString(); if ( v.canConvert<QString>() ) curModel = v.toString();
// get list of all predefined materials // get list of all predefined materials
Material_ResourceMgr aMatResMgr; QStringList materials = Material_ResourceMgr::resourceMgr()->materials();
QStringList matNameList = aMatResMgr.materials(); bool found = false;
foreach ( QString name, matNameList ) foreach ( QString material, materials )
{ {
QAction* menAct = matMenu->addAction( name ); QAction* menAct = matMenu->addAction( material );
connect(menAct, SIGNAL( toggled( bool ) ), signalMapper, SLOT( map() ) ); connect(menAct, SIGNAL( toggled( bool ) ), signalMapper, SLOT( map() ) );
signalMapper->setMapping( menAct, name ); signalMapper->setMapping( menAct, material );
menAct->setCheckable( true ); menAct->setCheckable( true );
// Set checked if this material is current // Set checked if this material is current
Material_Model aModel; Material_Model aModel;
aModel.fromResources( name ); aModel.fromResources( material );
menAct->setChecked( aModel.toProperties() == curModel ); if ( !found && aModel.toProperties() == curModel ) {
menAct->setChecked( true );
found = true;
}
} }
matMenu->insertAction( matMenu->addSeparator(), action( GEOMOp::OpPredefMaterCustom ) ); matMenu->insertAction( matMenu->addSeparator(), action( GEOMOp::OpPredefMaterCustom ) );
matMenu->insertSeparator( action( GEOMOp::OpPredefMaterCustom ) ); matMenu->insertSeparator( action( GEOMOp::OpPredefMaterCustom ) );
@ -1818,12 +1813,12 @@ void GeometryGUI::createPreferences()
int defl = addPreference( tr( "PREF_DEFLECTION" ), genGroup, int defl = addPreference( tr( "PREF_DEFLECTION" ), genGroup,
LightApp_Preferences::DblSpin, "Geometry", "deflection_coeff" ); LightApp_Preferences::DblSpin, "Geometry", "deflection_coeff" );
int predef_materials = addPreference( tr( "PREF_PREDEF_MATERIALS" ), genGroup, addPreference( tr( "PREF_PREDEF_MATERIALS" ), genGroup,
LightApp_Preferences::Bool, "Geometry", "predef_materials" ); LightApp_Preferences::Bool, "Geometry", "predef_materials" );
int material = addPreference( tr( "PREF_MATERIAL" ), genGroup, int material = addPreference( tr( "PREF_MATERIAL" ), genGroup,
LightApp_Preferences::Selector, LightApp_Preferences::Selector,
"Geometry", "material" ); "Geometry", "material" );
addPreference( tr( "PREF_EDITGROUP_COLOR" ), genGroup, addPreference( tr( "PREF_EDITGROUP_COLOR" ), genGroup,
LightApp_Preferences::Color, "Geometry", "editgroup_color" ); LightApp_Preferences::Color, "Geometry", "editgroup_color" );
@ -1942,11 +1937,8 @@ void GeometryGUI::createPreferences()
setPreferenceProperty( defl, "step", 1.0e-04 ); setPreferenceProperty( defl, "step", 1.0e-04 );
setPreferenceProperty( defl, "precision", 6 ); setPreferenceProperty( defl, "precision", 6 );
// Set property for 'Show predefined materials'
setPreferenceProperty( predef_materials, "eval", true);
// Set property for default material // Set property for default material
Material_ResourceMgr aMatResMgr; setPreferenceProperty( material, "strings", Material_ResourceMgr::resourceMgr()->materials() );
setPreferenceProperty( material, "strings", aMatResMgr.materials() );
// Set property vertex marker type // Set property vertex marker type
QList<QVariant> aMarkerTypeIndicesList; QList<QVariant> aMarkerTypeIndicesList;
@ -2396,3 +2388,22 @@ bool GeometryGUI::renameObject( const QString& entry, const QString& name)
} }
return result; return result;
} }
void GeometryGUI::updateMaterials()
{
LightApp_Preferences* pref = preferences();
if ( pref ) {
QStringList materials = Material_ResourceMgr::resourceMgr()->materials();
QString currentMaterial = SUIT_Session::session()->resourceMgr()->stringValue( "Geometry", "material" );
if ( !materials.contains( currentMaterial ) )
// user material set as default in the preferences, might be removed
SUIT_Session::session()->resourceMgr()->setValue( "Geometry", "material", QString( "Plastic" ) );
QtxPreferenceItem* prefItem = pref->rootItem()->findItem( tr( "PREF_MATERIAL" ), true );
if ( prefItem ) {
setPreferenceProperty( prefItem->id(),
"strings", materials );
prefItem->retrieve();
}
}
}

View File

@ -152,6 +152,7 @@ private slots:
void onWindowActivated( SUIT_ViewWindow* ); void onWindowActivated( SUIT_ViewWindow* );
void onViewAboutToShow(); void onViewAboutToShow();
void OnSetMaterial( const QString& ); void OnSetMaterial( const QString& );
void updateMaterials();
signals : signals :
void SignalDeactivateActiveDialog(); void SignalDeactivateActiveDialog();

View File

@ -36,6 +36,12 @@ dist_libMaterial_la_SOURCES = \
Material_Model.cxx \ Material_Model.cxx \
Material_ResourceMgr.cxx Material_ResourceMgr.cxx
MOC_FILES = \
Material_ResourceMgr_moc.cxx
nodist_libMaterial_la_SOURCES = \
$(MOC_FILES)
# additional information to compile and link file # additional information to compile and link file
libMaterial_la_CPPFLAGS = \ libMaterial_la_CPPFLAGS = \
$(QT_INCLUDES) \ $(QT_INCLUDES) \

View File

@ -25,6 +25,8 @@
#include "GEOM_VTKPropertyMaterial.hxx" #include "GEOM_VTKPropertyMaterial.hxx"
#include "Material_ResourceMgr.h" #include "Material_ResourceMgr.h"
#include <QMutexLocker>
/*! /*!
\brief Constructor \brief Constructor
@ -184,7 +186,7 @@ QString Material_Model::toProperties()
\param resMgr resource manager (if not specified, new resources manager is created) \param resMgr resource manager (if not specified, new resources manager is created)
\sa toResources() \sa toResources()
*/ */
void Material_Model::fromResources( const QString& material, QtxResourceMgr* resMgr ) void Material_Model::fromResources( const QString& material, Material_ResourceMgr* resMgr )
{ {
static QString common = "[common]"; static QString common = "[common]";
@ -194,15 +196,22 @@ void Material_Model::fromResources( const QString& material, QtxResourceMgr* res
// material name is not specified: use default values // material name is not specified: use default values
if ( material.isEmpty() ) return; if ( material.isEmpty() ) return;
bool ownResourcesMgr = resMgr == 0; if ( !resMgr )
resMgr = Material_ResourceMgr::resourceMgr();
if ( ownResourcesMgr )
resMgr = new Material_ResourceMgr(); // lock resources manager
QMutexLocker lock( &resMgr->myMutex );
// read common section // read common section
if ( material != common && resMgr->hasSection( common ) ) if ( material != common && resMgr->hasSection( common ) )
fromResources( common, resMgr ); read( common, resMgr );
// read material section
read( material, resMgr );
}
void Material_Model::read( const QString& material, Material_ResourceMgr* resMgr )
{
// physical // physical
if ( resMgr->hasValue( material, "physical" ) ) { if ( resMgr->hasValue( material, "physical" ) ) {
setPhysical( resMgr->booleanValue( material, "physical" ) ); setPhysical( resMgr->booleanValue( material, "physical" ) );
@ -276,9 +285,6 @@ void Material_Model::fromResources( const QString& material, QtxResourceMgr* res
if ( resMgr->hasValue( material, "emissive" ) ) { if ( resMgr->hasValue( material, "emissive" ) ) {
setReflection( Emissive, resMgr->booleanValue( material, "emissive" ) ); setReflection( Emissive, resMgr->booleanValue( material, "emissive" ) );
} }
if ( ownResourcesMgr )
delete resMgr;
} }
/*! /*!
@ -287,9 +293,12 @@ void Material_Model::fromResources( const QString& material, QtxResourceMgr* res
\param resMgr resource manager \param resMgr resource manager
\sa fromResources() \sa fromResources()
*/ */
void Material_Model::toResources( const QString& material, QtxResourceMgr* resMgr ) void Material_Model::toResources( const QString& material, Material_ResourceMgr* resMgr )
{ {
if ( resMgr && !material.isEmpty() ) { if ( resMgr && !material.isEmpty() ) {
// lock resources manager
QMutexLocker lock( &resMgr->myMutex );
// remove resources section (to clean-up all previous properties) // remove resources section (to clean-up all previous properties)
resMgr->remove( material ); resMgr->remove( material );

View File

@ -32,7 +32,7 @@
#include <Graphic3d_MaterialAspect.hxx> #include <Graphic3d_MaterialAspect.hxx>
class QtxResourceMgr; class Material_ResourceMgr;
class GEOM_VTKPropertyMaterial; class GEOM_VTKPropertyMaterial;
class MATERIAL_SALOME_EXPORT Material_Model class MATERIAL_SALOME_EXPORT Material_Model
@ -51,8 +51,8 @@ public:
void fromProperties( const QString& ); void fromProperties( const QString& );
QString toProperties(); QString toProperties();
void fromResources( const QString& = QString(), QtxResourceMgr* = 0 ); void fromResources( const QString& = QString(), Material_ResourceMgr* = 0 );
void toResources( const QString&, QtxResourceMgr* ); void toResources( const QString&, Material_ResourceMgr* );
bool isPhysical() const; bool isPhysical() const;
void setPhysical( bool ); void setPhysical( bool );
@ -77,6 +77,7 @@ public:
private: private:
void init(); void init();
void read( const QString&, Material_ResourceMgr* );
private: private:
typedef struct { typedef struct {

View File

@ -22,6 +22,31 @@
#include "Material_ResourceMgr.h" #include "Material_ResourceMgr.h"
#include <QFileSystemWatcher>
#include <QThread>
/*!
\class Material_ResourceMgr::Updater
\brief Updates the contents of the resource manager as soon as
user materials database file is changed
\internal
*/
class Material_ResourceMgr::Updater : public QThread
{
public:
Material_ResourceMgr* myResourceMgr;
Updater( Material_ResourceMgr* resMgr ) : myResourceMgr( resMgr )
{
start();
}
void run()
{
QMutexLocker lock( &myResourceMgr->myMutex );
myResourceMgr->clear();
myResourceMgr->load();
}
};
/*! /*!
\class Material_ResourceMgr \class Material_ResourceMgr
\brief Material properties resources manager. \brief Material properties resources manager.
@ -41,7 +66,8 @@
\brief Constructor \brief Constructor
*/ */
Material_ResourceMgr::Material_ResourceMgr() Material_ResourceMgr::Material_ResourceMgr()
: QtxResourceMgr( "SalomeMaterial", "%1Config" ) : QtxResourceMgr( "SalomeMaterial", "%1Config" ),
myWatcher( 0 )
{ {
if ( dirList().isEmpty() && ::getenv( "GEOM_ROOT_DIR" ) ) if ( dirList().isEmpty() && ::getenv( "GEOM_ROOT_DIR" ) )
setDirList( QStringList() << Qtx::addSlash( ::getenv( "GEOM_ROOT_DIR" ) ) + "share/salome/resources/geom" ); setDirList( QStringList() << Qtx::addSlash( ::getenv( "GEOM_ROOT_DIR" ) ) + "share/salome/resources/geom" );
@ -53,6 +79,24 @@ Material_ResourceMgr::Material_ResourceMgr()
*/ */
Material_ResourceMgr::~Material_ResourceMgr() Material_ResourceMgr::~Material_ResourceMgr()
{ {
watchUserFile( false );
}
/*!
\brief Get shared instance of resources manager
This instance of resource manager is global for the application;
it watches for changes in the user materials database file to
maintain the fresh version of the materials data.
*/
Material_ResourceMgr* Material_ResourceMgr::resourceMgr()
{
static Material_ResourceMgr* resMgr = 0;
if ( !resMgr ) {
resMgr = new Material_ResourceMgr();
resMgr->watchUserFile( true );
}
return resMgr;
} }
/*! /*!
@ -63,6 +107,8 @@ Material_ResourceMgr::~Material_ResourceMgr()
*/ */
QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort ) QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort )
{ {
QMutexLocker lock( &myMutex );
// store original working mode // store original working mode
WorkingMode m = workingMode(); WorkingMode m = workingMode();
@ -114,3 +160,34 @@ QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort
return result; return result;
} }
/*!
\brief Start/stop this resource manager watching the user materials database file.
\internal
*/
void Material_ResourceMgr::watchUserFile( bool on )
{
if ( on ) {
if ( !myWatcher ) {
myWatcher = new QFileSystemWatcher( this );
myWatcher->addPath( userFileName( appName() ) );
connect( myWatcher, SIGNAL( fileChanged( QString ) ), this, SLOT( update() ) );
}
}
else {
if ( myWatcher ) {
delete myWatcher;
myWatcher = 0;
}
}
}
/*!
\brief Update user database slot
\internal
*/
void Material_ResourceMgr::update()
{
Updater( this ).wait();
emit changed();
}

View File

@ -25,10 +25,18 @@
#include "Material.h" #include "Material.h"
#include <QObject>
#include <QMutex>
#include <QtxResourceMgr.h> #include <QtxResourceMgr.h>
class MATERIAL_SALOME_EXPORT Material_ResourceMgr : public QtxResourceMgr class QFileSystemWatcher;
class MATERIAL_SALOME_EXPORT Material_ResourceMgr : public QObject, public QtxResourceMgr
{ {
Q_OBJECT;
class Updater;
public: public:
//! Material type //! Material type
typedef enum { typedef enum {
@ -40,8 +48,24 @@ public:
Material_ResourceMgr(); Material_ResourceMgr();
~Material_ResourceMgr(); ~Material_ResourceMgr();
static Material_ResourceMgr* resourceMgr();
QStringList materials( MaterialType = All, bool = true ); QStringList materials( MaterialType = All, bool = true );
signals:
void changed();
private:
void watchUserFile( bool );
private slots:
void update();
private:
QFileSystemWatcher* myWatcher;
QMutex myMutex;
friend class Material_Model;
}; };
#endif // MATERIAL_RESOURCEMGR_H #endif // MATERIAL_RESOURCEMGR_H