1 // Copyright (C) 2007-2012 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.
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"
25 #include <QFileSystemWatcher>
29 \class Material_ResourceMgr::Updater
30 \brief Updates the contents of the resource manager as soon as
31 user materials database file is changed
34 class Material_ResourceMgr::Updater : public QThread
37 Material_ResourceMgr* myResourceMgr;
38 Updater( Material_ResourceMgr* resMgr ) : myResourceMgr( resMgr )
44 QMutexLocker lock( &myResourceMgr->myMutex );
45 myResourceMgr->clear();
46 myResourceMgr->load();
51 \class Material_ResourceMgr
52 \brief Material properties resources manager.
54 This class is used to manage the material properties throughout the application
55 in the similar way as QtxResourceMgr does it with application preferences.
57 Standard material types are stored in the global application settings files
58 (named as SalomeMaterial.xml). User-defined materials are stored in user's home
59 directory - in the file .SalomeMaterialrc.
61 The Material_ResourceMgr class is used by material properties dialog box
62 (GEOMToolsGUI_MaterialPropertiesDlg class).
68 Material_ResourceMgr::Material_ResourceMgr()
69 : QtxResourceMgr( "SalomeMaterial", "%1Config" ),
72 if ( dirList().isEmpty() && ::getenv( "GEOM_ROOT_DIR" ) )
73 setDirList( QStringList() << Qtx::addSlash( ::getenv( "GEOM_ROOT_DIR" ) ) + "share/salome/resources/geom" );
74 setCurrentFormat( "xml" );
80 Material_ResourceMgr::~Material_ResourceMgr()
82 watchUserFile( false );
86 \brief Get shared instance of resources manager
88 This instance of resource manager is global for the application;
89 it watches for changes in the user materials database file to
90 maintain the fresh version of the materials data.
92 Material_ResourceMgr* Material_ResourceMgr::resourceMgr()
94 static Material_ResourceMgr* resMgr = 0;
96 resMgr = new Material_ResourceMgr();
97 resMgr->watchUserFile( true );
103 \brief Get list of avaiable materials
104 \param theType material type
105 \param theSort if \c true (default), returns a list of materials sorted by name
106 \return list of avaiable materials names
108 QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort )
110 QMutexLocker lock( &myMutex );
112 // store original working mode
113 WorkingMode m = workingMode();
115 QStringList slglobal, sluser;
117 // retrieve all materials : global + user
118 setWorkingMode( AllowUserValues );
121 // retrieve only global materials
122 setWorkingMode( IgnoreUserValues );
123 slglobal = sections();
125 // remove global materials from user list to obtain only user materials
126 QMutableListIterator<QString> it( sluser );
127 while ( it.hasNext() ) {
128 QString s = it.next();
129 if ( slglobal.contains( s ) ) it.remove();
132 // remove 'common' material
133 slglobal.removeAll("[common]");
135 // restore original working mode
138 // sort if necessary (separately global and user materials)
144 // combine the materials to obtain result list
155 result = slglobal + sluser;
165 \brief Start/stop this resource manager watching the user materials database file.
168 void Material_ResourceMgr::watchUserFile( bool on )
172 myWatcher = new QFileSystemWatcher( this );
173 myWatcher->addPath( userFileName( appName() ) );
174 connect( myWatcher, SIGNAL( fileChanged( QString ) ), this, SLOT( update() ) );
186 \brief Update user database slot
189 void Material_ResourceMgr::update()
191 Updater( this ).wait();