[vlc-devel] [PATCH 06/18] qml: move color scheme to main interface

Pierre Lamot pierre at videolabs.io
Wed Sep 23 14:27:40 CEST 2020


---
 modules/gui/qt/Makefile.am                    |   2 +
 .../gui/qt/maininterface/main_interface.cpp   |   7 +-
 .../gui/qt/maininterface/main_interface.hpp   |   6 +
 modules/gui/qt/menus/qml/ViewMenu.qml         |   2 +-
 modules/gui/qt/style/VLCColors.qml            |   8 +-
 modules/gui/qt/util/color_scheme_model.cpp    | 106 ++++++++++++++++++
 modules/gui/qt/util/color_scheme_model.hpp    |  50 +++++++++
 7 files changed, 177 insertions(+), 4 deletions(-)
 create mode 100644 modules/gui/qt/util/color_scheme_model.cpp
 create mode 100644 modules/gui/qt/util/color_scheme_model.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 39860ad33b..8d9e614253 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -186,6 +186,7 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/playlist/playlist_model_p.hpp \
 	gui/qt/util/audio_device_model.cpp  \
 	gui/qt/util/audio_device_model.hpp \
+	gui/qt/util/color_scheme_model.cpp gui/qt/util/color_scheme_model.hpp \
 	gui/qt/util/imagehelper.cpp gui/qt/util/imagehelper.hpp \
 	gui/qt/util/i18n.cpp gui/qt/util/i18n.hpp \
 	gui/qt/util/navigation_history.cpp gui/qt/util/navigation_history.hpp \
@@ -324,6 +325,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/playlist/playlist_item.moc.cpp \
 	gui/qt/playlist/playlist_model.moc.cpp \
 	gui/qt/util/audio_device_model.moc.cpp \
+	gui/qt/util/color_scheme_model.moc.cpp \
 	gui/qt/util/i18n.moc.cpp \
 	gui/qt/util/navigation_history.moc.cpp \
 	gui/qt/util/qml_main_context.moc.cpp \
diff --git a/modules/gui/qt/maininterface/main_interface.cpp b/modules/gui/qt/maininterface/main_interface.cpp
index dfbae7a259..a2c721c0b5 100644
--- a/modules/gui/qt/maininterface/main_interface.cpp
+++ b/modules/gui/qt/maininterface/main_interface.cpp
@@ -36,6 +36,7 @@
 #include "util/qt_dirs.hpp"                     // toNativeSeparators
 #include "util/imagehelper.hpp"
 #include "util/recents.hpp"
+#include "util/color_scheme_model.hpp"
 
 #include "widgets/native/interface_widgets.hpp"     // bgWidget, videoWidget
 #include "dialogs/firstrun/firstrun.hpp"                 // First Run
