[vlc-devel] [PATCH 20/29] qt: qml: use enum for player controlbar identifier

Fatih Uzunoglu fuzun54 at outlook.com
Thu Apr 1 22:22:32 UTC 2021


Instead of using QString as the controlbar profile
model player identifier, use enum (int).

This patch constitutes the second step of the
integration of the new QML toolbar editor and the
refactored profile handling. Before this patch,
Players had to be referred again in the toolbar
editor which is bad for maintenance. Now, when
a player is added to the PlayerIdentifier enum,
Players should refer to the enum for their
identifiers.
---
 .../qt/dialogs/toolbar/controlbar_profile.cpp | 21 ++++-------
 .../qt/dialogs/toolbar/controlbar_profile.hpp | 14 ++++----
 .../toolbar/controlbar_profile_model.cpp      | 35 +++++++------------
 .../qt/dialogs/toolbar/qml/ToolbarEditor.qml  |  4 +--
 .../gui/qt/player/player_controlbar_model.hpp | 10 ++++++
 modules/gui/qt/player/qml/MiniPlayer.qml      |  2 +-
 modules/gui/qt/player/qml/Player.qml          |  2 +-
 .../gui/qt/player/qml/PlayerButtonsLayout.qml |  6 ++--
 8 files changed, 43 insertions(+), 51 deletions(-)

