From 0944338c27c8ab3f4ad912c7670b99c732ed755e Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Thu, 8 Aug 2013 12:49:30 +0200 Subject: [PATCH] avoid the use of hardcoded column numbers --- apps/opencs/model/world/idtable.cpp | 21 +++++++++++++++++++++ apps/opencs/model/world/idtable.hpp | 8 ++++++++ apps/opencs/view/world/genericcreator.cpp | 6 +++--- apps/opencs/view/world/genericcreator.hpp | 4 ++-- apps/opencs/view/world/referencecreator.cpp | 9 +++++++-- apps/opencs/view/world/table.cpp | 14 ++++++++------ 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/apps/opencs/model/world/idtable.cpp b/apps/opencs/model/world/idtable.cpp index 8d1bae972..baaf75289 100644 --- a/apps/opencs/model/world/idtable.cpp +++ b/apps/opencs/model/world/idtable.cpp @@ -157,4 +157,25 @@ void CSMWorld::IdTable::setRecord (const std::string& id, const RecordBase& reco const CSMWorld::RecordBase& CSMWorld::IdTable::getRecord (const std::string& id) const { return mIdCollection->getRecord (id); +} + +int CSMWorld::IdTable::searchColumnIndex (Columns::ColumnId id) const +{ + int columns = mIdCollection->getColumns(); + + for (int i=0; igetColumn (i).mColumnId==id) + return i; + + return -1; +} + +int CSMWorld::IdTable::findColumnIndex (Columns::ColumnId id) const +{ + int index = searchColumnIndex (id); + + if (index==-1) + throw std::logic_error ("invalid column index"); + + return index; } \ No newline at end of file diff --git a/apps/opencs/model/world/idtable.hpp b/apps/opencs/model/world/idtable.hpp index 556d7f0ba..00c577236 100644 --- a/apps/opencs/model/world/idtable.hpp +++ b/apps/opencs/model/world/idtable.hpp @@ -4,6 +4,7 @@ #include #include "universalid.hpp" +#include "columns.hpp" namespace CSMWorld { @@ -55,6 +56,13 @@ namespace CSMWorld ///< Add record or overwrite existing recrod. const RecordBase& getRecord (const std::string& id) const; + + int searchColumnIndex (Columns::ColumnId id) const; + ///< Return index of column with the given \a id. If no such column exists, -1 is returned. + + int findColumnIndex (Columns::ColumnId id) const; + ///< Return index of column with the given \a id. If no such column exists, an exception is + /// thrown. }; } diff --git a/apps/opencs/view/world/genericcreator.cpp b/apps/opencs/view/world/genericcreator.cpp index 7502b6555..df43d6c5f 100644 --- a/apps/opencs/view/world/genericcreator.cpp +++ b/apps/opencs/view/world/genericcreator.cpp @@ -46,14 +46,14 @@ std::string CSVWorld::GenericCreator::getId() const void CSVWorld::GenericCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const {} -const CSMWorld::Data& CSVWorld::GenericCreator::getData() const +CSMWorld::Data& CSVWorld::GenericCreator::getData() const { return mData; } -CSMWorld::Data& CSVWorld::GenericCreator::getData() +const CSMWorld::UniversalId& CSVWorld::GenericCreator::getCollectionId() const { - return mData; + return mListId; } CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, diff --git a/apps/opencs/view/world/genericcreator.hpp b/apps/opencs/view/world/genericcreator.hpp index 5409f0839..6752d8591 100644 --- a/apps/opencs/view/world/genericcreator.hpp +++ b/apps/opencs/view/world/genericcreator.hpp @@ -44,9 +44,9 @@ namespace CSVWorld virtual void configureCreateCommand (CSMWorld::CreateCommand& command) const; - const CSMWorld::Data& getData() const; + CSMWorld::Data& getData() const; - CSMWorld::Data& getData(); + const CSMWorld::UniversalId& getCollectionId() const; public: diff --git a/apps/opencs/view/world/referencecreator.cpp b/apps/opencs/view/world/referencecreator.cpp index 48a168a35..aef25a81d 100644 --- a/apps/opencs/view/world/referencecreator.cpp +++ b/apps/opencs/view/world/referencecreator.cpp @@ -6,6 +6,8 @@ #include "../../model/world/data.hpp" #include "../../model/world/commands.hpp" +#include "../../model/world/columns.hpp" +#include "../../model/world/idtable.hpp" std::string CSVWorld::ReferenceCreator::getId() const { @@ -14,8 +16,11 @@ std::string CSVWorld::ReferenceCreator::getId() const void CSVWorld::ReferenceCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const { - /// \todo avoid using hard-coded column numbers - command.addValue (2, mCell->text()); + int index = + dynamic_cast (*getData().getTableModel (getCollectionId())). + findColumnIndex (CSMWorld::Columns::ColumnId_Cell); + + command.addValue (index, mCell->text()); } CSVWorld::ReferenceCreator::ReferenceCreator (CSMWorld::Data& data, QUndoStack& undoStack, diff --git a/apps/opencs/view/world/table.cpp b/apps/opencs/view/world/table.cpp index fdbf67e42..4ae25d10b 100644 --- a/apps/opencs/view/world/table.cpp +++ b/apps/opencs/view/world/table.cpp @@ -44,7 +44,6 @@ void CSVWorld::Table::contextMenuEvent (QContextMenuEvent *event) std::vector CSVWorld::Table::listRevertableSelectedIds() const { - /// \todo Do not use hardcoded column numbers std::vector revertableIds; if (mProxyModel->columnCount()>0) @@ -62,7 +61,9 @@ std::vector CSVWorld::Table::listRevertableSelectedIds() const if (state!=CSMWorld::RecordBase::State_BaseOnly) { - std::string id = mModel->data (mModel->index (index.row(), 0)). + int columnIndex = mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Id); + + std::string id = mModel->data (mModel->index (index.row(), columnIndex)). toString().toUtf8().constData(); revertableIds.push_back (id); @@ -75,7 +76,6 @@ std::vector CSVWorld::Table::listRevertableSelectedIds() const std::vector CSVWorld::Table::listDeletableSelectedIds() const { - /// \todo Do not use hardcoded column numbers std::vector deletableIds; if (mProxyModel->columnCount()>0) @@ -93,7 +93,9 @@ std::vector CSVWorld::Table::listDeletableSelectedIds() const if (state!=CSMWorld::RecordBase::State_Deleted) { - std::string id = mModel->data (mModel->index (index.row(), 0)). + int columnIndex = mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Id); + + std::string id = mModel->data (mModel->index (index.row(), columnIndex)). toString().toUtf8().constData(); deletableIds.push_back (id); @@ -263,8 +265,8 @@ void CSVWorld::Table::tableSizeUpdate() { QModelIndex index = mProxyModel->mapToSource (mProxyModel->index (i, 0)); - /// \todo Do not use hardcoded column numbers - int state = mModel->data (mModel->index (index.row(), 1)).toInt(); + int columnIndex = mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Modification); + int state = mModel->data (mModel->index (index.row(), columnIndex)).toInt(); switch (state) {