1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 10:23:53 +00:00
openmw/apps/opencs/model/world/resourcetable.cpp

175 lines
3.8 KiB
C++
Raw Normal View History

2014-07-05 13:50:47 +00:00
#include "resourcetable.hpp"
#include <stdexcept>
2022-10-19 17:02:00 +00:00
#include <type_traits>
#include <apps/opencs/model/world/idtablebase.hpp>
2014-07-05 13:50:47 +00:00
#include "columnbase.hpp"
2022-09-22 18:26:05 +00:00
#include "resources.hpp"
2014-07-05 13:50:47 +00:00
#include "universalid.hpp"
2022-09-22 18:26:05 +00:00
CSMWorld::ResourceTable::ResourceTable(const Resources* resources, unsigned int features)
: IdTableBase(features | Feature_Constant)
, mResources(resources)
{
}
2014-07-05 13:50:47 +00:00
CSMWorld::ResourceTable::~ResourceTable() {}
2022-09-22 18:26:05 +00:00
int CSMWorld::ResourceTable::rowCount(const QModelIndex& parent) const
2014-07-05 13:50:47 +00:00
{
if (parent.isValid())
return 0;
return mResources->getSize();
}
2022-09-22 18:26:05 +00:00
int CSMWorld::ResourceTable::columnCount(const QModelIndex& parent) const
2014-07-05 13:50:47 +00:00
{
if (parent.isValid())
return 0;
2014-07-07 13:20:05 +00:00
return 2; // ID, type
2014-07-05 13:50:47 +00:00
}
2022-09-22 18:26:05 +00:00
QVariant CSMWorld::ResourceTable::data(const QModelIndex& index, int role) const
2014-07-05 13:50:47 +00:00
{
2022-09-22 18:26:05 +00:00
if (role != Qt::DisplayRole)
2014-07-05 13:50:47 +00:00
return QVariant();
2022-09-22 18:26:05 +00:00
if (index.column() == 0)
return QString::fromUtf8(mResources->getId(index.row()).c_str());
2014-07-05 13:50:47 +00:00
2022-09-22 18:26:05 +00:00
if (index.column() == 1)
return static_cast<int>(mResources->getType());
2014-07-07 13:20:05 +00:00
2022-09-22 18:26:05 +00:00
throw std::logic_error("Invalid column in resource table");
2014-07-05 13:50:47 +00:00
}
2022-09-22 18:26:05 +00:00
QVariant CSMWorld::ResourceTable::headerData(int section, Qt::Orientation orientation, int role) const
2014-07-05 13:50:47 +00:00
{
2022-09-22 18:26:05 +00:00
if (orientation == Qt::Vertical)
2014-07-05 13:50:47 +00:00
return QVariant();
2022-09-22 18:26:05 +00:00
if (role == ColumnBase::Role_Flags)
return section == 0 ? ColumnBase::Flag_Table : 0;
2014-07-05 13:50:47 +00:00
2014-07-07 13:20:05 +00:00
switch (section)
{
case 0:
2022-09-22 18:26:05 +00:00
if (role == Qt::DisplayRole)
return Columns::getName(Columns::ColumnId_Id).c_str();
2014-07-07 13:20:05 +00:00
2022-09-22 18:26:05 +00:00
if (role == ColumnBase::Role_Display)
2015-04-04 17:55:53 +00:00
return ColumnBase::Display_Id;
2014-07-07 13:20:05 +00:00
break;
case 1:
2022-09-22 18:26:05 +00:00
if (role == Qt::DisplayRole)
return Columns::getName(Columns::ColumnId_RecordType).c_str();
2014-07-07 13:20:05 +00:00
2022-09-22 18:26:05 +00:00
if (role == ColumnBase::Role_Display)
2014-07-07 13:20:05 +00:00
return ColumnBase::Display_Integer;
break;
}
2014-07-05 13:50:47 +00:00
return QVariant();
}
2022-09-22 18:26:05 +00:00
bool CSMWorld::ResourceTable::setData(const QModelIndex& index, const QVariant& value, int role)
2014-07-05 13:50:47 +00:00
{
return false;
}
2022-09-22 18:26:05 +00:00
Qt::ItemFlags CSMWorld::ResourceTable::flags(const QModelIndex& index) const
2014-07-05 13:50:47 +00:00
{
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
2014-07-05 13:50:47 +00:00
}
2022-09-22 18:26:05 +00:00
QModelIndex CSMWorld::ResourceTable::index(int row, int column, const QModelIndex& parent) const
2014-07-05 13:50:47 +00:00
{
if (parent.isValid())
return QModelIndex();
2022-09-22 18:26:05 +00:00
if (row < 0 || row >= mResources->getSize())
2014-07-05 13:50:47 +00:00
return QModelIndex();
2022-09-22 18:26:05 +00:00
if (column < 0 || column > 1)
2014-07-05 13:50:47 +00:00
return QModelIndex();
2022-09-22 18:26:05 +00:00
return createIndex(row, column);
2014-07-05 13:50:47 +00:00
}
2022-09-22 18:26:05 +00:00
QModelIndex CSMWorld::ResourceTable::parent(const QModelIndex& index) const
2014-07-05 13:50:47 +00:00
{
return QModelIndex();
}
2022-09-22 18:26:05 +00:00
QModelIndex CSMWorld::ResourceTable::getModelIndex(const std::string& id, int column) const
2014-07-05 13:50:47 +00:00
{
int row = mResources->searchId(id);
if (row != -1)
2022-09-22 18:26:05 +00:00
return index(row, column);
return QModelIndex();
2014-07-05 13:50:47 +00:00
}
2022-09-22 18:26:05 +00:00
int CSMWorld::ResourceTable::searchColumnIndex(Columns::ColumnId id) const
2014-07-05 13:50:47 +00:00
{
2022-09-22 18:26:05 +00:00
if (id == Columns::ColumnId_Id)
2014-07-05 13:50:47 +00:00
return 0;
2022-09-22 18:26:05 +00:00
if (id == Columns::ColumnId_RecordType)
2014-07-07 13:20:05 +00:00
return 1;
2014-07-05 13:50:47 +00:00
return -1;
}
2022-09-22 18:26:05 +00:00
int CSMWorld::ResourceTable::findColumnIndex(Columns::ColumnId id) const
2014-07-05 13:50:47 +00:00
{
2022-09-22 18:26:05 +00:00
int index = searchColumnIndex(id);
2014-07-05 13:50:47 +00:00
2022-09-22 18:26:05 +00:00
if (index == -1)
throw std::logic_error("invalid column index");
2014-07-05 13:50:47 +00:00
return index;
}
2022-09-22 18:26:05 +00:00
std::pair<CSMWorld::UniversalId, std::string> CSMWorld::ResourceTable::view(int row) const
2014-07-05 13:50:47 +00:00
{
2022-09-22 18:26:05 +00:00
return std::make_pair(UniversalId::Type_None, "");
2014-07-05 13:50:47 +00:00
}
2022-09-22 18:26:05 +00:00
bool CSMWorld::ResourceTable::isDeleted(const std::string& id) const
2014-07-05 13:50:47 +00:00
{
return false;
2015-03-11 14:54:45 +00:00
}
2022-09-22 18:26:05 +00:00
int CSMWorld::ResourceTable::getColumnId(int column) const
{
switch (column)
{
2022-09-22 18:26:05 +00:00
case 0:
return Columns::ColumnId_Id;
case 1:
return Columns::ColumnId_RecordType;
}
return -1;
}
2017-08-19 07:43:31 +00:00
void CSMWorld::ResourceTable::beginReset()
{
beginResetModel();
}
void CSMWorld::ResourceTable::endReset()
{
endResetModel();
}