Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / Material / Material_ResourceMgr.cxx
1 // Copyright (C) 2007-2013  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   // special processing for default material (to make it first in the list)
144   if ( slglobal.contains( "[ Default ]" ) ) {
145     slglobal.removeAll( "[ Default ]" );
146     slglobal.prepend( "[ Default ]" );
147   }
148
149   // combine the materials to obtain result list
150   QStringList result;
151   
152   switch ( theType ) {
153   case Global:
154     result = slglobal;
155     break;
156   case User:
157     result = sluser;
158     break;
159   case All:
160     result = slglobal + sluser;
161     break;
162   default:
163     break;
164   }
165
166   return result;
167 }
168
169 /*!
170   \brief Start/stop this resource manager watching the user materials database file.
171   \internal
172 */
173 void Material_ResourceMgr::watchUserFile( bool on )
174 {
175   if ( on ) {
176     if ( !myWatcher ) {
177       myWatcher = new QFileSystemWatcher( this );
178       myWatcher->addPath( userFileName( appName() ) );
179       connect( myWatcher, SIGNAL( fileChanged( QString ) ), this, SLOT( update() ) );
180     }
181   }
182   else {
183     if ( myWatcher ) {
184       delete myWatcher;
185       myWatcher = 0;
186     }
187   }
188 }
189
190 /*!
191   \brief Update user database slot
192   \internal
193 */
194 void Material_ResourceMgr::update()
195 {
196   Updater( this ).wait();
197   emit changed();
198 }