Reduced code duplication through new common base class

This commit is contained in:
Marek Kochanowicz 2014-07-21 09:52:09 +02:00
parent 87eed066c2
commit 3dd2ca15da
2 changed files with 29 additions and 20 deletions

View file

@ -179,7 +179,7 @@ CSMWorld::DeleteNestedCommand::DeleteNestedCommand (IdTable& model, const std::s
mParentColumn(parentColumn),
QUndoCommand(parent),
mNestedRow(nestedRow),
mOld (model.nestedTable(model.getModelIndex(id, parentColumn)))
NestedTableStoring(model, id, parentColumn)
{
setText (("Delete nested row in " + mId).c_str());
}
@ -196,12 +196,7 @@ void CSMWorld::DeleteNestedCommand::undo()
{
const QModelIndex& parentIndex = mModel.getModelIndex(mId, mParentColumn);
mModel.setNestedTable(parentIndex, *mOld);
}
CSMWorld::DeleteNestedCommand::~DeleteNestedCommand()
{
delete mOld;
mModel.setNestedTable(parentIndex, getOld());
}
CSMWorld::AddNestedCommand::AddNestedCommand(IdTable& model, const std::string& id, int nestedRow, int parentColumn, QUndoCommand* parent)
@ -210,7 +205,7 @@ CSMWorld::AddNestedCommand::AddNestedCommand(IdTable& model, const std::string&
mNewRow(nestedRow),
mParentColumn(parentColumn),
QUndoCommand(parent),
mOld (model.nestedTable(model.getModelIndex(id, parentColumn)))
NestedTableStoring(model, id, parentColumn)
{
setText (("Added nested row in " + mId).c_str());
}
@ -226,10 +221,18 @@ void CSMWorld::AddNestedCommand::undo()
{
const QModelIndex& parentIndex = mModel.getModelIndex(mId, mParentColumn);
mModel.setNestedTable(parentIndex, *mOld);
mModel.setNestedTable(parentIndex, getOld());
}
CSMWorld::AddNestedCommand::~AddNestedCommand()
CSMWorld::NestedTableStoring::NestedTableStoring(const IdTable& model, const std::string& id, int parentColumn)
: mOld(model.nestedTable(model.getModelIndex(id, parentColumn))) {}
CSMWorld::NestedTableStoring::~NestedTableStoring()
{
delete mOld;
}
const CSMWorld::NestedTableWrapperBase& CSMWorld::NestedTableStoring::getOld() const
{
return *mOld;
}

View file

@ -139,14 +139,26 @@ namespace CSMWorld
virtual void undo();
};
class DeleteNestedCommand : public QUndoCommand
class NestedTableStoring
{
NestedTableWrapperBase* mOld;
public:
NestedTableStoring(const IdTable& model, const std::string& id, int parentColumn);
~NestedTableStoring();
protected:
const NestedTableWrapperBase& getOld() const;
};
class DeleteNestedCommand : public QUndoCommand, private NestedTableStoring
{
IdTable& mModel;
std::string mId;
NestedTableWrapperBase* mOld;
int mParentColumn;
int mNestedRow;
@ -155,21 +167,17 @@ namespace CSMWorld
DeleteNestedCommand (IdTable& model, const std::string& id, int nestedRow, int parentColumn, QUndoCommand* parent = 0);
~DeleteNestedCommand();
virtual void redo();
virtual void undo();
};
class AddNestedCommand : public QUndoCommand
class AddNestedCommand : public QUndoCommand, private NestedTableStoring
{
IdTable& mModel;
std::string mId;
NestedTableWrapperBase* mOld;
int mNewRow;
int mParentColumn;
@ -177,8 +185,6 @@ namespace CSMWorld
public:
AddNestedCommand(IdTable& model, const std::string& id, int nestedRow, int parentColumn, QUndoCommand* parent = 0);
~AddNestedCommand();
virtual void redo();