From 9a567b3712c9a9575f03266884cbeece5c2764b5 Mon Sep 17 00:00:00 2001 From: Aesylwinn Date: Wed, 25 May 2016 22:52:41 -0400 Subject: [PATCH] Revert "Fix editor undo for nested data." This reverts commit 774e1497b6b058cc103af6426b883d94860e48e0. --- apps/opencs/model/world/commands.cpp | 96 +++++++++++----------------- apps/opencs/model/world/commands.hpp | 15 +++-- apps/opencs/view/render/pathgrid.cpp | 28 +++----- 3 files changed, 56 insertions(+), 83 deletions(-) diff --git a/apps/opencs/model/world/commands.cpp b/apps/opencs/model/world/commands.cpp index 308b5386e..b48d6eba2 100644 --- a/apps/opencs/model/world/commands.cpp +++ b/apps/opencs/model/world/commands.cpp @@ -17,13 +17,7 @@ CSMWorld::ModifyCommand::ModifyCommand (QAbstractItemModel& model, const QModelIndex& index, const QVariant& new_, QUndoCommand* parent) - : QUndoCommand (parent) - , mModel (&model) - , mIndex (index) - , mNew (new_) - , mHasRecordState(false) - , mOldRecordState(CSMWorld::RecordBase::State_BaseOnly) - , mModifyNestedCommand(0) + : QUndoCommand (parent), mModel (&model), mIndex (index), mNew (new_), mHasRecordState(false), mOldRecordState(CSMWorld::RecordBase::State_BaseOnly) { if (QAbstractProxyModel *proxy = dynamic_cast (&model)) { @@ -34,57 +28,40 @@ CSMWorld::ModifyCommand::ModifyCommand (QAbstractItemModel& model, const QModelI if (mIndex.parent().isValid()) { - IdTree& tree = static_cast(*mModel); - - mModifyNestedCommand = new ModifyNestedCommand(tree, mIndex, new_, this); - setText(mModifyNestedCommand->text()); + setText ("Modify " + dynamic_cast(mModel)->nestedHeaderData ( + mIndex.parent().column(), mIndex.column(), Qt::Horizontal, Qt::DisplayRole).toString()); } else { setText ("Modify " + mModel->headerData (mIndex.column(), Qt::Horizontal, Qt::DisplayRole).toString()); + } - // Remember record state before the modification - if (CSMWorld::IdTable *table = dynamic_cast(mModel)) - { - mHasRecordState = true; - int stateColumnIndex = table->findColumnIndex(Columns::ColumnId_Modification); - - int rowIndex = mIndex.row(); - if (mIndex.parent().isValid()) - { - rowIndex = mIndex.parent().row(); - } + // Remember record state before the modification + if (CSMWorld::IdTable *table = dynamic_cast(mModel)) + { + mHasRecordState = true; + int stateColumnIndex = table->findColumnIndex(Columns::ColumnId_Modification); - mRecordStateIndex = table->index(rowIndex, stateColumnIndex); - mOldRecordState = static_cast(table->data(mRecordStateIndex).toInt()); + int rowIndex = mIndex.row(); + if (mIndex.parent().isValid()) + { + rowIndex = mIndex.parent().row(); } + + mRecordStateIndex = table->index(rowIndex, stateColumnIndex); + mOldRecordState = static_cast(table->data(mRecordStateIndex).toInt()); } } void CSMWorld::ModifyCommand::redo() { - if (mModifyNestedCommand) - { - mModifyNestedCommand->redo(); - } - else - { - mOld = mModel->data (mIndex, Qt::EditRole); - mModel->setData (mIndex, mNew); - } + mOld = mModel->data (mIndex, Qt::EditRole); + mModel->setData (mIndex, mNew); } void CSMWorld::ModifyCommand::undo() { - if (mModifyNestedCommand) - { - mModifyNestedCommand->undo(); - } - else - { - mModel->setData (mIndex, mOld); - } - + mModel->setData (mIndex, mOld); if (mHasRecordState) { mModel->setData(mRecordStateIndex, mOldRecordState); @@ -324,29 +301,37 @@ void CSMWorld::UpdateCellCommand::undo() mModel.setData (mIndex, mOld); } -CSMWorld::ModifyNestedCommand::ModifyNestedCommand (IdTree& model, const QModelIndex& index, const QVariant& new_, - QUndoCommand* parent) +CSMWorld::ModifyNestedCommand::ModifyNestedCommand (IdTree& model, const std::string& id, int nestedRow, + int nestedColumn, int parentColumn, const QVariant& new_, QUndoCommand* parent) : QUndoCommand(parent) - , NestedTableStoring(model, index.parent()) + , NestedTableStoring(model, id, parentColumn) , mModel(model) - , mIndex(index) + , mId(id) + , mNestedRow(nestedRow) + , mNestedColumn(nestedColumn) + , mParentColumn(parentColumn) , mNew(new_) { - setText("Modify " + model.nestedHeaderData(mIndex.parent().column(), mIndex.column(), Qt::Horizontal, - Qt::DisplayRole).toString()); + std::string title = model.headerData(parentColumn, Qt::Horizontal, Qt::DisplayRole).toString().toUtf8().constData(); + setText (("Modify " + title + " sub-table of " + mId).c_str()); - mModifyParentCommand = new ModifyCommand(mModel, mIndex.parent(), mIndex.parent().data(Qt::EditRole), this); + QModelIndex parentIndex = mModel.getModelIndex(mId, mParentColumn); + mModifyParentCommand = new ModifyCommand(mModel, parentIndex, parentIndex.data(Qt::EditRole), this); } void CSMWorld::ModifyNestedCommand::redo() { - mModel.setData(mIndex, mNew); + QModelIndex parentIndex = mModel.getModelIndex(mId, mParentColumn); + QModelIndex nestedIndex = mModel.index(mNestedRow, mNestedColumn, parentIndex); + mModel.setData(nestedIndex, mNew); mModifyParentCommand->redo(); } + void CSMWorld::ModifyNestedCommand::undo() { - mModel.setNestedTable(mIndex.parent(), getOld()); + QModelIndex parentIndex = mModel.getModelIndex(mId, mParentColumn); + mModel.setNestedTable(parentIndex, getOld()); mModifyParentCommand->undo(); } @@ -417,14 +402,7 @@ void CSMWorld::AddNestedCommand::undo() } CSMWorld::NestedTableStoring::NestedTableStoring(const IdTree& model, const std::string& id, int parentColumn) - : mOld(model.nestedTable(model.getModelIndex(id, parentColumn))) -{ -} - -CSMWorld::NestedTableStoring::NestedTableStoring(const IdTree& model, const QModelIndex& parentIndex) - : mOld(model.nestedTable(parentIndex)) -{ -} + : mOld(model.nestedTable(model.getModelIndex(id, parentColumn))) {} CSMWorld::NestedTableStoring::~NestedTableStoring() { diff --git a/apps/opencs/model/world/commands.hpp b/apps/opencs/model/world/commands.hpp index d0aed97c7..b51720df8 100644 --- a/apps/opencs/model/world/commands.hpp +++ b/apps/opencs/model/world/commands.hpp @@ -23,7 +23,6 @@ namespace CSMWorld class IdTree; struct RecordBase; struct NestedTableWrapperBase; - class ModifyNestedCommand; class ModifyCommand : public QUndoCommand { @@ -36,8 +35,6 @@ namespace CSMWorld QModelIndex mRecordStateIndex; CSMWorld::RecordBase::State mOldRecordState; - ModifyNestedCommand* mModifyNestedCommand; - public: ModifyCommand (QAbstractItemModel& model, const QModelIndex& index, const QVariant& new_, @@ -194,7 +191,6 @@ namespace CSMWorld public: NestedTableStoring(const IdTree& model, const std::string& id, int parentColumn); - NestedTableStoring(const IdTree& model, const QModelIndex& parentIndex); ~NestedTableStoring(); @@ -207,7 +203,13 @@ namespace CSMWorld { IdTree& mModel; - QModelIndex mIndex; + std::string mId; + + int mNestedRow; + + int mNestedColumn; + + int mParentColumn; QVariant mNew; @@ -216,7 +218,8 @@ namespace CSMWorld public: - ModifyNestedCommand (IdTree& model, const QModelIndex& index, const QVariant& new_, QUndoCommand* parent = 0); + ModifyNestedCommand (IdTree& model, const std::string& id, int nestedRow, int nestedColumn, + int parentColumn, const QVariant& new_, QUndoCommand* parent = 0); virtual void redo(); diff --git a/apps/opencs/view/render/pathgrid.cpp b/apps/opencs/view/render/pathgrid.cpp index b3b582b7a..939558be9 100644 --- a/apps/opencs/view/render/pathgrid.cpp +++ b/apps/opencs/view/render/pathgrid.cpp @@ -254,7 +254,6 @@ namespace CSVRender int posY = clampToCell(static_cast(localCoords.y())); int posZ = clampToCell(static_cast(localCoords.z())); - int recordIndex = mPathgridCollection.getIndex (mId); int parentColumn = mPathgridCollection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridPoints); int posXColumn = mPathgridCollection.searchNestedColumnIndex(parentColumn, @@ -266,14 +265,13 @@ namespace CSVRender int posZColumn = mPathgridCollection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosZ); - QModelIndex parent = model->index(recordIndex, parentColumn); int row = static_cast(source->mPoints.size()); // Add node commands.push(new CSMWorld::AddNestedCommand(*model, mId, row, parentColumn)); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(row, posXColumn, parent), posX)); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(row, posYColumn, parent), posY)); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(row, posZColumn, parent), posZ)); + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, row, posXColumn, parentColumn, posX)); + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, row, posYColumn, parentColumn, posY)); + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, row, posZColumn, parentColumn, posZ)); } else { @@ -296,7 +294,6 @@ namespace CSVRender CSMWorld::IdTree* model = dynamic_cast(mData.getTableModel( CSMWorld::UniversalId::Type_Pathgrids)); - int recordIndex = mPathgridCollection.getIndex(mId); int parentColumn = mPathgridCollection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridPoints); int posXColumn = mPathgridCollection.searchNestedColumnIndex(parentColumn, @@ -308,20 +305,18 @@ namespace CSVRender int posZColumn = mPathgridCollection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosZ); - QModelIndex parent = model->index(recordIndex, parentColumn); - for (size_t i = 0; i < mSelected.size(); ++i) { const CSMWorld::Pathgrid::Point& point = source->mPoints[mSelected[i]]; int row = static_cast(mSelected[i]); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(row, posXColumn, parent), + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, row, posXColumn, parentColumn, clampToCell(point.mX + offsetX))); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(row, posYColumn, parent), + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, row, posYColumn, parentColumn, clampToCell(point.mY + offsetY))); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(row, posZColumn, parent), + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, row, posZColumn, parentColumn, clampToCell(point.mZ + offsetZ))); } } @@ -565,7 +560,6 @@ namespace CSVRender CSMWorld::IdTree* model = dynamic_cast(mData.getTableModel( CSMWorld::UniversalId::Type_Pathgrids)); - int recordIndex = mPathgridCollection.getIndex(mId); int parentColumn = mPathgridCollection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridEdges); int edge0Column = mPathgridCollection.searchNestedColumnIndex(parentColumn, @@ -574,21 +568,19 @@ namespace CSVRender int edge1Column = mPathgridCollection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridEdge1); - QModelIndex parent = model->index(recordIndex, parentColumn); - if (edgeExists(source, node1, node2) == -1) { // Set first edge last since that reorders the edge list commands.push(new CSMWorld::AddNestedCommand(*model, mId, 0, parentColumn)); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(0, edge1Column, parent), node2)); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(0, edge0Column, parent), node1)); + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, 0, edge1Column, parentColumn, node2)); + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, 0, edge0Column, parentColumn, node1)); } if (edgeExists(source, node2, node1) == -1) { commands.push(new CSMWorld::AddNestedCommand(*model, mId, 0, parentColumn)); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(0, edge1Column, parent), node1)); - commands.push(new CSMWorld::ModifyNestedCommand(*model, model->index(0, edge0Column, parent), node2)); + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, 0, edge1Column, parentColumn, node1)); + commands.push(new CSMWorld::ModifyNestedCommand(*model, mId, 0, edge0Column, parentColumn, node2)); } }