1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 13:23:55 +00:00
openmw/apps/opencs/model/world/nestedidcollection.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 lines
7.3 KiB
C++
Raw Normal View History

#ifndef CSM_WOLRD_NESTEDIDCOLLECTION_H
#define CSM_WOLRD_NESTEDIDCOLLECTION_H
#include <map>
2022-08-19 17:19:42 +00:00
#include <memory>
#include <stdexcept>
2022-08-19 17:19:42 +00:00
#include "collection.hpp"
#include "nestedcollection.hpp"
2022-08-19 17:19:42 +00:00
#include "nestedcolumnadapter.hpp"
namespace ESM
{
class ESMReader;
}
namespace CSMWorld
{
struct NestedTableWrapperBase;
struct Cell;
2022-08-19 17:19:42 +00:00
struct ColumnBase;
2022-08-19 17:19:42 +00:00
template <typename ESXRecordT>
struct IdAccessor;
template <typename ESXRecordT, typename IdAccessorT>
class IdCollection;
2022-08-19 17:19:42 +00:00
template <typename ESXRecordT>
class NestedColumnAdapter;
template <typename ESXRecordT, typename IdAccessorT = IdAccessor<ESXRecordT>>
class NestedIdCollection : public IdCollection<ESXRecordT, IdAccessorT>, public NestedCollection
{
2015-04-12 03:48:23 +00:00
std::map<const ColumnBase*, NestedColumnAdapter<ESXRecordT>*> mAdapters;
2015-04-12 03:48:23 +00:00
const NestedColumnAdapter<ESXRecordT>& getAdapter(const ColumnBase& column) const;
2022-09-22 18:26:05 +00:00
public:
NestedIdCollection();
~NestedIdCollection() override;
void addNestedRow(int row, int column, int position) override;
void removeNestedRows(int row, int column, int subRow) override;
QVariant getNestedData(int row, int column, int subRow, int subColumn) const override;
void setNestedData(int row, int column, const QVariant& data, int subRow, int subColumn) override;
NestedTableWrapperBase* nestedTable(int row, int column) const override;
void setNestedTable(int row, int column, const NestedTableWrapperBase& nestedTable) override;
int getNestedRowsCount(int row, int column) const override;
int getNestedColumnsCount(int row, int column) const override;
// this method is inherited from NestedCollection, not from Collection<ESXRecordT>
NestableColumn* getNestableColumn(int column) override;
2015-04-12 03:48:23 +00:00
void addAdapter(std::pair<const ColumnBase*, NestedColumnAdapter<ESXRecordT>*> adapter);
};
template <typename ESXRecordT, typename IdAccessorT>
NestedIdCollection<ESXRecordT, IdAccessorT>::NestedIdCollection()
{
}
template <typename ESXRecordT, typename IdAccessorT>
NestedIdCollection<ESXRecordT, IdAccessorT>::~NestedIdCollection()
{
2015-04-12 03:48:23 +00:00
for (typename std::map<const ColumnBase*, NestedColumnAdapter<ESXRecordT>*>::iterator iter(mAdapters.begin());
iter != mAdapters.end(); ++iter)
{
delete (*iter).second;
2015-04-12 03:48:23 +00:00
}
}
template <typename ESXRecordT, typename IdAccessorT>
2015-04-12 03:48:23 +00:00
void NestedIdCollection<ESXRecordT, IdAccessorT>::addAdapter(
std::pair<const ColumnBase*, NestedColumnAdapter<ESXRecordT>*> adapter)
{
mAdapters.insert(adapter);
}
template <typename ESXRecordT, typename IdAccessorT>
2015-04-12 03:48:23 +00:00
const NestedColumnAdapter<ESXRecordT>& NestedIdCollection<ESXRecordT, IdAccessorT>::getAdapter(
const ColumnBase& column) const
{
2015-04-12 03:48:23 +00:00
typename std::map<const ColumnBase*, NestedColumnAdapter<ESXRecordT>*>::const_iterator iter
= mAdapters.find(&column);
if (iter == mAdapters.end())
throw std::logic_error("No such column in the nestedidadapter");
return *iter->second;
}
template <typename ESXRecordT, typename IdAccessorT>
void NestedIdCollection<ESXRecordT, IdAccessorT>::addNestedRow(int row, int column, int position)
{
2022-05-29 11:25:17 +00:00
auto record = std::make_unique<Record<ESXRecordT>>();
record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).addRow(*record, position);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
}
template <typename ESXRecordT, typename IdAccessorT>
void NestedIdCollection<ESXRecordT, IdAccessorT>::removeNestedRows(int row, int column, int subRow)
{
2022-05-29 11:25:17 +00:00
auto record = std::make_unique<Record<ESXRecordT>>();
record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).removeRow(*record, subRow);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
}
template <typename ESXRecordT, typename IdAccessorT>
QVariant NestedIdCollection<ESXRecordT, IdAccessorT>::getNestedData(
int row, int column, int subRow, int subColumn) const
{
2015-04-18 22:58:52 +00:00
return getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column))
.getData(Collection<ESXRecordT, IdAccessorT>::getRecord(row), subRow, subColumn);
}
template <typename ESXRecordT, typename IdAccessorT>
void NestedIdCollection<ESXRecordT, IdAccessorT>::setNestedData(
int row, int column, const QVariant& data, int subRow, int subColumn)
{
2022-05-29 11:25:17 +00:00
auto record = std::make_unique<Record<ESXRecordT>>();
record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
2015-04-18 22:58:52 +00:00
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setData(*record, data, subRow, subColumn);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
}
template <typename ESXRecordT, typename IdAccessorT>
CSMWorld::NestedTableWrapperBase* NestedIdCollection<ESXRecordT, IdAccessorT>::nestedTable(
int row, int column) const
{
2015-04-18 23:42:44 +00:00
return getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column))
.table(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
}
template <typename ESXRecordT, typename IdAccessorT>
void NestedIdCollection<ESXRecordT, IdAccessorT>::setNestedTable(
int row, int column, const CSMWorld::NestedTableWrapperBase& nestedTable)
{
2022-05-29 11:25:17 +00:00
auto record = std::make_unique<Record<ESXRecordT>>();
record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
2015-04-18 22:58:52 +00:00
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setTable(*record, nestedTable);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
}
template <typename ESXRecordT, typename IdAccessorT>
int NestedIdCollection<ESXRecordT, IdAccessorT>::getNestedRowsCount(int row, int column) const
{
2015-04-18 22:58:52 +00:00
return getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column))
.getRowsCount(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
}
template <typename ESXRecordT, typename IdAccessorT>
int NestedIdCollection<ESXRecordT, IdAccessorT>::getNestedColumnsCount(int row, int column) const
{
const ColumnBase& nestedColumn = Collection<ESXRecordT, IdAccessorT>::getColumn(column);
int numRecords = Collection<ESXRecordT, IdAccessorT>::getSize();
if (row >= 0 && row < numRecords)
{
const Record<ESXRecordT>& record = Collection<ESXRecordT, IdAccessorT>::getRecord(row);
return getAdapter(nestedColumn).getColumnsCount(record);
}
else
{
// If the row is invalid (or there no records), retrieve the column count using a blank record
const Record<ESXRecordT> record;
return getAdapter(nestedColumn).getColumnsCount(record);
}
}
template <typename ESXRecordT, typename IdAccessorT>
CSMWorld::NestableColumn* NestedIdCollection<ESXRecordT, IdAccessorT>::getNestableColumn(int column)
{
return Collection<ESXRecordT, IdAccessorT>::getNestableColumn(column);
}
}
#endif // CSM_WOLRD_NESTEDIDCOLLECTION_H