1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00

Cloning works for single record type tables. Well, kinda.

This commit is contained in:
Marek Kochanowicz 2014-01-20 13:59:00 +01:00
parent 0ea2bb7c4c
commit 33620a001b
15 changed files with 150 additions and 78 deletions

View File

@ -97,7 +97,12 @@ namespace CSMWorld
virtual void appendBlankRecord (const std::string& id, virtual void appendBlankRecord (const std::string& id,
UniversalId::Type type = UniversalId::Type_None); UniversalId::Type type = UniversalId::Type_None);
///< \param type Will be ignored, unless the collection supports multiple record types ///< \param type Will be ignored, unless the collection supports multiple record types
virtual void cloneRecord(const std::string& origin,
const std::string& destination,
const UniversalId::Type type,
const UniversalId::ArgumentType argumentType);
virtual int searchId (const std::string& id) const; virtual int searchId (const std::string& id) const;
////< Search record with \a id. ////< Search record with \a id.
/// \return index of record (if found) or -1 (not found) /// \return index of record (if found) or -1 (not found)
@ -193,6 +198,26 @@ namespace CSMWorld
return true; return true;
} }
template<typename ESXRecordT, typename IdAccessorT>
void Collection<ESXRecordT, IdAccessorT>::cloneRecord(const std::string& origin,
const std::string& destination,
const UniversalId::Type type,
const UniversalId::ArgumentType argumentType)
{
Record<ESXRecordT> copy = getRecord(origin);
if (copy.isDeleted())
{
return;
}
if (argumentType == UniversalId::ArgumentType_Id)
{
copy.get().mId = Misc::StringUtils::lowerCase(destination);
}
insertRecord(copy, getAppendIndex(destination, type));
}
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
Collection<ESXRecordT, IdAccessorT>::Collection() Collection<ESXRecordT, IdAccessorT>::Collection()
{} {}

View File

@ -74,6 +74,11 @@ namespace CSMWorld
UniversalId::Type type = UniversalId::Type_None) = 0; UniversalId::Type type = UniversalId::Type_None) = 0;
///< If the record type does not match, an exception is thrown. ///< If the record type does not match, an exception is thrown.
virtual void cloneRecord(const std::string& origin,
const std::string& destination,
const UniversalId::Type type,
const UniversalId::ArgumentType argumentType) = 0;
virtual const RecordBase& getRecord (const std::string& id) const = 0; virtual const RecordBase& getRecord (const std::string& id) const = 0;
virtual const RecordBase& getRecord (int index) const = 0; virtual const RecordBase& getRecord (int index) const = 0;

View File

