diff --git a/core/autotablemodel.h b/core/autotablemodel.h index 6f45b0f..2b12a38 100644 --- a/core/autotablemodel.h +++ b/core/autotablemodel.h @@ -24,6 +24,7 @@ public: :ITableModel(parent) { filtered = false; + m_checkboxSelect = false; } virtual ~AutoTableModel() {} @@ -50,6 +51,21 @@ public: QVariant data(const QModelIndex &index, int role) const { + if (index.column() == 0 && m_checkboxSelect) + { + if (role == Qt::CheckStateRole) + { + if (m_selectedRows.contains(index.row())) + { + return Qt::Checked; + } + else + { + return Qt::Unchecked; + } + } + } + QSharedPointer entity = m_list.at(index.row()); QObject *rawEntity = (QObject*)entity.data(); @@ -189,6 +205,21 @@ public: m_translations = translations; } + QList selectedRows() const + { + return m_selectedRows; + } + + QList > selectedItems() + { + QList > ret; + foreach (int row, m_selectedRows) { + ret.append(m_list[row]); + } + + return ret; + } + protected: void handleFilter(const QString &filter) override { @@ -220,10 +251,10 @@ protected: private: QList > m_list; QList > m_fullList; + QList m_selectedRows; QMap m_translations; bool filtered; - // QAbstractItemModel interface public: virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override @@ -236,10 +267,21 @@ public: rawEntity->setProperty(rawEntity->metaObject()->property(index.column() + 1).name(), value); } + if (role == Qt::CheckStateRole) + { + if (m_selectedRows.contains(index.row())) + { + m_selectedRows.removeOne(index.row()); + } + else + { + m_selectedRows.append(index.row()); + } + } + emit editCompleted(); return true; } }; #endif // ODBTABLEMODEL_H - diff --git a/core/itablemodel.cpp b/core/itablemodel.cpp index dfde394..7933a8c 100644 --- a/core/itablemodel.cpp +++ b/core/itablemodel.cpp @@ -3,7 +3,17 @@ ITableModel::ITableModel(QObject *parent) :QAbstractTableModel(parent) { + m_checkboxSelect = false; +} + +bool ITableModel::checkboxSelect() const +{ + return m_checkboxSelect; +} +void ITableModel::setCheckboxSelect(bool checkboxSelect) +{ + m_checkboxSelect = checkboxSelect; } void ITableModel::filter(const QString &filter) @@ -18,6 +28,11 @@ void ITableModel::restore() Qt::ItemFlags ITableModel::flags(const QModelIndex &index) const { + if (index.column() == 0 && m_checkboxSelect) + { + return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled; + } + if (m_editableCols.contains(index.column())) { return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; diff --git a/core/itablemodel.h b/core/itablemodel.h index e30780f..b337c16 100644 --- a/core/itablemodel.h +++ b/core/itablemodel.h @@ -16,6 +16,7 @@ public: protected: virtual void handleFilter(const QString &filter) = 0; virtual void handleRestore() = 0; + bool m_checkboxSelect; public slots: void filter(const QString &filter); @@ -29,6 +30,9 @@ public: virtual Qt::ItemFlags flags(const QModelIndex &index) const override; void setEditableCols(const QList cols); + bool checkboxSelect() const; + void setCheckboxSelect(bool checkboxSelect); + private: QList m_editableCols; };