PIQt now provide PICout << operators for Qt types

MapView::setTileObsoleteTime() method. You can set maximum lifetime for tile, after that it will be redownloaded
This commit is contained in:
2024-09-28 09:15:25 +03:00
parent 2eacbfbfc1
commit dd4695780d
7 changed files with 128 additions and 6 deletions

View File

@@ -92,10 +92,18 @@ void MapView::setCachePath(const QString & p) {
} }
void collectDownloadIndeces(OSMTileCache * cache, QQueue<OSM::TileIndex> & indeces, OSM::TileIndex index, int target_zoom_level, bool only_new) { void MapView::setTileObsoleteTime(int secs) {
cache->setTileObsoleteTime(secs);
}
void collectDownloadIndeces(OSMTileCache * cache,
QQueue<OSM::TileIndex> & indeces,
OSM::TileIndex index,
int target_zoom_level,
bool only_new) {
if (only_new) { if (only_new) {
if (!cache->isTileFileExists(index)) if (!cache->isTileFileExists(index)) indeces << index;
indeces << index;
} else } else
indeces << index; indeces << index;
if (index.z >= target_zoom_level) return; if (index.z >= target_zoom_level) return;
@@ -205,7 +213,7 @@ void MapView::mouseMoveEvent(QMouseEvent * e) {
} }
} }
if (e->buttons() == Qt::RightButton) { if (e->buttons() == Qt::RightButton) {
auto dp = e->pos() - press_point; auto dp = e->pos() - press_point;
press_point = e->pos(); press_point = e->pos();
zoom(1. - dp.y() / 50., zoom_anchor); zoom(1. - dp.y() / 50., zoom_anchor);
} }

View File

@@ -61,6 +61,8 @@ public:
QString cachePath() const; QString cachePath() const;
void setCachePath(const QString & p); void setCachePath(const QString & p);
void setTileObsoleteTime(int secs);
void downloadCurrentView(int target_zoom_level = 17, bool only_new = true); void downloadCurrentView(int target_zoom_level = 17, bool only_new = true);
protected: protected:

View File

