[vlc-commits] [Git][videolan/vlc][master] 5 commits: qml: register VLCDuration and VLCTime as Q_GADGET

Steve Lhomme (@robUx4) gitlab at videolan.org
Sat Jun 14 17:42:03 UTC 2025



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
a1f23bc9 by Pierre Lamot at 2025-06-14T13:45:04+00:00
qml: register VLCDuration and VLCTime as Q_GADGET

Q_GADGET need to be registered in QML engine to be properly usable from QML

- - - - -
616fed4a by Pierre Lamot at 2025-06-14T13:45:04+00:00
qt: don't register VLCTick in qml

VLCTick can't be created, so it doesn't satisfy the requirement that value types
should be default-constructible

https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html#registering-value-types

- - - - -
4aeda6f9 by Pierre Lamot at 2025-06-14T13:45:04+00:00
qt: don't return raw vlc_tick_t in model::data()

They need to be wrapped in VLCDuration/VLCTick to be usable from QML

- - - - -
55235389 by Pierre Lamot at 2025-06-14T13:45:04+00:00
qt: don't store base and invalid value in each VLCTick

Theses values are constant in VLCDuration/VLCTime definition

- - - - -
af179886 by Pierre Lamot at 2025-06-14T13:45:04+00:00
qml: expose VLCTick::FormatFlag enum parameter in a QML namespace

this allows using the values as

```
import VLC.MainInterface

VLCTick.SubSecondFormattedAsMS
```

- - - - -


8 changed files:

- modules/gui/qt/maininterface/mainui.cpp
- modules/gui/qt/player/input_models.cpp
- modules/gui/qt/player/qml/ControlBar.qml
- modules/gui/qt/player/qml/SliderBar.qml
- modules/gui/qt/player/qml/controlbarcontrols/ArtworkInfoWidget.qml
- modules/gui/qt/playlist/playlist_model.cpp
- modules/gui/qt/util/vlctick.cpp
- modules/gui/qt/util/vlctick.hpp


Changes:

=====================================
modules/gui/qt/maininterface/mainui.cpp
=====================================
@@ -179,7 +179,9 @@ void MainUI::registerQMLTypes()
         qmlRegisterUncreatableType<QAbstractItemModel>(uri, versionMajor, versionMinor, "QtAbstractItemModel", "");
         qmlRegisterUncreatableType<QWindow>(uri, versionMajor, versionMinor, "QtWindow", "");
         qmlRegisterUncreatableType<QScreen>(uri, versionMajor, versionMinor, "QtScreen", "");
-        qmlRegisterUncreatableType<VLCTick>(uri, versionMajor, versionMinor, "vlcTick", "");
+        qmlRegisterTypesAndRevisions<VLCDuration>(uri, versionMajor);
+        qmlRegisterTypesAndRevisions<VLCTime>(uri, versionMajor);
+        qmlRegisterUncreatableMetaObject(VLCTickForeign::staticMetaObject, uri, versionMajor, versionMinor, "VLCTick", "Not Instantiable" );
         qmlRegisterType<VideoSurface>(uri, versionMajor, versionMinor, "VideoSurface");
         qmlRegisterUncreatableType<BaseModel>( uri, versionMajor, versionMinor, "BaseModel", "Base Model is uncreatable." );
         qmlRegisterUncreatableType<VLCVarChoiceModel>(uri, versionMajor, versionMinor, "VLCVarChoiceModel", "generic variable with choice model" );


=====================================
modules/gui/qt/player/input_models.cpp
=====================================
@@ -326,7 +326,7 @@ QVariant ChapterListModel::data(const QModelIndex &index, int role) const
     else if (role == Qt::CheckStateRole)
         return QVariant::fromValue<bool>(row == m_current);
     else if (role == ChapterListRoles::TimeRole )
-        return QVariant::fromValue<vlc_tick_t>(chapter.time);
+        return QVariant::fromValue<VLCTime>(chapter.time);
     else if (role == ChapterListRoles::StartPositionRole && (m_title->length != 0))
         return QVariant::fromValue<float>(chapter.time / (float) m_title->length);
     else if (role == ChapterListRoles::EndPositionRole && (m_title->length != 0))


