in progress selection model

This commit is contained in:
Chris Cochrun 2023-02-21 09:56:37 -06:00
parent 891f6e395d
commit 27b3d1d9d1
2 changed files with 68 additions and 2 deletions

View file

@ -7,6 +7,7 @@
#include <QSqlQuery> #include <QSqlQuery>
#include <QSql> #include <QSql>
#include <QSqlDatabase> #include <QSqlDatabase>
#include <QItemSelectionModel>
#include <qabstractitemmodel.h> #include <qabstractitemmodel.h>
#include <qdebug.h> #include <qdebug.h>
#include <qglobal.h> #include <qglobal.h>
@ -715,6 +716,8 @@ SongProxyModel::SongProxyModel(QObject *parent)
:QSortFilterProxyModel(parent) :QSortFilterProxyModel(parent)
{ {
m_songModel = new SongSqlModel; m_songModel = new SongSqlModel;
m_selectionModel = new QItemSelectionModel;
m_selectionModel->setModel(this);
setSourceModel(m_songModel); setSourceModel(m_songModel);
setDynamicSortFilter(true); setDynamicSortFilter(true);
setFilterRole(Qt::UserRole + 1); setFilterRole(Qt::UserRole + 1);
@ -725,11 +728,60 @@ SongSqlModel *SongProxyModel::songModel() {
return m_songModel; return m_songModel;
} }
QModelIndex SongProxyModel::idx(int row) {
QModelIndex idx = index(row, 0);
qDebug() << idx;
return idx;
}
QVariantMap SongProxyModel::getSong(const int &row) { QVariantMap SongProxyModel::getSong(const int &row) {
return QVariantMap(); auto model = qobject_cast<SongSqlModel *>(sourceModel());
QVariantMap song = model->getSong(mapToSource(index(row, 0)).row());
return song;
} }
void SongProxyModel::deleteSong(const int &row) { void SongProxyModel::deleteSong(const int &row) {
auto model = qobject_cast<SongSqlModel *>(sourceModel()); auto model = qobject_cast<SongSqlModel *>(sourceModel());
model->deleteSong(row); 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<QItemSelectionModel *>(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);
}

View file

@ -3,6 +3,7 @@
#include <QSqlTableModel> #include <QSqlTableModel>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QItemSelectionModel>
#include <qobjectdefs.h> #include <qobjectdefs.h>
#include <qqml.h> #include <qqml.h>
#include <qvariant.h> #include <qvariant.h>
@ -112,20 +113,33 @@ class SongProxyModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(SongSqlModel *songModel READ songModel) Q_PROPERTY(SongSqlModel *songModel READ songModel)
Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectedChanged)
public: public:
explicit SongProxyModel(QObject *parent = nullptr); explicit SongProxyModel(QObject *parent = nullptr);
~SongProxyModel() = default; ~SongProxyModel() = default;
SongSqlModel *songModel(); 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<int, QByteArray> roleNames() const override;
public slots: public slots:
Q_INVOKABLE QVariantMap getSong(const int &row); Q_INVOKABLE QVariantMap getSong(const int &row);
Q_INVOKABLE void deleteSong(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: private:
SongSqlModel *m_songModel; SongSqlModel *m_songModel;
QItemSelectionModel *m_selectionModel;
// bool m_selected;
}; };
#endif //SONGSQLMODEL_H #endif //SONGSQLMODEL_H