Config.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef CONFIG_H
  2. #define CONFIG_H
  3. #include <QSettings>
  4. #include "Theme.h"
  5. #include <QDebug>
  6. #include <iostream>
  7. #include <QMetaEnum>
  8. #include <type_traits>
  9. namespace Qfw
  10. {
  11. class ConfigValidator
  12. {
  13. public:
  14. virtual bool validate(int) { return true; }
  15. virtual int correct(int value) { return value; }
  16. };
  17. class RangeValidator : public ConfigValidator
  18. {
  19. public:
  20. RangeValidator(int min, int max) : m_min(min), m_max(max) { }
  21. bool validate(int value) override { return value >= m_min && value <= m_max; };
  22. int correct(int value) override { return std::min(std::max(m_min, value), m_max); }
  23. private:
  24. int m_min, m_max;
  25. };
  26. template<typename T>
  27. class OptionsValidator : public ConfigValidator
  28. {
  29. public:
  30. OptionsValidator(QList<T> options)
  31. {
  32. qDebug() << __FUNCTION__ << __LINE__;
  33. if (options.isEmpty()) {
  34. qDebug() << "The 'options' can't be empty" << endl;
  35. }
  36. qDebug() << __FUNCTION__ << __LINE__;
  37. auto value = std::is_same<int, typename std::decay<T>::type>::value;
  38. qDebug() << __FUNCTION__ << __LINE__;
  39. // qDebug() << "2" << metaEnum.isValid() << metaEnum.name() << metaEnum.enumName() <<
  40. // metaEnum.keyCount();
  41. }
  42. private:
  43. QList<T> m_options;
  44. };
  45. class ConfigItem : public QObject
  46. {
  47. Q_OBJECT
  48. public:
  49. ConfigItem(const QString &group, const QString &name, const QVariant &defaultValue, QObject *parent = nullptr);
  50. void setValue(const QVariant &value);
  51. QVariant value() const;
  52. QString key();
  53. signals:
  54. void valueChanged(const QVariant &);
  55. public:
  56. QString group;
  57. QString name;
  58. private:
  59. QVariant m_value;
  60. };
  61. class RangeConfigItem : public ConfigItem
  62. {
  63. public:
  64. void range() { }
  65. };
  66. class QConfig : public QObject
  67. {
  68. Q_OBJECT
  69. public:
  70. explicit QConfig(QObject *parent = nullptr);
  71. explicit QConfig(const QString &customPath, QObject *parent = nullptr);
  72. QVariant get(const QString &key, const QVariant &defaultValue = QVariant()) const;
  73. void set(const QString &key, const QVariant &val, bool save = true);
  74. void setTheme(Theme t);
  75. Theme theme() const;
  76. void setThemeColor(const QColor &c);
  77. QColor themeColor() const;
  78. signals:
  79. void appRestartSig();
  80. void themeChanged(Theme t);
  81. void themeColorChanged(const QColor &);
  82. private:
  83. void init();
  84. private:
  85. QString m_filePath;
  86. QScopedPointer<QSettings> m_settings;
  87. };
  88. } // namespace Qfw
  89. #endif // CONFIG_H