1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-22 22:40:12 +00:00

avoid the use of hardcoded column numbers

This commit is contained in:
Marc Zinnschlag 2013-08-08 12:49:30 +02:00
parent 76b9d3dbc1
commit 0944338c27
6 changed files with 49 additions and 13 deletions

View file

@ -158,3 +158,24 @@ const CSMWorld::RecordBase& CSMWorld::IdTable::getRecord (const std::string& id)
{ {
return mIdCollection->getRecord (id); return mIdCollection->getRecord (id);
} }
int CSMWorld::IdTable::searchColumnIndex (Columns::ColumnId id) const
{
int columns = mIdCollection->getColumns();
for (int i=0; i<columns; ++i)
if (mIdCollection->getColumn (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;
}

View file

@ -4,6 +4,7 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include "universalid.hpp" #include "universalid.hpp"
#include "columns.hpp"
namespace CSMWorld namespace CSMWorld
{ {
@ -55,6 +56,13 @@ namespace CSMWorld
///< Add record or overwrite existing recrod. ///< Add record or overwrite existing recrod.
const RecordBase& getRecord (const std::string& id) const; 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.
}; };
} }

View file

@ -46,14 +46,14 @@ std::string CSVWorld::GenericCreator::getId() const
void CSVWorld::GenericCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const {} void CSVWorld::GenericCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const {}
const CSMWorld::Data& CSVWorld::GenericCreator::getData() const CSMWorld::Data& CSVWorld::GenericCreator::getData() const
{ {
return mData; 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, CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack,

View file

@ -44,9 +44,9 @@ namespace CSVWorld
virtual void configureCreateCommand (CSMWorld::CreateCommand& command) const; 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: public:

View file

@ -6,6 +6,8 @@
#include "../../model/world/data.hpp" #include "../../model/world/data.hpp"
#include "../../model/world/commands.hpp" #include "../../model/world/commands.hpp"
#include "../../model/world/columns.hpp"
#include "../../model/world/idtable.hpp"
std::string CSVWorld::ReferenceCreator::getId() const std::string CSVWorld::ReferenceCreator::getId() const
{ {
@ -14,8 +16,11 @@ std::string CSVWorld::ReferenceCreator::getId() const
void CSVWorld::ReferenceCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const void CSVWorld::ReferenceCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const
{ {
/// \todo avoid using hard-coded column numbers int index =
command.addValue (2, mCell->text()); dynamic_cast<CSMWorld::IdTable&> (*getData().getTableModel (getCollectionId())).
findColumnIndex (CSMWorld::Columns::ColumnId_Cell);
command.addValue (index, mCell->text());
} }
CSVWorld::ReferenceCreator::ReferenceCreator (CSMWorld::Data& data, QUndoStack& undoStack, CSVWorld::ReferenceCreator::ReferenceCreator (CSMWorld::Data& data, QUndoStack& undoStack,

View file

@ -44,7 +44,6 @@ void CSVWorld::Table::contextMenuEvent (QContextMenuEvent *event)
std::vector<std::string> CSVWorld::Table::listRevertableSelectedIds() const std::vector<std::string> CSVWorld::Table::listRevertableSelectedIds() const
{ {
/// \todo Do not use hardcoded column numbers
std::vector<std::string> revertableIds; std::vector<std::string> revertableIds;
if (mProxyModel->columnCount()>0) if (mProxyModel->columnCount()>0)
@ -62,7 +61,9 @@ std::vector<std::string> CSVWorld::Table::listRevertableSelectedIds() const
if (state!=CSMWorld::RecordBase::State_BaseOnly) 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(); toString().toUtf8().constData();
revertableIds.push_back (id); revertableIds.push_back (id);
@ -75,7 +76,6 @@ std::vector<std::string> CSVWorld::Table::listRevertableSelectedIds() const
std::vector<std::string> CSVWorld::Table::listDeletableSelectedIds() const std::vector<std::string> CSVWorld::Table::listDeletableSelectedIds() const
{ {
/// \todo Do not use hardcoded column numbers
std::vector<std::string> deletableIds; std::vector<std::string> deletableIds;
if (mProxyModel->columnCount()>0) if (mProxyModel->columnCount()>0)
@ -93,7 +93,9 @@ std::vector<std::string> CSVWorld::Table::listDeletableSelectedIds() const
if (state!=CSMWorld::RecordBase::State_Deleted) 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(); toString().toUtf8().constData();
deletableIds.push_back (id); deletableIds.push_back (id);
@ -263,8 +265,8 @@ void CSVWorld::Table::tableSizeUpdate()
{ {
QModelIndex index = mProxyModel->mapToSource (mProxyModel->index (i, 0)); QModelIndex index = mProxyModel->mapToSource (mProxyModel->index (i, 0));
/// \todo Do not use hardcoded column numbers int columnIndex = mModel->findColumnIndex (CSMWorld::Columns::ColumnId_Modification);
int state = mModel->data (mModel->index (index.row(), 1)).toInt(); int state = mModel->data (mModel->index (index.row(), columnIndex)).toInt();
switch (state) switch (state)
{ {