1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-10 15:39:02 +00:00

Merge remote-tracking branch 'smbas/feature-cloning-enhancements'

This commit is contained in:
Marc Zinnschlag 2015-06-30 10:40:28 +02:00
commit d3bcf0711c
11 changed files with 122 additions and 7 deletions

View File

@ -58,6 +58,25 @@ void CSMWorld::CreateCommand::applyModifications()
{ {
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);
if (!mNestedValues.empty())
{
CSMWorld::IdTree *tree = dynamic_cast<CSMWorld::IdTree *>(&mModel);
if (tree == NULL)
{
throw std::logic_error("CSMWorld::CreateCommand: Attempt to add nested values to the non-nested model");
}
std::map<int, std::pair<int, QVariant> >::const_iterator current = mNestedValues.begin();
std::map<int, std::pair<int, QVariant> >::const_iterator end = mNestedValues.end();
for (; current != end; ++current)
{
QModelIndex index = tree->index(0,
current->second.first,
tree->getNestedModelIndex(mId, current->first));
tree->setData(index, current->second.second);
}
}
} }
CSMWorld::CreateCommand::CreateCommand (IdTable& model, const std::string& id, QUndoCommand* parent) CSMWorld::CreateCommand::CreateCommand (IdTable& model, const std::string& id, QUndoCommand* parent)
@ -71,6 +90,11 @@ void CSMWorld::CreateCommand::addValue (int column, const QVariant& value)
mValues[column] = value; mValues[column] = value;
} }
void CSMWorld::CreateCommand::addNestedValue(int parentColumn, int nestedColumn, const QVariant &value)
{
mNestedValues[parentColumn] = std::make_pair(nestedColumn, value);
}
void CSMWorld::CreateCommand::setType (UniversalId::Type type) void CSMWorld::CreateCommand::setType (UniversalId::Type type)
{ {
mType = type; mType = type;

View File

@ -48,6 +48,9 @@ namespace CSMWorld
class CreateCommand : public QUndoCommand class CreateCommand : public QUndoCommand
{ {
std::map<int, QVariant> mValues; std::map<int, QVariant> mValues;
std::map<int, std::pair<int, QVariant> > mNestedValues;
///< Parameter order: a parent column, a nested column, a data.
///< A nested row has index of 0.
protected: protected:
@ -68,6 +71,8 @@ namespace CSMWorld
void addValue (int column, const QVariant& value); void addValue (int column, const QVariant& value);
void addNestedValue(int parentColumn, int nestedColumn, const QVariant &value);
virtual void redo(); virtual void redo();
virtual void undo(); virtual void undo();

View File

@ -261,3 +261,13 @@ CSMWorld::NestedTableWrapperBase* CSMWorld::IdTree::nestedTable(const QModelInde
return mNestedCollection->nestedTable(index.row(), index.column()); return mNestedCollection->nestedTable(index.row(), index.column());
} }
int CSMWorld::IdTree::searchNestedColumnIndex(int parentColumn, Columns::ColumnId id)
{
return mNestedCollection->searchNestedColumnIndex(parentColumn, id);
}
int CSMWorld::IdTree::findNestedColumnIndex(int parentColumn, Columns::ColumnId id)
{
return mNestedCollection->findNestedColumnIndex(parentColumn, id);
}

View File

@ -73,6 +73,12 @@ namespace CSMWorld
virtual bool hasChildren (const QModelIndex& index) const; virtual bool hasChildren (const QModelIndex& index) const;
virtual int searchNestedColumnIndex(int parentColumn, Columns::ColumnId id);
///< \return the column index or -1 if the requested column wasn't found.
virtual int findNestedColumnIndex(int parentColumn, Columns::ColumnId id);
///< \return the column index or throws an exception if the requested column wasn't found.
signals: signals:
void resetStart(const QString& id); void resetStart(const QString& id);

View File

@ -15,3 +15,28 @@ int CSMWorld::NestedCollection::getNestedColumnsCount(int row, int column) const
{ {
return 0; return 0;
} }
int CSMWorld::NestedCollection::searchNestedColumnIndex(int parentColumn, Columns::ColumnId id)
{
// Assumed that the parentColumn is always a valid index
const NestableColumn *parent = getNestableColumn(parentColumn);
int nestedColumnCount = getNestedColumnsCount(0, parentColumn);
for (int i = 0; i < nestedColumnCount; ++i)
{
if (parent->nestedColumn(i).mColumnId == id)
{
return i;
}
}
return -1;
}
int CSMWorld::NestedCollection::findNestedColumnIndex(int parentColumn, Columns::ColumnId id)
{
int index = searchNestedColumnIndex(parentColumn, id);
if (index == -1)
{
throw std::logic_error("CSMWorld::NestedCollection: No such nested column");
}
return index;
}

View File

@ -1,6 +1,8 @@
#ifndef CSM_WOLRD_NESTEDCOLLECTION_H #ifndef CSM_WOLRD_NESTEDCOLLECTION_H
#define CSM_WOLRD_NESTEDCOLLECTION_H #define CSM_WOLRD_NESTEDCOLLECTION_H
#include "columns.hpp"
class QVariant; class QVariant;
namespace CSMWorld namespace CSMWorld
@ -33,6 +35,12 @@ namespace CSMWorld
virtual int getNestedColumnsCount(int row, int column) const; virtual int getNestedColumnsCount(int row, int column) const;
virtual NestableColumn *getNestableColumn(int column) = 0; virtual NestableColumn *getNestableColumn(int column) = 0;
virtual int searchNestedColumnIndex(int parentColumn, Columns::ColumnId id);
///< \return the column index or -1 if the requested column wasn't found.
virtual int findNestedColumnIndex(int parentColumn, Columns::ColumnId id);
///< \return the column index or throws an exception if the requested column wasn't found.
}; };
} }