@@ -148,9 +149,12 @@ MainInterface::MainInterface(intf_thread_t *_p_intf , QWidget* parent, Qt::Windo
     playlistVisible  = getSettings()->value( "MainWindow/playlist-visible", false ).toBool();
     playlistWidthFactor = getSettings()->value( "MainWindow/playlist-width-factor", 4.0 ).toDouble();
     m_gridView = getSettings()->value( "MainWindow/grid-view", true).toBool();
-
+    QString currentColorScheme = getSettings()->value( "MainWindow/color-scheme", "system").toString();
     m_showRemainingTime = getSettings()->value( "MainWindow/ShowRemainingTime", false ).toBool();
 
+    m_colorScheme = new ColorSchemeModel(this);
+    m_colorScheme->setCurrent(currentColorScheme);
+
     /* Should the UI stays on top of other windows */
     b_interfaceOnTop = var_InheritBool( p_intf, "video-on-top" );
 
@@ -236,6 +240,7 @@ MainInterface::~MainInterface()
     settings->setValue( "playlist-width-factor", playlistWidthFactor);
 
     settings->setValue( "grid-view", m_gridView );
+    settings->setValue( "color-scheme", m_colorScheme->getCurrent() );
     /* Save the stackCentralW sizes */
     settings->endGroup();
 
diff --git a/modules/gui/qt/maininterface/main_interface.hpp b/modules/gui/qt/maininterface/main_interface.hpp
index e99011dff9..ed5db4e32c 100644
--- a/modules/gui/qt/maininterface/main_interface.hpp
+++ b/modules/gui/qt/maininterface/main_interface.hpp
@@ -28,6 +28,7 @@
 
 #include "widgets/native/qvlcframe.hpp"
 #include "player/player_controller.hpp"
+#include "util/color_scheme_model.hpp"
 
 #include <QSystemTrayIcon>
 #include <QStackedWidget>
@@ -150,6 +151,7 @@ class MainInterface : public QVLCMW
     Q_PROPERTY(float intfScaleFactor READ getIntfScaleFactor NOTIFY intfScaleFactorChanged)
     Q_PROPERTY(bool mediaLibraryAvailable READ hasMediaLibrary CONSTANT)
     Q_PROPERTY(bool gridView READ hasGridView WRITE setGridView NOTIFY gridViewChanged)
+    Q_PROPERTY(ColorSchemeModel* colorScheme READ getColorScheme CONSTANT)
 
 public:
     /* tors */
@@ -185,6 +187,8 @@ public:
     inline float getIntfScaleFactor() const { return m_intfScaleFactor; }
     inline bool hasMediaLibrary() const { return b_hasMedialibrary; }
     inline bool hasGridView() const { return m_gridView; }
+    inline ColorSchemeModel* getColorScheme() const { return m_colorScheme; }
+
 
     bool hasEmbededVideo() const;
     VideoSurfaceProvider* getVideoSurfaceProvider() const;
@@ -247,6 +251,7 @@ protected:
 #endif
     bool                 b_hasMedialibrary;
     bool                 m_gridView;
+    ColorSchemeModel*    m_colorScheme;
 
     /* States */
     bool                 playlistVisible;       ///< Is the playlist visible ?
@@ -312,6 +317,7 @@ signals:
     void toolBarConfUpdated();
     void showRemainingTimeChanged(bool);
     void gridViewChanged( bool );
+    void colorSchemeChanged( QString );
 
     void intfScaleFactorChanged();
 };
diff --git a/modules/gui/qt/menus/qml/ViewMenu.qml b/modules/gui/qt/menus/qml/ViewMenu.qml
index 62cd24e9e1..44d92e5b1a 100644
--- a/modules/gui/qt/menus/qml/ViewMenu.qml
+++ b/modules/gui/qt/menus/qml/ViewMenu.qml
@@ -63,7 +63,7 @@ Widgets.MenuExt {
                 text: modelData
                 checkable: true
                 checked: modelData === VLCStyle.colors.state
-                onTriggered: settings.VLCStyle_colors_state = modelData
+                onTriggered: mainInterface.colorScheme = modelData
             }
         }
     }