@ -6,81 +6,88 @@
#include "idtable.hpp" #include "idtable.hpp"
#include "idtable.hpp" #include "idtable.hpp"
CSMWorld::ModifyCommand::ModifyCommand (QAbstractItemModel& model, const QModelIndex& index, CSMWorld::ModifyCommand::ModifyCommand(QAbstractItemModel& model, const QModelIndex& index,
const QVariant& new_, QUndoCommand *parent) const QVariant& new_, QUndoCommand* parent)
: QUndoCommand (parent), mModel (model), mIndex (index), mNew (new_) : QUndoCommand(parent), mModel(model), mIndex(index), mNew(new_)
{ {
mOld = mModel.data (mIndex, Qt::EditRole); mOld = mModel.data(mIndex, Qt::EditRole);
setText ("Modify " + mModel.headerData (mIndex.column(), Qt::Horizontal, Qt::DisplayRole).toString()); setText("Modify " + mModel.headerData(mIndex.column(), Qt::Horizontal, Qt::DisplayRole).toString());
} }
void CSMWorld::ModifyCommand::redo() void CSMWorld::ModifyCommand::redo()
{ {
mModel.setData (mIndex, mNew); mModel.setData(mIndex, mNew);
} }
void CSMWorld::ModifyCommand::undo() void CSMWorld::ModifyCommand::undo()
{ {
mModel.setData (mIndex, mOld); mModel.setData(mIndex, mOld);
} }
CSMWorld::CloneCommand::CloneCommand (CSMWorld::IdTable& model, CSMWorld::CloneCommand::CloneCommand(CSMWorld::IdTable& model,
const std::string& idOrigin, const std::string& idOrigin,
const std::string& IdDestination, const std::string& IdDestination,
CSMWorld::UniversalId::Type type, const CSMWorld::UniversalId::Type type,
QUndoCommand* parent) : const CSMWorld::UniversalId::ArgumentType argumentType,
QUndoCommand(parent), QUndoCommand* parent) :
mModel(model), QUndoCommand(parent),
mIdOrigin(idOrigin), mModel(model),
mIdOrigin(idOrigin),
mIdDestination(IdDestination), mIdDestination(IdDestination),
mArgumentType(argumentType),
mType(type) mType(type)
{ {
setText (("Clone record " + idOrigin + " to the " + IdDestination).c_str()); setText(("Clone record " + idOrigin + " to the " + IdDestination).c_str());
} }
void CSMWorld::CloneCommand::redo() void CSMWorld::CloneCommand::redo()
{ {
mModel.cloneRecord(mIdOrigin, mIdDestination, mType); mModel.cloneRecord(mIdOrigin, mIdDestination, mArgumentType, mType);
} }
void CSMWorld::CloneCommand::undo()
CSMWorld::CreateCommand::CreateCommand (IdTable& model, const std::string& id, QUndoCommand *parent)
: QUndoCommand (parent), mModel (model), mId (id), mType (UniversalId::Type_None)
{ {
setText (("Create record " + id).c_str()); mModel.removeRow(mModel.getModelIndex(mIdDestination, 0).row());
} }
void CSMWorld::CreateCommand::addValue (int column, const QVariant& value)
CSMWorld::CreateCommand::CreateCommand(IdTable& model, const std::string& id, QUndoCommand* parent)
: QUndoCommand(parent), mModel(model), mId(id), mType(UniversalId::Type_None)
{
setText(("Create record " + id).c_str());
}
void CSMWorld::CreateCommand::addValue(int column, const QVariant& value)
{ {
mValues[column] = value; mValues[column] = value;
} }
void CSMWorld::CreateCommand::setType (UniversalId::Type type) void CSMWorld::CreateCommand::setType(UniversalId::Type type)
{ {
mType = type; mType = type;
} }
void CSMWorld::CreateCommand::redo() void CSMWorld::CreateCommand::redo()
{ {
mModel.addRecord (mId, mType); mModel.addRecord(mId, mType);
for (std::map<int, QVariant>::const_iterator iter (mValues.begin()); iter!=mValues.end(); ++iter) for (std::map<int, QVariant>::const_iterator iter(mValues.begin()); iter != mValues.end(); ++iter)
mModel.setData (mModel.getModelIndex (mId, iter->first), iter->second); mModel.setData(mModel.getModelIndex(mId, iter->first), iter->second);
} }
void CSMWorld::CreateCommand::undo() void CSMWorld::CreateCommand::undo()
{ {
mModel.removeRow (mModel.getModelIndex (mId, 0).row()); mModel.removeRow(mModel.getModelIndex(mId, 0).row());
} }
CSMWorld::RevertCommand::RevertCommand (IdTable& model, const std::string& id, QUndoCommand *parent) CSMWorld::RevertCommand::RevertCommand(IdTable& model, const std::string& id, QUndoCommand* parent)
: QUndoCommand (parent), mModel (model), mId (id), mOld (0) : QUndoCommand(parent), mModel(model), mId(id), mOld(0)
{ {
setText (("Revert record " + id).c_str()); setText(("Revert record " + id).c_str());
mOld = model.getRecord (id).clone(); mOld = model.getRecord(id).clone();
} }
CSMWorld::RevertCommand::~RevertCommand() CSMWorld::RevertCommand::~RevertCommand()
@ -90,32 +97,32 @@ CSMWorld::RevertCommand::~RevertCommand()
void CSMWorld::RevertCommand::redo() void CSMWorld::RevertCommand::redo()
{ {
int column = mModel.findColumnIndex (Columns::ColumnId_Modification); int column = mModel.findColumnIndex(Columns::ColumnId_Modification);
QModelIndex index = mModel.getModelIndex (mId, column); QModelIndex index = mModel.getModelIndex(mId, column);
RecordBase::State state = static_cast<RecordBase::State> (mModel.data (index).toInt()); RecordBase::State state = static_cast<RecordBase::State>(mModel.data(index).toInt());
if (state==RecordBase::State_ModifiedOnly) if (state == RecordBase::State_ModifiedOnly)
{ {
mModel.removeRows (index.row(), 1); mModel.removeRows(index.row(), 1);
} }
else else
{ {
mModel.setData (index, static_cast<int> (RecordBase::State_BaseOnly)); mModel.setData(index, static_cast<int>(RecordBase::State_BaseOnly));
} }
} }
void CSMWorld::RevertCommand::undo() void CSMWorld::RevertCommand::undo()
{ {
mModel.setRecord (mId, *mOld); mModel.setRecord(mId, *mOld);
} }
CSMWorld::DeleteCommand::DeleteCommand (IdTable& model, const std::string& id, QUndoCommand *parent) CSMWorld::DeleteCommand::DeleteCommand(IdTable& model, const std::string& id, QUndoCommand* parent)
: QUndoCommand (parent), mModel (model), mId (id), mOld (0) : QUndoCommand(parent), mModel(model), mId(id), mOld(0)
{ {
setText (("Delete record " + id).c_str()); setText(("Delete record " + id).c_str());
mOld = model.getRecord (id).clone(); mOld = model.getRecord(id).clone();
} }
CSMWorld::DeleteCommand::~DeleteCommand() CSMWorld::DeleteCommand::~DeleteCommand()
@ -125,44 +132,45 @@ CSMWorld::DeleteCommand::~DeleteCommand()
void CSMWorld::DeleteCommand::redo() void CSMWorld::DeleteCommand::redo()
{ {
int column = mModel.findColumnIndex (Columns::ColumnId_Modification); int column = mModel.findColumnIndex(Columns::ColumnId_Modification);
QModelIndex index = mModel.getModelIndex (mId, column); QModelIndex index = mModel.getModelIndex(mId, column);
RecordBase::State state = static_cast<RecordBase::State> (mModel.data (index).toInt()); RecordBase::State state = static_cast<RecordBase::State>(mModel.data(index).toInt());
if (state==RecordBase::State_ModifiedOnly) if (state == RecordBase::State_ModifiedOnly)
{ {
mModel.removeRows (index.row(), 1); mModel.removeRows(index.row(), 1);
} }
else else
{ {
mModel.setData (index, static_cast<int> (RecordBase::State_Deleted)); mModel.setData(index, static_cast<int>(RecordBase::State_Deleted));
} }
} }
void CSMWorld::DeleteCommand::undo() void CSMWorld::DeleteCommand::undo()
{ {
mModel.setRecord (mId, *mOld); mModel.setRecord(mId, *mOld);
} }
CSMWorld::ReorderRowsCommand::ReorderRowsCommand (IdTable& model, int baseIndex, CSMWorld::ReorderRowsCommand::ReorderRowsCommand(IdTable& model, int baseIndex,
const std::vector<int>& newOrder) const std::vector<int>& newOrder)
: mModel (model), mBaseIndex (baseIndex), mNewOrder (newOrder) : mModel(model), mBaseIndex(baseIndex), mNewOrder(newOrder)
{} {}
void CSMWorld::ReorderRowsCommand::redo() void CSMWorld::ReorderRowsCommand::redo()
{ {
mModel.reorderRows (mBaseIndex, mNewOrder); mModel.reorderRows(mBaseIndex, mNewOrder);
} }
void CSMWorld::ReorderRowsCommand::undo() void CSMWorld::ReorderRowsCommand::undo()
{ {
int size = static_cast<int> (mNewOrder.size()); int size = static_cast<int>(mNewOrder.size());
std::vector<int> reverse (size); std::vector<int> reverse(size);
for (int i=0; i<size; ++i) for (int i = 0; i < size; ++i)
reverse.at (mNewOrder[i]) = i; reverse.at(mNewOrder[i]) = i;
mModel.reorderRows (mBaseIndex, reverse); mModel.reorderRows(mBaseIndex, reverse);
} }
// kate: indent-mode cstyle; indent-width 4; replace-tabs on;

