SettingsSection.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include <QDebug>
  2. #include "SettingsSection.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. qWarning() << 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 "removed"
  26. for(const QString& key : m_values.keys())
  27. {
  28. if (!map.contains(key))
  29. resetValueNoNotify(key, updatedValues);
  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. {
  51. emit valuesUpdated(updatedValues);
  52. emit SettingsComponent::Get().sectionValueUpdate(m_sectionID, updatedValues);
  53. }
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. QVariant SettingsSection::defaultValue(const QString& key)
  57. {
  58. if (m_values.contains(key))
  59. return m_values[key]->defaultValue();
  60. qWarning() << "Looking for defaultValue:" << key << "in section:" << m_sectionID << "but it can't be found";
  61. return QVariant();
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////
  64. QVariant SettingsSection::value(const QString& key)
  65. {
  66. if (m_values.contains(key))
  67. return m_values[key]->value();
  68. qWarning() << "Looking for value:" << key << "in section:" << m_sectionID << "but it can't be found";
  69. return QVariant();
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////////////////////////
  72. void SettingsSection::updatePossibleValues(const QString &key, const QVariantList &possibleValues)
  73. {
  74. if (m_values.contains(key))
  75. m_values[key]->setPossibleValues(possibleValues);
  76. emit SettingsComponent::Get().groupUpdate(m_sectionID, descriptions());
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////////////////////////
  79. QVariantList SettingsSection::possibleValues(const QString& key)
  80. {
  81. if (m_values.contains(key))
  82. return m_values[key]->possibleValues();
  83. return QVariantList();
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////////////////////////
  86. bool SettingsSection::setValue(const QString& key, const QVariant& value)
  87. {
  88. if (key == "index")
  89. return false;
  90. QVariantMap values;
  91. // populate with default values (setValues() resets missing values)
  92. for(const QString& entry : m_values.keys())
  93. values[entry] = m_values[entry]->value();
  94. values[key] = value;
  95. setValues(values);
  96. return true;
  97. }
  98. ///////////////////////////////////////////////////////////////////////////////////////////////////
  99. void SettingsSection::resetValueNoNotify(const QString& key, QVariantMap& updatedValues)
  100. {
  101. if (!m_values.contains(key))
  102. return;
  103. SettingsValue* val = m_values[key];
  104. if (val->hasDescription())
  105. {
  106. if (val->value() == val->defaultValue())
  107. return;
  108. val->setValue(val->defaultValue());
  109. updatedValues[key] = val->value();
  110. }
  111. else
  112. {
  113. m_values[key]->deleteLater();
  114. m_values.remove(key);
  115. }
  116. }
  117. ///////////////////////////////////////////////////////////////////////////////////////////////////
  118. void SettingsSection::resetValue(const QString &key)
  119. {
  120. QVariantMap updatedValues;
  121. resetValueNoNotify(key, updatedValues);
  122. if (updatedValues.size() > 0)
  123. emit valuesUpdated(updatedValues);
  124. }
  125. ///////////////////////////////////////////////////////////////////////////////////////////////////
  126. void SettingsSection::resetValues()
  127. {
  128. QVariantMap updatedValues;
  129. for (auto key : m_values.keys())
  130. resetValueNoNotify(key, updatedValues);
  131. if (updatedValues.size() > 0)
  132. emit valuesUpdated(updatedValues);
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////
  135. const QVariantMap SettingsSection::allValues() const
  136. {
  137. QVariantMap values;
  138. for(SettingsValue* val : m_values.values())
  139. values[val->key()] = val->value();
  140. return values;
  141. }
  142. /////////////////////////////////////////////////////////////////////////////////////////
  143. struct ValueSortOrder
  144. {
  145. inline bool operator()(SettingsValue* a, SettingsValue* b)
  146. {
  147. return a->indexOrder() < b->indexOrder();
  148. }
  149. };
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////
  151. const QVariantMap SettingsSection::descriptions() const
  152. {
  153. QVariantMap map;
  154. map.insert("key", m_sectionID);
  155. QList<SettingsValue*> list = m_values.values();
  156. std::sort(list.begin(), list.end(), ValueSortOrder());
  157. QVariantList settings;
  158. for(SettingsValue* value : list)
  159. {
  160. if (!value->isHidden())
  161. settings.push_back(value->descriptions());
  162. }
  163. map.insert("settings", settings);
  164. return map;
  165. }
  166. /////////////////////////////////////////////////////////////////////////////////////////
  167. bool SettingsSection::isHidden() const
  168. {
  169. bool correctPlatform = ((m_platform & Utils::CurrentPlatform()) == Utils::CurrentPlatform());
  170. return (m_hidden || !correctPlatform);
  171. }