View File

@ -8,6 +8,9 @@
#include <QSpinBox> #include <QSpinBox>
#include <QLabel> #include <QLabel>
#include "../../model/world/commands.hpp"
#include "../../model/world/idtree.hpp"
std::string CSVWorld::CellCreator::getId() const std::string CSVWorld::CellCreator::getId() const
{ {
if (mType->currentIndex()==0) if (mType->currentIndex()==0)
@ -20,6 +23,15 @@ std::string CSVWorld::CellCreator::getId() const
return stream.str(); return stream.str();
} }
void CSVWorld::CellCreator::configureCreateCommand(CSMWorld::CreateCommand& command) const
{
CSMWorld::IdTree *model = dynamic_cast<CSMWorld::IdTree *>(getData().getTableModel(getCollectionId()));
Q_ASSERT(model != NULL);
int parentIndex = model->findColumnIndex(CSMWorld::Columns::ColumnId_Cell);
int index = model->findNestedColumnIndex(parentIndex, CSMWorld::Columns::ColumnId_Interior);
command.addNestedValue(parentIndex, index, mType->currentIndex() == 0);
}
CSVWorld::CellCreator::CellCreator (CSMWorld::Data& data, QUndoStack& undoStack, CSVWorld::CellCreator::CellCreator (CSMWorld::Data& data, QUndoStack& undoStack,
const CSMWorld::UniversalId& id) const CSMWorld::UniversalId& id)
: GenericCreator (data, undoStack, id) : GenericCreator (data, undoStack, id)
@ -95,9 +107,16 @@ void CSVWorld::CellCreator::cloneMode(const std::string& originId,
} }
} }
std::string CSVWorld::CellCreator::getErrors() const
void CSVWorld::CellCreator::toggleWidgets(bool active)
{ {
CSVWorld::GenericCreator::toggleWidgets(active); std::string errors;
mType->setEnabled(active); if (mType->currentIndex() == 0)
{
errors = GenericCreator::getErrors();
}
else if (getData().hasId(getId()))
{
errors = "The Exterior Cell is already exist";
}
return errors;
} }

View File

@ -23,17 +23,22 @@ namespace CSVWorld
virtual std::string getId() const; virtual std::string getId() const;
/// Allow subclasses to add additional data to \a command.
virtual void configureCreateCommand(CSMWorld::CreateCommand& command) const;
public: public:
CellCreator (CSMWorld::Data& data, QUndoStack& undoStack, const CSMWorld::UniversalId& id); CellCreator (CSMWorld::Data& data, QUndoStack& undoStack, const CSMWorld::UniversalId& id);
virtual void reset(); virtual void reset();
virtual void toggleWidgets(bool active = true);
virtual void cloneMode(const std::string& originId, virtual void cloneMode(const std::string& originId,
const CSMWorld::UniversalId::Type type); const CSMWorld::UniversalId::Type type);
virtual std::string getErrors() const;
///< Return formatted error descriptions for the current state of the creator. if an empty
/// string is returned, there is no error.
private slots: private slots:
void setType (int index); void setType (int index);

View File

@ -161,6 +161,8 @@ CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undo
connect (mCreate, SIGNAL (clicked (bool)), this, SLOT (create())); connect (mCreate, SIGNAL (clicked (bool)), this, SLOT (create()));
connect (mId, SIGNAL (textChanged (const QString&)), this, SLOT (textChanged (const QString&))); connect (mId, SIGNAL (textChanged (const QString&)), this, SLOT (textChanged (const QString&)));
connect (&mData, SIGNAL (idListChanged()), this, SLOT (dataIdListChanged()));
} }
void CSVWorld::GenericCreator::setEditLock (bool locked) void CSVWorld::GenericCreator::setEditLock (bool locked)
@ -291,3 +293,12 @@ void CSVWorld::GenericCreator::scopeChanged (int index)
update(); update();
updateNamespace(); updateNamespace();
} }
void CSVWorld::GenericCreator::dataIdListChanged()
{
// If the original ID of cloned record was removed, cancel the creator
if (mCloneMode && !mData.hasId(mClonedId))
{
emit done();
}
}

View File

@ -113,6 +113,8 @@ namespace CSVWorld
void create(); void create();
void scopeChanged (int index); void scopeChanged (int index);
void dataIdListChanged();
}; };
} }

View File

@ -447,7 +447,7 @@ void CSVWorld::Table::cloneRecord()
{ {
QModelIndexList selectedRows = selectionModel()->selectedRows(); QModelIndexList selectedRows = selectionModel()->selectedRows();
const CSMWorld::UniversalId& toClone = getUniversalId(selectedRows.begin()->row()); const CSMWorld::UniversalId& toClone = getUniversalId(selectedRows.begin()->row());
if (selectedRows.size()==1 && !mModel->isDeleted (toClone.getId())) if (selectedRows.size() == 1)
{ {
emit cloneRequest (toClone); emit cloneRequest (toClone);
} }