View File

@ -45,18 +45,20 @@ namespace CSMWorld
std::string mIdOrigin; std::string mIdOrigin;
std::string mIdDestination; std::string mIdDestination;
UniversalId::Type mType; UniversalId::Type mType;
UniversalId::ArgumentType mArgumentType;
std::map<int, QVariant> mValues; std::map<int, QVariant> mValues;
public: public:
CloneCommand (IdTable& model, const std::string& idOrigin, CloneCommand (IdTable& model, const std::string& idOrigin,
const std::string& IdDestination, const std::string& IdDestination,
UniversalId::Type type, const UniversalId::Type type,
const UniversalId::ArgumentType argumentType,
QUndoCommand* parent = 0); QUndoCommand* parent = 0);
virtual void redo(); virtual void redo();
// virtual void undo(); virtual void undo();
}; };
class CreateCommand : public QUndoCommand class CreateCommand : public QUndoCommand

View File

@ -124,11 +124,12 @@ void CSMWorld::IdTable::addRecord (const std::string& id, UniversalId::Type type
endInsertRows(); endInsertRows();
} }
void CSMWorld::IdTable::cloneRecord(const std::string& origin, void CSMWorld::IdTable::cloneRecord(const std::string& origin,
const std::string& destination, const std::string& destination,
CSMWorld::UniversalId::ArgumentType argumentType,
CSMWorld::UniversalId::Type type) CSMWorld::UniversalId::Type type)
{ {
mIdCollection->cloneRecord(origin, destination, type, argumentType);
} }

