From: vsr Date: Mon, 4 Feb 2013 12:22:30 +0000 (+0000) Subject: Improve material management (performance issues) X-Git-Tag: V6_main_FINAL~45 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=a81cc8e71de42a13650434096640602315f7d681;p=modules%2Fgeom.git Improve material management (performance issues) --- diff --git a/src/GEOMGUI/GEOMGUI_Selection.cxx b/src/GEOMGUI/GEOMGUI_Selection.cxx index e6a016041..4d7a490b0 100644 --- a/src/GEOMGUI/GEOMGUI_Selection.cxx +++ b/src/GEOMGUI/GEOMGUI_Selection.cxx @@ -213,27 +213,20 @@ int GEOMGUI_Selection::typeId( const int index ) const bool GEOMGUI_Selection::isVisible( const int index ) const { - bool res = false; - #ifdef USE_VISUAL_PROP_MAP - bool found = false; QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::Visibility ) ); - if ( v.canConvert( QVariant::Bool ) ) { - res = v.toBool(); - found = true; - } - - if ( !found ) { + if ( v.canConvert( QVariant::Bool ) ) + return v.toBool(); #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 + + bool res = false; + + 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 ); } -#endif return res; } @@ -288,6 +281,7 @@ QString GEOMGUI_Selection::displayMode( const int index ) const { QString res; QString viewType = activeViewType(); + #ifdef USE_VISUAL_PROP_MAP QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::DisplayMode ) ); if ( v.canConvert( QVariant::Int ) ) { @@ -297,113 +291,103 @@ QString GEOMGUI_Selection::displayMode( const int index ) const } else if ( viewType == SVTK_Viewer::Type() ) { VTK_DISPLAY_MODE_TO_STRING( res, dm ); } + return res; } - - if ( res.isEmpty() ) { #endif - SALOME_View* view = GEOM_Displayer::GetActiveView(); - if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) { - SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); - if ( prs ) { - 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() ) { - int dm; - Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io); - if(!aSh.IsNull()) { - dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->DisplayMode(); - } else { - dm = io->DisplayMode(); - } + + SALOME_View* view = GEOM_Displayer::GetActiveView(); + if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) { + SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); + if ( prs ) { + 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() ) { + int dm; + Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io); + if(!aSh.IsNull()) { + dm = aSh->isTopLevel() ? aSh->prevDisplayMode() : aSh->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 ); - 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( prs ); - vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0; - if ( lst ) { - lst->InitTraversal(); - vtkActor* actor = lst->GetNextActor(); - if ( actor ) { - SALOME_Actor* salActor = dynamic_cast( 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( prs ); + vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0; + if ( lst ) { + lst->InitTraversal(); + vtkActor* actor = lst->GetNextActor(); + if ( actor ) { + SALOME_Actor* salActor = dynamic_cast( 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; } bool GEOMGUI_Selection::isVectorsMode( const int index ) const { - bool res = false; - #ifdef USE_VISUAL_PROP_MAP - bool found = false; QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::EdgesDirection ) ); - if ( v.canConvert( QVariant::Bool ) ) { - res = v.toBool(); - found = true; - } - - if ( !found ) { + if ( v.canConvert( QVariant::Bool ) ) + return v.toBool(); #endif - SALOME_View* view = GEOM_Displayer::GetActiveView(); - QString viewType = activeViewType(); - if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) { - SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); - if ( prs ) { - 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 = aSh->isShowVectors(); - } + + bool res = false; + + SALOME_View* view = GEOM_Displayer::GetActiveView(); + QString viewType = activeViewType(); + if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) { + SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); + if ( prs ) { + 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 = aSh->isShowVectors(); } - } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK - SVTK_Prs* vtkPrs = dynamic_cast( 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( 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; } @@ -548,35 +532,31 @@ QString GEOMGUI_Selection::selectionMode() const return ""; } -bool GEOMGUI_Selection::topLevel( const int index ) const { - bool res = false; - +bool GEOMGUI_Selection::topLevel( const int index ) const +{ #ifdef USE_VISUAL_PROP_MAP - bool found = false; QVariant v = visibleProperty( entry( index ), GEOM::propertyName( GEOM::TopLevel ) ); - if ( v.canConvert() ) { - res = v.toBool(); - found = true; - } - - if ( !found ) { + if ( v.canConvert() ) + return v.toBool(); #endif - SALOME_View* view = GEOM_Displayer::GetActiveView(); - QString viewType = activeViewType(); - if ( view && viewType == OCCViewer_Viewer::Type() ) { - SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); - if ( prs ) { - 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(); - } + + bool res = false; + + SALOME_View* view = GEOM_Displayer::GetActiveView(); + QString viewType = activeViewType(); + if ( view && viewType == OCCViewer_Viewer::Type() ) { + SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() ); + if ( prs ) { + 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; } -bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const{ - bool res = false; - +bool GEOMGUI_Selection::isPhysicalMaterial( const int idx ) const +{ #ifdef USE_VISUAL_PROP_MAP - bool found = false; - QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) ); - if ( v.canConvert() ) { - Material_Model material; - material.fromProperties( v.toString() ); - res = material.isPhysical(); - found = true; - } - - if ( !found ) { + QVariant v = visibleProperty( entry( idx ), GEOM::propertyName( GEOM::Material ) ); + if ( v.canConvert() ) { + Material_Model material; + material.fromProperties( v.toString() ); + return material.isPhysical(); + } #endif - SALOME_View* view = GEOM_Displayer::GetActiveView(); - QString viewType = activeViewType(); - if ( view ) { - SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() ); - if ( prs ) { - 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->Attributes()->ShadingAspect()-> - Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC ); - } - } - } - else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK - SVTK_Prs* vtkPrs = dynamic_cast( prs ); - vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0; - if ( lst ) { - lst->InitTraversal(); - vtkActor* actor = lst->GetNextActor(); - if ( actor ) { - GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor ); - if ( aGeomGActor ) { - GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty()); - res = mat->GetPhysical(); - } // if ( salome actor ) - } // if ( actor ) - } // if ( lst == vtkPrs->GetObjects() ) - } - } - } - } - return res; + + bool res = false; + + SALOME_View* view = GEOM_Displayer::GetActiveView(); + QString viewType = activeViewType(); + if ( view ) { + SALOME_Prs* prs = view->CreatePrs( entry( idx ).toLatin1().constData() ); + if ( prs ) { + 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->Attributes()->ShadingAspect()-> + Material(Aspect_TOFM_BOTH_SIDE).MaterialType( Graphic3d_MATERIAL_PHYSIC ); + } + } + } + else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK + SVTK_Prs* vtkPrs = dynamic_cast( prs ); + vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0; + if ( lst ) { + lst->InitTraversal(); + vtkActor* actor = lst->GetNextActor(); + if ( actor ) { + GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( actor ); + if ( aGeomGActor ) { + GEOM_VTKPropertyMaterial* mat = GEOM_VTKPropertyMaterial::SafeDownCast(aGeomGActor->GetProperty()); + res = mat->GetPhysical(); + } // if ( salome actor ) + } // if ( actor ) + } // if ( lst == vtkPrs->GetObjects() ) + } + } + } + + return res; } diff --git a/src/GEOMGUI/GeometryGUI.cxx b/src/GEOMGUI/GeometryGUI.cxx index 1eef55c39..31b134a62 100644 --- a/src/GEOMGUI/GeometryGUI.cxx +++ b/src/GEOMGUI/GeometryGUI.cxx @@ -207,6 +207,8 @@ GeometryGUI::GeometryGUI() : myDisplayer = 0; 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 ); else 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 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 = ""; if ( v.canConvert() ) curModel = v.toString(); // get list of all predefined materials - Material_ResourceMgr aMatResMgr; - QStringList matNameList = aMatResMgr.materials(); - foreach ( QString name, matNameList ) + QStringList materials = Material_ResourceMgr::resourceMgr()->materials(); + bool found = false; + foreach ( QString material, materials ) { - QAction* menAct = matMenu->addAction( name ); + QAction* menAct = matMenu->addAction( material ); connect(menAct, SIGNAL( toggled( bool ) ), signalMapper, SLOT( map() ) ); - signalMapper->setMapping( menAct, name ); + signalMapper->setMapping( menAct, material ); menAct->setCheckable( true ); // Set checked if this material is current Material_Model aModel; - aModel.fromResources( name ); - menAct->setChecked( aModel.toProperties() == curModel ); + aModel.fromResources( material ); + if ( !found && aModel.toProperties() == curModel ) { + menAct->setChecked( true ); + found = true; + } } matMenu->insertAction( matMenu->addSeparator(), action( GEOMOp::OpPredefMaterCustom ) ); matMenu->insertSeparator( action( GEOMOp::OpPredefMaterCustom ) ); @@ -1818,12 +1813,12 @@ void GeometryGUI::createPreferences() int defl = addPreference( tr( "PREF_DEFLECTION" ), genGroup, LightApp_Preferences::DblSpin, "Geometry", "deflection_coeff" ); - int predef_materials = addPreference( tr( "PREF_PREDEF_MATERIALS" ), genGroup, - LightApp_Preferences::Bool, "Geometry", "predef_materials" ); + addPreference( tr( "PREF_PREDEF_MATERIALS" ), genGroup, + LightApp_Preferences::Bool, "Geometry", "predef_materials" ); int material = addPreference( tr( "PREF_MATERIAL" ), genGroup, - LightApp_Preferences::Selector, - "Geometry", "material" ); + LightApp_Preferences::Selector, + "Geometry", "material" ); addPreference( tr( "PREF_EDITGROUP_COLOR" ), genGroup, LightApp_Preferences::Color, "Geometry", "editgroup_color" ); @@ -1942,11 +1937,8 @@ void GeometryGUI::createPreferences() setPreferenceProperty( defl, "step", 1.0e-04 ); setPreferenceProperty( defl, "precision", 6 ); - // Set property for 'Show predefined materials' - setPreferenceProperty( predef_materials, "eval", true); // Set property for default material - Material_ResourceMgr aMatResMgr; - setPreferenceProperty( material, "strings", aMatResMgr.materials() ); + setPreferenceProperty( material, "strings", Material_ResourceMgr::resourceMgr()->materials() ); // Set property vertex marker type QList aMarkerTypeIndicesList; @@ -2396,3 +2388,22 @@ bool GeometryGUI::renameObject( const QString& entry, const QString& name) } 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(); + } + } +} diff --git a/src/GEOMGUI/GeometryGUI.h b/src/GEOMGUI/GeometryGUI.h index b9553c157..9bf6e6326 100644 --- a/src/GEOMGUI/GeometryGUI.h +++ b/src/GEOMGUI/GeometryGUI.h @@ -152,6 +152,7 @@ private slots: void onWindowActivated( SUIT_ViewWindow* ); void onViewAboutToShow(); void OnSetMaterial( const QString& ); + void updateMaterials(); signals : void SignalDeactivateActiveDialog(); diff --git a/src/Material/Makefile.am b/src/Material/Makefile.am index c0c43367f..e87bf9360 100644 --- a/src/Material/Makefile.am +++ b/src/Material/Makefile.am @@ -36,6 +36,12 @@ dist_libMaterial_la_SOURCES = \ Material_Model.cxx \ Material_ResourceMgr.cxx +MOC_FILES = \ + Material_ResourceMgr_moc.cxx + +nodist_libMaterial_la_SOURCES = \ + $(MOC_FILES) + # additional information to compile and link file libMaterial_la_CPPFLAGS = \ $(QT_INCLUDES) \ diff --git a/src/Material/Material_Model.cxx b/src/Material/Material_Model.cxx index f90d849bb..949d7d2b0 100644 --- a/src/Material/Material_Model.cxx +++ b/src/Material/Material_Model.cxx @@ -25,6 +25,8 @@ #include "GEOM_VTKPropertyMaterial.hxx" #include "Material_ResourceMgr.h" +#include + /*! \brief Constructor @@ -184,7 +186,7 @@ QString Material_Model::toProperties() \param resMgr resource manager (if not specified, new resources manager is created) \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]"; @@ -194,15 +196,22 @@ void Material_Model::fromResources( const QString& material, QtxResourceMgr* res // material name is not specified: use default values if ( material.isEmpty() ) return; - bool ownResourcesMgr = resMgr == 0; - - if ( ownResourcesMgr ) - resMgr = new Material_ResourceMgr(); + if ( !resMgr ) + resMgr = Material_ResourceMgr::resourceMgr(); + + // lock resources manager + QMutexLocker lock( &resMgr->myMutex ); // read common section 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 if ( resMgr->hasValue( 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" ) ) { 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 \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() ) { + // lock resources manager + QMutexLocker lock( &resMgr->myMutex ); + // remove resources section (to clean-up all previous properties) resMgr->remove( material ); diff --git a/src/Material/Material_Model.h b/src/Material/Material_Model.h index 96ef16fae..d27ad1236 100644 --- a/src/Material/Material_Model.h +++ b/src/Material/Material_Model.h @@ -32,7 +32,7 @@ #include -class QtxResourceMgr; +class Material_ResourceMgr; class GEOM_VTKPropertyMaterial; class MATERIAL_SALOME_EXPORT Material_Model @@ -51,8 +51,8 @@ public: void fromProperties( const QString& ); QString toProperties(); - void fromResources( const QString& = QString(), QtxResourceMgr* = 0 ); - void toResources( const QString&, QtxResourceMgr* ); + void fromResources( const QString& = QString(), Material_ResourceMgr* = 0 ); + void toResources( const QString&, Material_ResourceMgr* ); bool isPhysical() const; void setPhysical( bool ); @@ -77,6 +77,7 @@ public: private: void init(); + void read( const QString&, Material_ResourceMgr* ); private: typedef struct { diff --git a/src/Material/Material_ResourceMgr.cxx b/src/Material/Material_ResourceMgr.cxx index 97853f532..eef31d76d 100644 --- a/src/Material/Material_ResourceMgr.cxx +++ b/src/Material/Material_ResourceMgr.cxx @@ -22,6 +22,31 @@ #include "Material_ResourceMgr.h" +#include +#include + +/*! + \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 \brief Material properties resources manager. @@ -41,7 +66,8 @@ \brief Constructor */ Material_ResourceMgr::Material_ResourceMgr() - : QtxResourceMgr( "SalomeMaterial", "%1Config" ) + : QtxResourceMgr( "SalomeMaterial", "%1Config" ), + myWatcher( 0 ) { if ( dirList().isEmpty() && ::getenv( "GEOM_ROOT_DIR" ) ) setDirList( QStringList() << Qtx::addSlash( ::getenv( "GEOM_ROOT_DIR" ) ) + "share/salome/resources/geom" ); @@ -53,6 +79,24 @@ 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 ) { + QMutexLocker lock( &myMutex ); + // store original working mode WorkingMode m = workingMode(); @@ -114,3 +160,34 @@ QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort 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(); +} diff --git a/src/Material/Material_ResourceMgr.h b/src/Material/Material_ResourceMgr.h index a98a531d7..de71b1028 100644 --- a/src/Material/Material_ResourceMgr.h +++ b/src/Material/Material_ResourceMgr.h @@ -25,10 +25,18 @@ #include "Material.h" +#include +#include #include -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: //! Material type typedef enum { @@ -40,8 +48,24 @@ public: Material_ResourceMgr(); ~Material_ResourceMgr(); + static Material_ResourceMgr* resourceMgr(); + 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