diff --git a/modules/gui/qt/style/VLCColors.qml b/modules/gui/qt/style/VLCColors.qml
index a10628e543..efaa276fe4 100644
--- a/modules/gui/qt/style/VLCColors.qml
+++ b/modules/gui/qt/style/VLCColors.qml
@@ -107,9 +107,13 @@ Item {
 
     property color seekpoint: "red";
 
-    property var colorSchemes: ["system", "day", "night"]
+    property var colorSchemes: mainInterface.colorScheme
+    Component.onCompleted:  {
+        mainInterface.colorScheme.setAvailableColorSchemes(["system", "day", "night"])
+    }
+
 
-    state: settings.VLCStyle_colors_state
+    state: mainInterface.colorScheme.current
     states: [
         //other styles are provided for testing purpose
         State {
diff --git a/modules/gui/qt/util/color_scheme_model.cpp b/modules/gui/qt/util/color_scheme_model.cpp
new file mode 100644
index 0000000000..79aacbba3d
--- /dev/null
+++ b/modules/gui/qt/util/color_scheme_model.cpp
@@ -0,0 +1,106 @@
+/*****************************************************************************
+ * Copyright (C) 2020 the VideoLAN team
+ *
+ * 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 "color_scheme_model.hpp"
+
+ColorSchemeModel::ColorSchemeModel(QObject* parent)
+    : QStringListModel(parent)
+{
+}
+
+void ColorSchemeModel::setAvailableColorSchemes(const QStringList& colorSchemeList)
+{
+    setStringList(colorSchemeList);
+
+    //indexOf return -1 on "not found", wich will generate an invalid index
+    int position = colorSchemeList.indexOf(m_current);
+
+    QPersistentModelIndex oldIndex = m_checkedItem;
+    m_checkedItem = index(position);
+    if (oldIndex.isValid())
+        emit dataChanged(oldIndex, oldIndex);
+    if (m_checkedItem.isValid())
+        emit dataChanged(m_checkedItem, m_checkedItem);
+    else
+    {
+        m_current = QString{};
+        emit currentChanged(m_current);
+    }
+}
+
+Qt::ItemFlags ColorSchemeModel::flags (const QModelIndex & index) const
+{
+    Qt::ItemFlags defaultFlags = QStringListModel::flags(index);
+    if (index.isValid()){
+        return defaultFlags | Qt::ItemIsUserCheckable;
+    }
+    return defaultFlags;
+}
+
+
+bool ColorSchemeModel::setData(const QModelIndex &index,
+                                const QVariant &value, int role)
+{
+    if(!index.isValid())
+        return false;
+
+    if (role != Qt::CheckStateRole)
+        return QStringListModel::setData(index, value, role);
+
+    if (value.type() != QVariant::Bool || value.toBool() == false)
+        return false;
+
+    if (index != m_checkedItem) {
+        QPersistentModelIndex oldIndex = m_checkedItem;
+        m_checkedItem = index;
+        if (oldIndex.isValid())
+            emit dataChanged(oldIndex, oldIndex);
+        emit dataChanged(m_checkedItem, m_checkedItem);
+        m_current = data(index, Qt::DisplayRole).toString();
+        emit currentChanged(m_current);
+    }
+
+    return true;
+}
+
+void ColorSchemeModel::setCurrent(const QString& current)
+{
+    //indexOf return -1 on "not found", wich will generate an invalid index
+    int position = stringList().indexOf(current);
+
+    QPersistentModelIndex oldIndex = m_checkedItem;
+    m_checkedItem = index(position);
+    if (oldIndex.isValid())
+        emit dataChanged(oldIndex, oldIndex);
+    if (m_checkedItem.isValid())
+        emit dataChanged(m_checkedItem, m_checkedItem);
+
+    m_current = current;
+    emit currentChanged(m_current);
+}
+
+QVariant ColorSchemeModel::data(const QModelIndex &index, int role) const
+{
+    if (!index.isValid())
+        return QVariant();
+
+    if(role == Qt::CheckStateRole)
+        return m_checkedItem == index ? Qt::Checked : Qt::Unchecked;
+
+    return QStringListModel::data(index, role);
+}
diff --git a/modules/gui/qt/util/color_scheme_model.hpp b/modules/gui/qt/util/color_scheme_model.hpp
new file mode 100644
index 0000000000..5731fd07f5
--- /dev/null
+++ b/modules/gui/qt/util/color_scheme_model.hpp
@@ -0,0 +1,50 @@
+/*****************************************************************************
+ * Copyright (C) 2020 the VideoLAN team
+ *
+ * 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 COLORSCHEMEMODEL_HPP
+#define COLORSCHEMEMODEL_HPP
+
+#include <QStringListModel>
+
+class ColorSchemeModel : public QStringListModel
+{
+    Q_OBJECT
+    Q_PROPERTY(QString current READ getCurrent WRITE setCurrent NOTIFY currentChanged)
+public:
+    explicit ColorSchemeModel(QObject* parent = nullptr);
+
+    Q_INVOKABLE void setAvailableColorSchemes(const QStringList& colorSchemeList);
+
+    virtual Qt::ItemFlags flags (const QModelIndex& index) const override;
+    virtual QVariant data(const QModelIndex &index, int role) const override;
+    virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
+
+    inline QString getCurrent() const { return m_current; }
+    void setCurrent(const QString& );
+
+signals:
+    void currentChanged(const QString& colorScheme);
+
+private:
+    QString               m_current;
+    QPersistentModelIndex m_checkedItem;
+
+
+};
+
+#endif // COLORSCHEMEMODEL_HPP
-- 
2.25.1



More information about the vlc-devel mailing list