View File

@ -63,7 +63,10 @@ namespace CSMWorld
void addRecord (const std::string& id, UniversalId::Type type = UniversalId::Type_None); void addRecord (const std::string& id, UniversalId::Type type = UniversalId::Type_None);
///< \param type Will be ignored, unless the collection supports multiple record types ///< \param type Will be ignored, unless the collection supports multiple record types
void cloneRecord(const std::string& origin, const std::string& destination, UniversalId::Type type = UniversalId::Type_None); void cloneRecord(const std::string& origin,
const std::string& destination,
UniversalId::ArgumentType argumentType,
UniversalId::Type type = UniversalId::Type_None);
QModelIndex getModelIndex (const std::string& id, int column) const; QModelIndex getModelIndex (const std::string& id, int column) const;

View File

@ -449,6 +449,14 @@ void CSMWorld::RefIdCollection::replace (int index, const RecordBase& record)
mData.getRecord (mData.globalToLocalIndex (index)).assign (record); mData.getRecord (mData.globalToLocalIndex (index)).assign (record);
} }
void CSMWorld::RefIdCollection::cloneRecord(const std::string& origin,
const std::string& destination,
const CSMWorld::UniversalId::Type type,
const CSMWorld::UniversalId::ArgumentType argumentType)
{
}
void CSMWorld::RefIdCollection::appendRecord (const RecordBase& record, void CSMWorld::RefIdCollection::appendRecord (const RecordBase& record,
UniversalId::Type type) UniversalId::Type type)
{ {

View File

@ -69,6 +69,11 @@ namespace CSMWorld
virtual void removeRows (int index, int count); virtual void removeRows (int index, int count);
virtual void cloneRecord(const std::string& origin,
const std::string& destination,
const UniversalId::Type type,
const UniversalId::ArgumentType argumentType);
virtual void appendBlankRecord (const std::string& id, UniversalId::Type type); virtual void appendBlankRecord (const std::string& id, UniversalId::Type type);
///< \param type Will be ignored, unless the collection supports multiple record types ///< \param type Will be ignored, unless the collection supports multiple record types

View File

@ -25,7 +25,7 @@ namespace CSVWorld
virtual void reset() = 0; virtual void reset() = 0;
virtual void cloneMode(const std::string& originid, const CSMWorld::UniversalId::Type type) = 0; virtual void cloneMode(const std::string& originid, const CSMWorld::UniversalId::Type type, const CSMWorld::UniversalId::ArgumentType argumentType) = 0;
virtual void setEditLock (bool locked) = 0; virtual void setEditLock (bool locked) = 0;

View File

@ -59,7 +59,8 @@ const CSMWorld::UniversalId& CSVWorld::GenericCreator::getCollectionId() const
CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack,
const CSMWorld::UniversalId& id, bool relaxedIdRules) const CSMWorld::UniversalId& id, bool relaxedIdRules)
: mData (data), mUndoStack (undoStack), mListId (id), mLocked (false), mCloneMode(false), mClonedType(CSMWorld::UniversalId::Type_None) : mData (data), mUndoStack (undoStack), mListId (id), mLocked (false), mCloneMode(false), mClonedType(CSMWorld::UniversalId::Type_None),
mArgumentType(CSMWorld::UniversalId::ArgumentType_None)
{ {
mLayout = new QHBoxLayout; mLayout = new QHBoxLayout;
mLayout->setContentsMargins (0, 0, 0, 0); mLayout->setContentsMargins (0, 0, 0, 0);
@ -126,7 +127,12 @@ void CSVWorld::GenericCreator::create()
{ {
std::string id = getId(); std::string id = getId();
std::auto_ptr<CSMWorld::CloneCommand> command (new CSMWorld::CloneCommand ( std::auto_ptr<CSMWorld::CloneCommand> command (new CSMWorld::CloneCommand (
dynamic_cast<CSMWorld::IdTable&> (*mData.getTableModel(mListId)), mClonedId, id, mClonedType)); dynamic_cast<CSMWorld::IdTable&> (*mData.getTableModel(mListId)), mClonedId, id, mClonedType, mArgumentType));
mUndoStack.push(command.release());
emit done();
emit requestFocus(id);
} else { } else {
std::string id = getId(); std::string id = getId();
@ -143,11 +149,14 @@ void CSVWorld::GenericCreator::create()
} }
} }
void CSVWorld::GenericCreator::cloneMode(const std::string& originid, const CSMWorld::UniversalId::Type type) void CSVWorld::GenericCreator::cloneMode(const std::string& originid,
const CSMWorld::UniversalId::Type type,
const CSMWorld::UniversalId::ArgumentType argumentType)
{ {
mCloneMode = true; mCloneMode = true;
mClonedId = originid; mClonedId = originid;
mClonedType = type; mClonedType = type;
mArgumentType = argumentType;
mId->setText(QString::fromStdString(mClonedId)); mId->setText(QString::fromStdString(mClonedId));
} }

