SettingsSection.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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::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::removeValue(const QString &key)
  97. {
  98. if (m_values.contains(key))
  99. {
  100. m_values[key]->deleteLater();
  101. m_values.remove(key);
  102. }
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////////////////////////
  105. void SettingsSection::removeValues()
  106. {
  107. m_values.clear();
  108. }
  109. ///////////////////////////////////////////////////////////////////////////////////////////////////
  110. const QVariantMap SettingsSection::allValues() const
  111. {
  112. QVariantMap values;
  113. for(SettingsValue* val : m_values.values())
  114. values[val->key()] = val->value();
  115. return values;
  116. }
  117. /////////////////////////////////////////////////////////////////////////////////////////
  118. struct ValueSortOrder
  119. {
  120. inline bool operator()(SettingsValue* a, SettingsValue* b)
  121. {
  122. return a->indexOrder() < b->indexOrder();
  123. }
  124. };
  125. ///////////////////////////////////////////////////////////////////////////////////////////////////
  126. const QVariantMap SettingsSection::descriptions() const
  127. {
  128. QVariantMap map;
  129. map.insert("key", m_sectionID);
  130. QList<SettingsValue*> list = m_values.values();
  131. std::sort(list.begin(), list.end(), ValueSortOrder());
  132. QVariantList settings;
  133. for(SettingsValue* value : list)
  134. {
  135. if (!value->isHidden())
  136. settings.push_back(value->descriptions());
  137. }
  138. map.insert("settings", settings);
  139. return map;
  140. }
  141. ///////////////////////////////////////////////////////////////////////////////////////////////////
  142. void SettingsSection::resetToDefault()
  143. {
  144. for(const QString& key : m_values.keys())
  145. m_values[key]->reset();
  146. }
  147. /////////////////////////////////////////////////////////////////////////////////////////
  148. bool SettingsSection::isHidden() const
  149. {
  150. bool correctPlatform = ((m_platform & Utils::CurrentPlatform()) == Utils::CurrentPlatform());
  151. return (m_hidden || !correctPlatform);
  152. }