qHexWalker 0.0.1
Hexagonal Grid Pathfinding & Maze Visualization on Interactive Maps
Loading...
Searching...
No Matches
mapProvider.cpp
Go to the documentation of this file.
1#include "mapProvider.h"
2
3#include <QSqlError>
4#include <QSqlQuery>
5#include <QTemporaryFile>
6
7MapProvider::MapProvider(QObject *parent) : QObject(parent) { tempStyleFile_ = new QTemporaryFile(this); }
8
10
12 // Сначала проверим, существует ли файл
13 if (!QFile::exists(filePath)) {
14 spdlog::critical("{} {}", "Карта maplibre.mbtiles не существует:", filePath.toStdString());
16 }
17
18 // Подключаемся к базе
19 const QString connectionName = "temp_connection";
20 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
21 db.setDatabaseName(filePath);
22
23 if (!db.open()) {
24 spdlog::critical("{} {}", "Не удалось открыть maplibre.mbtiles:", db.lastError().text().toStdString());
25 QSqlDatabase::removeDatabase(connectionName);
27 }
28
29 bool isValid = true;
30 {
31 if (QSqlQuery query(db); !query.exec("PRAGMA integrity_check;")) {
32 spdlog::critical("{} {}", "Не удалось выполнить проверку целостности карты map.mbtiles:",
33 db.lastError().text().toStdString());
34 isValid = false;
35 } else {
36 while (query.next()) {
37 if (QString result = query.value(0).toString(); result != "ok") {
38 spdlog::critical("{} {}", "Карта maplibre.mbtiles повреждена:", result.toStdString());
39 isValid = false;
40 break;
41 }
42 }
43 }
44 }
45
46 db.close();
47 if (!isValid) {
49 }
50
52}
53
54void MapProvider::exchangeUrlOffline(const QString &pathToMap) {
55 QFile file;
56 file.setFileName(QStringLiteral(":/QHexWalker/data/style.json"));
57
58 auto isOpened = file.open(QIODevice::ReadOnly);
59 if (!isOpened) {
60 return;
61 }
62 QByteArray data = file.readAll();
63 file.close();
64
65 data.replace("%URL%", pathToMap.toUtf8());
66
67 tempStyleFile_ = new QTemporaryFile(this);
68 isOpened = tempStyleFile_->open();
69 if (!isOpened) {
70 return;
71 }
72 tempStyleFile_->write(data);
73 tempStyleFile_->close();
74
75 setUrl("file:///" + tempStyleFile_->fileName());
76 // setUrl(/*"file:///" + */ tempStyleFile_->fileName());
77}
static MapProviderError isSQLiteFileValidOffline(const QString &filePath)
QTemporaryFile * tempStyleFile_
Definition mapProvider.h:34
~MapProvider() override
void setUrl(const QString &url) noexcept
Definition mapProvider.h:22
MapProvider(QObject *parent=nullptr)
void exchangeUrlOffline(const QString &pathToMap)
MapProviderError
Definition mapProvider.h:6