<html><head></head><body>No thanks. We already have a Qt wrapper for variables and callback. Extend it or replace it.<br><br><div class="gmail_quote">Le 1 février 2019 15:01:19 GMT+02:00, Pierre Lamot <pierre@videolabs.io> a écrit :<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail"> values are accesible from Q_PROPERTIES for QML integration<hr> modules/gui/qt/Makefile.am | 3 +<br> modules/gui/qt/util/vlc_var_observer.cpp | 59 +++++<br> modules/gui/qt/util/vlc_var_observer.hpp | 276 +++++++++++++++++++++++<br> 3 files changed, 338 insertions(+)<br> create mode 100644 modules/gui/qt/util/vlc_var_observer.cpp<br> create mode 100644 modules/gui/qt/util/vlc_var_observer.hpp<br><br>diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am<br>index 0b20509bc0..1f51781643 100644<br>--- a/modules/gui/qt/Makefile.am<br>+++ b/modules/gui/qt/Makefile.am<br>@@ -146,6 +146,8 @@ libqt_plugin_la_SOURCES = \<br> gui/qt/util/singleton.hpp \<br> gui/qt/util/vlctick.cpp \<br> gui/qt/util/vlctick.hpp \<br>+ gui/qt/util/vlc_var_observer.cpp \<br>+ gui/qt/util/vlc_var_observer.hpp \<br> gui/qt/util/imagehelper.cpp gui/qt/util/imagehelper.hpp \<br> gui/qt/styles/seekstyle.cpp gui/qt/styles/seekstyle.hpp<br> if HAVE_WIN32<br>@@ -232,6 +234,7 @@ nodist_libqt_plugin_la_SOURCES = \<br> gui/qt/util/qmenuview.moc.cpp \<br> gui/qt/util/qvlcapp.moc.cpp \<br> gui/qt/util/pictureflow.moc.cpp \<br>+ gui/qt/util/vlc_var_observer.moc.cpp \<br> gui/qt/util/vlctick.moc.cpp \<br> gui/qt/util/validators.moc.cpp \<br> gui/qt/util/buttons/RoundButton.moc.cpp \<br>diff --git a/modules/gui/qt/util/vlc_var_observer.cpp b/modules/gui/qt/util/vlc_var_observer.cpp<br>new file mode 100644<br>index 0000000000..72785cd791<br>--- /dev/null<br>+++ b/modules/gui/qt/util/vlc_var_observer.cpp<br>@@ -0,0 +1,59 @@<br>+/*****************************************************************************<br>+ * Copyright (C) 2019 VLC authors and VideoLAN<br>+ *<br>+ * This program is free software; you can redistribute it and/or modify<br>+ * it under the terms of the GNU General Public License as published by<br>+ * the Free Software Foundation; either version 2 of the License, or<br>+ * ( at your option ) any later version.<br>+ *<br>+ * This program is distributed in the hope that it will be useful,<br>+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br>+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>+ * GNU General Public License for more details.<br>+ *<br>+ * You should have received a copy of the GNU General Public License<br>+ * along with this program; if not, write to the Free Software<br>+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.<br>+ *****************************************************************************/<br>+<br>+#include "vlc_var_observer.hpp"<br>+<br>+VLCVarBooleanObserver::VLCVarBooleanObserver(vlc_object_t *object, QString property, QObject *parent)<br>+ : VLCVarObserver<VLCVarBooleanObserver, bool>(object, property, parent)<br>+{<br>+}<br>+<br>+void VLCVarBooleanObserver::setValue(bool value)<br>+{<br>+ setValueInternal(value);<br>+}<br>+<br>+VLCVarStringObserver::VLCVarStringObserver(vlc_object_t *object, QString property, QObject *parent)<br>+ : VLCVarObserver<VLCVarStringObserver, QString>(object, property, parent)<br>+{<br>+}<br>+<br>+void VLCVarStringObserver::setValue(QString value)<br>+{<br>+ setValueInternal(value);<br>+}<br>+<br>+VLCVarFloatObserver::VLCVarFloatObserver(vlc_object_t *object, QString property, QObject *parent)<br>+ : VLCVarObserver<VLCVarFloatObserver, float>(object, property, parent)<br>+{<br>+}<br>+<br>+void VLCVarFloatObserver::setValue(float value)<br>+{<br>+ setValueInternal(value);<br>+}<br>+<br>+VLCVarIntObserver::VLCVarIntObserver(vlc_object_t *object, QString property, QObject *parent)<br>+ : VLCVarObserver<VLCVarIntObserver, int64_t>(object, property, parent)<br>+{<br>+}<br>+<br>+void VLCVarIntObserver::setValue(int64_t value)<br>+{<br>+ setValueInternal(value);<br>+}<br>diff --git a/modules/gui/qt/util/vlc_var_observer.hpp b/modules/gui/qt/util/vlc_var_observer.hpp<br>new file mode 100644<br>index 0000000000..fd3a5ed20a<br>--- /dev/null<br>+++ b/modules/gui/qt/util/vlc_var_observer.hpp<br>@@ -0,0 +1,276 @@<br>+/*****************************************************************************<br>+ * Copyright (C) 2019 VLC authors and VideoLAN<br>+ *<br>+ * This program is free software; you can redistribute it and/or modify<br>+ * it under the terms of the GNU General Public License as published by<br>+ * the Free Software Foundation; either version 2 of the License, or<br>+ * ( at your option ) any later version.<br>+ *<br>+ * This program is distributed in the hope that it will be useful,<br>+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br>+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>+ * GNU General Public License for more details.<br>+ *<br>+ * You should have received a copy of the GNU General Public License<br>+ * along with this program; if not, write to the Free Software<br>+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.<br>+ *****************************************************************************/<br>+<br>+#ifndef VLC_VAR_OBSERVER_HPP<br>+#define VLC_VAR_OBSERVER_HPP<br>+<br>+<br>+#ifdef HAVE_CONFIG_H<br>+# include "config.h"<br>+#endif<br>+<br>+#include <vlc_common.h><br>+#include <vlc_variables.h><br>+#include <vlc_cxx_helpers.hpp><br>+<br>+#include <QObject><br>+<br>+#include "qt.hpp"<br>+<br>+/*<br>+ * type traits to convert VLC_VAR to C++types<br>+ */<br>+template<typename T><br>+struct VLCVarTypeTraits {<br>+};<br>+<br>+template<><br>+struct VLCVarTypeTraits<QString><br>+{<br>+ static const int var_type = VLC_VAR_STRING;<br>+ inline static QString fromValue(const vlc_value_t& value) {<br>+ return value.psz_string;<br>+ }<br>+ inline static vlc_value_t toValue(const QString& value) {<br>+ vlc_value_t ret;<br>+ ret.psz_string = strdup(qtu(value));<br>+ return ret;<br>+ }<br>+ inline static void releaseValue(vlc_value_t value) {<br>+ free(value.psz_string);<br>+ }<br>+};<br>+<br>+template<><br>+struct VLCVarTypeTraits<bool><br>+{<br>+ static const int var_type = VLC_VAR_BOOL;<br>+ inline static bool fromValue(const vlc_value_t& value) {<br>+ return value.b_bool;<br>+ }<br>+ inline static vlc_value_t toValue(bool value) {<br>+ vlc_value_t ret;<br>+ ret.b_bool = value;<br>+ return ret;<br>+ }<br>+ inline static void releaseValue(vlc_value_t) {}<br>+};<br>+<br>+template<><br>+struct VLCVarTypeTraits<int64_t><br>+{<br>+ static const int var_type = VLC_VAR_INTEGER;<br>+ inline static int64_t fromValue(const vlc_value_t& value) {<br>+ return value.i_int;<br>+ }<br>+ inline static vlc_value_t toValue(int64_t value) {<br>+ vlc_value_t ret;<br>+ ret.i_int = value;<br>+ return ret;<br>+ }<br>+ inline static void releaseValue(vlc_value_t) {}<br>+};<br>+<br>+template<><br>+struct VLCVarTypeTraits<float><br>+{<br>+ static const int var_type = VLC_VAR_FLOAT;<br>+ inline static float fromValue(const vlc_value_t& value) {<br>+ return value.f_float;<br>+ }<br>+ inline static vlc_value_t toValue(float value) {<br>+ vlc_value_t ret;<br>+ ret.f_float = value;<br>+ return ret;<br>+ }<br>+ inline static void releaseValue(vlc_value_t) {}<br>+};<br>+<br>+<br>+//Generic observer<br>+template<typename Derived, typename BaseType><br>+class VLCVarObserver : public QObject<br>+{<br>+public:<br>+ VLCVarObserver(vlc_object_t* object, QString property, QObject* parent)<br>+ : QObject(parent)<br>+ , m_property(property)<br>+ {<br>+ resetObject(object);<br>+ Derived* derived = static_cast<Derived*>(this);<br>+ connect( derived, &Derived::valueChangedInternal, [this](vlc_object_t* object, BaseType val) {<br>+ this->onValueChangedInternal(object, val);<br>+ });<br>+ }<br>+<br>+ virtual ~VLCVarObserver()<br>+ {<br>+ if (m_object)<br>+ {<br>+ Derived* derived = static_cast<Derived*>(this);<br>+ var_DelCallback(m_object.get(), qtu(m_property), value_modified, derived);<br>+ }<br>+ }<br>+<br>+ ///change the object beeing observed<br>+ void resetObject( vlc_object_t* object )<br>+ {<br>+ Derived* derived = static_cast<Derived*>(this);<br>+ if (m_object)<br>+ var_DelCallback( m_object.get(), qtu(m_property), value_modified, derived );<br>+<br>+ m_object.reset( object, true );<br>+ if (m_object)<br>+ {<br>+ int type = var_Type(object, qtu(m_property));<br>+ if (type == 0) //variable not found<br>+ {<br>+ msg_Warn(m_object.get(), "variable %s not found in object", qtu(m_property));<br>+ m_object.reset(nullptr);<br>+ return;<br>+ }<br>+ assert((type & VLC_VAR_CLASS) == VLCVarTypeTraits<BaseType>::var_type);<br>+ vlc_value_t currentvalue;<br>+ if (var_Get(m_object.get(), qtu(m_property), ¤tvalue) == VLC_SUCCESS)<br>+ {<br>+ BaseType value = VLCVarTypeTraits<BaseType>::fromValue(currentvalue);<br>+ if (m_value != value)<br>+ {<br>+ m_value = value;<br>+ emit derived->valueChanged( m_value );<br>+ }<br>+ }<br>+<br>+ var_AddCallback(m_object.get(), qtu(m_property), value_modified, derived);<br>+ }<br>+ }<br>+<br>+ BaseType getValue() const<br>+ {<br>+ if (!m_object)<br>+ return BaseType{};<br>+ return m_value;<br>+ }<br>+<br>+protected:<br>+ //called by setValue in child classes<br>+ void setValueInternal(BaseType value)<br>+ {<br>+ if (! m_object)<br>+ return;<br>+ vlc_value_t vlcvalue = VLCVarTypeTraits<BaseType>::toValue( value );<br>+ var_Set(m_object.get(), qtu(m_property), vlcvalue);<br>+ VLCVarTypeTraits<BaseType>::releaseValue(vlcvalue);<br>+ }<br>+<br>+private:<br>+ //executed on UI thread<br>+ virtual void onValueChangedInternal(vlc_object_t* object, BaseType value)<br>+ {<br>+ if (m_object.get() != object)<br>+ return;<br>+ if (m_value != value) {<br>+ m_value = value;<br>+ Derived* derived = static_cast<Derived*>(this);<br>+ emit derived->valueChanged( m_value );<br>+ }<br>+ }<br>+<br>+<br>+ //executed on variable thread, this forwards the callback to the UI thread<br>+ static int value_modified( vlc_object_t * object, char const *, vlc_value_t, vlc_value_t newValue, void * data)<br>+ {<br>+ Derived* that = static_cast<Derived*>(data);<br>+ emit that->onValueChangedInternal( object, VLCVarTypeTraits<BaseType>::fromValue( newValue ) );<br>+ return VLC_SUCCESS;<br>+ }<br>+<br>+ typedef vlc_shared_data_ptr_type(vlc_object_t, vlc_object_hold, vlc_object_release) ObjectPtr;<br>+ ObjectPtr m_object;<br>+ QString m_property;<br>+ BaseType m_value;<br>+};<br>+<br>+//specialisation<br>+<br>+class VLCVarBooleanObserver : public VLCVarObserver<VLCVarBooleanObserver, bool><br>+{<br>+ Q_OBJECT<br>+public:<br>+ Q_PROPERTY(bool value READ getValue WRITE setValue NOTIFY valueChanged)<br>+<br>+ VLCVarBooleanObserver(vlc_object_t* object, QString property, QObject* parent = nullptr);<br>+<br>+public slots:<br>+ void setValue(bool value);<br>+<br>+signals:<br>+ void valueChanged( bool );<br>+ void valueChangedInternal(vlc_object_t *, bool );<br>+};<br>+<br>+class VLCVarStringObserver : public VLCVarObserver<VLCVarStringObserver, QString><br>+{<br>+ Q_OBJECT<br>+public:<br>+ Q_PROPERTY(QString value READ getValue WRITE setValue NOTIFY valueChanged)<br>+<br>+ VLCVarStringObserver(vlc_object_t* object, QString property, QObject* parent = nullptr);<br>+<br>+public slots:<br>+ void setValue(QString value);<br>+<br>+signals:<br>+ void valueChanged( QString );<br>+ void valueChangedInternal( vlc_object_t *, QString );<br>+};<br>+<br>+class VLCVarFloatObserver : public VLCVarObserver<VLCVarFloatObserver, float><br>+{<br>+ Q_OBJECT<br>+public:<br>+ Q_PROPERTY(float value READ getValue WRITE setValue NOTIFY valueChanged)<br>+<br>+ VLCVarFloatObserver(vlc_object_t* object, QString property, QObject* parent = nullptr);<br>+<br>+public slots:<br>+ void setValue(float value);<br>+<br>+signals:<br>+ void valueChanged( float );<br>+ void valueChangedInternal( vlc_object_t *, float );<br>+};<br>+<br>+<br>+class VLCVarIntObserver : public VLCVarObserver<VLCVarIntObserver, int64_t><br>+{<br>+ Q_OBJECT<br>+public:<br>+ Q_PROPERTY(int64_t value READ getValue WRITE setValue NOTIFY valueChanged)<br>+<br>+ VLCVarIntObserver(vlc_object_t* object, QString property, QObject* parent = nullptr);<br>+<br>+public slots:<br>+ void setValue(int64_t value);<br>+<br>+signals:<br>+ void valueChanged( int64_t );<br>+ void valueChangedInternal( vlc_object_t *, int64_t );<br>+};<br>+<br>+#endif // VLC_VAR_OBSERVER_HPP</pre></blockquote></div><br>-- <br>Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.</body></html>