Adding new items to the model can now be made undone/redone.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
#include "tablemodel.h"
|
||||
|
||||
#include "../formats/jsonparser.h"
|
||||
#include "commands/insertrowscommand.h"
|
||||
#include "modelitem.h"
|
||||
|
||||
QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
||||
@ -9,8 +10,9 @@ QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
||||
{AmountRole, "Amount"},
|
||||
{FactorRole, "Factor"}};
|
||||
|
||||
TableModel::TableModel(QObject* parent)
|
||||
: QAbstractTableModel{parent} {
|
||||
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
||||
: QAbstractTableModel{parent}
|
||||
, m_undoStack(undoStack) {
|
||||
for (int row = 0; row < 5; ++row) {
|
||||
QHash<int, QVariant> values;
|
||||
values[NameRole] = QString("Item %1").arg(row);
|
||||
@ -56,6 +58,12 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
return m_items.at(row)->data(roleForColumn);
|
||||
case NameRole:
|
||||
case DescriptionRole:
|
||||
case InfoRole:
|
||||
case AmountRole:
|
||||
case FactorRole:
|
||||
return m_items.at(row)->data(role);
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
@ -122,14 +130,9 @@ void TableModel::insertItems(int startPosition,
|
||||
}
|
||||
|
||||
QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc);
|
||||
const int nRows = valueList.size();
|
||||
beginInsertRows(QModelIndex(), startPosition, startPosition + nRows - 1);
|
||||
for (int row = 0; row < nRows; ++row) {
|
||||
const int rowPosition = startPosition + row;
|
||||
shared_ptr<ModelItem> item = make_unique<ModelItem>(valueList.at(row));
|
||||
m_items.insert(rowPosition, std::move(item));
|
||||
}
|
||||
endInsertRows();
|
||||
|
||||
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList);
|
||||
m_undoStack->push(insertCommand);
|
||||
}
|
||||
|
||||
int TableModel::getRoleForColumn(const int column) const {
|
||||
@ -154,3 +157,24 @@ int TableModel::getRoleForColumn(const int column) const {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TableModel::execInsertItems(const int firstRow, const QList<QHash<int, QVariant>> valueList) {
|
||||
const int nRows = valueList.size();
|
||||
qDebug() << "Inserting" << nRows << "items...";
|
||||
|
||||
const int lastRow = firstRow + nRows - 1;
|
||||
beginInsertRows(QModelIndex(), firstRow, lastRow);
|
||||
for (int row = 0; row < nRows; ++row) {
|
||||
const int rowPosition = firstRow + row;
|
||||
shared_ptr<ModelItem> item = make_unique<ModelItem>(valueList.at(row));
|
||||
m_items.insert(rowPosition, std::move(item));
|
||||
}
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void TableModel::execRemoveItems(const int firstRow, const int nRows) {
|
||||
const int lastRow = firstRow + nRows - 1;
|
||||
beginRemoveRows(QModelIndex(), firstRow, lastRow);
|
||||
m_items.remove(firstRow, nRows);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user