Config.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "Config.h"
  2. #include <QColor>
  3. #include <QCoreApplication>
  4. #include <QTextCodec>
  5. using namespace Qfw;
  6. ConfigItem::ConfigItem(const QString &_group, const QString &_name, const QVariant &_defaultValue, QObject *parent)
  7. : QObject(parent), group(_group), name(_name), m_value(_defaultValue)
  8. {
  9. }
  10. void ConfigItem::setValue(const QVariant &value)
  11. {
  12. m_value = value;
  13. }
  14. QVariant ConfigItem::value() const
  15. {
  16. return m_value;
  17. }
  18. /// get the config key separated by `.`
  19. QString ConfigItem::key()
  20. {
  21. if (name.isEmpty()) {
  22. return group;
  23. } else {
  24. return group + "." + name;
  25. }
  26. }
  27. QConfig::QConfig(QObject *parent)
  28. : QObject(parent), m_filePath(QCoreApplication::applicationDirPath() + "/config/config.ini")
  29. {
  30. init();
  31. }
  32. QConfig::QConfig(const QString &customPath, QObject *parent) : QObject(parent), m_filePath(customPath)
  33. {
  34. init();
  35. }
  36. void QConfig::init()
  37. {
  38. m_settings.reset(new QSettings(m_filePath, QSettings::IniFormat));
  39. m_settings->setIniCodec(QTextCodec::codecForName("utf-8"));
  40. }
  41. QVariant QConfig::get(const QString &key, const QVariant &defaultValue) const
  42. {
  43. return m_settings->value(key, defaultValue);
  44. }
  45. void QConfig::set(const QString &key, const QVariant &val, bool save)
  46. {
  47. if (m_settings->value(key) == val) {
  48. return;
  49. }
  50. if (save) {
  51. m_settings->setValue(key, val);
  52. }
  53. if (key.toLower() == "qfluentwidgets/restart") {
  54. emit appRestartSig();
  55. }
  56. if (key.toLower() == "qfluentwidgets/thememode") {
  57. emit themeChanged((Theme)val.toInt());
  58. }
  59. if (key.toLower() == "qfluentwidgets/themecolor") {
  60. emit themeColorChanged(val.value<QColor>());
  61. }
  62. }
  63. void QConfig::setTheme(Theme t)
  64. {
  65. set("QFluentWidgets/ThemeMode", int(t));
  66. }
  67. Theme QConfig::theme() const
  68. {
  69. return (Theme)get("QFluentWidgets/ThemeMode", 0).toInt();
  70. }
  71. void QConfig::setThemeColor(const QColor &c)
  72. {
  73. set("QFluentWidgets/ThemeColor", c);
  74. }
  75. QColor QConfig::themeColor() const
  76. {
  77. return get("QFluentWidgets/ThemeColor", QColor("#009faa")).value<QColor>();
  78. }