View File

@ -32,6 +32,7 @@ namespace CSVWorld
bool mCloneMode; bool mCloneMode;
std::string mClonedId; std::string mClonedId;
CSMWorld::UniversalId::Type mClonedType; CSMWorld::UniversalId::Type mClonedType;
CSMWorld::UniversalId::ArgumentType mArgumentType;
protected: protected:
@ -61,13 +62,14 @@ namespace CSVWorld
virtual void reset(); virtual void reset();
virtual void cloneMode(const std::string& originid, const CSMWorld::UniversalId::Type type); virtual void cloneMode(const std::string& originid,
const CSMWorld::UniversalId::Type type,
const CSMWorld::UniversalId::ArgumentType argumentType);
virtual std::string getErrors() const; virtual std::string getErrors() const;
///< Return formatted error descriptions for the current state of the creator. if an empty ///< Return formatted error descriptions for the current state of the creator. if an empty
/// string is returned, there is no error. /// string is returned, there is no error.
private slots: private slots:
void textChanged (const QString& text); void textChanged (const QString& text);

View File

@ -158,10 +158,12 @@ void CSVWorld::TableBottomBox::createRequest()
mCreating = true; mCreating = true;
} }
void CSVWorld::TableBottomBox::cloneRequest(const std::string& id, const CSMWorld::UniversalId::Type type) void CSVWorld::TableBottomBox::cloneRequest(const std::string& id,
const CSMWorld::UniversalId::Type type,
const CSMWorld::UniversalId::ArgumentType argumnetType)
{ {
mCreator->reset(); mCreator->reset();
mCreator->cloneMode(id, type); mCreator->cloneMode(id, type, argumnetType);
mLayout->setCurrentWidget(mCreator); mLayout->setCurrentWidget(mCreator);
setVisible (true); setVisible (true);
mCreating = true; mCreating = true;

View File

@ -77,7 +77,7 @@ namespace CSVWorld
/// \param modified Number of added and modified records /// \param modified Number of added and modified records
void createRequest(); void createRequest();
void cloneRequest(const std::string& id, const CSMWorld::UniversalId::Type type); void cloneRequest(const std::string& id, const CSMWorld::UniversalId::Type type, const CSMWorld::UniversalId::ArgumentType argumentType);
}; };
} }

