]> git.sur5r.net Git - minitube/blobdiff - src/playlistmodel.cpp
New upstream version 3.9.1
[minitube] / src / playlistmodel.cpp
index c4a7d72b9fdfd096491c56de9ffa870fc4c8902e..f2e0e5f32d0f5c9a10c4c6e84e3dfa85e7c311e8 100644 (file)
@@ -20,14 +20,15 @@ $END_LICENSE */
 
 #include "playlistmodel.h"
 #include "mediaview.h"
+#include "playlistitemdelegate.h"
 #include "searchparams.h"
 #include "video.h"
 #include "videomimedata.h"
 #include "videosource.h"
-#include "ytsearch.h"
+
+#include "searchvideosource.h"
 
 namespace {
-const int maxItems = 50;
 const QString recentKeywordsKey = "recentKeywords";
 const QString recentChannelsKey = "recentChannels";
 } // namespace
@@ -75,11 +76,8 @@ QVariant PlaylistModel::data(const QModelIndex &index, int role) const {
         case Qt::TextAlignmentRole:
             return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
         case Qt::ForegroundRole:
-            if (!errorMessage.isEmpty())
-                return palette.color(QPalette::ToolTipText);
-            else
-                return palette.color(QPalette::Dark);
-        case Qt::BackgroundColorRole:
+            return palette.color(QPalette::Dark);
+        case Qt::BackgroundRole:
             if (!errorMessage.isEmpty())
                 return palette.color(QPalette::ToolTipBase);
             else
@@ -186,27 +184,25 @@ void PlaylistModel::setVideoSource(VideoSource *videoSource) {
             },
             Qt::UniqueConnection);
 
+    canSearchMore = true;
     searchMore();
 }
 
-void PlaylistModel::searchMore(int max) {
-    if (videoSource == nullptr || searching) return;
+void PlaylistModel::searchMore() {
+    if (!canSearchMore || videoSource == nullptr || searching) return;
     searching = true;
     firstSearch = startIndex == 1;
-    this->max = max;
+    max = videoSource->maxResults();
+    if (max == 0) max = 20;
     errorMessage.clear();
     videoSource->loadVideos(max, startIndex);
     startIndex += max;
 }
 
-void PlaylistModel::searchMore() {
-    searchMore(maxItems);
-}
-
 void PlaylistModel::searchNeeded() {
     const int desiredRowsAhead = 10;
     int remainingRows = videos.size() - m_activeRow;
-    if (remainingRows < desiredRowsAhead) searchMore(maxItems);
+    if (remainingRows < desiredRowsAhead) searchMore();
 }
 
 void PlaylistModel::abortSearch() {
@@ -229,7 +225,7 @@ void PlaylistModel::searchFinished(int total) {
     canSearchMore = videoSource->hasMoreVideos();
 
     // update the message item
-    emit dataChanged(createIndex(maxItems, 0), createIndex(maxItems, columnCount() - 1));
+    emit dataChanged(createIndex(videos.size(), 0), createIndex(videos.size(), columnCount() - 1));
 
     if (firstSearch && !videos.isEmpty()) handleFirstVideo(videos.at(0));
 }
@@ -237,7 +233,7 @@ void PlaylistModel::searchFinished(int total) {
 void PlaylistModel::searchError(const QString &message) {
     errorMessage = message;
     // update the message item
-    emit dataChanged(createIndex(maxItems, 0), createIndex(maxItems, columnCount() - 1));
+    emit dataChanged(createIndex(videos.size(), 0), createIndex(videos.size(), columnCount() - 1));
 }
 
 void PlaylistModel::addVideos(const QVector<Video *> &newVideos) {
@@ -247,8 +243,10 @@ void PlaylistModel::addVideos(const QVector<Video *> &newVideos) {
     videos.append(newVideos);
     endInsertRows();
     for (Video *video : newVideos) {
-        connect(video, SIGNAL(gotThumbnail()), SLOT(updateVideoSender()), Qt::UniqueConnection);
-        video->loadThumbnail();
+        connect(video, &Video::changed, this, [video, this] {
+            int row = rowForVideo(video);
+            emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
+        });
     }
 }
 
@@ -261,13 +259,13 @@ void PlaylistModel::handleFirstVideo(Video *video) {
         if (!settings.value("manualplay", false).toBool()) setActiveRow(0);
     }
 
-    if (videoSource->metaObject()->className() == QLatin1String("YTSearch")) {
-        static const int maxRecentElements = 10;
-
-        YTSearch *search = qobject_cast<YTSearch *>(videoSource);
+    auto clazz = videoSource->metaObject()->className();
+    if (clazz == QLatin1String("SearchVideoSource")) {
+        auto search = qobject_cast<SearchVideoSource *>(videoSource);
         SearchParams *searchParams = search->getSearchParams();
 
         // save keyword
+        static const int maxRecentElements = 10;
         QString query = searchParams->keywords();
         if (!query.isEmpty() && !searchParams->isTransient()) {
             if (query.startsWith("http://")) {
@@ -302,16 +300,6 @@ void PlaylistModel::handleFirstVideo(Video *video) {
     }
 }
 
-void PlaylistModel::updateVideoSender() {
-    Video *video = static_cast<Video *>(sender());
-    if (!video) {
-        qDebug() << "Cannot get sender";
-        return;
-    }
-    int row = rowForVideo(video);
-    emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
-}
-
 void PlaylistModel::emitDataChanged() {
     QModelIndex index = createIndex(rowCount() - 1, 0);
     emit dataChanged(index, index);
@@ -416,7 +404,11 @@ bool PlaylistModel::dropMimeData(const QMimeData *data,
 
         // and then add them again at the new position
         beginInsertRows(QModelIndex(), beginRow, beginRow);
-        videos.insert(beginRow, video);
+        if (beginRow >= videos.size()) {
+            videos.push_back(video);
+        } else {
+            videos.insert(beginRow, video);
+        }
         endInsertRows();
     }