@@ -31,8 +31,10 @@ void OSMTileCache::tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap)
OSM::TilePixmap OSMTileCache::getTile(OSM::TileIndex index, QRectF & rect_src) { OSM::TilePixmap OSMTileCache::getTile(OSM::TileIndex index, QRectF & rect_src) {
OSM::TilePixmap ret = getTileFromCache(index); OSM::TilePixmap ret = getTileFromCache(index);
if (ret.isEmpty()) { if (ret.isEmpty() || ret.obsolete) {
parent->downloader->queueTile(index); parent->downloader->queueTile(index);
}
if (ret.isEmpty()) {
QVector<QPointF> offsets; QVector<QPointF> offsets;
while (index.z >= 0) { while (index.z >= 0) {
index.z--; index.z--;
@@ -81,10 +83,15 @@ OSM::TilePixmap OSMTileCache::getTileFromCache(OSM::TileIndex index) {
if (cache_dir.exists(hashdir)) { if (cache_dir.exists(hashdir)) {
QString hashname = hashdir + "/" + index.hashName(); QString hashname = hashdir + "/" + index.hashName();
if (cache_dir.exists(hashname)) { if (cache_dir.exists(hashname)) {
ret.pixmap.load(cache_dir.absoluteFilePath(hashname), "png"); QString fname = cache_dir.absoluteFilePath(hashname);
ret.pixmap.load(fname, "png");
tile = new OSM::TilePixmap(); tile = new OSM::TilePixmap();
tile->index = index; tile->index = index;
tile->pixmap = ret.pixmap; tile->pixmap = ret.pixmap;
if (secs_obsolete > 0) {
int secs_old = QFileInfo(fname).lastModified().secsTo(QDateTime::currentDateTime());
ret.obsolete = secs_old >= secs_obsolete;
}
tile_cache.insert(tile->index.hash(), tile, tile->pixmapBytes()); tile_cache.insert(tile->index.hash(), tile, tile->pixmapBytes());
return ret; return ret;
} }

View File

@@ -46,6 +46,7 @@ public:
add_size_b = mb * 1024 * 1024; add_size_b = mb * 1024 * 1024;
updateCacheSize(); updateCacheSize();
} }
void setTileObsoleteTime(int secs) { secs_obsolete = secs; }
void tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap); void tileDownloaded(OSM::TileIndex index, const QPixmap & pixmap);
OSM::TilePixmap getTile(OSM::TileIndex index, QRectF & rect_src); OSM::TilePixmap getTile(OSM::TileIndex index, QRectF & rect_src);
@@ -67,6 +68,7 @@ private:
QDir cache_dir; QDir cache_dir;
QWaitCondition cond; QWaitCondition cond;
QMutex cond_mutex; QMutex cond_mutex;
int secs_obsolete = -1;
int fixed_size_b = 0, add_size_b = 0; int fixed_size_b = 0, add_size_b = 0;
QQueue<OSM::TilePixmap> queue; QQueue<OSM::TilePixmap> queue;
QCache<quint64, OSM::TilePixmap> tile_cache; QCache<quint64, OSM::TilePixmap> tile_cache;

View File

@@ -36,6 +36,7 @@ struct TileIndex {
struct TilePixmap { struct TilePixmap {
TileIndex index; TileIndex index;
QPixmap pixmap; QPixmap pixmap;
bool obsolete = false;
bool isEmpty() const; bool isEmpty() const;
int pixmapBytes() const; int pixmapBytes() const;
}; };

View File

@@ -794,4 +794,104 @@ T piqDeserialize(const QByteArray & data) {
} }
/// PICout with Qt
inline PICout operator<<(PICout s, const QVariant & v) {
s << Q2PIVariant(v);
return s;
}
inline PICout operator<<(PICout s, const QByteArray & v) {
s << Q2PIByteArray(v);
return s;
}
inline PICout operator<<(PICout s, const QString & v) {
s << Q2PIString(v);
return s;
}
inline PICout operator<<(PICout s, const QStringList & v) {
s << Q2PIStringList(v);
return s;
}
#if QT_VERSION_MAJOR == 5
template<typename T>
inline PICout operator<<(PICout s, const QVector<T> & v) {
s << Q2PIVector(v);
return s;
}
#endif
template<typename T>
inline PICout operator<<(PICout s, const QList<T> & v) {
s << Q2PIVector(v);
return s;
}
template<typename K, typename T>
inline PICout operator<<(PICout s, const QMap<K, T> & v) {
s << Q2PIMap(v);
return s;
}
inline PICout operator<<(PICout s, const QTime & v) {
s << Q2PITime(v);
return s;
}
inline PICout operator<<(PICout s, const QDate & v) {
s << Q2PIDate(v);
return s;
}
inline PICout operator<<(PICout s, const QDateTime & v) {
s << Q2PIDateTime(v);
return s;
}
inline PICout operator<<(PICout s, const QPoint & v) {
s << Q2PIPoint(v);
return s;
}
inline PICout operator<<(PICout s, const QPointF & v) {
s << Q2PIPoint(v);
return s;
}
inline PICout operator<<(PICout s, const QLine & v) {
s << Q2PILine(v);
return s;
}
inline PICout operator<<(PICout s, const QLineF & v) {
s << Q2PILine(v);
return s;
}
inline PICout operator<<(PICout s, const QRect & v) {
s << Q2PIRect(v);
return s;
}
inline PICout operator<<(PICout s, const QRectF & v) {
s << Q2PIRect(v);
return s;
}
inline PICout operator<<(PICout s, const QColor & v) {
s << Q2PIColor(v);
return s;
}
#ifdef PIQT_HAS_GEOPOSITION
inline PICout operator<<(PICout s, const QGeoCoordinate & v) {
s << Q2PIGeoPosition(v);
return s;
}
#endif
#endif // PIQT_H #endif // PIQT_H

View File

@@ -7,6 +7,7 @@
#include "mapitempolygon.h" #include "mapitempolygon.h"
#include "mapitemtext.h" #include "mapitemtext.h"
#include "osm_geocoding.h" #include "osm_geocoding.h"
#include "piliterals_time.h"
#include "piqt.h" #include "piqt.h"
#include "qad_locations.h" #include "qad_locations.h"
@@ -18,6 +19,7 @@
MainWindow::MainWindow(QWidget * parent): EMainWindow(parent), Ui::MainWindow() { MainWindow::MainWindow(QWidget * parent): EMainWindow(parent), Ui::MainWindow() {
setupUi(this); setupUi(this);
map->setTileObsoleteTime((5_d).toSeconds());
session.setFile(QAD::userPath(QAD::ltConfig, "session_mapviewer")); session.setFile(QAD::userPath(QAD::ltConfig, "session_mapviewer"));
session.addEntry(this); session.addEntry(this);
session.load(); session.load();