File indexing completed on 2024-05-12 05:39:52
0001 /*************************************************************************** 0002 * Copyright (C) 2010 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #ifndef PREFERENCESMANAGER_H 0021 #define PREFERENCESMANAGER_H 0022 0023 #include <QMap> 0024 #include <QSettings> 0025 #include <QString> 0026 #include <QVariant> 0027 #include <functional> 0028 0029 class PreferencesListener; 0030 #include <core_global.h> 0031 /** 0032 * @index <h2>How To use the Preference Manager.</h2> 0033 * <p>It is always painful to manage of data between two sessions of Rolisteam. 0034 * We implemented a powerful tool which helps contributors to make it easier. 0035 * We will see all required steps to load/save and update data. 0036 * </p> 0037 * <h3>First thing at all</h3> You need to get pointer to this class by including preferencesmanager.h. Don't forget to 0038 * add pointer to PreferencesManager into your class. Now, it's better if you get the address of the unique instance of 0039 * the preferences manager, this can be done by calling the static function PreferencesManager::getInstance(); 0040 * 0041 */ 0042 /** 0043 * @brief Store options and manage access to their value. 0044 * Save/load values in/from QSetting instance. 0045 */ 0046 class CORE_EXPORT PreferencesManager : public QObject 0047 { 0048 Q_OBJECT 0049 public: 0050 /** 0051 * @brief Private constructor to make sure there is only one instance of this. 0052 */ 0053 PreferencesManager(const QString& applicationName, const QString& subname); 0054 0055 [[deprecated]] static PreferencesManager* getInstance(); 0056 0057 /** 0058 * @brief desturctor 0059 */ 0060 ~PreferencesManager(); 0061 0062 /** 0063 * @brief register a couple key/value in the map. 0064 * 0065 * @param key unique string to identify the value 0066 * @param value current value for the couple 0067 * @param overwrite replace or not the value associated to the key if there is already stored. 0068 * @return whether or not the insertion was successfully done 0069 */ 0070 bool registerValue(const QString& key, QVariant value, bool overwrite= true); 0071 0072 /** 0073 * @brief accessor to the map 0074 * 0075 * @param key unique string to identify the value 0076 * @param defaultValue this value is returned if the key is notin the dictionary 0077 * @return const reference to the value 0078 */ 0079 const QVariant value(const QString& key, const QVariant& defaultValue); 0080 void readSettings(); 0081 void writeSettings(); 0082 /** 0083 * @brief registerListener 0084 */ 0085 void registerListener(const QString&, PreferencesListener*); 0086 void registerLambda(const QString& key, std::function<void(QVariant)> func); 0087 0088 private: 0089 /** 0090 * @brief notifyListener 0091 */ 0092 void notifyListener(const QString& key, const QVariant& value); 0093 void notifyAllListener(); 0094 0095 signals: 0096 void dataChanged(const QString& key, const QVariant& value); 0097 0098 private: 0099 /** 0100 * Static reference, part of the singleton pattern 0101 */ 0102 static PreferencesManager* m_singleton; 0103 0104 /** 0105 * data structure to store any element. 0106 * The key is a QString, the value is a QVariant. 0107 */ 0108 QMap<QString, QVariant>* m_optionDictionary; 0109 /** 0110 * @brief m_listernerMap 0111 */ 0112 QMap<QString, PreferencesListener*> m_listernerMap; 0113 /** 0114 * @brief m_lambdaMap 0115 */ 0116 std::map<QString, std::function<void(QVariant)>> m_lambdaMap; 0117 QString m_applicationName; 0118 QString m_subname; 0119 }; 0120 0121 #endif // PREFERENCESMANAGER_H