=====================================
modules/gui/qt/player/qml/ControlBar.qml
=====================================
@@ -140,7 +140,7 @@ T.Pane {
 
                 text: {
                     const length = Player.length
-                    return Player.time.formatHMS(length.isSubSecond() ? length.SubSecondFormattedAsMS : 0)
+                    return Player.time.formatHMS(length.isSubSecond() ? VLCTick.SubSecondFormattedAsMS : 0)
                 }
                 color: theme.fg.primary
                 font: timeTextMetrics.font


=====================================
modules/gui/qt/player/qml/SliderBar.qml
=====================================
@@ -24,6 +24,7 @@ import VLC.Player
 import VLC.Widgets as Widgets
 import VLC.Style
 import VLC.Util
+import VLC.MainInterface
 
 T.ProgressBar {
     id: control
@@ -87,7 +88,7 @@ T.ProgressBar {
             else
                 _text = Player.time
 
-            _text = _text.formatHMS(length.isSubSecond() ? length.SubSecondFormattedAsMS : 0)
+            _text = _text.formatHMS(length.isSubSecond() ? VLCTick.SubSecondFormattedAsMS : 0)
 
             if (Player.hasChapters)
                 _text += " - " + Player.chapters.getNameAtPosition(control._tooltipPosition)


=====================================
modules/gui/qt/player/qml/controlbarcontrols/ArtworkInfoWidget.qml
=====================================
@@ -250,7 +250,7 @@ AbstractButton {
                         " -- / -- "
                     else {
                         const length = Player.length
-                        return Player.time.formatHMS(length.isSubSecond() ? length.SubSecondFormattedAsMS : 0) +
+                        return Player.time.formatHMS(length.isSubSecond() ? VLCTick.SubSecondFormattedAsMS : 0) +
                                 " / " +
                                 length.formatHMS()
                     }


=====================================
modules/gui/qt/playlist/playlist_model.cpp
=====================================
@@ -481,11 +481,7 @@ PlaylistListModel::data(const QModelIndex &index, int role) const
     case IsCurrentRole:
         return row == d->m_current;
     case DurationRole:
-    {
-        QVariant var;
-        var.setValue(d->m_items[row].getDuration());
-        return var;
-    }
+        return QVariant::fromValue<VLCDuration>(d->m_items[row].getDuration());
     case ArtistRole:
         return d->m_items[row].getArtist();
     case AlbumRole:


=====================================
modules/gui/qt/util/vlctick.cpp
=====================================
@@ -29,30 +29,10 @@ int64_t roundNearestMultiple(int64_t number, int64_t multiple)
 
 }
 
-VLCTick::VLCTick(vlc_tick_t base_offset, vlc_tick_t invalid_value)
-    : m_base_offset(base_offset), m_invalid_value(invalid_value), m_ticks(invalid_value)
-{
-}
-
-vlc_tick_t VLCTick::toVLCTick() const
-{
-    return m_ticks;
-}
-
-vlc_tick_t VLCTick::asMilliseconds() const
-{
-    return MS_FROM_VLC_TICK(m_ticks - m_base_offset);
-}
-
-vlc_tick_t VLCTick::asSeconds() const
-{
-    return SEC_FROM_VLC_TICK(m_ticks - m_base_offset);
-}
+VLCTick::VLCTick(vlc_tick_t ticks)
+    : m_ticks(ticks)
+{}
 
-bool VLCTick::valid() const
-{
-    return m_ticks != m_invalid_value;
-}
 
 bool VLCTick::isSubSecond() const
 {
@@ -183,16 +163,17 @@ int VLCTick::toMilliseconds() const
     return valid() ? asMilliseconds() : 0;
 }
 
+
+///// VLCDuration
+
 VLCDuration::VLCDuration()
-    : VLCTick::VLCTick(0, 0)
+    : VLCTick(0)
 {
-
 }
 
 VLCDuration::VLCDuration(vlc_tick_t t)
-    : VLCTick::VLCTick(0, 0)
+    : VLCTick(t)
 {
-    m_ticks = t;
 }
 
 VLCDuration VLCDuration::operator*(double f) const
@@ -225,21 +206,36 @@ VLCDuration VLCDuration::fromMS(int64_t ms)
     return VLCDuration(VLC_TICK_FROM_MS(ms));
 }
 
+int64_t VLCDuration::asMilliseconds() const
+{
+    return MS_FROM_VLC_TICK(m_ticks);
+}
+
+int64_t VLCDuration::asSeconds() const
+{
+    return SEC_FROM_VLC_TICK(m_ticks);
+}
+
+bool VLCDuration::valid() const
+{
+    return true;
+}
+
+///// VLCTime
+
 VLCTime::VLCTime()
-    : VLCTick(VLC_TICK_0, VLC_TICK_INVALID)
+    : VLCTick(VLC_TICK_INVALID)
 {
 }
 
 VLCTime::VLCTime(vlc_tick_t t)
-    : VLCTick(VLC_TICK_0, VLC_TICK_INVALID)
+    : VLCTick(t)
 {
-    m_ticks = t;
 }
 
 VLCTime::VLCTime(VLCDuration d)
-    : VLCTick(VLC_TICK_0, VLC_TICK_INVALID)
+    : VLCTick(VLC_TICK_0 + d.toVLCTick())
 {
-    m_ticks = m_base_offset + d.toVLCTick();
 }
 
 VLCDuration VLCTime::operator-(const VLCTime &rhs) const
@@ -258,5 +254,20 @@ VLCTime VLCTime::scale(float scalar) const
 {
     if(!valid())
         return VLCTime(VLC_TICK_INVALID);
-    return VLCTime(m_base_offset + ((m_ticks - m_base_offset) * scalar));
+    return VLCTime(VLC_TICK_0 + ((m_ticks - VLC_TICK_0) * scalar));
+}
+
+int64_t VLCTime::asMilliseconds() const
+{
+    return MS_FROM_VLC_TICK(m_ticks - VLC_TICK_0);
+}
+
+int64_t VLCTime::asSeconds() const
+{
+    return SEC_FROM_VLC_TICK(m_ticks - VLC_TICK_0);
+}
+
+bool VLCTime::valid() const
+{
+    return m_ticks != VLC_TICK_INVALID;
 }


=====================================
modules/gui/qt/util/vlctick.hpp
=====================================
@@ -25,6 +25,7 @@
 #include <QObject>
 #include <vlc_common.h>
 #include <vlc_tick.h>
+#include <QtQml/qqmlregistration.h>
 
 class VLCTick
 {
@@ -35,7 +36,7 @@ public:
     };
     Q_ENUM(FormatFlag)
 
-    Q_INVOKABLE bool valid() const;
+    Q_INVOKABLE virtual bool valid() const = 0;
 
     Q_INVOKABLE bool isSubSecond() const;
 
@@ -74,26 +75,30 @@ public:
      */
     Q_INVOKABLE QString formatShort(int formatFlags = SubSecondFormattedAsMS) const;
 
-    vlc_tick_t toVLCTick() const;
+    inline vlc_tick_t toVLCTick() const {
+        return m_ticks;
+    }
+
     Q_INVOKABLE int toMinutes() const;
     Q_INVOKABLE int toSeconds() const;
     Q_INVOKABLE int toHours()   const;
     int toMilliseconds() const;
 
 protected:
+    VLCTick(vlc_tick_t ticks);
     VLCTick() = delete;
-    VLCTick(vlc_tick_t, vlc_tick_t);
 
-    vlc_tick_t asMilliseconds() const;
-    vlc_tick_t asSeconds() const;
+    virtual int64_t asMilliseconds() const = 0;
+    virtual int64_t asSeconds() const = 0;
 
-    vlc_tick_t m_base_offset;
-    vlc_tick_t m_invalid_value;
     vlc_tick_t m_ticks;
 };
 
 class VLCDuration : public VLCTick
 {
+    Q_GADGET
+    QML_VALUE_TYPE(vlcDuration)
+
 public:
     VLCDuration();
     VLCDuration(vlc_tick_t);
@@ -107,10 +112,16 @@ public:
     Q_INVOKABLE VLCDuration scale(float) const;
 
     static VLCDuration fromMS(int64_t ms);
+
+    int64_t asMilliseconds() const override;
+    int64_t asSeconds() const override;
+    bool valid() const override;
 };
 
 class VLCTime : public VLCTick
 {
+    Q_GADGET
+    QML_VALUE_TYPE(vlcTime)
 public:
     VLCTime();
     VLCTime(vlc_tick_t);
@@ -119,6 +130,21 @@ public:
     bool operator<=(const VLCTime &) const;
 
     Q_INVOKABLE VLCTime scale(float) const;
+
+    int64_t asMilliseconds() const override;
+    int64_t asSeconds() const override;
+    bool valid() const override;
 };
 
+//following boilerplate allow registering FormatFlag enum in the VLCTick namespace in QML
+//VLCTick is not a QML_VALUE_TYPE so we don't need to define a distinct type to
+//register it in the namespace
+
+namespace VLCTickForeign
+{
+    Q_NAMESPACE
+    QML_NAMED_ELEMENT(VLCTick)
+    QML_FOREIGN_NAMESPACE(VLCTick)
+}
+
 #endif // VLCTICK_HPP



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/0c69eba89f8ba3f3baff8dcc4d61665c8b535fe0...af1798860977052ea6c9563279ea7c9dac8da080

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/0c69eba89f8ba3f3baff8dcc4d61665c8b535fe0...af1798860977052ea6c9563279ea7c9dac8da080
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list