Adding new items to the model can now be made undone/redone.

This commit is contained in:
2025-12-11 15:46:28 +01:00
parent 432e81d4be
commit 0166a00d9d
7 changed files with 114 additions and 18 deletions

View File

@ -25,6 +25,7 @@ add_library(${TARGET_APP} STATIC
model/tablemodel.h model/tablemodel.cpp model/tablemodel.h model/tablemodel.cpp
model/modelitem.h model/modelitem.cpp model/modelitem.h model/modelitem.cpp
formats/jsonparser.h formats/jsonparser.cpp formats/jsonparser.h formats/jsonparser.cpp
model/commands/insertrowscommand.h model/commands/insertrowscommand.cpp
) )
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})

View File

@ -19,7 +19,8 @@ using namespace std;
GenericCore::GenericCore() { GenericCore::GenericCore() {
qDebug() << "Creating core..."; qDebug() << "Creating core...";
m_modelUndoStack = make_shared<QUndoStack>(this); // TODO let the model own its undo stack (& use TableModel::getUndoStack() if necessary)
m_modelUndoStack = new QUndoStack(this);
setupModels(); setupModels();
} }
@ -74,12 +75,12 @@ void GenericCore::triggerApplicationUpdate() {
QProcess::startDetached(toolFilePath, args); QProcess::startDetached(toolFilePath, args);
} }
std::shared_ptr<QUndoStack> GenericCore::getModUndoStack() const { return m_modelUndoStack; } QUndoStack* GenericCore::getModelUndoStack() const { return m_modelUndoStack; }
std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; } std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; }
void GenericCore::setupModels() { void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(this); m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
// TODO add QAbstractItemModelTester // TODO add QAbstractItemModelTester
} }

View File

@ -22,14 +22,14 @@ class GenericCore : public QObject {
bool isApplicationUpdateAvailable(); bool isApplicationUpdateAvailable();
void triggerApplicationUpdate(); void triggerApplicationUpdate();
std::shared_ptr<QUndoStack> getModUndoStack() const; QUndoStack* getModelUndoStack() const;
std::shared_ptr<TableModel> getModel() const; std::shared_ptr<TableModel> getModel() const;
signals: signals:
void displayStatusMessage(QString message); void displayStatusMessage(QString message);
private: private:
std::shared_ptr<QUndoStack> m_modelUndoStack; QUndoStack* m_modelUndoStack;
std::shared_ptr<TableModel> m_mainModel; std::shared_ptr<TableModel> m_mainModel;
void setupModels(); void setupModels();

View File

@ -0,0 +1,33 @@
#include "insertrowscommand.h"
#include <QDebug>
#include "../tablemodel.h"
InsertRowsCommand::InsertRowsCommand(TableModel* model,
int startRow,
QList<QHash<int, QVariant> > valueList,
QUndoCommand* parent)
: QUndoCommand(parent)
, m_tableModel(model)
, m_startRow(startRow)
, m_valueList(valueList) {
qInfo() << "New InsertCommand...";
const QString commandText =
QString("inserting %1 item(s) on row %2").arg(valueList.length()).arg(startRow);
setText(commandText);
}
void InsertRowsCommand::undo() {
qDebug() << "Undoing the InsertCommand...";
if (m_tableModel) {
m_tableModel->execRemoveItems(m_startRow, m_valueList.length());
}
}
void InsertRowsCommand::redo() {
qDebug() << "(Re-)doing the InsertCommand...";
if (m_tableModel) {
m_tableModel->execInsertItems(m_startRow, m_valueList);
}
}

View File

@ -0,0 +1,28 @@
#ifndef INSERTROWSCOMMAND_H
#define INSERTROWSCOMMAND_H
#include <QUndoCommand>
class TableModel;
class InsertRowsCommand : public QUndoCommand {
public:
// TODO don't use simple pointer to model
/// Using simple pointer to model because there was a crash when closing the application with an
/// unclean undo stack
InsertRowsCommand(TableModel* model,
int startRow,
QList<QHash<int, QVariant> > valueList,
QUndoCommand* parent = nullptr);
/// QUndoCommand interface
void undo() override;
void redo() override;
private:
TableModel* m_tableModel;
const int m_startRow;
const QList<QHash<int, QVariant> > m_valueList;
};
#endif // INSERTROWSCOMMAND_H

View File

@ -1,6 +1,7 @@
#include "tablemodel.h" #include "tablemodel.h"
#include "../formats/jsonparser.h" #include "../formats/jsonparser.h"
#include "commands/insertrowscommand.h"
#include "modelitem.h" #include "modelitem.h"
QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"}, QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
@ -9,8 +10,9 @@ QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
{AmountRole, "Amount"}, {AmountRole, "Amount"},
{FactorRole, "Factor"}}; {FactorRole, "Factor"}};
TableModel::TableModel(QObject* parent) TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
: QAbstractTableModel{parent} { : QAbstractTableModel{parent}
, m_undoStack(undoStack) {
for (int row = 0; row < 5; ++row) { for (int row = 0; row < 5; ++row) {
QHash<int, QVariant> values; QHash<int, QVariant> values;
values[NameRole] = QString("Item %1").arg(row); 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::DisplayRole:
case Qt::EditRole: case Qt::EditRole:
return m_items.at(row)->data(roleForColumn); 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(); return QVariant();
@ -122,14 +130,9 @@ void TableModel::insertItems(int startPosition,
} }
QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc); QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc);
const int nRows = valueList.size();
beginInsertRows(QModelIndex(), startPosition, startPosition + nRows - 1); InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList);
for (int row = 0; row < nRows; ++row) { m_undoStack->push(insertCommand);
const int rowPosition = startPosition + row;
shared_ptr<ModelItem> item = make_unique<ModelItem>(valueList.at(row));
m_items.insert(rowPosition, std::move(item));
}
endInsertRows();
} }
int TableModel::getRoleForColumn(const int column) const { int TableModel::getRoleForColumn(const int column) const {
@ -154,3 +157,24 @@ int TableModel::getRoleForColumn(const int column) const {
break; 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();
}

View File

@ -3,6 +3,7 @@
#include <QAbstractTableModel> #include <QAbstractTableModel>
class QUndoStack;
class ModelItem; class ModelItem;
using namespace std; using namespace std;
@ -10,11 +11,13 @@ using namespace std;
class TableModel : public QAbstractTableModel { class TableModel : public QAbstractTableModel {
Q_OBJECT Q_OBJECT
friend class InsertRowsCommand;
public: public:
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole }; enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
static QHash<int, QByteArray> ROLE_NAMES; static QHash<int, QByteArray> ROLE_NAMES;
explicit TableModel(QObject* parent = nullptr); explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
/// QAbstractItemModel interface /// QAbstractItemModel interface
Qt::ItemFlags flags(const QModelIndex& index) const override; Qt::ItemFlags flags(const QModelIndex& index) const override;
@ -36,10 +39,16 @@ class TableModel : public QAbstractTableModel {
void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex); void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex);
private: private:
/// members /// *** members ***
QList<shared_ptr<ModelItem>> m_items; QList<shared_ptr<ModelItem>> m_items;
QUndoStack* m_undoStack;
/// functions /// *** functions ***
/// undo/redo functions
void execInsertItems(const int firstRow, const QList<QHash<int, QVariant>> valueList);
void execRemoveItems(const int firstRow, const int nRows);
/// misc functions
int getRoleForColumn(const int column) const; int getRoleForColumn(const int column) const;
}; };