[vlc-devel] [RFC 38/82] qt: provide model to be able to use QML as dialog provider

Pierre Lamot pierre at videolabs.io
Fri Feb 1 14:01:42 CET 2019


---
 modules/gui/qt/Makefile.am                |   3 +
 modules/gui/qt/components/dialogmodel.cpp | 113 ++++++++++++++++++++++
 modules/gui/qt/components/dialogmodel.hpp | 100 +++++++++++++++++++
 3 files changed, 216 insertions(+)
 create mode 100644 modules/gui/qt/components/dialogmodel.cpp
 create mode 100644 modules/gui/qt/components/dialogmodel.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 144f5cf987..526eb8b183 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -77,6 +77,8 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/dialogs/fingerprintdialog.hpp \
 	gui/qt/components/qml_main_context.cpp \
 	gui/qt/components/qml_main_context.hpp \
+	gui/qt/components/dialogmodel.cpp \
+	gui/qt/components/dialogmodel.hpp \
 	gui/qt/components/extended_panels.cpp \
 	gui/qt/components/extended_panels.hpp \
 	gui/qt/components/info_panels.cpp gui/qt/components/info_panels.hpp \
@@ -198,6 +200,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/dialogs/extensions.moc.cpp \
 	gui/qt/dialogs/fingerprintdialog.moc.cpp \
 	gui/qt/components/qml_main_context.moc.cpp \
+	gui/qt/components/dialogmodel.moc.cpp \
 	gui/qt/components/extended_panels.moc.cpp \
 	gui/qt/components/info_panels.moc.cpp \
 	gui/qt/components/info_widgets.moc.cpp \
