[vlc-commits] qt: create MLRecentsVideoModel

Abel Tesfaye git at videolan.org
Tue Jul 30 17:57:00 CEST 2019


vlc | branch: master | Abel Tesfaye <Abeltesfaye45 at gmail.com> | Tue Jul 30 15:49:53 2019 +0300| [1f31213fe343e66eb6035c7211eabfb790676918] | committer: Jean-Baptiste Kempf

qt: create MLRecentsVideoModel

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=1f31213fe343e66eb6035c7211eabfb790676918
---

 modules/gui/qt/Makefile.am                         |   3 +
 .../gui/qt/components/mediacenter/mlbasemodel.hpp  |   2 +-
 .../components/mediacenter/mlrecentsvideomodel.cpp | 147 +++++++++++++++++++++
 .../components/mediacenter/mlrecentsvideomodel.hpp |  62 +++++++++
 modules/gui/qt/main_interface.cpp                  |   2 +
 5 files changed, 215 insertions(+), 1 deletion(-)

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 36ccbb6ae2..c57914da16 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -158,6 +158,8 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mlvideo.cpp \
 	gui/qt/components/mediacenter/mlvideomodel.cpp \
 	gui/qt/components/mediacenter/mlvideomodel.hpp \
+	gui/qt/components/mediacenter/mlrecentsvideomodel.cpp \
+	gui/qt/components/mediacenter/mlrecentsvideomodel.hpp \
 	gui/qt/components/playlist/media.hpp \
 	gui/qt/components/playlist/playlist_common.cpp \
 	gui/qt/components/playlist/playlist_common.hpp \
@@ -289,6 +291,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/components/mediacenter/mlnetworkmodel.moc.cpp \
 	gui/qt/components/mediacenter/mlvideo.moc.cpp \
 	gui/qt/components/mediacenter/mlvideomodel.moc.cpp \
+	gui/qt/components/mediacenter/mlrecentsvideomodel.moc.cpp \
 	gui/qt/components/playlist/playlist_common.moc.cpp \
 	gui/qt/components/playlist/playlist_item.moc.cpp \
 	gui/qt/components/playlist/playlist_model.moc.cpp \
diff --git a/modules/gui/qt/components/mediacenter/mlbasemodel.hpp b/modules/gui/qt/components/mediacenter/mlbasemodel.hpp
index cb8af2ca90..8bb273075a 100644
--- a/modules/gui/qt/components/mediacenter/mlbasemodel.hpp
+++ b/modules/gui/qt/components/mediacenter/mlbasemodel.hpp
@@ -140,9 +140,9 @@ public:
         vlc_mutex_locker lock( &m_item_lock );
         if ( m_initialized == false )
         {
+            m_item_list = const_cast<MLSlidingWindowModel<T>*>(this)->fetch();
             m_total_count = countTotalElements();
             m_initialized = true;
-            m_item_list = const_cast<MLSlidingWindowModel<T>*>(this)->fetch();
         }
         return m_total_count;
     }
