Commit 8cfb48f9 authored by Yuya Nishihara's avatar Yuya Nishihara
Browse files

preallocate vacant tile

parent 9038f6b1
......@@ -42,7 +42,10 @@ Window {
required property int tileIndex
columns: 2
visible: name
Component.onCompleted: {
if (!name)
return;
dummyField.text = tile.name + ": " + tile.index;
}
......
#include <QtDebug>
#include <algorithm>
#include <numeric>
#include "tilelistmodel.h"
namespace {
......@@ -66,15 +68,58 @@ void TileListModel::resetLayout(Qt::Orientation majorOrientation, const QVector<
{
m_majorOrientation = majorOrientation;
m_minorCounts = minorCounts;
// Collect known tiles, unassign if tile index is out of range.
const int newTileCount = std::reduce(m_minorCounts.begin(), m_minorCounts.end(), 0);
std::vector<bool> knownTileSet(static_cast<size_t>(newTileCount), false);
int knownTileCount = 0;
for (size_t i = 0; i < m_tiles.size(); ++i) {
auto &t = m_tiles.at(i);
if (t.tileIndex < 0)
continue;
if (t.tileIndex >= newTileCount) {
t.tileIndex = -1;
} else {
Q_ASSERT(!knownTileSet.at(static_cast<size_t>(t.tileIndex)));
knownTileSet.at(static_cast<size_t>(t.tileIndex)) = true;
++knownTileCount;
}
}
// TODO: strip or reuse vacant tiles
// Allocate vacant tiles for new indices.
if (knownTileCount < newTileCount) {
const int newCount = static_cast<int>(m_tiles.size()) + (newTileCount - knownTileCount);
beginInsertRows(QModelIndex(), static_cast<int>(m_tiles.size()), newCount - 1);
for (int i = 0; i < newTileCount; ++i) {
if (knownTileSet.at(static_cast<size_t>(i)))
continue;
m_tiles.push_back({ {}, i });
}
endInsertRows();
Q_ASSERT(m_tiles.size() == static_cast<size_t>(newCount));
}
emitAllDataChanged({ TileIndexRole });
emit layoutReset();
}
void TileListModel::assignTileItem(const QString &name)
{
const int row = static_cast<int>(m_tiles.size());
beginInsertRows(QModelIndex(), row, row);
m_tiles.push_back({ name, row }); // TODO: assign free tile index
endInsertRows();
const auto p = std::find_if(m_tiles.begin(), m_tiles.end(),
[](const auto &t) { return t.name.isEmpty(); });
if (p != m_tiles.end()) {
p->name = name;
const int row = static_cast<int>(p - m_tiles.begin());
emit dataChanged(index(row), index(row), { NameRole });
} else {
// no vacant tile
const int row = static_cast<int>(m_tiles.size());
beginInsertRows(QModelIndex(), row, row);
m_tiles.push_back({ name, -1 });
endInsertRows();
}
}
void TileListModel::setTileIndex(int row, int tileIndex)
......@@ -95,3 +140,11 @@ void TileListModel::setTileIndex(int row, int tileIndex)
tile.tileIndex = tileIndex;
emit dataChanged(index(row), index(row), { TileIndexRole });
}
void TileListModel::emitAllDataChanged(const QVector<int> &roles)
{
if (m_tiles.empty())
return;
const int last = static_cast<int>(m_tiles.size()) - 1;
emit dataChanged(index(0), index(last), roles);
}
......@@ -38,6 +38,8 @@ private:
int tileIndex;
};
void emitAllDataChanged(const QVector<int> &roles);
Qt::Orientation m_majorOrientation;
QVector<int> m_minorCounts;
std::vector<Tile> m_tiles;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment