From 09ca278c449b9fac7f90225971c5deb01a354e76 Mon Sep 17 00:00:00 2001
From: Adam Leung <adamjleung123@gmail.com>
Date: Wed, 18 Aug 2021 17:36:20 +1000
Subject: [PATCH] qt: Added distinct profile id parameter to controlbar
 profiles

---
 .../qt/dialogs/toolbar/controlbar_profile.cpp | 15 +++++++++
 .../qt/dialogs/toolbar/controlbar_profile.hpp | 10 ++++++
 .../toolbar/controlbar_profile_model.cpp      | 33 ++++++++++++++++---
 .../toolbar/controlbar_profile_model.hpp      |  6 +++-
 4 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp b/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp
index c04c46dafed7..dff7d853a996 100644
--- a/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp
+++ b/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp
@@ -162,6 +162,16 @@ void ControlbarProfile::setName(const QString &name)
     emit nameChanged(m_name);
 }
 
+void ControlbarProfile::setId( const int id )
+{
+    if(id == m_id)
+        return;
+
+    m_id = id;
+
+    emit idChanged(m_id);
+}
+
 bool ControlbarProfile::dirty() const
 {
     return (m_dirty > 0);
@@ -172,6 +182,11 @@ QString ControlbarProfile::name() const
     return m_name;
 }
 
+int ControlbarProfile::id() const
+{
+    return m_id;
+}
+
 void ControlbarProfile::injectDefaults(bool resetDirty)
 {
     injectModel(m_defaults);
diff --git a/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp b/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp
index c686b9e03694..fb1a22e73c5a 100644
--- a/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp
+++ b/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp
@@ -31,6 +31,7 @@ class ControlbarProfile : public QObject
 
     Q_PROPERTY(bool dirty READ dirty RESET resetDirty NOTIFY dirtyChanged FINAL)
     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
+    Q_PROPERTY(int profileId READ id WRITE setId NOTIFY idChanged FINAL)
 
     friend class ControlbarProfileModel;
 
@@ -50,9 +51,16 @@ public:
     bool dirty() const;
     QString name() const;
 
+    // The id is a unique number supplied to each profile
+    // which differentiates it independently from its index
+    // in the profiles array. Provides a more stable way to
+    // identify a profile than using its name.
+    int id() const;
+
 public slots:
     void resetDirty();
     void setName(const QString& name);
+    void setId( const int id );
 
 private:
     // m_dirty indicates the count of PlayerControlbarModel
@@ -60,6 +68,7 @@ private:
     // set true.
     int m_dirty = 0;
 
+    int m_id = -1;
     QString m_name {"N/A"};
     bool m_pauseControlListGeneration = false;
 
@@ -84,6 +93,7 @@ private slots:
 signals:
     void dirtyChanged(bool dirty);
     void nameChanged(QString name);
+    void idChanged(int id);
 
     void controlListChanged(const QVector<int>& linearControlList);
 };
diff --git a/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp b/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp
index 5d7a4cc8cb22..23b9f7987ca5 100644
--- a/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp
+++ b/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp
@@ -28,6 +28,7 @@
 #define SETTINGS_ARRAYNAME_PROFILES "Profiles"
 #define SETTINGS_KEY_NAME "Name"
 #define SETTINGS_KEY_MODEL "Model"
+#define SETTINGS_KEY_ID "Id"
 
 #define SETTINGS_CONTROL_SEPARATOR ","
 #define SETTINGS_CONFIGURATION_SEPARATOR "|"
