SettingsSection.cpp 5.9 KB

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