1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00

replaced CellIndex typedef with new CellCoordinates class

This commit is contained in:
Marc Zinnschlag 2014-04-11 10:06:16 +02:00
parent 0516d95253
commit d0ea23431c
2 changed files with 66 additions and 100 deletions

View File

@ -1,6 +1,7 @@
#include "regionmap.hpp" #include "regionmap.hpp"
#include <cmath>
#include <algorithm> #include <algorithm>
#include <QBrush> #include <QBrush>
@ -25,17 +26,16 @@ CSMWorld::RegionMap::CellDescription::CellDescription (const Record<Cell>& cell)
mName = cell2.mName; mName = cell2.mName;
} }
CSMWorld::RegionMap::CellIndex CSMWorld::RegionMap::getIndex (const QModelIndex& index) const CSMWorld::CellCoordinates CSMWorld::RegionMap::getIndex (const QModelIndex& index) const
{ {
return CellIndex (index.column()+mMin.first, return mMin.move (index.column(), mMax.getY()-mMin.getY() - index.row()-1);
(mMax.second-mMin.second - index.row()-1)+mMin.second);
} }
QModelIndex CSMWorld::RegionMap::getIndex (const CellIndex& index) const QModelIndex CSMWorld::RegionMap::getIndex (const CellCoordinates& index) const
{ {
// I hate you, Qt API naming scheme! // I hate you, Qt API naming scheme!
return QAbstractTableModel::index (mMax.second-mMin.second - (index.second-mMin.second)-1, return QAbstractTableModel::index (mMax.getY()-mMin.getY() - (index.getY()-mMin.getY())-1,
index.first-mMin.first); index.getX()-mMin.getX());
} }
void CSMWorld::RegionMap::buildRegions() void CSMWorld::RegionMap::buildRegions()
@ -70,21 +70,21 @@ void CSMWorld::RegionMap::buildMap()
{ {
CellDescription description (cell); CellDescription description (cell);
CellIndex index (cell2.mData.mX, cell2.mData.mY); CellCoordinates index (cell2.mData.mX, cell2.mData.mY);
mMap.insert (std::make_pair (index, description)); mMap.insert (std::make_pair (index, description));
} }
} }
std::pair<CellIndex, CellIndex> mapSize = getSize(); std::pair<CellCoordinates, CellCoordinates> mapSize = getSize();
mMin = mapSize.first; mMin = mapSize.first;
mMax = mapSize.second; mMax = mapSize.second;
} }
void CSMWorld::RegionMap::addCell (const CellIndex& index, const CellDescription& description) void CSMWorld::RegionMap::addCell (const CellCoordinates& index, const CellDescription& description)
{ {
std::map<CellIndex, CellDescription>::iterator cell = mMap.find (index); std::map<CellCoordinates, CellDescription>::iterator cell = mMap.find (index);
if (cell!=mMap.end()) if (cell!=mMap.end())
{ {
@ -114,7 +114,7 @@ void CSMWorld::RegionMap::addCells (int start, int end)
if (cell2.isExterior()) if (cell2.isExterior())
{ {
CellIndex index (cell2.mData.mX, cell2.mData.mY); CellCoordinates index (cell2.mData.mX, cell2.mData.mY);
CellDescription description (cell); CellDescription description (cell);
@ -123,9 +123,9 @@ void CSMWorld::RegionMap::addCells (int start, int end)
} }
} }
void CSMWorld::RegionMap::removeCell (const CellIndex& index) void CSMWorld::RegionMap::removeCell (const CellCoordinates& index)
{ {
std::map<CellIndex, CellDescription>::iterator cell = mMap.find (index); std::map<CellCoordinates, CellDescription>::iterator cell = mMap.find (index);
if (cell!=mMap.end()) if (cell!=mMap.end())
{ {
@ -160,7 +160,7 @@ void CSMWorld::RegionMap::updateRegions (const std::vector<std::string>& regions
std::for_each (regions2.begin(), regions2.end(), &Misc::StringUtils::lowerCase); std::for_each (regions2.begin(), regions2.end(), &Misc::StringUtils::lowerCase);
std::sort (regions2.begin(), regions2.end()); std::sort (regions2.begin(), regions2.end());
for (std::map<CellIndex, CellDescription>::const_iterator iter (mMap.begin()); for (std::map<CellCoordinates, CellDescription>::const_iterator iter (mMap.begin());
iter!=mMap.end(); ++iter) iter!=mMap.end(); ++iter)
{ {
if (!iter->second.mRegion.empty() && if (!iter->second.mRegion.empty() &&
@ -176,90 +176,57 @@ void CSMWorld::RegionMap::updateRegions (const std::vector<std::string>& regions
void CSMWorld::RegionMap::updateSize() void CSMWorld::RegionMap::updateSize()
{ {
std::pair<CellIndex, CellIndex> size = getSize(); std::pair<CellCoordinates, CellCoordinates> size = getSize();
if (int diff = size.first.getX() - mMin.getX())
{ {
int diff = size.first.first - mMin.first; beginInsertColumns (QModelIndex(), 0, std::abs (diff)-1);
mMin = CellCoordinates (size.first.getX(), mMin.getY());
if (diff<0) endInsertColumns();
{
beginInsertColumns (QModelIndex(), 0, -diff-1);
mMin.first = size.first.first;
endInsertColumns();
}
else if (diff>0)
{
beginRemoveColumns (QModelIndex(), 0, diff-1);
mMin.first = size.first.first;
endRemoveColumns();
}
} }
if (int diff = size.first.getY() - mMin.getY())
{ {
int diff = size.first.second - mMin.second; beginInsertRows (QModelIndex(), 0, std::abs (diff)-1);
mMin = CellCoordinates (mMin.getX(), size.first.getY());
if (diff<0) endInsertRows();
{
beginInsertRows (QModelIndex(), 0, -diff-1);
mMin.second = size.first.second;
endInsertRows();
}
else if (diff>0)
{
beginRemoveRows (QModelIndex(), 0, diff-1);
mMin.second = size.first.second;
endRemoveRows();
}
} }
if (int diff = size.second.getX() - mMax.getX())
{ {
int diff = size.second.first - mMax.first; int columns = columnCount();
if (diff>0) if (diff>0)
{
int columns = columnCount();
beginInsertColumns (QModelIndex(), columns, columns+diff-1); beginInsertColumns (QModelIndex(), columns, columns+diff-1);
mMax.first = size.second.first; else
endInsertColumns();
}
else if (diff<0)
{
int columns = columnCount();
beginRemoveColumns (QModelIndex(), columns+diff, columns-1); beginRemoveColumns (QModelIndex(), columns+diff, columns-1);
mMax.first = size.second.first;
endRemoveColumns(); mMax = CellCoordinates (size.second.getX(), mMax.getY());
} endInsertColumns();
} }
if (int diff = size.second.getY() - mMax.getY())
{ {
int diff = size.second.second - mMax.second; int rows = rowCount();
if (diff>0) if (diff>0)
{
int rows = rowCount();
beginInsertRows (QModelIndex(), rows, rows+diff-1); beginInsertRows (QModelIndex(), rows, rows+diff-1);
mMax.second = size.second.second; else
endInsertRows();
}
else if (diff<0)
{
int rows = rowCount();
beginRemoveRows (QModelIndex(), rows+diff, rows-1); beginRemoveRows (QModelIndex(), rows+diff, rows-1);
mMax.second = size.second.second;
endRemoveRows(); mMax = CellCoordinates (mMax.getX(), size.second.getY());
} endInsertRows();
} }
} }
std::pair<CSMWorld::RegionMap::CellIndex, CSMWorld::RegionMap::CellIndex> CSMWorld::RegionMap::getSize() std::pair<CSMWorld::CellCoordinates, CSMWorld::CellCoordinates> CSMWorld::RegionMap::getSize() const
const
{ {
const IdCollection<Cell>& cells = mData.getCells(); const IdCollection<Cell>& cells = mData.getCells();
int size = cells.getSize(); int size = cells.getSize();
CellIndex min (0, 0); CellCoordinates min (0, 0);
CellIndex max (0, 0); CellCoordinates max (0, 0);
for (int i=0; i<size; ++i) for (int i=0; i<size; ++i)
{ {
@ -269,24 +236,24 @@ std::pair<CSMWorld::RegionMap::CellIndex, CSMWorld::RegionMap::CellIndex> CSMWor
if (cell2.isExterior()) if (cell2.isExterior())
{ {
CellIndex index (cell2.mData.mX, cell2.mData.mY); CellCoordinates index (cell2.mData.mX, cell2.mData.mY);
if (min==max) if (min==max)
{ {
min = index; min = index;
max = std::make_pair (min.first+1, min.second+1); max = min.move (1, 1);
} }
else else
{ {
if (index.first<min.first) if (index.getX()<min.getX())
min.first = index.first; min = CellCoordinates (index.getX(), min.getY());
else if (index.first>=max.first) else if (index.getX()>=max.getX())
max.first = index.first + 1; max = CellCoordinates (index.getX()+1, max.getY());
if (index.second<min.second) if (index.getY()<min.getY())
min.second = index.second; min = CellCoordinates (min.getX(), index.getY());
else if (index.second>=max.second) else if (index.getY()>=max.getY())
max.second = index.second + 1; max = CellCoordinates (max.getX(), index.getY() + 1);
} }
} }
} }
@ -323,7 +290,7 @@ int CSMWorld::RegionMap::rowCount (const QModelIndex& parent) const
if (parent.isValid()) if (parent.isValid())
return 0; return 0;
return mMax.second-mMin.second; return mMax.getY()-mMin.getY();
} }
int CSMWorld::RegionMap::columnCount (const QModelIndex& parent) const int CSMWorld::RegionMap::columnCount (const QModelIndex& parent) const
@ -331,7 +298,7 @@ int CSMWorld::RegionMap::columnCount (const QModelIndex& parent) const
if (parent.isValid()) if (parent.isValid())
return 0; return 0;
return mMax.first-mMin.first; return mMax.getX()-mMin.getX();
} }
QVariant CSMWorld::RegionMap::data (const QModelIndex& index, int role) const QVariant CSMWorld::RegionMap::data (const QModelIndex& index, int role) const
@ -343,7 +310,7 @@ QVariant CSMWorld::RegionMap::data (const QModelIndex& index, int role) const
{ {
/// \todo GUI class in non-GUI code. Needs to be addressed eventually. /// \todo GUI class in non-GUI code. Needs to be addressed eventually.
std::map<CellIndex, CellDescription>::const_iterator cell = std::map<CellCoordinates, CellDescription>::const_iterator cell =
mMap.find (getIndex (index)); mMap.find (getIndex (index));
if (cell!=mMap.end()) if (cell!=mMap.end())
@ -370,13 +337,13 @@ QVariant CSMWorld::RegionMap::data (const QModelIndex& index, int role) const
if (role==Qt::ToolTipRole) if (role==Qt::ToolTipRole)
{ {
CellIndex cellIndex = getIndex (index); CellCoordinates cellIndex = getIndex (index);
std::ostringstream stream; std::ostringstream stream;
stream << cellIndex.first << ", " << cellIndex.second; stream << cellIndex;
std::map<CellIndex, CellDescription>::const_iterator cell = std::map<CellCoordinates, CellDescription>::const_iterator cell =
mMap.find (cellIndex); mMap.find (cellIndex);
if (cell!=mMap.end()) if (cell!=mMap.end())
@ -408,9 +375,9 @@ QVariant CSMWorld::RegionMap::data (const QModelIndex& index, int role) const
if (role==Role_Region) if (role==Role_Region)
{ {
CellIndex cellIndex = getIndex (index); CellCoordinates cellIndex = getIndex (index);
std::map<CellIndex, CellDescription>::const_iterator cell = std::map<CellCoordinates, CellDescription>::const_iterator cell =
mMap.find (cellIndex); mMap.find (cellIndex);
if (cell!=mMap.end() && !cell->second.mRegion.empty()) if (cell!=mMap.end() && !cell->second.mRegion.empty())
@ -503,7 +470,7 @@ void CSMWorld::RegionMap::cellsAboutToBeRemoved (const QModelIndex& parent, int
if (cell2.isExterior()) if (cell2.isExterior())
{ {
CellIndex index (cell2.mData.mX, cell2.mData.mY); CellCoordinates index (cell2.mData.mX, cell2.mData.mY);
removeCell (index); removeCell (index);
} }

View File

@ -9,6 +9,7 @@
#include "record.hpp" #include "record.hpp"
#include "cell.hpp" #include "cell.hpp"
#include "cellcoordinates.hpp"
namespace CSMWorld namespace CSMWorld
{ {
@ -23,8 +24,6 @@ namespace CSMWorld
public: public:
typedef std::pair<int, int> CellIndex;
enum Role enum Role
{ {
Role_Region = Qt::UserRole Role_Region = Qt::UserRole
@ -44,27 +43,27 @@ namespace CSMWorld
}; };
Data& mData; Data& mData;
std::map<CellIndex, CellDescription> mMap; std::map<CellCoordinates, CellDescription> mMap;
CellIndex mMin; ///< inclusive CellCoordinates mMin; ///< inclusive
CellIndex mMax; ///< exclusive CellCoordinates mMax; ///< exclusive
std::map<std::string, unsigned int> mColours; ///< region ID, colour (RGBA) std::map<std::string, unsigned int> mColours; ///< region ID, colour (RGBA)
CellIndex getIndex (const QModelIndex& index) const; CellCoordinates getIndex (const QModelIndex& index) const;
///< Translates a Qt model index into a cell index (which can contain negative components) ///< Translates a Qt model index into a cell index (which can contain negative components)
QModelIndex getIndex (const CellIndex& index) const; QModelIndex getIndex (const CellCoordinates& index) const;
void buildRegions(); void buildRegions();
void buildMap(); void buildMap();
void addCell (const CellIndex& index, const CellDescription& description); void addCell (const CellCoordinates& index, const CellDescription& description);
///< May be called on a cell that is already in the map (in which case an update is ///< May be called on a cell that is already in the map (in which case an update is
// performed) // performed)
void addCells (int start, int end); void addCells (int start, int end);
void removeCell (const CellIndex& index); void removeCell (const CellCoordinates& index);
///< May be called on a cell that is not in the map (in which case the call is ignored) ///< May be called on a cell that is not in the map (in which case the call is ignored)
void addRegion (const std::string& region, unsigned int colour); void addRegion (const std::string& region, unsigned int colour);
@ -83,7 +82,7 @@ namespace CSMWorld
void updateSize(); void updateSize();
std::pair<CellIndex, CellIndex> getSize() const; std::pair<CellCoordinates, CellCoordinates> getSize() const;
public: public: