Salome HOME
Merge from V6_main 11/02/2013
[modules/geom.git] / src / Material / Material_ResourceMgr.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : Material_ResourceMgr.cxx
21 // Author : Margarita KARPUNINA, Open CASCADE S.A.S. (margarita.karpunina@opencascade.com)
22
23 #include "Material_ResourceMgr.h"
24
25 #include <QFileSystemWatcher>
26 #include <QThread>
27
28 /*!
29   \class Material_ResourceMgr::Updater
30   \brief Updates the contents of the resource manager as soon as
31   user materials database file is changed
32   \internal
33 */
34 class Material_ResourceMgr::Updater : public QThread
35 {
36 public:
37   Material_ResourceMgr* myResourceMgr;
38   Updater( Material_ResourceMgr* resMgr ) : myResourceMgr( resMgr )
39   {
40     start();
41   }
42   void run()
43   {
44     QMutexLocker lock( &myResourceMgr->myMutex );
45     myResourceMgr->clear();
46     myResourceMgr->load();
47   }
48 };
49
50 /*!
51   \class Material_ResourceMgr
52   \brief Material properties resources manager.
53
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.
56
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.
60
61   The Material_ResourceMgr class is used by material properties dialog box
62   (GEOMToolsGUI_MaterialPropertiesDlg class).
63 */
64
65 /*!
66   \brief Constructor
67 */
68 Material_ResourceMgr::Material_ResourceMgr()
69   : QtxResourceMgr( "SalomeMaterial", "%1Config" ),
70     myWatcher( 0 )
71 {
72   if ( dirList().isEmpty() && ::getenv( "GEOM_ROOT_DIR" ) )
73     setDirList( QStringList() << Qtx::addSlash( ::getenv( "GEOM_ROOT_DIR" ) ) + "share/salome/resources/geom" );
74   setCurrentFormat( "xml" );
75 }
76
77 /*!
78   \brief Destructor
79 */
80 Material_ResourceMgr::~Material_ResourceMgr()
81 {
82   watchUserFile( false );
83 }
84
85 /*!
86   \brief Get shared instance of resources manager
87   
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.
91 */
92 Material_ResourceMgr* Material_ResourceMgr::resourceMgr()
93 {
94   static Material_ResourceMgr* resMgr = 0;
95   if ( !resMgr ) {
96     resMgr = new Material_ResourceMgr();
97     resMgr->watchUserFile( true );
98   }
99   return resMgr;
100 }
101
102 /*!
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
107 */
108 QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort )
109 {
110   QMutexLocker lock( &myMutex );
111
112   // store original working mode
113   WorkingMode m = workingMode();
114
115   QStringList slglobal, sluser;
116
117   // retrieve all materials : global + user
118   setWorkingMode( AllowUserValues );
119   sluser = sections();
120
121   // retrieve only global materials
122   setWorkingMode( IgnoreUserValues );
123   slglobal = sections();
124
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();
130   }
131
132   // remove 'common' material 
133   slglobal.removeAll("[common]");
134
135   // restore original working mode
136   setWorkingMode( m );
137
138   // sort if necessary (separately global and user materials)
139   if ( theSort ) {
140     qSort( slglobal );
141     qSort( sluser );
142   }
143
144   // combine the materials to obtain result list
145   QStringList result;
146   
147   switch ( theType ) {
148   case Global:
149     result = slglobal;
150     break;
151   case User:
152     result = sluser;
153     break;
154   case All:
155     result = slglobal + sluser;
156     break;
157   default:
158     break;
159   }
160
161   return result;
162 }
163
164 /*!
165   \brief Start/stop this resource manager watching the user materials database file.
166   \internal
167 */
168 void Material_ResourceMgr::watchUserFile( bool on )
169 {
170   if ( on ) {
171     if ( !myWatcher ) {
172       myWatcher = new QFileSystemWatcher( this );
173       myWatcher->addPath( userFileName( appName() ) );
174       connect( myWatcher, SIGNAL( fileChanged( QString ) ), this, SLOT( update() ) );
175     }
176   }
177   else {
178     if ( myWatcher ) {
179       delete myWatcher;
180       myWatcher = 0;
181     }
182   }
183 }
184
185 /*!
186   \brief Update user database slot
187   \internal
188 */
189 void Material_ResourceMgr::update()
190 {
191   Updater( this ).wait();
192   emit changed();
193 }