Skip to content

Commit

Permalink
fix: crash caused by urlByIndex() with invalid model index
Browse files Browse the repository at this point in the history
Also save a QVariant convert for QUrl
  • Loading branch information
BLumia committed Jul 20, 2024
1 parent eb2e2e9 commit f32cb99
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void MainWindow::clearGallery()

void MainWindow::loadGalleryBySingleLocalFile(const QString &path)
{
m_pm->loadPlaylist({QUrl::fromLocalFile(path)});
m_pm->loadPlaylist(QUrl::fromLocalFile(path));
}

void MainWindow::galleryPrev()
Expand Down Expand Up @@ -700,7 +700,7 @@ void MainWindow::on_actionPaste_triggered()
} else if (clipboardFileUrl.isValid()) {
QString localFile(clipboardFileUrl.toLocalFile());
m_graphicsView->showFileFromPath(localFile, true);
m_pm->loadPlaylist({clipboardFileUrl});
m_pm->loadPlaylist(clipboardFileUrl);
}
}

Expand Down
16 changes: 14 additions & 2 deletions app/playlistmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ int PlaylistModel::indexOf(const QUrl &url) const
return m_playlist.indexOf(url);
}

QUrl PlaylistModel::urlByIndex(int index) const
{
return m_playlist.value(index);
}

QStringList PlaylistModel::autoLoadFilterSuffixes() const
{
return m_autoLoadSuffixes;
Expand Down Expand Up @@ -157,7 +162,7 @@ PlaylistManager::~PlaylistManager()

}

const PlaylistModel *PlaylistManager::model() const
PlaylistModel *PlaylistManager::model()
{
return &m_model;
}
Expand All @@ -174,6 +179,13 @@ QModelIndex PlaylistManager::loadPlaylist(const QList<QUrl> &urls)
return idx;
}

QModelIndex PlaylistManager::loadPlaylist(const QUrl &url)
{
QModelIndex idx = m_model.loadPlaylist(url);
setProperty("currentIndex", idx.row());
return idx;
}

int PlaylistManager::totalCount() const
{
return m_model.rowCount();
Expand Down Expand Up @@ -209,7 +221,7 @@ void PlaylistManager::setCurrentIndex(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)
Expand Down
4 changes: 3 additions & 1 deletion app/playlistmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PlaylistModel : public QAbstractListModel
QModelIndex appendToPlaylist(const QUrl & url);
bool removeAt(int index);
int indexOf(const QUrl & url) const;
QUrl urlByIndex(int index) const;
QStringList autoLoadFilterSuffixes() const;

QModelIndex createIndex(int row) const;
Expand Down Expand Up @@ -52,10 +53,11 @@ class PlaylistManager : public QObject
explicit PlaylistManager(QObject *parent = nullptr);
~PlaylistManager();

const PlaylistModel * model() const;
PlaylistModel * model();

void setPlaylist(const QList<QUrl> & url);
QModelIndex loadPlaylist(const QList<QUrl> & urls);
QModelIndex loadPlaylist(const QUrl & url);

int totalCount() const;
QModelIndex previousIndex() const;
Expand Down

0 comments on commit f32cb99

Please sign in to comment.