diff --git a/modules/gui/qt/medialibrary/mlbasemodel.cpp b/modules/gui/qt/medialibrary/mlbasemodel.cpp index c49b9659a5..9590f4bbeb 100644 --- a/modules/gui/qt/medialibrary/mlbasemodel.cpp +++ b/modules/gui/qt/medialibrary/mlbasemodel.cpp @@ -251,6 +251,18 @@ void MLBaseModel::onVlcMlEvent(void* data, const vlc_ml_event_t* event) }); } +void MLBaseModel::classBegin() +{ + m_qmlInitializing = true; +} + +void MLBaseModel::componentComplete() +{ + m_qmlInitializing = false; + + validateCache(); +} + MLItemId MLBaseModel::parentId() const { return m_parent; @@ -349,7 +361,7 @@ void MLBaseModel::unsetSortCriteria() int MLBaseModel::rowCount(const QModelIndex &parent) const { - if (!m_mediaLib || parent.isValid()) + if (!cachable() || parent.isValid()) return 0; validateCache(); @@ -409,9 +421,11 @@ QVariantList MLBaseModel::getIdsForIndexes(const QVariantList & indexes) const unsigned MLBaseModel::getCount() const { - if (!m_mediaLib) + if (!cachable()) return 0; + validateCache(); + if (m_cache->count() == COUNT_UNINITIALIZED) return 0; return static_cast(m_cache->count()); @@ -443,7 +457,7 @@ void MLBaseModel::validateCache() const if (m_cache) return; - if (!m_mediaLib) + if (!cachable()) return; auto loader = createLoader(); diff --git a/modules/gui/qt/medialibrary/mlbasemodel.hpp b/modules/gui/qt/medialibrary/mlbasemodel.hpp index c53408e506..906d394067 100644 --- a/modules/gui/qt/medialibrary/mlbasemodel.hpp +++ b/modules/gui/qt/medialibrary/mlbasemodel.hpp @@ -27,6 +27,8 @@ #include #include +#include + #include #include "mlqmltypes.hpp" #include "medialib.hpp" @@ -39,9 +41,10 @@ class MLListCache; class MediaLib; -class MLBaseModel : public QAbstractListModel +class MLBaseModel : public QAbstractListModel, public QQmlParserStatus { Q_OBJECT + Q_INTERFACES(QQmlParserStatus) Q_PROPERTY(MLItemId parentId READ parentId WRITE setParentId NOTIFY parentIdChanged RESET unsetParentId FINAL) @@ -99,6 +102,9 @@ private: static void onVlcMlEvent( void* data, const vlc_ml_event_t* event ); protected: + void classBegin() override; + void componentComplete() override; + virtual vlc_ml_sorting_criteria_t roleToCriteria(int role) const = 0; static QString getFirstSymbol(QString str); virtual vlc_ml_sorting_criteria_t nameToCriteria(QByteArray) const { @@ -183,6 +189,8 @@ private: void onCacheBeginRemoveRows(int first, int last); void onCacheBeginMoveRows(int first, int last, int destination); + inline bool cachable() const { return m_mediaLib && !m_qmlInitializing; } + protected: MLItemId m_parent; @@ -199,6 +207,8 @@ protected: //loader used to load single items std::shared_ptr m_itemLoader; + + bool m_qmlInitializing = false; }; #endif // MLBASEMODEL_HPP