Move pathgrid abstraction handling to save code.

coverity_scan^2
Aesylwinn 9 years ago
parent 239727531f
commit 564d0db68c

@ -27,20 +27,8 @@ namespace CSMWorld
point.mConnectionNum = 0; point.mConnectionNum = 0;
point.mUnknown = 0; point.mUnknown = 0;
// inserting a point should trigger re-indexing of the edges
//
// FIXME: does not auto refresh edges table view
std::vector<ESM::Pathgrid::Edge>::iterator iter = pathgrid.mEdges.begin();
for (;iter != pathgrid.mEdges.end(); ++iter)
{
if ((*iter).mV0 >= position)
(*iter).mV0++;
if ((*iter).mV1 >= position)
(*iter).mV1++;
}
points.insert(points.begin()+position, point); points.insert(points.begin()+position, point);
pathgrid.mData.mS2 += 1; // increment the number of points pathgrid.mData.mS2 = pathgrid.mPoints.size();
record.setModified (pathgrid); record.setModified (pathgrid);
} }
@ -54,28 +42,10 @@ namespace CSMWorld
if (rowToRemove < 0 || rowToRemove >= static_cast<int> (points.size())) if (rowToRemove < 0 || rowToRemove >= static_cast<int> (points.size()))
throw std::runtime_error ("index out of range"); throw std::runtime_error ("index out of range");
// deleting a point should trigger re-indexing of the edges // Do not remove dangling edges, does not work with current undo mechanism
// dangling edges are not allowed and hence removed // Do not automatically adjust indices, what would be done with dangling edges?
//
// FIXME: does not auto refresh edges table view
std::vector<ESM::Pathgrid::Edge>::iterator iter = pathgrid.mEdges.begin();
for (; iter != pathgrid.mEdges.end();)
{
if (((*iter).mV0 == rowToRemove) || ((*iter).mV1 == rowToRemove))
iter = pathgrid.mEdges.erase(iter);
else
{
if ((*iter).mV0 > rowToRemove)
(*iter).mV0--;
if ((*iter).mV1 > rowToRemove)
(*iter).mV1--;
++iter;
}
}
points.erase(points.begin()+rowToRemove); points.erase(points.begin()+rowToRemove);
pathgrid.mData.mS2 -= 1; // decrement the number of points pathgrid.mData.mS2 = pathgrid.mPoints.size();
record.setModified (pathgrid); record.setModified (pathgrid);
} }
@ -84,14 +54,8 @@ namespace CSMWorld
const NestedTableWrapperBase& nestedTable) const const NestedTableWrapperBase& nestedTable) const
{ {
Pathgrid pathgrid = record.get(); Pathgrid pathgrid = record.get();
pathgrid.mPoints = static_cast<const NestedTableWrapper<ESM::Pathgrid::PointList> &>(nestedTable).mNestedTable;
pathgrid.mPoints = pathgrid.mData.mS2 = pathgrid.mPoints.size();
static_cast<const PathgridPointsWrap &>(nestedTable).mRecord.mPoints;
pathgrid.mData.mS2 =
static_cast<const PathgridPointsWrap &>(nestedTable).mRecord.mData.mS2;
// also update edges in case points were added/removed
pathgrid.mEdges =
static_cast<const PathgridPointsWrap &>(nestedTable).mRecord.mEdges;
record.setModified (pathgrid); record.setModified (pathgrid);
} }
@ -99,7 +63,7 @@ namespace CSMWorld
NestedTableWrapperBase* PathgridPointListAdapter::table(const Record<Pathgrid>& record) const NestedTableWrapperBase* PathgridPointListAdapter::table(const Record<Pathgrid>& record) const
{ {
// deleted by dtor of NestedTableStoring // deleted by dtor of NestedTableStoring
return new PathgridPointsWrap(record.get()); return new NestedTableWrapper<ESM::Pathgrid::PointList>(record.get().mPoints);
} }
QVariant PathgridPointListAdapter::getData(const Record<Pathgrid>& record, QVariant PathgridPointListAdapter::getData(const Record<Pathgrid>& record,
@ -147,7 +111,6 @@ namespace CSMWorld
PathgridEdgeListAdapter::PathgridEdgeListAdapter () {} PathgridEdgeListAdapter::PathgridEdgeListAdapter () {}
// ToDo: seems to be auto-sorted in the dialog table display after insertion
void PathgridEdgeListAdapter::addRow(Record<Pathgrid>& record, int position) const void PathgridEdgeListAdapter::addRow(Record<Pathgrid>& record, int position) const
{ {
Pathgrid pathgrid = record.get(); Pathgrid pathgrid = record.get();
@ -218,7 +181,6 @@ namespace CSMWorld
} }
} }
// ToDo: detect duplicates in mEdges
void PathgridEdgeListAdapter::setData(Record<Pathgrid>& record, void PathgridEdgeListAdapter::setData(Record<Pathgrid>& record,
const QVariant& value, int subRowIndex, int subColIndex) const const QVariant& value, int subRowIndex, int subColIndex) const
{ {

@ -25,21 +25,6 @@ namespace CSMWorld
struct Pathgrid; struct Pathgrid;
struct Info; struct Info;
struct PathgridPointsWrap : public NestedTableWrapperBase
{
ESM::Pathgrid mRecord;
PathgridPointsWrap(ESM::Pathgrid pathgrid)
: mRecord(pathgrid) {}
virtual ~PathgridPointsWrap() {}
virtual int size() const
{
return mRecord.mPoints.size(); // used in IdTree::setNestedTable()
}
};
class PathgridPointListAdapter : public NestedColumnAdapter<Pathgrid> class PathgridPointListAdapter : public NestedColumnAdapter<Pathgrid>
{ {
public: public:

@ -127,6 +127,28 @@ namespace ESM
void Pathgrid::save(ESMWriter &esm, bool isDeleted) const void Pathgrid::save(ESMWriter &esm, bool isDeleted) const
{ {
// Correct connection count and sort edges by point
// Can probably be optimized
PointList correctedPoints = mPoints;
std::vector<int> sortedEdges;
sortedEdges.reserve(mEdges.size());
for (size_t point = 0; point < correctedPoints.size(); ++point)
{
correctedPoints[point].mConnectionNum = 0;
for (EdgeList::const_iterator it = mEdges.begin(); it != mEdges.end(); ++it)
{
if (static_cast<size_t>(it->mV0) == point)
{
sortedEdges.push_back(it->mV1);
++correctedPoints[point].mConnectionNum;
}
}
}
// Save
esm.writeHNCString("NAME", mCell); esm.writeHNCString("NAME", mCell);
esm.writeHNT("DATA", mData, 12); esm.writeHNT("DATA", mData, 12);
@ -136,22 +158,22 @@ namespace ESM
return; return;
} }
if (!mPoints.empty()) if (!correctedPoints.empty())
{ {
esm.startSubRecord("PGRP"); esm.startSubRecord("PGRP");
for (PointList::const_iterator it = mPoints.begin(); it != mPoints.end(); ++it) for (PointList::const_iterator it = correctedPoints.begin(); it != correctedPoints.end(); ++it)
{ {
esm.writeT(*it); esm.writeT(*it);
} }
esm.endRecord("PGRP"); esm.endRecord("PGRP");
} }
if (!mEdges.empty()) if (!sortedEdges.empty())
{ {
esm.startSubRecord("PGRC"); esm.startSubRecord("PGRC");
for (std::vector<Edge>::const_iterator it = mEdges.begin(); it != mEdges.end(); ++it) for (std::vector<int>::const_iterator it = sortedEdges.begin(); it != sortedEdges.end(); ++it)
{ {
esm.writeT(it->mV1); esm.writeT(*it);
} }
esm.endRecord("PGRC"); esm.endRecord("PGRC");
} }

Loading…
Cancel
Save