From 564d0db68cda16af7c1c8bdd2779f8046aa339bd Mon Sep 17 00:00:00 2001 From: Aesylwinn Date: Thu, 26 May 2016 22:11:27 -0400 Subject: [PATCH] Move pathgrid abstraction handling to save code. --- .../model/world/nestedcoladapterimp.cpp | 52 +++---------------- .../model/world/nestedcoladapterimp.hpp | 15 ------ components/esm/loadpgrd.cpp | 32 ++++++++++-- 3 files changed, 34 insertions(+), 65 deletions(-) diff --git a/apps/opencs/model/world/nestedcoladapterimp.cpp b/apps/opencs/model/world/nestedcoladapterimp.cpp index 3189f76ed..464757cd4 100644 --- a/apps/opencs/model/world/nestedcoladapterimp.cpp +++ b/apps/opencs/model/world/nestedcoladapterimp.cpp @@ -27,20 +27,8 @@ namespace CSMWorld point.mConnectionNum = 0; point.mUnknown = 0; - // inserting a point should trigger re-indexing of the edges - // - // FIXME: does not auto refresh edges table view - std::vector::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); - pathgrid.mData.mS2 += 1; // increment the number of points + pathgrid.mData.mS2 = pathgrid.mPoints.size(); record.setModified (pathgrid); } @@ -54,28 +42,10 @@ namespace CSMWorld if (rowToRemove < 0 || rowToRemove >= static_cast (points.size())) throw std::runtime_error ("index out of range"); - // deleting a point should trigger re-indexing of the edges - // dangling edges are not allowed and hence removed - // - // FIXME: does not auto refresh edges table view - std::vector::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; - } - } + // Do not remove dangling edges, does not work with current undo mechanism + // Do not automatically adjust indices, what would be done with dangling edges? points.erase(points.begin()+rowToRemove); - pathgrid.mData.mS2 -= 1; // decrement the number of points + pathgrid.mData.mS2 = pathgrid.mPoints.size(); record.setModified (pathgrid); } @@ -84,14 +54,8 @@ namespace CSMWorld const NestedTableWrapperBase& nestedTable) const { Pathgrid pathgrid = record.get(); - - pathgrid.mPoints = - static_cast(nestedTable).mRecord.mPoints; - pathgrid.mData.mS2 = - static_cast(nestedTable).mRecord.mData.mS2; - // also update edges in case points were added/removed - pathgrid.mEdges = - static_cast(nestedTable).mRecord.mEdges; + pathgrid.mPoints = static_cast &>(nestedTable).mNestedTable; + pathgrid.mData.mS2 = pathgrid.mPoints.size(); record.setModified (pathgrid); } @@ -99,7 +63,7 @@ namespace CSMWorld NestedTableWrapperBase* PathgridPointListAdapter::table(const Record& record) const { // deleted by dtor of NestedTableStoring - return new PathgridPointsWrap(record.get()); + return new NestedTableWrapper(record.get().mPoints); } QVariant PathgridPointListAdapter::getData(const Record& record, @@ -147,7 +111,6 @@ namespace CSMWorld PathgridEdgeListAdapter::PathgridEdgeListAdapter () {} - // ToDo: seems to be auto-sorted in the dialog table display after insertion void PathgridEdgeListAdapter::addRow(Record& record, int position) const { Pathgrid pathgrid = record.get(); @@ -218,7 +181,6 @@ namespace CSMWorld } } - // ToDo: detect duplicates in mEdges void PathgridEdgeListAdapter::setData(Record& record, const QVariant& value, int subRowIndex, int subColIndex) const { diff --git a/apps/opencs/model/world/nestedcoladapterimp.hpp b/apps/opencs/model/world/nestedcoladapterimp.hpp index e24bcb134..131f547a5 100644 --- a/apps/opencs/model/world/nestedcoladapterimp.hpp +++ b/apps/opencs/model/world/nestedcoladapterimp.hpp @@ -25,21 +25,6 @@ namespace CSMWorld struct Pathgrid; 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 { public: diff --git a/components/esm/loadpgrd.cpp b/components/esm/loadpgrd.cpp index 15accd95b..708685e72 100644 --- a/components/esm/loadpgrd.cpp +++ b/components/esm/loadpgrd.cpp @@ -127,6 +127,28 @@ namespace ESM 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 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(it->mV0) == point) + { + sortedEdges.push_back(it->mV1); + ++correctedPoints[point].mConnectionNum; + } + } + } + + // Save esm.writeHNCString("NAME", mCell); esm.writeHNT("DATA", mData, 12); @@ -136,22 +158,22 @@ namespace ESM return; } - if (!mPoints.empty()) + if (!correctedPoints.empty()) { 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.endRecord("PGRP"); } - if (!mEdges.empty()) + if (!sortedEdges.empty()) { esm.startSubRecord("PGRC"); - for (std::vector::const_iterator it = mEdges.begin(); it != mEdges.end(); ++it) + for (std::vector::const_iterator it = sortedEdges.begin(); it != sortedEdges.end(); ++it) { - esm.writeT(it->mV1); + esm.writeT(*it); } esm.endRecord("PGRC"); }