[vlc-devel] [PATCH 1/8] qml: Add the playercontrolbarmodel

Rohan Rajpal rohan17089 at iiitd.ac.in
Mon Jun 17 16:54:55 CEST 2019


Add the model for the player control bar
This model has the config of the buttons and their
positions on the player controlbar
---
 modules/gui/qt/Makefile.am                    |   2 +
 .../qt/components/playercontrolbarmodel.cpp   | 161 ++++++++++++++++++
 .../qt/components/playercontrolbarmodel.hpp   | 136 +++++++++++++++
 modules/gui/qt/main_interface.cpp             |   2 +
 4 files changed, 301 insertions(+)
 create mode 100644 modules/gui/qt/components/playercontrolbarmodel.cpp
 create mode 100644 modules/gui/qt/components/playercontrolbarmodel.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index f84c0e3954..feed9827b6 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -101,6 +101,7 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/components/interface_widgets.cpp \
 	gui/qt/components/interface_widgets.hpp \
 	gui/qt/components/controller.cpp gui/qt/components/controller.hpp \
+	gui/qt/components/playercontrolbarmodel.cpp gui/qt/components/playercontrolbarmodel.hpp \
 	gui/qt/components/controller_widget.cpp \
 	gui/qt/components/controller_widget.hpp \
 	gui/qt/components/recent_media_model.cpp  \
@@ -253,6 +254,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/components/interface_widgets.moc.cpp \
 	gui/qt/components/navigation_history.moc.cpp \
 	gui/qt/components/controller.moc.cpp \
+	gui/qt/components/playercontrolbarmodel.moc.cpp \
 	gui/qt/components/controller_widget.moc.cpp \
 	gui/qt/components/custom_menus.moc.cpp \
 	gui/qt/components/recent_media_model.moc.cpp \
diff --git a/modules/gui/qt/components/playercontrolbarmodel.cpp b/modules/gui/qt/components/playercontrolbarmodel.cpp
new file mode 100644
index 0000000000..2fd1df9682
--- /dev/null
+++ b/modules/gui/qt/components/playercontrolbarmodel.cpp
@@ -0,0 +1,161 @@
+/*****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+#include <QSettings>
+
+#include "qt.hpp"
+#include "playercontrolbarmodel.hpp"
+
+#define MAIN_TB1_DEFAULT "67;68;37;65;19;22;0-2;23;20;65;35;7"
+
+PlayerControlBarModel::PlayerControlBarModel(QObject *_parent) : QAbstractListModel(_parent)
+{
+}
+
+void PlayerControlBarModel::loadConfig()
+{
+    p_intf = m_mainCtx->getIntf();
+    QString config = getSettings() ->value( "MainWindow/PlayerControlToolbar1", MAIN_TB1_DEFAULT )
+                                            .toString();
+    parseAndAdd(config);
+}
+
+QVector<PlayerControlBarModel::IconToolButton> PlayerControlBarModel::buttons() const
+{
+    return mButtons;
+}
+
+bool PlayerControlBarModel::setButtonAt(int index, const IconToolButton &button)
+{
+    if(index < 0 || index >= mButtons.size())
+        return false;
+    const IconToolButton &oldButton = mButtons.at(index);
+
+    if (button.size == oldButton.size && button.id == oldButton.id)
+        return false;
+
+    mButtons[index] = button;
+    return true;
+}
+
+void PlayerControlBarModel::parseAndAdd(QString &config)
+{
+    beginInsertRows(QModelIndex(),rowCount(),rowCount()+config.split(";", QString::SkipEmptyParts).length() - 1);
+
+    for (const QString& iconPropertyTxt : config.split( ";", QString::SkipEmptyParts ) )
+    {
+        QStringList list2 = iconPropertyTxt.split( "-" );
+
+        if( list2.count() < 1 )
+        {
+            msg_Warn( p_intf, "Parsing error 1. Please, report this." );
+            continue;
+        }
+        bool ok;
+        int i_option = WIDGET_NORMAL;
+        ButtonType_e i_type = static_cast<ButtonType_e>(list2.at( 0 ).toInt( &ok ));
+        if( !ok )
+        {
+            msg_Warn( p_intf, "Parsing error 2. Please, report this." );
+            continue;
+        }
+
+        if( list2.count() > 1 )
+        {
+            i_option = list2.at( 1 ).toInt( &ok );
+            if( !ok )
+            {
+                msg_Warn( p_intf, "Parsing error 3. Please, report this." );
+                continue;
+            }
+        }
+
+        mButtons.append({ i_type , i_option});
+    }
+
+    endInsertRows();
+}
+
+int PlayerControlBarModel::rowCount(const QModelIndex &parent) const
+{
+    // For list models only the root node (an invalid parent) should return the list's size. For all
+    // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
+    if (parent.isValid() )
+        return 0;
+
+    return buttons().size();
+}
+
+QVariant PlayerControlBarModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid() )
+        return QVariant();
+
+    const IconToolButton button = mButtons.at(index.row());
+
+    switch (role) {
+        case ID_ROLE:
+            return QVariant(button.id);
+
+        case SIZE_ROLE:
+            return QVariant(button.size);
+    }
+    return QVariant();
+}
+
+bool PlayerControlBarModel::setData(const QModelIndex &index, const QVariant &value, int role)
+{
+    IconToolButton button = mButtons.at(index.row());
+    switch (role) {
+        case ID_ROLE:
+            button.id = value.toInt();
+            break;
+        case SIZE_ROLE:
+            button.size = value.toInt();
+    }
+
+    if (setButtonAt(index.row(),button)) {
+        emit dataChanged(index, index, QVector<int>() << role);
+        return true;
+    }
+    return false;
+}
+
+Qt::ItemFlags PlayerControlBarModel::flags(const QModelIndex &index) const
+{
+    if (!index.isValid())
+        return Qt::NoItemFlags;
+
+    return Qt::ItemIsEditable;
+}
+
+QHash<int, QByteArray> PlayerControlBarModel::roleNames() const
+{
+    QHash<int, QByteArray> names;
+
+    names[ID_ROLE] = "id";
+    names[SIZE_ROLE] = "size";
+
+    return names;
+}
+
+void PlayerControlBarModel::setMainCtx(QmlMainContext* ctx)
+{
+    m_mainCtx = ctx;
+    loadConfig();
+    emit ctxChanged(ctx);
+}
diff --git a/modules/gui/qt/components/playercontrolbarmodel.hpp b/modules/gui/qt/components/playercontrolbarmodel.hpp
new file mode 100644
index 0000000000..f10f29ce0e
--- /dev/null
+++ b/modules/gui/qt/components/playercontrolbarmodel.hpp
@@ -0,0 +1,136 @@
+/*****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef CONTROLLERMODEL_H
+#define CONTROLLERMODEL_H
+
+#include <QAbstractListModel>
+#include <QVector>
+
+#include "qml_main_context.hpp"
+
+class PlayerControlBarModel : public QAbstractListModel
+{
+    Q_OBJECT
+    Q_PROPERTY(QmlMainContext* mainCtx READ getMainCtx WRITE setMainCtx NOTIFY ctxChanged)
+
+public:
+    explicit PlayerControlBarModel(QObject *_parent = nullptr);
+    struct IconToolButton
+    {
+        int id;
+        int size;
+    };
+    enum{
+        ID_ROLE,
+        SIZE_ROLE
+    };
+    enum ButtonType_e
+    {
+
+        PLAY_BUTTON,            //0
+        STOP_BUTTON,            //1
+        OPEN_BUTTON,            //2
+        PREV_SLOW_BUTTON,       //3
+        NEXT_FAST_BUTTON,       //4
+        SLOWER_BUTTON,          //5
+        FASTER_BUTTON,          //6
+        FULLSCREEN_BUTTON,      //7
+        DEFULLSCREEN_BUTTON,    //8
+        EXTENDED_BUTTON,        //9
+        PLAYLIST_BUTTON,        //10
+        SNAPSHOT_BUTTON,        //11
+        RECORD_BUTTON,          //12
+        ATOB_BUTTON,            //13
+        FRAME_BUTTON,           //14
+        REVERSE_BUTTON,         //15
+        SKIP_BACK_BUTTON,       //16
+        SKIP_FW_BUTTON,         //17
+        QUIT_BUTTON,            //18
+        RANDOM_BUTTON,          //19
+        LOOP_BUTTON,            //20
+        INFO_BUTTON,            //21
+        PREVIOUS_BUTTON,        //22
+        NEXT_BUTTON,            //23
+        OPEN_SUB_BUTTON,        //24
+        FULLWIDTH_BUTTON,       //25
+        BUTTON_MAX,             //26
+
+        SPLITTER = 0x20,        //32
+        INPUT_SLIDER,           //33
+        TIME_LABEL,             //34
+        VOLUME,                 //35
+        VOLUME_SPECIAL,         //36
+        MENU_BUTTONS,           //37
+        TELETEXT_BUTTONS,       //38
+        ADVANCED_CONTROLLER,    //39
+        PLAYBACK_BUTTONS,       //40
+        ASPECT_RATIO_COMBOBOX,  //41
+        SPEED_LABEL,            //42
+        TIME_LABEL_ELAPSED,     //43
+        TIME_LABEL_REMAINING,   //44
+        SPECIAL_MAX,            //45
+
+        WIDGET_SPACER = 0x40,   //64
+        WIDGET_SPACER_EXTEND,   //65
+        WIDGET_MAX,             //66
+        GOBACK_BUTTON,          //67
+        LANG_BUTTON             //68
+    };
+    Q_ENUM(ButtonType_e)
+
+    enum ButtonSize
+    {
+        WIDGET_NORMAL = 0x0,
+        WIDGET_BIG    = 0x2,
+    };
+    Q_ENUM(ButtonSize)
+    // Basic functionality:
+    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
+
+    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+
+    // Editable:
+    bool setData(const QModelIndex &index, const QVariant &value,
+                 int role = Qt::EditRole) override;
+
+    Qt::ItemFlags flags(const QModelIndex& index) const override;
+
+    virtual QHash<int, QByteArray> roleNames() const override;
+
+    inline QmlMainContext* getMainCtx() const { return m_mainCtx; }
+    void setMainCtx(QmlMainContext*);
+
+signals:
+    void ctxChanged(QmlMainContext*);
+
+protected:
+    intf_thread_t       *p_intf;
+
+private:
+    QVector<IconToolButton> mButtons;
+    QVector<IconToolButton> buttons() const;
+
+    void parseAndAdd(QString& config);
+    void loadConfig();
+    bool setButtonAt(int index, const IconToolButton &button);
+
+    QmlMainContext* m_mainCtx;
+};
+
+#endif // CONTROLLERMODEL_H
diff --git a/modules/gui/qt/main_interface.cpp b/modules/gui/qt/main_interface.cpp
index 04f05d58fd..029a9f9d0b 100644
--- a/modules/gui/qt/main_interface.cpp
+++ b/modules/gui/qt/main_interface.cpp
@@ -56,6 +56,7 @@
 #include "components/navigation_history.hpp"
 #include "components/aboutmodel.hpp"
 #include "components/dialogmodel.hpp"
+#include "components/playercontrolbarmodel.hpp"
 
 #include "components/voutwindow/qvoutwindowdummy.hpp"
 
@@ -361,6 +362,7 @@ void MainInterface::createMainWidget( QSettings * )
 
     qmlRegisterType<QmlEventFilter>( "org.videolan.vlc", 0, 1, "EventFilter" );
 
+    qmlRegisterType<PlayerControlBarModel>( "org.videolan.vlc", 0, 1, "PlayerControlBarModel");
 
     mediacenterView = new QQuickWidget(this);
     mediacenterView->setClearColor(Qt::transparent);
-- 
2.17.1



More information about the vlc-devel mailing list