1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : Material_ResourceMgr.cxx
21 // Author : Margarita KARPUNINA, Open CASCADE S.A.S. (margarita.karpunina@opencascade.com)
23 #include "Material_ResourceMgr.h"
26 #include <QFileSystemWatcher>
30 \class Material_ResourceMgr::Updater
31 \brief Updates the contents of the resource manager as soon as
32 user materials database file is changed
35 class Material_ResourceMgr::Updater : public QThread
38 Material_ResourceMgr* myResourceMgr;
39 Updater( Material_ResourceMgr* resMgr ) : myResourceMgr( resMgr )
45 QMutexLocker lock( &myResourceMgr->myMutex );
46 myResourceMgr->clear();
47 myResourceMgr->load();
52 \class Material_ResourceMgr
53 \brief Material properties resources manager.
55 This class is used to manage the material properties throughout the application
56 in the similar way as QtxResourceMgr does it with application preferences.
58 Standard material types are stored in the global application settings files
59 (named as SalomeMaterial.xml). User-defined materials are stored in user's home
60 directory - in the file .SalomeMaterialrc.
62 The Material_ResourceMgr class is used by material properties dialog box
63 (GEOMToolsGUI_MaterialPropertiesDlg class).
69 Material_ResourceMgr::Material_ResourceMgr()
70 : QtxResourceMgr( "SalomeMaterial", "%1Config" ),
73 if ( dirList().isEmpty() && ::getenv( "GEOM_ROOT_DIR" ) )
74 setDirList( QStringList() << Qtx::addSlash( ::getenv( "GEOM_ROOT_DIR" ) ) + "share/salome/resources/geom" );
75 setCurrentFormat( "xml" );
81 Material_ResourceMgr::~Material_ResourceMgr()
83 watchUserFile( false );
87 \brief Get shared instance of resources manager
89 This instance of resource manager is global for the application;
90 it watches for changes in the user materials database file to
91 maintain the fresh version of the materials data.
93 Material_ResourceMgr* Material_ResourceMgr::resourceMgr()
95 static Material_ResourceMgr* resMgr = 0;
97 resMgr = new Material_ResourceMgr();
98 resMgr->watchUserFile( true );
104 \brief Get list of avaiable materials
105 \param theType material type
106 \param theSort if \c true (default), returns a list of materials sorted by name
107 \return list of avaiable materials names
109 QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort )
111 QMutexLocker lock( &myMutex );
113 // store original working mode
114 WorkingMode m = workingMode();
116 QStringList slglobal, sluser;
118 // retrieve all materials : global + user
119 setWorkingMode( AllowUserValues );
122 // retrieve only global materials
123 setWorkingMode( IgnoreUserValues );
124 slglobal = sections();
126 // remove global materials from user list to obtain only user materials
127 QMutableListIterator<QString> it( sluser );
128 while ( it.hasNext() ) {
129 QString s = it.next();
130 if ( slglobal.contains( s ) ) it.remove();
133 // remove 'common' material
134 slglobal.removeAll("[common]");
136 // restore original working mode
139 // sort if necessary (separately global and user materials)
144 // special processing for default material (to make it first in the list)
145 if ( slglobal.contains( "[ Default ]" ) ) {
146 slglobal.removeAll( "[ Default ]" );
147 slglobal.prepend( "[ Default ]" );
150 // combine the materials to obtain result list
161 result = slglobal + sluser;
171 \brief Start/stop this resource manager watching the user materials database file.
174 void Material_ResourceMgr::watchUserFile( bool on )
178 myWatcher = new QFileSystemWatcher( this );
179 QFileInfo ufile = userFileName( appName() );
180 if ( ufile.exists() ) {
181 myWatcher->addPath( ufile.filePath() );
183 connect( myWatcher, SIGNAL( fileChanged( QString ) ), this, SLOT( update() ) );
195 \brief This function is called after user configuration file is saved.
198 void Material_ResourceMgr::saved()
200 if ( resourceMgr() != this ) {
201 resourceMgr()->saved();
203 else if ( myWatcher ) {
204 QStringList files = myWatcher->files();
205 QFileInfo ufile = userFileName( appName() );
206 if ( ufile.exists() && !files.contains( ufile.filePath() ) ) {
207 myWatcher->addPath( ufile.filePath() );
214 \brief Update user database slot
217 void Material_ResourceMgr::update()
219 Updater( this ).wait();