Added support for checkbox select in AutoTableModel.

print
Josef Rokos 8 years ago
parent fb6b4fe027
commit f15a361b50

@ -24,6 +24,7 @@ public:
:ITableModel(parent) :ITableModel(parent)
{ {
filtered = false; filtered = false;
m_checkboxSelect = false;
} }
virtual ~AutoTableModel() {} virtual ~AutoTableModel() {}
@ -50,6 +51,21 @@ public:
QVariant data(const QModelIndex &index, int role) const 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<T> entity = m_list.at(index.row()); QSharedPointer<T> entity = m_list.at(index.row());
QObject *rawEntity = (QObject*)entity.data(); QObject *rawEntity = (QObject*)entity.data();
@ -189,6 +205,21 @@ public:
m_translations = translations; m_translations = translations;
} }
QList<int> selectedRows() const
{
return m_selectedRows;
}
QList<QSharedPointer<T> > selectedItems()
{
QList<QSharedPointer<T> > ret;
foreach (int row, m_selectedRows) {
ret.append(m_list[row]);
}
return ret;
}
protected: protected:
void handleFilter(const QString &filter) override void handleFilter(const QString &filter) override
{ {
@ -220,10 +251,10 @@ protected:
private: private:
QList<QSharedPointer<T> > m_list; QList<QSharedPointer<T> > m_list;
QList<QSharedPointer<T> > m_fullList; QList<QSharedPointer<T> > m_fullList;
QList<int> m_selectedRows;
QMap<QString, QString> m_translations; QMap<QString, QString> m_translations;
bool filtered; bool filtered;
// QAbstractItemModel interface // QAbstractItemModel interface
public: public:
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override 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); 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(); emit editCompleted();
return true; return true;
} }
}; };
#endif // ODBTABLEMODEL_H #endif // ODBTABLEMODEL_H

@ -3,7 +3,17 @@
ITableModel::ITableModel(QObject *parent) ITableModel::ITableModel(QObject *parent)
:QAbstractTableModel(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) void ITableModel::filter(const QString &filter)
@ -18,6 +28,11 @@ void ITableModel::restore()
Qt::ItemFlags ITableModel::flags(const QModelIndex &index) const 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())) if (m_editableCols.contains(index.column()))
{ {
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;

@ -16,6 +16,7 @@ public:
protected: protected:
virtual void handleFilter(const QString &filter) = 0; virtual void handleFilter(const QString &filter) = 0;
virtual void handleRestore() = 0; virtual void handleRestore() = 0;
bool m_checkboxSelect;
public slots: public slots:
void filter(const QString &filter); void filter(const QString &filter);
@ -29,6 +30,9 @@ public:
virtual Qt::ItemFlags flags(const QModelIndex &index) const override; virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
void setEditableCols(const QList<int> cols); void setEditableCols(const QList<int> cols);
bool checkboxSelect() const;
void setCheckboxSelect(bool checkboxSelect);
private: private:
QList<int> m_editableCols; QList<int> m_editableCols;
}; };

Loading…
Cancel
Save