diff --git a/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.cpp b/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.cpp
new file mode 100644
index 0000000000..a1dc8ede7d
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.cpp
@@ -0,0 +1,147 @@
+/*****************************************************************************
+ * 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 "mlrecentsvideomodel.hpp"
+
+namespace {
+
+enum Role {
+    VIDEO_ID = Qt::UserRole + 1,
+    VIDEO_TITLE,
+    VIDEO_THUMBNAIL,
+    VIDEO_DURATION,
+    VIDEO_PROGRESS,
+    VIDEO_PLAYCOUNT,
+    VIDEO_RESOLUTION,
+    VIDEO_CHANNEL,
+    VIDEO_POSITION,
+    VIDEO_MRL,
+    VIDEO_VIDEO_TRACK,
+    VIDEO_AUDIO_TRACK,
+};
+
+}
+
+MLRecentsVideoModel::MLRecentsVideoModel( QObject* parent )
+    : MLSlidingWindowModel<MLVideo>( parent )
+{
+}
+
+QVariant MLRecentsVideoModel::data( const QModelIndex& index , int role ) const
+{
+    const auto video = item( static_cast<unsigned int>( index.row() ) );
+    if ( video == nullptr )
+        return {};
+    switch ( role )
+    {
+        case VIDEO_ID:
+            return QVariant::fromValue( video->getId() );
+        case VIDEO_TITLE:
+            return QVariant::fromValue( video->getTitle() );
+        case VIDEO_THUMBNAIL:
+            return QVariant::fromValue( video->getThumbnail() );
+        case VIDEO_DURATION:
+            return QVariant::fromValue( video->getDuration() );
+        case VIDEO_PROGRESS:
+            return QVariant::fromValue( video->getProgress() );
+        case VIDEO_PLAYCOUNT:
+            return QVariant::fromValue( video->getPlayCount() );
+        case VIDEO_RESOLUTION:
+            return QVariant::fromValue( video->getResolutionName() );
+        case VIDEO_CHANNEL:
+            return QVariant::fromValue( video->getChannel() );
+        case VIDEO_POSITION:
+            return QVariant::fromValue( video->getSavedPosition() );
+        case VIDEO_MRL:
+            return QVariant::fromValue( video->getMRL() );
+        case VIDEO_VIDEO_TRACK:
+            return QVariant::fromValue( video->getVideoDesc() );
+        case VIDEO_AUDIO_TRACK:
+            return QVariant::fromValue( video->getAudioDesc() );
+        default:
+            return {};
+    }
+}
+
+QHash<int, QByteArray> MLRecentsVideoModel::roleNames() const
+{
+    return {
+        { VIDEO_ID, "id" },
+        { VIDEO_TITLE, "title" },
+        { VIDEO_THUMBNAIL, "thumbnail" },
+        { VIDEO_DURATION, "duration" },
+        { VIDEO_PROGRESS, "progress" },
+        { VIDEO_PLAYCOUNT, "playcount" },
+        { VIDEO_RESOLUTION, "resolution_name" },
+        { VIDEO_CHANNEL, "channel" },
+        { VIDEO_POSITION, "saved_position" },
+        { VIDEO_MRL, "mrl" },
+        { VIDEO_AUDIO_TRACK, "audioDesc" },
+        { VIDEO_VIDEO_TRACK, "videoDesc" },
+    };
+}
+
+std::vector<std::unique_ptr<MLVideo> > MLRecentsVideoModel::fetch()
+{
+    ml_unique_ptr<vlc_ml_media_list_t> media_list{ vlc_ml_list_history(
+                m_ml, &m_query_param ) };
+    if ( media_list == nullptr )
+        return {};
+    std::vector<std::unique_ptr<MLVideo>> res;
+    m_video_count = 0;
+    for( vlc_ml_media_t &media: ml_range_iterate<vlc_ml_media_t>( media_list ) )
+        if( media.i_type == VLC_ML_MEDIA_TYPE_VIDEO )
+        {
+            m_video_count++;
+            res.emplace_back( std::unique_ptr<MLVideo>{ new MLVideo( m_ml, &media ) } );
+        }
+    return res;
+}
+
+size_t MLRecentsVideoModel::countTotalElements() const
+{
+
+    if(numberOfItemsToShow == -1){
+        return m_video_count;
+    }
+    return std::min(m_video_count,numberOfItemsToShow);
+}
+
+void MLRecentsVideoModel::onVlcMlEvent( const vlc_ml_event_t* event )
+{
+    switch ( event->i_type )
+    {
+        case VLC_ML_EVENT_MEDIA_ADDED:
+        case VLC_ML_EVENT_MEDIA_UPDATED:
+            if ( event->modification.p_media->i_type == VLC_ML_MEDIA_TYPE_VIDEO )
+                m_need_reset = true;
+            break;
+        case VLC_ML_EVENT_MEDIA_DELETED:
+            m_need_reset = true;
+            break;
+        default:
+            break;
+    }
+    MLBaseModel::onVlcMlEvent( event );
+}
+void MLRecentsVideoModel::setNumberOfItemsToShow( int n ){
+    numberOfItemsToShow = n;
+}
+int MLRecentsVideoModel::getNumberOfItemsToShow(){
+    return numberOfItemsToShow;
+}
diff --git a/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.hpp b/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.hpp
new file mode 100644
index 0000000000..055eecf174
--- /dev/null
+++ b/modules/gui/qt/components/mediacenter/mlrecentsvideomodel.hpp
@@ -0,0 +1,62 @@
+/*****************************************************************************
+ * 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 MCRECENTSMODEL_H
+#define MCRECENTSMODEL_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_media_library.h>
+
+#include "mlbasemodel.hpp"
+#include "mlvideo.hpp"
+
+#include <QObject>
+
+class MLRecentsVideoModel : public MLSlidingWindowModel<MLVideo>
+{
+    Q_OBJECT
+    Q_PROPERTY(int numberOfItemsToShow READ getNumberOfItemsToShow WRITE setNumberOfItemsToShow)
+
+public:
+    explicit MLRecentsVideoModel( QObject* parent = nullptr );
+    virtual ~MLRecentsVideoModel() = default;
+
+    QVariant data( const QModelIndex& index , int role ) const override;
+    QHash<int, QByteArray> roleNames() const override;
+    int numberOfItemsToShow = 10;
+
+private:
+    std::vector<std::unique_ptr<MLVideo>> fetch() override;
+    size_t countTotalElements() const override;
+    vlc_ml_sorting_criteria_t roleToCriteria( int /* role */ ) const override{
+        return VLC_ML_SORTING_DEFAULT;
+    }
+    vlc_ml_sorting_criteria_t nameToCriteria( QByteArray /* name */ ) const override{
+        return VLC_ML_SORTING_DEFAULT;
+    }
+    virtual void onVlcMlEvent( const vlc_ml_event_t* event ) override;
+    void setNumberOfItemsToShow(int);
+    int getNumberOfItemsToShow();
+    int m_video_count;
+};
+
+#endif // MCRECENTSMODEL_H
diff --git a/modules/gui/qt/main_interface.cpp b/modules/gui/qt/main_interface.cpp
index 4d83b3fda1..eb28983885 100644
--- a/modules/gui/qt/main_interface.cpp
+++ b/modules/gui/qt/main_interface.cpp
@@ -50,6 +50,7 @@
 #include "components/mediacenter/mlalbumtrackmodel.hpp"
 #include "components/mediacenter/mlgenremodel.hpp"
 #include "components/mediacenter/mlvideomodel.hpp"
+#include "components/mediacenter/mlrecentsvideomodel.hpp"
 #include "components/mediacenter/mlnetworkmodel.hpp"
 #include "components/recent_media_model.hpp"
 #include "components/settings.hpp"
@@ -341,6 +342,7 @@ void MainInterface::createMainWidget( QSettings * )
         qmlRegisterType<MLAlbumTrackModel>( "org.videolan.medialib", 0, 1, "MLAlbumTrackModel" );
         qmlRegisterType<MLGenreModel>( "org.videolan.medialib", 0, 1, "MLGenreModel" );
         qmlRegisterType<MLVideoModel>( "org.videolan.medialib", 0, 1, "MLVideoModel" );
+        qmlRegisterType<MLRecentsVideoModel>( "org.videolan.medialib", 0, 1, "MLRecentsVideoModel" );
         qRegisterMetaType<NetworkTreeItem>();
         qmlRegisterType<MLNetworkModel>( "org.videolan.medialib", 0, 1, "MLNetworkModel");
 



More information about the vlc-commits mailing list