Deleting items from the model can now be made undone/redone.
This commit is contained in:
@ -26,6 +26,7 @@ add_library(${TARGET_APP} STATIC
|
||||
model/modelitem.h model/modelitem.cpp
|
||||
formats/jsonparser.h formats/jsonparser.cpp
|
||||
model/commands/insertrowscommand.h model/commands/insertrowscommand.cpp
|
||||
model/commands/removerowscommand.h model/commands/removerowscommand.cpp
|
||||
)
|
||||
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
47
model/commands/removerowscommand.cpp
Normal file
47
model/commands/removerowscommand.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "removerowscommand.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "../tablemodel.h"
|
||||
|
||||
RemoveRowsCommand::RemoveRowsCommand(TableModel* model,
|
||||
const int startRow,
|
||||
const int nRows,
|
||||
QUndoCommand* parent)
|
||||
: QUndoCommand(parent)
|
||||
, m_tableModel(model)
|
||||
, m_startRow(startRow) {
|
||||
qInfo() << "New RemoveCommand...";
|
||||
const QString commandText =
|
||||
QString("removing %1 item(s) on position %2").arg(nRows).arg(startRow);
|
||||
setText(commandText);
|
||||
|
||||
for (int row = 0; row < nRows; ++row) {
|
||||
const int rowPosition = startRow + row;
|
||||
QModelIndex index = m_tableModel->index(rowPosition, 0);
|
||||
|
||||
// TODO use a (static) function "getRoleValueHash" or something
|
||||
QHash<int, QVariant> values;
|
||||
values[TableModel::NameRole] = m_tableModel->data(index, TableModel::NameRole);
|
||||
values[TableModel::DescriptionRole] = m_tableModel->data(index, TableModel::DescriptionRole);
|
||||
values[TableModel::InfoRole] = m_tableModel->data(index, TableModel::InfoRole);
|
||||
values[TableModel::AmountRole] = m_tableModel->data(index, TableModel::AmountRole);
|
||||
values[TableModel::FactorRole] = m_tableModel->data(index, TableModel::FactorRole);
|
||||
|
||||
m_valueList.append(values);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveRowsCommand::undo() {
|
||||
qDebug() << "Undoing the RemoveCommand...";
|
||||
if (m_tableModel) {
|
||||
m_tableModel->execInsertItems(m_startRow, m_valueList);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveRowsCommand::redo() {
|
||||
qDebug() << "(Re-)doing the RemoveCommand...";
|
||||
if (m_tableModel) {
|
||||
m_tableModel->execRemoveItems(m_startRow, m_valueList.length());
|
||||
}
|
||||
}
|
||||
28
model/commands/removerowscommand.h
Normal file
28
model/commands/removerowscommand.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef REMOVEROWSCOMMAND_H
|
||||
#define REMOVEROWSCOMMAND_H
|
||||
|
||||
#include <QUndoCommand>
|
||||
|
||||
class TableModel;
|
||||
|
||||
class RemoveRowsCommand : 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
|
||||
RemoveRowsCommand(TableModel* model,
|
||||
const int startRow,
|
||||
const int nRows,
|
||||
QUndoCommand* parent = nullptr);
|
||||
|
||||
/// QUndoCommand interface
|
||||
void undo() override;
|
||||
void redo() override;
|
||||
|
||||
private:
|
||||
TableModel* m_tableModel;
|
||||
const int m_startRow;
|
||||
QList<QHash<int, QVariant>> m_valueList;
|
||||
};
|
||||
|
||||
#endif // REMOVEROWSCOMMAND_H
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include "../formats/jsonparser.h"
|
||||
#include "commands/insertrowscommand.h"
|
||||
#include "commands/removerowscommand.h"
|
||||
#include "modelitem.h"
|
||||
|
||||
QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
||||
@ -108,9 +109,8 @@ bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIn
|
||||
return false;
|
||||
}
|
||||
|
||||
beginRemoveRows(QModelIndex(), firstRow, lastRow);
|
||||
m_items.remove(firstRow, nRows);
|
||||
endRemoveRows();
|
||||
RemoveRowsCommand* removeCommand = new RemoveRowsCommand(this, firstRow, nRows);
|
||||
m_undoStack->push(removeCommand);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ class TableModel : public QAbstractTableModel {
|
||||
Q_OBJECT
|
||||
|
||||
friend class InsertRowsCommand;
|
||||
friend class RemoveRowsCommand;
|
||||
|
||||
public:
|
||||
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
||||
|
||||
Reference in New Issue
Block a user