Added support for checkbox select in AutoTableModel.

print
Josef Rokos 9 years ago
parent fb6b4fe027
commit f15a361b50

@ -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<T> entity = m_list.at(index.row());
QObject *rawEntity = (QObject*)entity.data();
@ -189,6 +205,21 @@ public:
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:
void handleFilter(const QString &filter) override
{
@ -220,10 +251,10 @@ protected:
private:
QList<QSharedPointer<T> > m_list;
QList<QSharedPointer<T> > m_fullList;
QList<int> m_selectedRows;
QMap<QString, QString> 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

@ -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;

@ -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<int> cols);
bool checkboxSelect() const;
void setCheckboxSelect(bool checkboxSelect);
private:
QList<int> m_editableCols;
};

Loading…
Cancel
Save