Salome HOME
Merge from V6_main_20120808 08Aug12
[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 /*!
26   \class Material_ResourceMgr
27   \brief Material properties resources manager.
28
29   This class is used to manage the material properties throughout the application
30   in the similar way as QtxResourceMgr does it with application preferences.
31
32   Standard material types are stored in the global application settings files
33   (named as SalomeMaterial.xml). User-defined materials are stored in user's home
34   directory - in the file .SalomeMaterialrc.
35
36   The Material_ResourceMgr class is used by material properties dialog box
37   (GEOMToolsGUI_MaterialPropertiesDlg class).
38 */
39
40 /*!
41   \brief Constructor
42 */
43 Material_ResourceMgr::Material_ResourceMgr()
44   : QtxResourceMgr( "SalomeMaterial", "%1Config" )
45 {
46   if ( dirList().isEmpty() && ::getenv( "GEOM_ROOT_DIR" ) )
47     setDirList( QStringList() << Qtx::addSlash( ::getenv( "GEOM_ROOT_DIR" ) ) + "share/salome/resources/geom" );
48   setCurrentFormat( "xml" );
49 }
50
51 /*!
52   \brief Destructor
53 */
54 Material_ResourceMgr::~Material_ResourceMgr()
55 {
56 }
57
58 /*!
59   \brief Get list of avaiable materials
60   \param theType material type
61   \param theSort if \c true (default), returns a list of materials sorted by name
62   \return list of avaiable materials names
63 */
64 QStringList Material_ResourceMgr::materials( MaterialType theType, bool theSort )
65 {
66   // store original working mode
67   WorkingMode m = workingMode();
68
69   QStringList slglobal, sluser;
70
71   // retrieve all materials : global + user
72   setWorkingMode( AllowUserValues );
73   sluser = sections();
74
75   // retrieve only global materials
76   setWorkingMode( IgnoreUserValues );
77   slglobal = sections();
78
79   // remove global materials from user list to obtain only user materials
80   QMutableListIterator<QString> it( sluser );
81   while ( it.hasNext() ) {
82     QString s = it.next();
83     if ( slglobal.contains( s ) ) it.remove();
84   }
85
86   // remove 'common' material 
87   slglobal.removeAll("[common]");
88
89   // restore original working mode
90   setWorkingMode( m );
91
92   // sort if necessary (separately global and user materials)
93   if ( theSort ) {
94     qSort( slglobal );
95     qSort( sluser );
96   }
97
98   // combine the materials to obtain result list
99   QStringList result;
100   
101   switch ( theType ) {
102   case Global:
103     result = slglobal;
104     break;
105   case User:
106     result = sluser;
107     break;
108   case All:
109     result = slglobal + sluser;
110     break;
111   default:
112     break;
113   }
114
115   return result;
116 }