Prechádzať zdrojové kódy

Add generic settings cycle command

Vincent Lang 8 rokov pred
rodič
commit
16133d54f0

+ 47 - 0
src/settings/SettingsComponent.cpp

@@ -18,6 +18,53 @@ SettingsComponent::SettingsComponent(QObject *parent) : ComponentBase(parent), m
 {
 }
 
+/////////////////////////////////////////////////////////////////////////////////////////
+void SettingsComponent::componentPostInitialize()
+{
+  InputComponent::Get().registerHostCommand("cycle_setting", this, "cycleSetting");
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+void SettingsComponent::cycleSetting(const QString& args)
+{
+  QString settingName = args;
+  QStringList sub = settingName.split(".");
+  if (sub.size() != 2)
+  {
+    QLOG_ERROR() << "Setting must be in the form section.name but got:" << settingName;
+    return;
+  }
+  QString sectionID = sub[0];
+  QString valueName = sub[1];
+  SettingsSection* section = getSection(sectionID);
+  if (!section)
+  {
+    QLOG_ERROR() << "Section" << sectionID << "is unknown";
+    return;
+  }
+  QVariantList values = section->possibleValues(valueName);
+  if (values.size() == 0)
+  {
+    QLOG_ERROR() << "Setting" << settingName << "is unknown or is not cycleable.";
+    return;
+  }
+  QVariant currentValue = section->value(valueName);
+  int nextValueIndex = 0;
+  for (int n = 0; n < values.size(); n++)
+  {
+    if (currentValue == values[n].toMap()["value"])
+    {
+      nextValueIndex = n + 1;
+      break;
+    }
+  }
+  if (nextValueIndex >= values.size())
+    nextValueIndex = 0;
+  QVariant nextValue = values[nextValueIndex].toMap()["value"];
+  QLOG_DEBUG() << "Setting" << settingName << "to" << nextValue;
+  setValue(sectionID, valueName, nextValue);
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 void SettingsComponent::updatePossibleValues(const QString &sectionID, const QString &key, const QVariantList &possibleValues)
 {

+ 4 - 0
src/settings/SettingsComponent.h

@@ -33,6 +33,7 @@ class SettingsComponent : public ComponentBase
 
 public:
   bool componentInitialize() override;
+  void componentPostInitialize() override;
 
   const char* componentName() override { return "settings"; }
   bool componentExport() override { return true; }
@@ -51,6 +52,9 @@ public:
   Q_INVOKABLE void resetToDefault();
   Q_INVOKABLE QVariantList settingDescriptions();
 
+  // host commands
+  Q_SLOT void cycleSetting(const QString& args);
+
   void updatePossibleValues(const QString& sectionID, const QString& key, const QVariantList& possibleValues);
 
   void saveSettings();

+ 8 - 0
src/settings/SettingsSection.cpp

@@ -77,6 +77,14 @@ void SettingsSection::updatePossibleValues(const QString &key, const QVariantLis
     m_values[key]->setPossibleValues(possibleValues);
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+QVariantList SettingsSection::possibleValues(const QString& key)
+{
+  if (m_values.contains(key))
+    return m_values[key]->possibleValues();
+  return QVariantList();
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 bool SettingsSection::setValue(const QString& key, const QVariant& value)
 {

+ 1 - 0
src/settings/SettingsSection.h

@@ -16,6 +16,7 @@ public:
                            int _orderIndex = -1, QObject* parent = nullptr);
 
   void updatePossibleValues(const QString& key, const QVariantList& possibleValues);
+  QVariantList possibleValues(const QString& key);
 
   void setValues(const QVariant& values);
   bool setValue(const QString& key, const QVariant& value);