Salome HOME
updated copyright message
[modules/gui.git] / src / Qtx / QtxResourceMgr.h
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      QtxResourceMgr.h
24 // Author:    Alexander SOLOVYOV, Sergey TELKOV
25 //
26 #ifndef QTXRESOURCEMGR_H
27 #define QTXRESOURCEMGR_H
28
29 #include "Qtx.h"
30
31 #ifndef QTX_NO_INDEXED_MAP
32 #include "QtxMap.h"
33 #endif
34
35 #include <QMap>
36 #include <QList>
37 #include <QFont>
38 #include <QColor>
39 #include <QPixmap>
40 #include <QByteArray>
41 #include <QStringList>
42 #include <QLinearGradient>
43 #include <QRadialGradient>
44 #include <QConicalGradient>
45
46 class QTranslator;
47
48 #ifdef WIN32
49 #pragma warning( disable:4251 )
50 #endif
51
52 class QTX_EXPORT QtxResourceMgr
53 {
54   class IniFormat;
55   class XmlFormat;
56   class JsonFormat;
57   class SalomexFormat;
58   class Resources;
59
60 public:
61   class Format;
62
63 #ifdef QTX_NO_INDEXED_MAP
64   typedef QMap<QString, QString> Section;   //!< resource section
65 #else
66   typedef IMap<QString, QString> Section;   //!< resource section
67 #endif
68
69   //! Working mode; defines a way how resource manager handles user preferences
70   typedef enum {
71     AllowUserValues,       //!< User values are processed by the resource manager
72     IgnoreUserValues       //!< User values are ignored by the resource manager
73   } WorkingMode;
74
75 public:
76   QtxResourceMgr();
77   QtxResourceMgr( const QString&, const QString& = QString() );
78   virtual ~QtxResourceMgr();
79
80   QString          appName() const;
81   QStringList      dirList() const;
82
83   bool             checkExisting() const;
84   virtual void     setCheckExisting( const bool );
85
86   bool             isPixmapCached() const;
87   void             setIsPixmapCached( const bool );
88
89   void             clear();
90
91   WorkingMode      workingMode() const;
92   WorkingMode      setWorkingMode( WorkingMode );
93
94   bool             value( const QString&, const QString&, int& ) const;
95   bool             value( const QString&, const QString&, double& ) const;
96   bool             value( const QString&, const QString&, bool& ) const;
97   bool             value( const QString&, const QString&, QColor& ) const;
98   bool             value( const QString&, const QString&, QFont& ) const;  
99   bool             value( const QString&, const QString&, QByteArray& ) const;  
100   bool             value( const QString&, const QString&, QLinearGradient& ) const;  
101   bool             value( const QString&, const QString&, QRadialGradient& ) const;  
102   bool             value( const QString&, const QString&, QConicalGradient& ) const;  
103   bool             value( const QString&, const QString&, Qtx::BackgroundData& ) const;  
104   bool             value( const QString&, const QString&, QString&, const bool = true ) const;
105
106   int              integerValue( const QString&, const QString&, const int = 0 ) const;
107   double           doubleValue( const QString&, const QString&, const double = 0 ) const;
108   bool             booleanValue( const QString&, const QString&, const bool = false ) const;
109   QFont            fontValue( const QString&, const QString&, const QFont& = QFont() ) const;
110   QColor           colorValue( const QString&, const QString&, const QColor& = QColor() ) const;
111   QString          stringValue( const QString&, const QString&, const QString& = QString(), const bool = true ) const;
112   QByteArray       byteArrayValue( const QString&, const QString&, const QByteArray& = QByteArray() ) const;
113   QLinearGradient  linearGradientValue( const QString&, const QString&, const QLinearGradient& = QLinearGradient() ) const;
114   QRadialGradient  radialGradientValue( const QString&, const QString&, const QRadialGradient& = QRadialGradient() ) const;
115   QConicalGradient conicalGradientValue( const QString&, const QString&, const QConicalGradient& = QConicalGradient() ) const;
116   Qtx::BackgroundData backgroundValue( const QString&, const QString&, const Qtx::BackgroundData& = Qtx::BackgroundData() ) const;
117
118   bool             hasSection( const QString& ) const;
119   bool             hasValue( const QString&, const QString& ) const;
120
121   void             setValue( const QString&, const QString&, const int );
122   void             setValue( const QString&, const QString&, const double );
123   void             setValue( const QString&, const QString&, const bool );
124   void             setValue( const QString&, const QString&, const QFont& );
125   void             setValue( const QString&, const QString&, const QColor& );
126   void             setValue( const QString&, const QString&, const QString& );
127   void             setValue( const QString&, const QString&, const QByteArray& );
128   void             setValue( const QString&, const QString&, const QLinearGradient& );
129   void             setValue( const QString&, const QString&, const QRadialGradient& );
130   void             setValue( const QString&, const QString&, const QConicalGradient& );
131   void             setValue( const QString&, const QString&, const Qtx::BackgroundData& );
132
133   void             remove( const QString& );
134   void             remove( const QString&, const QString& );
135
136   QString          currentFormat() const;
137   void             setCurrentFormat( const QString& );
138
139   Format*          format( const QString& ) const;
140   void             installFormat( Format* );
141   void             removeFormat( Format* );
142
143   QStringList      options() const;
144   QString          option( const QString& ) const;
145   void             setOption( const QString&, const QString& );
146
147   QStringList      constants() const;
148   QString          constant( const QString& ) const;
149   void             setConstant( const QString&, const QString& );
150
151   QPixmap          defaultPixmap() const;
152   virtual void     setDefaultPixmap( const QPixmap& );
153
154   QString          resSection() const;
155   QString          langSection() const;
156   QString          sectionsToken() const;
157
158   QPixmap          loadPixmap( const QString&, const QString& ) const;
159   QPixmap          loadPixmap( const QString&, const QString&, const bool ) const;
160   QPixmap          loadPixmap( const QString&, const QString&, const QPixmap& ) const;
161   void             loadLanguage( const QString& = QString(), const QString& = QString() );
162
163   void             raiseTranslators( const QString& );
164   void             removeTranslators( const QString& );
165   void             loadTranslator( const QString&, const QString& );
166   void             loadTranslators( const QString&, const QStringList& );
167   void             addTranslator( const QString&, QTranslator* );
168
169   QString          path( const QString&, const QString&, const QString& ) const;
170
171   bool             load();
172   bool             import( const QString& );
173   bool             save();
174   bool             addResource( const QString& );
175
176   QStringList      sections() const;
177   QStringList      sections(const QRegExp&) const;
178   QStringList      sections(const QStringList&) const;
179   QStringList      subSections(const QString&, const bool = true) const;
180   QStringList      parameters( const QString& ) const;
181   QStringList      parameters( const QStringList& ) const;
182
183   void             refresh();
184
185   QString          language( const QString& = QString() ) const;
186
187 protected:
188   virtual QString  defaultLanguage() const;
189
190   virtual void     setDirList( const QStringList& );
191   virtual void     setResource( const QString&, const QString&, const QString& );
192
193   virtual QString  userFileName( const QString&, const bool = true ) const;
194   virtual QString  globalFileName( const QString& ) const;
195
196   virtual void     saved();
197
198 private:
199   void             initialize( const bool = true ) const;
200   QString          substMacro( const QString&, const QMap<QChar, QString>& ) const;
201
202 private:
203   typedef QList<Resources*>        ResList;
204   typedef QList<QTranslator*>      TransList;
205   typedef QList<Format*>           FormatList;
206   typedef QMap<QString, QString>   OptionsMap;
207   typedef QMap<QString, TransList> TransListMap;
208
209 private:
210   QString          myAppName;                 //!< application name
211   QStringList      myDirList;                 //!< list of resources directories
212   FormatList       myFormats;                 //!< list of formats
213   OptionsMap       myOptions;                 //!< options map
214   OptionsMap       myConstants;               //!< constants map
215   ResList          myResources;               //!< resources list
216   bool             myCheckExist;              //!< "check existance" flag
217   TransListMap     myTranslator;              //!< map of loaded translators
218   QPixmap*         myDefaultPix;              //!< default icon
219   bool             myIsPixmapCached;          //!< "cached pixmaps" flag
220
221   bool             myHasUserValues;           //!< \c true if user preferences has been read
222   WorkingMode      myWorkingMode;             //!< working mode
223
224   friend class QtxResourceMgr::Format;
225 };
226
227 class QTX_EXPORT QtxResourceMgr::Format
228 {
229 public:
230   Format( const QString& );
231   virtual ~Format();
232
233   QString                format() const;
234
235   QStringList            options() const;
236   QString                option( const QString& ) const;
237   void                   setOption( const QString&, const QString& );
238
239   bool                   load( Resources* );
240   bool                   save( Resources* );
241
242 protected:
243   virtual bool           load( const QString&, QMap<QString, Section>& ) = 0;
244   virtual bool           save( const QString&, const QMap<QString, Section>& ) = 0;
245
246 private:
247   QString                myFmt;    //!< format name
248   QMap<QString, QString> myOpt;    //!< options map
249 };
250
251 #endif // QTXRESOURCEMGR_H