#include "videosource.h"
#include "ytsearch.h"
-static const int maxItems = 50;
-static const QString recentKeywordsKey = "recentKeywords";
-static const QString recentChannelsKey = "recentChannels";
+namespace {
+const int maxItems = 50;
+const QString recentKeywordsKey = "recentKeywords";
+const QString recentChannelsKey = "recentChannels";
+} // namespace
PlaylistModel::PlaylistModel(QWidget *parent) : QAbstractListModel(parent) {
- videoSource = 0;
+ videoSource = nullptr;
searching = false;
canSearchMore = true;
firstSearch = false;
- m_activeVideo = 0;
+ m_activeVideo = nullptr;
m_activeRow = -1;
startIndex = 1;
max = 0;
return ItemTypeShowMore;
case Qt::DisplayRole:
if (!errorMessage.isEmpty()) return errorMessage;
- if (searching) return tr("Searching...");
+ if (searching) return QString(); // tr("Searching...");
if (canSearchMore) return tr("Show %1 More").arg("").simplified();
if (videos.isEmpty())
return tr("No videos");
void PlaylistModel::setActiveRow(int row, bool notify) {
if (rowExists(row)) {
m_activeRow = row;
+ Video *previousVideo = m_activeVideo;
m_activeVideo = videoAt(row);
int oldactiverow = m_activeRow;
createIndex(oldactiverow, columnCount() - 1));
emit dataChanged(createIndex(m_activeRow, 0), createIndex(m_activeRow, columnCount() - 1));
- if (notify) emit activeRowChanged(row);
+ if (notify) emit activeVideoChanged(m_activeVideo, previousVideo);
} else {
m_activeRow = -1;
- m_activeVideo = 0;
+ m_activeVideo = nullptr;
}
}
Video *PlaylistModel::videoAt(int row) const {
if (rowExists(row)) return videos.at(row);
- return 0;
+ return nullptr;
}
Video *PlaylistModel::activeVideo() const {
void PlaylistModel::setVideoSource(VideoSource *videoSource) {
beginResetModel();
- while (!videos.isEmpty())
- delete videos.takeFirst();
- m_activeVideo = 0;
+
+ qDeleteAll(videos);
+ videos.clear();
+
+ qDeleteAll(deletedVideos);
+ deletedVideos.clear();
+
+ m_activeVideo = nullptr;
m_activeRow = -1;
startIndex = 1;
endResetModel();
Qt::UniqueConnection);
connect(videoSource, SIGNAL(finished(int)), SLOT(searchFinished(int)), Qt::UniqueConnection);
connect(videoSource, SIGNAL(error(QString)), SLOT(searchError(QString)), Qt::UniqueConnection);
+ connect(videoSource, &QObject::destroyed, this,
+ [this, videoSource] {
+ if (this->videoSource == videoSource) {
+ this->videoSource = nullptr;
+ }
+ },
+ Qt::UniqueConnection);
searchMore();
}
void PlaylistModel::searchMore(int max) {
- if (searching) return;
+ if (videoSource == nullptr || searching) return;
searching = true;
firstSearch = startIndex == 1;
this->max = max;
videos.squeeze();
searching = false;
m_activeRow = -1;
- m_activeVideo = 0;
+ m_activeVideo = nullptr;
startIndex = 1;
endResetModel();
}
void PlaylistModel::removeIndexes(QModelIndexList &indexes) {
QVector<Video *> originalList(videos);
- QVector<Video *> delitems;
- delitems.reserve(indexes.size());
for (const QModelIndex &index : indexes) {
if (index.row() >= originalList.size()) continue;
Video *video = originalList.at(index.row());
int idx = videos.indexOf(video);
if (idx != -1) {
beginRemoveRows(QModelIndex(), idx, idx);
- delitems.append(video);
+ deletedVideos.append(video);
+ if (m_activeVideo == video) {
+ m_activeVideo = nullptr;
+ m_activeRow = -1;
+ }
videos.removeAll(video);
- video->deleteLater();
endRemoveRows();
}
}
- qDeleteAll(delitems);
videos.squeeze();
}