From 27b3d1d9d1a1c71d2415640b31325b4d8fe20030 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 21 Feb 2023 09:56:37 -0600 Subject: [PATCH] in progress selection model --- src/cpp/songsqlmodel.cpp | 54 +++++++++++++++++++++++++++++++++++++++- src/cpp/songsqlmodel.h | 16 +++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/cpp/songsqlmodel.cpp b/src/cpp/songsqlmodel.cpp index ad4bef1..01b633b 100644 --- a/src/cpp/songsqlmodel.cpp +++ b/src/cpp/songsqlmodel.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -715,6 +716,8 @@ SongProxyModel::SongProxyModel(QObject *parent) :QSortFilterProxyModel(parent) { m_songModel = new SongSqlModel; + m_selectionModel = new QItemSelectionModel; + m_selectionModel->setModel(this); setSourceModel(m_songModel); setDynamicSortFilter(true); setFilterRole(Qt::UserRole + 1); @@ -725,11 +728,60 @@ SongSqlModel *SongProxyModel::songModel() { return m_songModel; } +QModelIndex SongProxyModel::idx(int row) { + QModelIndex idx = index(row, 0); + qDebug() << idx; + return idx; +} + QVariantMap SongProxyModel::getSong(const int &row) { - return QVariantMap(); + auto model = qobject_cast(sourceModel()); + QVariantMap song = model->getSong(mapToSource(index(row, 0)).row()); + return song; } void SongProxyModel::deleteSong(const int &row) { auto model = qobject_cast(sourceModel()); model->deleteSong(row); } + +bool SongProxyModel::selected(int row) { + QModelIndex idx = index(row, 0); + return m_selectionModel->isSelected(idx); +} + +bool SongProxyModel::setSelected(int row, bool select) { + if (selected(row) == select) + return false; + QModelIndex idx = index(row, 0); + if (select) + m_selectionModel->select(idx, QItemSelectionModel::SelectCurrent); + else + m_selectionModel->select(idx, QItemSelectionModel::Deselect); + emit selectedChanged(select); + return true; +} + +void SongProxyModel::select(int id) { + QModelIndex idx = index(id, 0); + //check to make sure this item isn't already selected + if (m_selectionModel->isSelected(idx)) + return; + + // deselect all items + QModelIndex first = index(0, 0); + QModelIndex last = index(rowCount() - 1, 0); + QItemSelection all = new QItemSelection(first, last); + m_selectionModel->select(all, QItemSelectionModel::Deselect); + + m_selectionModel->select(idx, QItemSelectionModel::SelectCurrent); +} + +void SongProxyModel::selectSongs(int row) { + auto model = qobject_cast(m_selectionModel); + // deselect all items + QModelIndex first = index(0, 0); + QModelIndex last = index(rowCount() - 1, 0); + QItemSelection all = new QItemSelection(first, last); + m_selectionModel->select(all, QItemSelectionModel::Deselect); +} diff --git a/src/cpp/songsqlmodel.h b/src/cpp/songsqlmodel.h index d0ec02f..909bc8e 100644 --- a/src/cpp/songsqlmodel.h +++ b/src/cpp/songsqlmodel.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -112,20 +113,33 @@ class SongProxyModel : public QSortFilterProxyModel { Q_OBJECT Q_PROPERTY(SongSqlModel *songModel READ songModel) + Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectedChanged) public: explicit SongProxyModel(QObject *parent = nullptr); ~SongProxyModel() = default; SongSqlModel *songModel(); + Q_INVOKABLE QModelIndex idx(int row); + bool selected(int row); + bool setSelected(int row, bool select); + // QVariant data(const QModelIndex &index, int role) const override; + // QHash roleNames() const override; + public slots: Q_INVOKABLE QVariantMap getSong(const int &row); Q_INVOKABLE void deleteSong(const int &row); + Q_INVOKABLE void select(int row); + Q_INVOKABLE void selectSongs(int row); + +signals: + void selectedChanged(bool selected); private: SongSqlModel *m_songModel; + QItemSelectionModel *m_selectionModel; + // bool m_selected; }; - #endif //SONGSQLMODEL_H