X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fplaylistmodel.cpp;h=c4a7d72b9fdfd096491c56de9ffa870fc4c8902e;hb=434d88418722fd7717038e44bd74271ca1d92771;hp=63cd6a3ff675d2d04c087f8ef0c9cd8976e4bf89;hpb=7ca7741e1eb9bc70617fbb4207738e291218c495;p=minitube diff --git a/src/playlistmodel.cpp b/src/playlistmodel.cpp index 63cd6a3..c4a7d72 100644 --- a/src/playlistmodel.cpp +++ b/src/playlistmodel.cpp @@ -19,23 +19,25 @@ along with Minitube. If not, see . $END_LICENSE */ #include "playlistmodel.h" +#include "mediaview.h" +#include "searchparams.h" +#include "video.h" #include "videomimedata.h" #include "videosource.h" #include "ytsearch.h" -#include "video.h" -#include "searchparams.h" -#include "mediaview.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; @@ -44,33 +46,32 @@ PlaylistModel::PlaylistModel(QWidget *parent) : QAbstractListModel(parent) { authorPressed = false; } -int PlaylistModel::rowCount(const QModelIndex &/*parent*/) const { +int PlaylistModel::rowCount(const QModelIndex & /*parent*/) const { int count = videos.size(); - + // add the message item - if (videos.isEmpty() || !searching) - count++; - + if (videos.isEmpty() || !searching) count++; + return count; } QVariant PlaylistModel::data(const QModelIndex &index, int role) const { - int row = index.row(); - + if (row == videos.size()) { - QPalette palette; - + switch (role) { case ItemTypeRole: 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"); - else return tr("No more videos"); + if (videos.isEmpty()) + return tr("No videos"); + else + return tr("No more videos"); case Qt::TextAlignmentRole: return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter)); case Qt::ForegroundRole: @@ -86,12 +87,12 @@ QVariant PlaylistModel::data(const QModelIndex &index, int role) const { default: return QVariant(); } - + } else if (row < 0 || row >= videos.size()) return QVariant(); - + Video *video = videos.at(row); - + switch (role) { case ItemTypeRole: return ItemTypeVideo; @@ -100,7 +101,7 @@ QVariant PlaylistModel::data(const QModelIndex &index, int role) const { case ActiveTrackRole: return video == m_activeVideo; case Qt::DisplayRole: - return video->title(); + return video->getTitle(); case HoveredItemRole: return hoveredRow == index.row(); case AuthorHoveredRole: @@ -112,77 +113,84 @@ QVariant PlaylistModel::data(const QModelIndex &index, int role) const { return video->description(); */ } - + return QVariant(); } void PlaylistModel::setActiveRow(int row, bool notify) { - if ( rowExists( row ) ) { - + if (rowExists(row)) { m_activeRow = row; + Video *previousVideo = m_activeVideo; m_activeVideo = videoAt(row); - + int oldactiverow = m_activeRow; - - if ( rowExists( oldactiverow ) ) - emit dataChanged( createIndex( oldactiverow, 0 ), createIndex( oldactiverow, columnCount() - 1 ) ); - - emit dataChanged( createIndex( m_activeRow, 0 ), createIndex( m_activeRow, columnCount() - 1 ) ); - if (notify) emit activeRowChanged(row); - + + if (rowExists(oldactiverow)) + emit dataChanged(createIndex(oldactiverow, 0), + createIndex(oldactiverow, columnCount() - 1)); + + emit dataChanged(createIndex(m_activeRow, 0), createIndex(m_activeRow, columnCount() - 1)); + if (notify) emit activeVideoChanged(m_activeVideo, previousVideo); + } else { m_activeRow = -1; - m_activeVideo = 0; + m_activeVideo = nullptr; } - } int PlaylistModel::nextRow() const { int nextRow = m_activeRow + 1; - if (rowExists(nextRow)) - return nextRow; + if (rowExists(nextRow)) return nextRow; return -1; } int PlaylistModel::previousRow() const { int prevRow = m_activeRow - 1; - if (rowExists(prevRow)) - return prevRow; + if (rowExists(prevRow)) return prevRow; return -1; } -Video* PlaylistModel::videoAt( int row ) const { - if ( rowExists( row ) ) - return videos.at( row ); - return 0; +Video *PlaylistModel::videoAt(int row) const { + if (rowExists(row)) return videos.at(row); + return nullptr; } -Video* PlaylistModel::activeVideo() const { +Video *PlaylistModel::activeVideo() const { return m_activeVideo; } void PlaylistModel::setVideoSource(VideoSource *videoSource) { beginResetModel(); - while (!videos.isEmpty()) delete videos.takeFirst(); + + qDeleteAll(videos); videos.clear(); - m_activeVideo = 0; + + qDeleteAll(deletedVideos); + deletedVideos.clear(); + + m_activeVideo = nullptr; m_activeRow = -1; startIndex = 1; endResetModel(); this->videoSource = videoSource; - connect(videoSource, SIGNAL(gotVideos(QList)), - SLOT(addVideos(QList)), Qt::UniqueConnection); - connect(videoSource, SIGNAL(finished(int)), - SLOT(searchFinished(int)), Qt::UniqueConnection); - connect(videoSource, SIGNAL(error(QString)), - SLOT(searchError(QString)), Qt::UniqueConnection); + connect(videoSource, SIGNAL(gotVideos(QVector