diff --git a/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp b/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp
index 4c3e82fb38..aa3c87eb60 100644
--- a/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp
+++ b/modules/gui/qt/dialogs/toolbar/controlbar_profile.cpp
@@ -25,9 +25,7 @@ decltype(ControlbarProfile::m_defaults)
     ControlbarProfile::m_defaults =
         {
             {
-                {
-                    "MainPlayer"
-                },
+                PlayerControlbarModel::Mainplayer,
                 {
                     {
                         {
@@ -49,9 +47,7 @@ decltype(ControlbarProfile::m_defaults)
                 }
             },
             {
-                {
-                    "MiniPlayer"
-                },
+                PlayerControlbarModel::Miniplayer,
                 {
                     {
                         {
@@ -79,11 +75,8 @@ ControlbarProfile::ControlbarProfile(QObject *parent) : QObject(parent)
     injectDefaults();
 }
 
-PlayerControlbarModel *ControlbarProfile::newModel(const QString &identifier)
+PlayerControlbarModel *ControlbarProfile::newModel(int identifier)
 {
-    if (identifier.isEmpty())
-        return nullptr;
-
     if (m_models.contains(identifier))
         return nullptr; // can not allow the same identifier
 
@@ -105,7 +98,7 @@ PlayerControlbarModel *ControlbarProfile::newModel(const QString &identifier)
     return model;
 }
 
-PlayerControlbarModel *ControlbarProfile::getModel(const QString &identifier) const
+PlayerControlbarModel *ControlbarProfile::getModel(int identifier) const
 {
     if (m_models.contains(identifier))
     {
@@ -117,7 +110,7 @@ PlayerControlbarModel *ControlbarProfile::getModel(const QString &identifier) co
     }
 }
 
-void ControlbarProfile::setModelData(const QString &identifier, const std::array<QVector<int>, 3> &data)
+void ControlbarProfile::setModelData(int identifier, const std::array<QVector<int>, 3> &data)
 {
     auto ptrModel = getModel(identifier);
 
@@ -138,7 +131,7 @@ void ControlbarProfile::setModelData(const QString &identifier, const std::array
     ptrModel->setDirty(true);
 }
 
-std::array<QVector<int>, 3> ControlbarProfile::getModelData(const QString &identifier) const
+std::array<QVector<int>, 3> ControlbarProfile::getModelData(int identifier) const
 {
     const auto ptrModel = getModel(identifier);
 
@@ -148,7 +141,7 @@ std::array<QVector<int>, 3> ControlbarProfile::getModelData(const QString &ident
     return ptrModel->serializeModels();
 }
 
-void ControlbarProfile::deleteModel(const QString &identifier)
+void ControlbarProfile::deleteModel(int identifier)
 {
     if (m_models.contains(identifier))
     {
diff --git a/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp b/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp
index e9c486dd01..5e199cdf9f 100644
--- a/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp
+++ b/modules/gui/qt/dialogs/toolbar/controlbar_profile.hpp
@@ -37,13 +37,13 @@ class ControlbarProfile : public QObject
 public:
     explicit ControlbarProfile(QObject *parent = nullptr);
 
-    PlayerControlbarModel* newModel(const QString& identifier);
-    Q_INVOKABLE PlayerControlbarModel* getModel(const QString& identifier) const;
+    PlayerControlbarModel* newModel(int identifier);
+    Q_INVOKABLE PlayerControlbarModel* getModel(int identifier) const;
 
-    void setModelData(const QString& identifier, const std::array<QVector<int>, 3>& data);
-    std::array<QVector<int>, 3> getModelData(const QString& identifier) const;
+    void setModelData(int identifier, const std::array<QVector<int>, 3>& data);
+    std::array<QVector<int>, 3> getModelData(int identifier) const;
 
-    void deleteModel(const QString& identifier);
+    void deleteModel(int identifier);
 
     Q_INVOKABLE void injectDefaults(bool resetDirty = true);
 
@@ -67,10 +67,10 @@ private:
     // QHash when item count is less than 32.
     // Assuming model (player) count to stay below that,
     // QMap is used here.
-    QMap<QString, PlayerControlbarModel *> m_models;
+    QMap<int, PlayerControlbarModel *> m_models;
 
     struct Configuration {
-        QString identifier;
+        int identifier;
         std::array<QVector<int>, 3> data;
     };
     static const QVector<Configuration> m_defaults;
diff --git a/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp b/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp
index 2e6a0cda01..9490c5e5e4 100644
--- a/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp
+++ b/modules/gui/qt/dialogs/toolbar/controlbar_profile_model.cpp
@@ -22,6 +22,7 @@
 #include "qt.hpp"
 #include "controlbar_profile.hpp"
 #include "player/control_list_model.hpp"
+#include "player/player_controlbar_model.hpp"
 
 #define SETTINGS_KEY_SELECTEDPROFILE "SelectedProfile"
 #define SETTINGS_ARRAYNAME_PROFILES "Profiles"
@@ -39,9 +40,7 @@ decltype (ControlbarProfileModel::m_defaults)
                 N_("Minimalist Style"),
                 {
                     {
-                        {
-                            "MainPlayer"
-                        },
+                        PlayerControlbarModel::Mainplayer,
                         {
                             {
                                 {
@@ -69,9 +68,7 @@ decltype (ControlbarProfileModel::m_defaults)
                         }
                     },
                     {
-                        {
-                            "MiniPlayer"
-                        },
+                        PlayerControlbarModel::Miniplayer,
                         {
                             {
                                 {
@@ -95,9 +92,7 @@ decltype (ControlbarProfileModel::m_defaults)
                 N_("One-liner Style"),
                 {
                     {
-                        {
-                            "MainPlayer"
-                        },
+                        PlayerControlbarModel::Mainplayer,
                         {
                             {
                                 {
@@ -127,9 +122,7 @@ decltype (ControlbarProfileModel::m_defaults)
                         }
                     },
                     {
-                        {
-                            "MiniPlayer"
-                        },
+                        PlayerControlbarModel::Miniplayer,
                         {
                             {
                                 {
@@ -155,9 +148,7 @@ decltype (ControlbarProfileModel::m_defaults)
                 N_("Simplest Style"),
                 {
                     {
-                        {
-                            "MainPlayer"
-                        },
+                        PlayerControlbarModel::Mainplayer,
                         {
                             {
                                 {
@@ -175,9 +166,7 @@ decltype (ControlbarProfileModel::m_defaults)
                         }
                     },
                     {
-                        {
-                            "MiniPlayer"
-                        },
+                        PlayerControlbarModel::Miniplayer,
                         {
                             {
                                 {
@@ -421,7 +410,7 @@ void ControlbarProfileModel::save(bool clearDirty) const
         QString val;
         for (auto it = ptrModelMap.constBegin(); it != ptrModelMap.end(); ++it)
         {
-            const QString identifier = it.key();
+            const int identifier = it.key();
 
             const auto serializedModels = m_profiles.at(i)->getModelData(identifier);
 
@@ -443,7 +432,7 @@ void ControlbarProfileModel::save(bool clearDirty) const
                            SETTINGS_CONFIGURATION_SEPARATOR
                            "%3"
                            SETTINGS_CONFIGURATION_SEPARATOR
-                           "%4").arg(identifier,
+                           "%4").arg(QString::number(identifier),
                                      join(serializedModels[0]),
                                      join(serializedModels[1]),
                                      join(serializedModels[2]));
@@ -511,7 +500,9 @@ bool ControlbarProfileModel::reload()
             if (alignments.length() != 4)
                 continue;
 
-            if (alignments[0].toString().isEmpty())
+            bool ok = false;
+            int identifier = alignments[0].toInt(&ok);
+            if (!ok || identifier < 0)
                 continue;
 
             static const auto split = [](auto ref) {
@@ -535,7 +526,7 @@ bool ControlbarProfileModel::reload()
                                                      split(alignments[2]),
                                                      split(alignments[3]) };
 
-            ptrNewProfile->setModelData(alignments[0].toString(), data);
+            ptrNewProfile->setModelData(identifier, data);
             ptrNewProfile->resetDirty(); // Newly loaded model can not be dirty
         }
 
diff --git a/modules/gui/qt/dialogs/toolbar/qml/ToolbarEditor.qml b/modules/gui/qt/dialogs/toolbar/qml/ToolbarEditor.qml
index 3355e93433..fbd4e63ac9 100644
--- a/modules/gui/qt/dialogs/toolbar/qml/ToolbarEditor.qml
+++ b/modules/gui/qt/dialogs/toolbar/qml/ToolbarEditor.qml
@@ -113,7 +113,7 @@ Rectangle{
                         font.pixelSize: VLCStyle.fontSize_xxlarge
                     }
 
-                    readonly property string identifier: "MainPlayer"
+                    readonly property int identifier: PlayerControlbarModel.MainPlayer
                     property var model: mainInterface.controlbarProfileModel.currentModel.getModel(identifier)
 
                     EditorDNDView {
@@ -214,7 +214,7 @@ Rectangle{
                     Layout.preferredHeight: VLCStyle.heightBar_large * 1.25
                     Layout.fillWidth: true
 
-                    readonly property string identifier: "MiniPlayer"
+                    readonly property int identifier: PlayerControlbarModel.MiniPlayer
                     property var model: mainInterface.controlbarProfileModel.currentModel.getModel(identifier)
 
                     EditorDNDView {
diff --git a/modules/gui/qt/player/player_controlbar_model.hpp b/modules/gui/qt/player/player_controlbar_model.hpp
index a6b71319cf..0463122d47 100644
--- a/modules/gui/qt/player/player_controlbar_model.hpp
+++ b/modules/gui/qt/player/player_controlbar_model.hpp
@@ -35,6 +35,16 @@ class PlayerControlbarModel : public QObject
     Q_PROPERTY(ControlListModel* right READ right CONSTANT)
 
 public:
+    // When there is a need to add a new Player, just
+    // add its identifier in this enum and set QML buttons layout
+    // identifier to it. Such as `property int identifier =
+    // PlayerControlbarModel.Mainplayer`.
+    enum PlayerIdentifier {
+        Mainplayer = 0,
+        Miniplayer
+    };
+    Q_ENUM(PlayerIdentifier)
+
     explicit PlayerControlbarModel(QObject *parent = nullptr);
     ~PlayerControlbarModel();
 
diff --git a/modules/gui/qt/player/qml/MiniPlayer.qml b/modules/gui/qt/player/qml/MiniPlayer.qml
index 9cc983dc05..4ee67e412a 100644
--- a/modules/gui/qt/player/qml/MiniPlayer.qml
+++ b/modules/gui/qt/player/qml/MiniPlayer.qml
@@ -83,7 +83,7 @@ Widgets.NavigableFocusScope {
         sliderHeight: VLCStyle.dp(3, VLCStyle.scale)
         sliderBackgroundColor: colors.sliderBarMiniplayerBgColor
         sliderProgressColor: colors.accent
-        identifier: "MiniPlayer"
+        identifier: PlayerControlbarModel.Miniplayer
         navigationParent: root
 
         Keys.onPressed: {
diff --git a/modules/gui/qt/player/qml/Player.qml b/modules/gui/qt/player/qml/Player.qml
index ebe95f5792..6086394665 100644
--- a/modules/gui/qt/player/qml/Player.qml
+++ b/modules/gui/qt/player/qml/Player.qml
@@ -500,7 +500,7 @@ Widgets.NavigableFocusScope {
 
                 onRequestLockUnlockAutoHide: rootPlayer.lockUnlockAutoHide(lock, source)
 
-                identifier: "MainPlayer"
+                identifier: PlayerControlbarModel.Mainplayer
             }
         }
     }
diff --git a/modules/gui/qt/player/qml/PlayerButtonsLayout.qml b/modules/gui/qt/player/qml/PlayerButtonsLayout.qml
index 15f11ae293..dd0f7c5e01 100644
--- a/modules/gui/qt/player/qml/PlayerButtonsLayout.qml
+++ b/modules/gui/qt/player/qml/PlayerButtonsLayout.qml
@@ -42,7 +42,7 @@ Widgets.NavigableFocusScope {
     property real spacing: VLCStyle.margin_normal // spacing between controls
     property real layoutSpacing: VLCStyle.margin_xlarge // spacing between layouts (left, center, and right)
 
-    property string identifier
+    property int identifier: -1
     readonly property var model: {
         if (!!mainInterface.controlbarProfileModel.currentModel)
             mainInterface.controlbarProfileModel.currentModel.getModel(identifier)
@@ -52,10 +52,8 @@ Widgets.NavigableFocusScope {
 
     signal requestLockUnlockAutoHide(bool lock, var source)
 
-
     Component.onCompleted: {
-        console.assert(!!identifier)
-        console.assert(identifier.length > 0)
+        console.assert(identifier >= 0)
     }
 
     ControlButtons {
-- 
2.27.0



More information about the vlc-devel mailing list