diff --git a/modules/gui/qt/components/dialogmodel.cpp b/modules/gui/qt/components/dialogmodel.cpp
new file mode 100644
index 0000000000..9db269a9de
--- /dev/null
+++ b/modules/gui/qt/components/dialogmodel.cpp
@@ -0,0 +1,113 @@
+/*****************************************************************************
+ * 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 "dialogmodel.hpp"
+
+static void displayErrorCb(void *p_data, const char *psz_title, const char *psz_text)
+{
+    DialogModel* that = static_cast<DialogModel*>(p_data);
+    emit that->errorDisplayed(psz_title, psz_text);
+}
+
+static void displayLoginCb(void *p_data, vlc_dialog_id *dialogId,
+                        const char *psz_title, const char *psz_text,
+                        const char *psz_default_username,
+                        bool b_ask_store)
+{
+    DialogModel* that = static_cast<DialogModel*>(p_data);
+    emit that->loginDisplayed( dialogId, psz_title, psz_text, psz_default_username, b_ask_store );
+}
+
+static void displayQuestionCb(void *p_data, vlc_dialog_id *dialogId,
+                       const char *psz_title, const char *psz_text,
+                       vlc_dialog_question_type i_type,
+                       const char *psz_cancel, const char *psz_action1,
+                       const char *psz_action2)
+{
+    DialogModel* that = static_cast<DialogModel*>(p_data);
+    emit that->questionDisplayed( dialogId, psz_title, psz_text, static_cast<int>(i_type), psz_cancel, psz_action1, psz_action2 );
+}
+
+static void displayProgressCb(void *p_data, vlc_dialog_id *dialogId,
+                            const char *psz_title, const char *psz_text,
+                            bool b_indeterminate, float f_position,
+                            const char *psz_cancel)
+{
+    DialogModel* that = static_cast<DialogModel*>(p_data);
+    emit that->progressDisplayed( dialogId, psz_title, psz_text, b_indeterminate, f_position, psz_cancel);
+}
+
+static void cancelCb(void *p_data, vlc_dialog_id *dialogId)
+{
+    DialogModel* that = static_cast<DialogModel*>(p_data);
+    emit that->cancelled(dialogId);
+}
+
+static void updateProgressCb(void *p_data, vlc_dialog_id *dialogId, float f_value, const char *psz_text)
+{
+    DialogModel* that = static_cast<DialogModel*>(p_data);
+    emit that->progressUpdated( dialogId, f_value, psz_text );
+}
+
+const vlc_dialog_cbs cbs = {
+    displayErrorCb,
+    displayLoginCb,
+    displayQuestionCb,
+    displayProgressCb,
+    cancelCb,
+    updateProgressCb
+};
+
+DialogModel::DialogModel(QObject *parent)
+    : QObject(parent)
+{
+}
+
+DialogModel::~DialogModel()
+{
+    if (m_mainCtx)
+        vlc_dialog_provider_set_callbacks(m_mainCtx->getIntf(), nullptr, nullptr);
+}
+
+void DialogModel::dismiss(DialogId dialogId)
+{
+    vlc_dialog_id_dismiss(dialogId.m_id);
+}
+
+void DialogModel::post_login(DialogId dialogId, const QString& username, const QString& password, bool store)
+{
+    vlc_dialog_id_post_login(dialogId.m_id, qtu(username), qtu(password), store);
+}
+
+void DialogModel::post_action1(DialogId dialogId)
+{
+    vlc_dialog_id_post_action(dialogId.m_id, 1);
+}
+
+void DialogModel::post_action2(DialogId dialogId)
+{
+    vlc_dialog_id_post_action(dialogId.m_id, 2);
+}
+
+void DialogModel::setMainCtx(QmlMainContext* ctx)
+{
+    if (ctx)
+        vlc_dialog_provider_set_callbacks(ctx->getIntf(), &cbs, this);
+    else if (m_mainCtx)
+        vlc_dialog_provider_set_callbacks(m_mainCtx->getIntf(), nullptr, nullptr);
+    m_mainCtx = ctx;
+}
diff --git a/modules/gui/qt/components/dialogmodel.hpp b/modules/gui/qt/components/dialogmodel.hpp
new file mode 100644
index 0000000000..cc711fa1e5
--- /dev/null
+++ b/modules/gui/qt/components/dialogmodel.hpp
@@ -0,0 +1,100 @@
+/*****************************************************************************
+ * 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 DIALOGMODEL_HPP
+#define DIALOGMODEL_HPP
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_dialog.h>
+
+#include <QObject>
+
+#include "qml_main_context.hpp"
+
+
+class DialogId {
+    Q_GADGET
+public:
+    DialogId(vlc_dialog_id * id = nullptr)
+        : m_id(id)
+    {}
+
+    bool operator ==(const DialogId& other) const {
+        return m_id == other.m_id;
+    }
+    vlc_dialog_id *m_id;
+};
+
+Q_DECLARE_METATYPE(DialogId)
+
+class DialogModel : public QObject
+{
+    Q_OBJECT
+
+public:
+    Q_PROPERTY(QmlMainContext* mainCtx READ getMainCtx WRITE setMainCtx NOTIFY mainCtxChanged)
+
+    enum QuestionType {
+        QUESTION_NORMAL,
+        QUESTION_WARNING,
+        QUESTION_CRITICAL
+    };
+    Q_ENUM(QuestionType)
+
+public:
+    explicit DialogModel(QObject *parent = nullptr);
+    ~DialogModel();
+
+    inline QmlMainContext* getMainCtx() const { return m_mainCtx; }
+    void setMainCtx(QmlMainContext*);
+
+signals:
+    void errorDisplayed(const QString &title, const QString &text);
+    void loginDisplayed(DialogId dialogId, const QString &title,
+                        const QString &text, const QString &defaultUsername,
+                        bool b_ask_store);
+
+    void questionDisplayed(DialogId dialogId, const QString &title,
+                           const QString &text, int type,
+                           const QString &cancel, const QString &action1,
+                           const QString &action2);
+
+    void progressDisplayed(DialogId dialogId, const QString &title,
+                           const QString &text, bool b_indeterminate,
+                           float f_position, const QString &cancel);
+
+    void cancelled(DialogId dialogId);
+
+    void progressUpdated(DialogId dialogId, float f_value, const QString &text);
+
+    void mainCtxChanged();
+
+public slots:
+    void dismiss(DialogId dialogId);
+    void post_login(DialogId dialogId, const QString& username, const QString& password, bool store = false);
+    void post_action1(DialogId dialogId);
+    void post_action2(DialogId dialogId);
+
+private:
+    QmlMainContext* m_mainCtx;
+};
+
+#endif // DIALOGMODEL_HPP
-- 
2.19.1



More information about the vlc-devel mailing list