SettingsSection.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "SettingsSection.h"
  2. #include "QsLog.h"
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. SettingsSection::SettingsSection(const QString& sectionID, quint8 platforms, int _orderIndex, QObject* parent)
  5. : QObject(parent), m_sectionID(sectionID), m_orderIndex(_orderIndex), m_platform(platforms), m_hidden(false), m_storage(false)
  6. {
  7. m_values.clear();
  8. }
  9. /////////////////////////////////////////////////////////////////////////////////////////
  10. void SettingsSection::registerSetting(SettingsValue* value)
  11. {
  12. if (m_values.contains(value->key()))
  13. {
  14. QLOG_WARN() << QString("Trying to register %1.%2 multiple times").arg(m_sectionID).arg(value->key());
  15. return;
  16. }
  17. value->setParent(this);
  18. m_values[value->key()] = value;
  19. }
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////
  21. void SettingsSection::setValues(const QVariant& values)
  22. {
  23. QVariantMap map = values.toMap();
  24. QVariantMap updatedValues;
  25. // values not included in the map are reset to default
  26. for(const QString& key : m_values.keys())
  27. {
  28. if (!map.contains(key))
  29. map[key] = m_values[key]->defaultValue();
  30. }
  31. for(const QString& key : map.keys())
  32. {
  33. if (key.isEmpty())
  34. continue;
  35. bool haveKey = m_values.contains(key);
  36. if (haveKey && m_values[key]->value() == map[key])
  37. continue;
  38. if (haveKey)
  39. {
  40. m_values[key]->setValue(map[key]);
  41. }
  42. else
  43. {
  44. m_values[key] = new SettingsValue(key, QVariant(), PLATFORM_ANY, this);
  45. m_values[key]->setValue(map[key]);
  46. }
  47. updatedValues.insert(key, map[key]);
  48. }
  49. if (updatedValues.size() > 0)
  50. emit valuesUpdated(updatedValues);
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////
  53. QVariant SettingsSection::value(const QString& key)
  54. {
  55. if (m_values.contains(key))
  56. return m_values[key]->value();
  57. QLOG_WARN() << "Looking for value:" << key << "in section:" << m_sectionID << "but it can't be found";
  58. return QVariant();
  59. }
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////
  61. void SettingsSection::updatePossibleValues(const QString &key, const QVariantList &possibleValues)
  62. {
  63. if (m_values.contains(key))
  64. m_values[key]->setPossibleValues(possibleValues);
  65. }
  66. ///////////////////////////////////////////////////////////////////////////////////////////////////
  67. QVariantList SettingsSection::possibleValues(const QString& key)
  68. {
  69. if (m_values.contains(key))
  70. return m_values[key]->possibleValues();
  71. return QVariantList();
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////
  74. bool SettingsSection::setValue(const QString& key, const QVariant& value)
  75. {
  76. if (key == "index")
  77. return false;
  78. QVariantMap values;
  79. // populate with default values (setValues() resets missing values)
  80. for(const QString& entry : m_values.keys())
  81. values[entry] = m_values[entry]->value();
  82. values[key] = value;
  83. setValues(values);
  84. return true;
  85. }
  86. ///////////////////////////////////////////////////////////////////////////////////////////////////
  87. void SettingsSection::removeValue(const QString &key)
  88. {
  89. if (m_values.contains(key))
  90. {
  91. m_values[key]->deleteLater();
  92. m_values.remove(key);
  93. }
  94. }
  95. ///////////////////////////////////////////////////////////////////////////////////////////////////
  96. void SettingsSection::removeValues()
  97. {
  98. m_values.clear();
  99. }
  100. ///////////////////////////////////////////////////////////////////////////////////////////////////
  101. const QVariantMap SettingsSection::allValues() const
  102. {
  103. QVariantMap values;
  104. for(SettingsValue* val : m_values.values())
  105. values[val->key()] = val->value();
  106. return values;
  107. }
  108. /////////////////////////////////////////////////////////////////////////////////////////
  109. struct ValueSortOrder
  110. {
  111. inline bool operator()(SettingsValue* a, SettingsValue* b)
  112. {
  113. return a->indexOrder() < b->indexOrder();
  114. }
  115. };
  116. ///////////////////////////////////////////////////////////////////////////////////////////////////
  117. const QVariantMap SettingsSection::descriptions() const
  118. {
  119. QVariantMap map;
  120. map.insert("key", m_sectionID);
  121. QList<SettingsValue*> list = m_values.values();
  122. std::sort(list.begin(), list.end(), ValueSortOrder());
  123. QVariantList settings;
  124. for(SettingsValue* value : list)
  125. {
  126. if (!value->isHidden())
  127. settings.push_back(value->descriptions());
  128. }
  129. map.insert("settings", settings);
  130. return map;
  131. }
  132. ///////////////////////////////////////////////////////////////////////////////////////////////////
  133. void SettingsSection::resetToDefault()
  134. {
  135. for(const QString& key : m_values.keys())
  136. m_values[key]->reset();
  137. }
  138. /////////////////////////////////////////////////////////////////////////////////////////
  139. bool SettingsSection::isHidden() const
  140. {
  141. bool correctPlatform = ((m_platform & Utils::CurrentPlatform()) == Utils::CurrentPlatform());
  142. return (m_hidden || !correctPlatform);
  143. }