fix: crash caused by urlByIndex() with invalid model index

Also save a QVariant convert for QUrl
This commit is contained in:
Gary Wang 2024-07-21 00:52:32 +08:00
parent eb2e2e93f9
commit f32cb998ae
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
3 changed files with 19 additions and 5 deletions

View File

@ -247,7 +247,7 @@ void MainWindow::clearGallery()
void MainWindow::loadGalleryBySingleLocalFile(const QString &path) void MainWindow::loadGalleryBySingleLocalFile(const QString &path)
{ {
m_pm->loadPlaylist({QUrl::fromLocalFile(path)}); m_pm->loadPlaylist(QUrl::fromLocalFile(path));
} }
void MainWindow::galleryPrev() void MainWindow::galleryPrev()
@ -700,7 +700,7 @@ void MainWindow::on_actionPaste_triggered()
} else if (clipboardFileUrl.isValid()) { } else if (clipboardFileUrl.isValid()) {
QString localFile(clipboardFileUrl.toLocalFile()); QString localFile(clipboardFileUrl.toLocalFile());
m_graphicsView->showFileFromPath(localFile, true); m_graphicsView->showFileFromPath(localFile, true);
m_pm->loadPlaylist({clipboardFileUrl}); m_pm->loadPlaylist(clipboardFileUrl);
} }
} }

View File

@ -104,6 +104,11 @@ int PlaylistModel::indexOf(const QUrl &url) const
return m_playlist.indexOf(url); return m_playlist.indexOf(url);
} }
QUrl PlaylistModel::urlByIndex(int index) const
{
return m_playlist.value(index);
}
QStringList PlaylistModel::autoLoadFilterSuffixes() const QStringList PlaylistModel::autoLoadFilterSuffixes() const
{ {
return m_autoLoadSuffixes; return m_autoLoadSuffixes;
@ -157,7 +162,7 @@ PlaylistManager::~PlaylistManager()
} }
const PlaylistModel *PlaylistManager::model() const PlaylistModel *PlaylistManager::model()
{ {
return &m_model; return &m_model;
} }
@ -174,6 +179,13 @@ QModelIndex PlaylistManager::loadPlaylist(const QList<QUrl> &urls)
return idx; return idx;
} }
QModelIndex PlaylistManager::loadPlaylist(const QUrl &url)
{
QModelIndex idx = m_model.loadPlaylist(url);
setProperty("currentIndex", idx.row());
return idx;
}
int PlaylistManager::totalCount() const int PlaylistManager::totalCount() const
{ {
return m_model.rowCount(); return m_model.rowCount();
@ -209,7 +221,7 @@ void PlaylistManager::setCurrentIndex(const QModelIndex &index)
QUrl PlaylistManager::urlByIndex(const QModelIndex &index) QUrl PlaylistManager::urlByIndex(const QModelIndex &index)
{ {
return m_model.data(index, PlaylistModel::UrlRole).toUrl(); return m_model.urlByIndex(index.row());
} }
QString PlaylistManager::localFileByIndex(const QModelIndex &index) QString PlaylistManager::localFileByIndex(const QModelIndex &index)

View File

@ -25,6 +25,7 @@ public:
QModelIndex appendToPlaylist(const QUrl & url); QModelIndex appendToPlaylist(const QUrl & url);
bool removeAt(int index); bool removeAt(int index);
int indexOf(const QUrl & url) const; int indexOf(const QUrl & url) const;
QUrl urlByIndex(int index) const;
QStringList autoLoadFilterSuffixes() const; QStringList autoLoadFilterSuffixes() const;
QModelIndex createIndex(int row) const; QModelIndex createIndex(int row) const;
@ -52,10 +53,11 @@ public:
explicit PlaylistManager(QObject *parent = nullptr); explicit PlaylistManager(QObject *parent = nullptr);
~PlaylistManager(); ~PlaylistManager();
const PlaylistModel * model() const; PlaylistModel * model();
void setPlaylist(const QList<QUrl> & url); void setPlaylist(const QList<QUrl> & url);
QModelIndex loadPlaylist(const QList<QUrl> & urls); QModelIndex loadPlaylist(const QList<QUrl> & urls);
QModelIndex loadPlaylist(const QUrl & url);
int totalCount() const; int totalCount() const;
QModelIndex previousIndex() const; QModelIndex previousIndex() const;