Files
GenericQtClientCore/model/commands/removerowscommand.cpp

49 lines
1.5 KiB
C++

#include "removerowscommand.h"
#include <QDebug>
#include "../metadata.h"
#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[NameRole] = m_tableModel->data(index, NameRole);
values[DescriptionRole] = m_tableModel->data(index, DescriptionRole);
values[InfoRole] = m_tableModel->data(index, InfoRole);
values[AmountRole] = m_tableModel->data(index, AmountRole);
values[FactorRole] = m_tableModel->data(index, 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());
}
}