@@ -37,6 +38,7 @@ decltype (ControlbarProfileModel::m_defaults)
     ControlbarProfileModel::m_defaults =
         {
             {
+                MINIMALIST_STYLE,
                 N_("Minimalist Style"),
                 {
                     {
@@ -89,6 +91,7 @@ decltype (ControlbarProfileModel::m_defaults)
                 }
             },
             {
+                ONE_LINER_STYLE,
                 N_("One-liner Style"),
                 {
                     {
@@ -145,6 +148,7 @@ decltype (ControlbarProfileModel::m_defaults)
                 }
             },
             {
+                SIMPLEST_STYLE,
                 N_("Simplest Style"),
                 {
                     {
@@ -186,6 +190,7 @@ decltype (ControlbarProfileModel::m_defaults)
                 }
             },
             {
+                CLASSIC_STYLE,
                 N_("Classic Style"),
                 {
                     {
@@ -281,12 +286,13 @@ void ControlbarProfileModel::insertDefaults()
 {
     // First, add a blank new profile:
     // ControlbarProfile will inject the default configurations during its construction.
-    newProfile(tr("Default Profile"));
+    m_maxId = 0;
+    newProfile(tr("Default Profile"), DEFAULT_STYLE);
 
     // Add default profiles:
     for (const auto& i : m_defaults)
     {
-        const auto ptrNewProfile = newProfile(qfut(i.name));
+        const auto ptrNewProfile = newProfile(qfut(i.name), i.id);
         if (!ptrNewProfile)
             continue;
 
@@ -481,6 +487,18 @@ ControlbarProfile* ControlbarProfileModel::currentModel() const
     return getProfile(selectedProfile());
 }
 
+/* Set the selected profile to the profile with the matching id */
+bool ControlbarProfileModel::setSelectedProfileFromId(int id)
+{
+    for(int i = 0; i < rowCount(); i++)
+    {
+        if(id == m_profiles.at(i)->id())
+            return setSelectedProfile(i);
+    }
+
+    return false;
+}
+
 void ControlbarProfileModel::save(bool clearDirty) const
 {
     assert(m_intf);
@@ -541,6 +559,7 @@ void ControlbarProfileModel::save(bool clearDirty) const
 
         settings->setValue(SETTINGS_KEY_NAME, m_profiles.at(i)->name());
         settings->setValue(SETTINGS_KEY_MODEL, val);
+        settings->setValue(SETTINGS_KEY_ID, m_profiles.at(i)->id());
     }
 
     settings->endArray();
@@ -587,6 +606,7 @@ bool ControlbarProfileModel::reload()
 
         const auto ptrNewProfile = new ControlbarProfile(this);
         ptrNewProfile->setName(settings->value(SETTINGS_KEY_NAME).toString());
+        ptrNewProfile->setId(settings->value(SETTINGS_KEY_ID).toInt());
 
         for (auto j : val)
         {
@@ -642,6 +662,7 @@ bool ControlbarProfileModel::reload()
 
     bool ok = false;
     int index = settings->value(SETTINGS_KEY_SELECTEDPROFILE).toInt(&ok);
+    m_maxId = m_profiles.isEmpty() ? 0 : m_profiles.back()->id() + 1;
 
     if (ok)
         setSelectedProfile(index);
@@ -692,7 +713,7 @@ ControlbarProfile *ControlbarProfileModel::getProfile(int index) const
     return m_profiles.at(index);
 }
 
-ControlbarProfile *ControlbarProfileModel::newProfile(const QString &name)
+ControlbarProfile *ControlbarProfileModel::newProfile(const QString &name, const int id)
 {
     if (name.isEmpty())
         return nullptr;
@@ -700,6 +721,8 @@ ControlbarProfile *ControlbarProfileModel::newProfile(const QString &name)
     const auto ptrProfile = newProfile();
 
     ptrProfile->setName(generateUniqueName(name));
+    ptrProfile->setId(id);
+    m_maxId++;
 
     return ptrProfile;
 }
@@ -719,7 +742,8 @@ ControlbarProfile *ControlbarProfileModel::newProfile()
 
 ControlbarProfile *ControlbarProfileModel::cloneProfile(const ControlbarProfile *profile)
 {
-    const auto ptrNewProfile = newProfile(profile->name());
+    // Any new profiles will just have the next incremental id
+    const auto ptrNewProfile = newProfile(profile->name(), m_maxId);
 
     if (!ptrNewProfile)
         return nullptr;
@@ -763,7 +787,6 @@ void ControlbarProfileModel::deleteSelectedProfile()
         return;
 
     const auto _selectedProfile = m_selectedProfile;
-
     beginRemoveRows(QModelIndex(), _selectedProfile, _selectedProfile);
 
     m_selectedProfile = -1;
diff --git a/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.hpp b/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.hpp
index bbf2b3a3bf18..4ce71bbe2549 100644
--- a/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.hpp
+++ b/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.hpp
@@ -42,6 +42,7 @@ public:
     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
 
     QHash<int, QByteArray> roleNames() const override;
+    enum {DEFAULT_STYLE, MINIMALIST_STYLE, ONE_LINER_STYLE, SIMPLEST_STYLE, CLASSIC_STYLE};
 
     // Editable:
     Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value,
@@ -62,13 +63,14 @@ public:
 
     int selectedProfile() const;
     ControlbarProfile* currentModel() const;
+    bool setSelectedProfileFromId(int id);
 
     ControlbarProfile* cloneProfile(const ControlbarProfile* profile);
     Q_INVOKABLE void cloneSelectedProfile(const QString& newProfileName);
 
     Q_INVOKABLE ControlbarProfile* getProfile(int index) const;
 
-    Q_INVOKABLE ControlbarProfile* newProfile(const QString& name);
+    Q_INVOKABLE ControlbarProfile* newProfile(const QString& name, const int id);
     ControlbarProfile* newProfile();
 
     Q_INVOKABLE void deleteSelectedProfile();
@@ -89,8 +91,10 @@ private:
     QVector<ControlbarProfile *> m_profiles;
 
     int m_selectedProfile = -1;
+    int m_maxId = 0;
 
     struct Profile {
+        const int id;
         const char* name;
         QVector<ControlbarProfile::Configuration> modelData;
     };
-- 
GitLab