View File

@ -50,8 +50,8 @@ CSVWorld::TableSubView::TableSubView (const CSMWorld::UniversalId& id, CSMDoc::D
connect (mTable, SIGNAL (createRequest()), mBottom, SLOT (createRequest())); connect (mTable, SIGNAL (createRequest()), mBottom, SLOT (createRequest()));
connect (mTable, SIGNAL (cloneRequest(int)), this, SLOT(cloneRequest(int))); connect (mTable, SIGNAL (cloneRequest(int)), this, SLOT(cloneRequest(int)));
connect (this, SIGNAL(cloneRequest(const std::string&, const CSMWorld::UniversalId::Type)), connect (this, SIGNAL(cloneRequest(const std::string&, const CSMWorld::UniversalId::Type, const CSMWorld::UniversalId::ArgumentType)),
mBottom, SLOT(cloneRequest(const std::string&, const CSMWorld::UniversalId::Type))); mBottom, SLOT(cloneRequest(const std::string&, const CSMWorld::UniversalId::Type, const CSMWorld::UniversalId::ArgumentType)));
} }
connect (mBottom, SIGNAL (requestFocus (const std::string&)), connect (mBottom, SIGNAL (requestFocus (const std::string&)),
mTable, SLOT (requestFocus (const std::string&))); mTable, SLOT (requestFocus (const std::string&)));
@ -85,5 +85,5 @@ void CSVWorld::TableSubView::setStatusBar (bool show)
void CSVWorld::TableSubView::cloneRequest(int row) void CSVWorld::TableSubView::cloneRequest(int row)
{ {
const CSMWorld::UniversalId& toClone(mTable->getUniversalId(row)); const CSMWorld::UniversalId& toClone(mTable->getUniversalId(row));
emit cloneRequest(toClone.getId(), toClone.getType()); emit cloneRequest(toClone.getId(), toClone.getType(), toClone.getArgumentType());
} }

View File

@ -35,7 +35,9 @@ namespace CSVWorld
virtual void setStatusBar (bool show); virtual void setStatusBar (bool show);
signals: signals:
void cloneRequest(const std::string& id, const CSMWorld::UniversalId::Type type); void cloneRequest(const std::string& id,
const CSMWorld::UniversalId::Type type,
const CSMWorld::UniversalId::ArgumentType argumentType);
private slots: private slots: