7 auto zoomToResolution = [&](
const double zoom) {
8 return std::max(std::min(zoom / 1.5,
static_cast<double>(
maxZoom_c)), 0.0);
11 zoomToRes_.emplace(zoom, std::floor(zoomToResolution(zoom)));
22 return static_cast<int>(
cells_.size());
26 if (!index.isValid() || index.row() >=
cells_.size()) {
33 return QVariant::fromValue(
data->res());
35 return QVariant::fromValue(
data->zoom());
37 return QVariant::fromValue(
data->order());
39 return QVariant::fromValue(
data->index());
41 return QVariant::fromValue(
data->color());
43 return QVariant::fromValue(
data->path());
45 return QVariant::fromValue(
data->coordinate());
64 std::vector<H3Index> indexes;
65 indexes.reserve(
cells_.size());
66 for (
const auto &cell :
cells_) {
67 indexes.emplace_back(cell->index());
72 if (from < 0 || from >=
cells_.size() || to < 0 || to >=
cells_.size() || from == to)
76 const int destinationRow = to > from ? to + 1 : to;
81 beginMoveRows(QModelIndex(), from, from, QModelIndex(), destinationRow);
90 const int start = std::min(from, to);
91 const int end = std::max(from, to);
92 for (
int i = start; i <= end; ++i) {
93 cells_[i]->setOrder(
static_cast<quint16
>(i + 1));
96 emit dataChanged(index(start), index(end), {
OrderRole});
104 if (row < 0 || row >=
cells_.size())
111 beginRemoveRows(QModelIndex(), row, row);
117 for (
int i = row; i <
cells_.size(); ++i) {
118 cells_[i]->setOrder(
static_cast<quint16
>(i + 1));
122 if (row <
cells_.size()) {
123 emit dataChanged(index(row), index(
cells_.size() - 1), {OrderRole});
124 }
else if (
cells_.isEmpty()) {
126 emit dataChanged(index(0), index(0), {
OrderRole});
132 std::vector<H3Index> indexes;
133 indexes.reserve(
cells_.size());
134 for (
const auto &c :
cells_) {
135 indexes.emplace_back(c->index());
156 }
catch (
const std::out_of_range &err) {
157 spdlog::error(
"Выбран недопустимый зум под разрешение {}", err.what());
163 const auto message = QString(
"Cannot add target: point is outside the allowed area!");
169 H3Index h3Index = H3_NULL;
170 const LatLng ll{.lat = degsToRads(coordinate.latitude()), .lng = degsToRads(coordinate.longitude())};
171 if (
const auto errIdx = latLngToCell(&ll, res, &h3Index); errIdx != E_SUCCESS || h3Index == H3_NULL) {
172 spdlog::warn(
"Impossible to convert this lat:{} lng:{} coordinate to H3Index {}", coordinate.latitude(),
173 coordinate.longitude(), errIdx);
179 const auto message = QString(
"Cannot add target: this cell is a wall!");
184 auto comp = [h3Index](
const H3Target *cell) {
return cell->index() == h3Index; };
185 if (
const auto isUnique = std::ranges::find_if(
cells_, comp); isUnique !=
cells_.end()) {
190 if (!polygon.has_value()) {
196 cell->setZoom(mapZoom);
197 cell->setIndex(h3Index);
198 cell->setCoordinate(coordinate);
199 cell->setPath(polygon.value());
200 cell->setColor(
"red");
202 beginInsertRows(QModelIndex(),
static_cast<int>(
cells_.size()),
static_cast<int>(
cells_.size()));
203 cells_.emplace_back(cell);
206 cell->setOrder(
static_cast<quint16
>(
cells_.size()));
217 spdlog::info(
"Already clearing, skipping...");
221 spdlog::info(
"Starting clearAllCells, count: {}",
cells_.size());
236 if (!coordinate.isValid()) {
248 spdlog::info(
"H3TargetsModel: Maze walls updated, {} wall cells",
mazeWalls_.size());
254 spdlog::info(
"H3TargetsModel: Maze bounds set - center ({}, {}), radius {} meters", center.latitude(),
255 center.longitude(), radiusMeters);
void onCompute(const std::vector< H3Index > &indexes)
void setMazeWalls(const std::unordered_set< H3Index > &walls)
QList< H3Target * > cells_
void setMazeBounds(const QGeoCoordinate ¢er, double radiusMeters)
qsizetype remove(int row)
std::unordered_map< uint8_t, uint8_t > zoomToRes_
std::unordered_set< H3Index > mazeWalls_
H3TargetsModel(QObject *parent=nullptr)
void showNotification(const QString &message, const QString &type)
QVariant data(const QModelIndex &index, int role) const override
bool isCoordinateTargetValid(quint8 zoom, const QGeoCoordinate &coordinate) const
QHash< int, QByteArray > roleNames() const override
std::atomic_bool isClearing_
public::void onRemoveCell(const std::vector< H3Index > &indexes)
QGeoCoordinate mazeCenter_
void move(int from, int to)
int rowCount(const QModelIndex &parent) const override
~H3TargetsModel() override
void requestCell(quint8 mapZoom, const QGeoCoordinate &coordinate)
static std::optional< QVariantList > indexToPolygon(const H3Index index)