From 6d8ccd2216cc7264fcdefd6041a57504f772c8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sun, 17 Jan 2021 19:33:06 +0100 Subject: [PATCH] Unified path -> name conversion --- src/GUI/axisitem.cpp | 4 ++-- src/GUI/scaleitem.cpp | 10 +++++----- src/common/util.cpp | 11 +++++++++-- src/common/util.h | 10 ++++++++-- src/data/exifparser.cpp | 3 ++- src/data/igcparser.cpp | 28 ++++++++++++++-------------- src/data/nmeaparser.cpp | 28 ++++++++++++++-------------- src/map/angularunits.cpp | 8 ++++---- src/map/geotiffmap.cpp | 7 +------ src/map/geotiffmap.h | 2 -- src/map/jnxmap.cpp | 3 +-- src/map/jnxmap.h | 3 --- src/map/kmzmap.cpp | 7 +------ src/map/kmzmap.h | 2 -- src/map/map.h | 3 ++- src/map/mbtilesmap.cpp | 4 ++-- src/map/rmap.cpp | 6 ------ src/map/rmap.h | 2 -- 18 files changed, 65 insertions(+), 76 deletions(-) diff --git a/src/GUI/axisitem.cpp b/src/GUI/axisitem.cpp index 4049f548..a6ed930f 100644 --- a/src/GUI/axisitem.cpp +++ b/src/GUI/axisitem.cpp @@ -29,8 +29,8 @@ private: Ticks::Ticks(double minValue, double maxValue, int maxCount) { - double range = niceNum(maxValue - minValue, false); - _d = niceNum(range / maxCount, false); + double range = Util::niceNum(maxValue - minValue, false); + _d = Util::niceNum(range / maxCount, false); _min = ceil(minValue / _d) * _d; _max = floor(maxValue / _d) * _d; } diff --git a/src/GUI/scaleitem.cpp b/src/GUI/scaleitem.cpp index bc0e98b3..0c95f9ff 100644 --- a/src/GUI/scaleitem.cpp +++ b/src/GUI/scaleitem.cpp @@ -61,9 +61,9 @@ void ScaleItem::computeScale() qreal res = _res * pow(2, -_digitalZoom); if (_units == Imperial) { - _length = niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, true); + _length = Util::niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, true); if (_length >= MIINFT) { - _length = niceNum((res * M2MI * SCALE_WIDTH) / SEGMENTS, true); + _length = Util::niceNum((res * M2MI * SCALE_WIDTH) / SEGMENTS, true); _width = (_length / (res * M2MI)); _scale = true; } else { @@ -71,9 +71,9 @@ void ScaleItem::computeScale() _scale = false; } } else if (_units == Nautical) { - _length = niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, true); + _length = Util::niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, true); if (_length >= NMIINFT) { - _length = niceNum((res * M2NMI * SCALE_WIDTH) / SEGMENTS, true); + _length = Util::niceNum((res * M2NMI * SCALE_WIDTH) / SEGMENTS, true); _width = (_length / (res * M2NMI)); _scale = true; } else { @@ -81,7 +81,7 @@ void ScaleItem::computeScale() _scale = false; } } else { - _length = niceNum((res * SCALE_WIDTH) / SEGMENTS, true); + _length = Util::niceNum((res * SCALE_WIDTH) / SEGMENTS, true); if (_length >= KMINM) { _length *= M2KM; _width = (_length / (res * M2KM)); diff --git a/src/common/util.cpp b/src/common/util.cpp index 664f296a..7a198f5a 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -1,9 +1,10 @@ #include #include +#include #include "util.h" -int str2int(const char *str, int len) +int Util::str2int(const char *str, int len) { int res = 0; @@ -17,7 +18,7 @@ int str2int(const char *str, int len) return res; } -double niceNum(double x, bool round) +double Util::niceNum(double x, bool round) { int expv; double f; @@ -48,3 +49,9 @@ double niceNum(double x, bool round) return nf * pow(10.0, expv); } + +QString Util::file2name(const QString &path) +{ + QFileInfo fi(path); + return fi.baseName().replace('_', ' '); +} diff --git a/src/common/util.h b/src/common/util.h index a42367e5..62cf69c8 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -1,7 +1,13 @@ #ifndef UTIL_H #define UTIL_H -int str2int(const char *str, int len); -double niceNum(double x, bool round); +#include + +namespace Util +{ + int str2int(const char *str, int len); + double niceNum(double x, bool round); + QString file2name(const QString &path); +} #endif // UTIL_H diff --git a/src/data/exifparser.cpp b/src/data/exifparser.cpp index 9c257ff4..628bfc0c 100644 --- a/src/data/exifparser.cpp +++ b/src/data/exifparser.cpp @@ -2,6 +2,7 @@ #include #include #include "common/tifffile.h" +#include "common/util.h" #include "exifparser.h" @@ -211,7 +212,7 @@ bool EXIFParser::parseTIFF(QFile *file, QVector &waypoints) ImageInfo img(file->fileName(), QImageReader(file).size()); Waypoint wp(c); - wp.setName(QFileInfo(file->fileName()).baseName()); + wp.setName(Util::file2name(file->fileName())); wp.addImage(img); wp.setElevation(altitude(tiff, GPSIFD.value(GPSAltitude), GPSIFD.value(GPSAltitudeRef))); diff --git a/src/data/igcparser.cpp b/src/data/igcparser.cpp index 71f022d9..f8362990 100644 --- a/src/data/igcparser.cpp +++ b/src/data/igcparser.cpp @@ -5,9 +5,9 @@ static bool readLat(const char *data, qreal &lat) { - int d = str2int(data, 2); - int mi = str2int(data + 2, 2); - int mf = str2int(data + 4, 3); + int d = Util::str2int(data, 2); + int mi = Util::str2int(data + 2, 2); + int mf = Util::str2int(data + 4, 3); if (d < 0 || mi < 0 || mf < 0) return false; @@ -26,9 +26,9 @@ static bool readLat(const char *data, qreal &lat) static bool readLon(const char *data, qreal &lon) { - int d = str2int(data, 3); - int mi = str2int(data + 3, 2); - int mf = str2int(data + 5, 3); + int d = Util::str2int(data, 3); + int mi = Util::str2int(data + 3, 2); + int mf = Util::str2int(data + 5, 3); if (d < 0 || mi < 0 || mf < 0) return false; @@ -53,11 +53,11 @@ static bool readAltitude(const char *data, qreal &ele) return false; if (data[6] == '-') { - if ((ga = str2int(data + 7, 4)) < 0) + if ((ga = Util::str2int(data + 7, 4)) < 0) return false; ga = -ga; } else { - if ((ga = str2int(data + 6, 5)) < 0) + if ((ga = Util::str2int(data + 6, 5)) < 0) return false; } @@ -71,9 +71,9 @@ static bool readAltitude(const char *data, qreal &ele) static bool readTimestamp(const char *data, QTime &time) { - int h = str2int(data, 2); - int m = str2int(data + 2, 2); - int s = str2int(data + 4, 2); + int h = Util::str2int(data, 2); + int m = Util::str2int(data + 2, 2); + int s = Util::str2int(data + 4, 2); if (h < 0 || m < 0 || s < 0) return false; @@ -105,9 +105,9 @@ bool IGCParser::readHRecord(CTX &ctx, const char *line, int len) int offset = (len < 16 || ::strncmp(line + 5, "DATE:", 5)) ? 5 : 10; - int d = str2int(line + offset, 2); - int m = str2int(line + offset + 2, 2); - int y = str2int(line + offset + 4, 2); + int d = Util::str2int(line + offset, 2); + int m = Util::str2int(line + offset + 2, 2); + int y = Util::str2int(line + offset + 4, 2); if (y < 0 || m < 0 || d < 0) { _errorString = "Invalid date header format"; diff --git a/src/data/nmeaparser.cpp b/src/data/nmeaparser.cpp index 9dc4dacb..af603405 100644 --- a/src/data/nmeaparser.cpp +++ b/src/data/nmeaparser.cpp @@ -71,14 +71,14 @@ bool NMEAParser::readTime(const char *data, int len, QTime &time) if (len < 6) goto error; - h = str2int(data, 2); - m = str2int(data + 2, 2); - s = str2int(data + 4, 2); + h = Util::str2int(data, 2); + m = Util::str2int(data + 2, 2); + s = Util::str2int(data + 4, 2); if (h < 0 || m < 0 || s < 0) goto error; if (len > 7 && data[6] == '.') { - if ((ms = str2int(data + 7, len - 7)) < 0) + if ((ms = Util::str2int(data + 7, len - 7)) < 0) goto error; } @@ -106,9 +106,9 @@ bool NMEAParser::readDate(const char *data, int len, QDate &date) if (len < 6) goto error; - d = str2int(data, 2); - m = str2int(data + 2, 2); - y = str2int(data + 4, len - 4); + d = Util::str2int(data, 2); + m = Util::str2int(data + 2, 2); + y = Util::str2int(data + 4, len - 4); if (d < 0 || m < 0 || y < 0) goto error; @@ -142,8 +142,8 @@ bool NMEAParser::readLat(const char *data, int len, qreal &lat) if (len < 7 || data[4] != '.') goto error; - d = str2int(data, 2); - mi = str2int(data + 2, 2); + d = Util::str2int(data, 2); + mi = Util::str2int(data + 2, 2); mf = QString(QByteArray::fromRawData(data + 4, len - 4)).toFloat(&ok); if (d < 0 || mi < 0 || !ok) goto error; @@ -192,8 +192,8 @@ bool NMEAParser::readLon(const char *data, int len, qreal &lon) if (len < 8 || data[5] != '.') goto error; - d = str2int(data, 3); - mi = str2int(data + 3, 2); + d = Util::str2int(data, 3); + mi = Util::str2int(data + 3, 2); mf = QString(QByteArray::fromRawData(data + 5, len - 5)).toFloat(&ok); if (d < 0 || mi < 0 || !ok) goto error; @@ -437,7 +437,7 @@ bool NMEAParser::readZDA(CTX &ctx, const char *line, int len) case 2: if (!(lp - vp)) return true; - if ((d = str2int(vp, lp - vp)) < 0) { + if ((d = Util::str2int(vp, lp - vp)) < 0) { _errorString = "Invalid day"; return false; } @@ -445,7 +445,7 @@ bool NMEAParser::readZDA(CTX &ctx, const char *line, int len) case 3: if (!(lp - vp)) return true; - if ((m = str2int(vp, lp - vp)) < 0) { + if ((m = Util::str2int(vp, lp - vp)) < 0) { _errorString = "Invalid month"; return false; } @@ -453,7 +453,7 @@ bool NMEAParser::readZDA(CTX &ctx, const char *line, int len) case 4: if (!(lp - vp)) return true; - if ((y = str2int(vp, lp - vp)) < 0) { + if ((y = Util::str2int(vp, lp - vp)) < 0) { _errorString = "Invalid year"; return false; } diff --git a/src/map/angularunits.cpp b/src/map/angularunits.cpp index 3e41d414..f5d90af9 100644 --- a/src/map/angularunits.cpp +++ b/src/map/angularunits.cpp @@ -11,10 +11,10 @@ static double sDMS2deg(double val) QByteArray ba = qstr.toLatin1(); const char *str = ba.constData(); decimal = strrchr(str, '.'); - int deg = str2int(str, decimal - str); - int min = str2int(decimal + 1, 2); - int sec = str2int(decimal + 3, 2); - int f = str2int(decimal + 5, 3); + int deg = Util::str2int(str, decimal - str); + int min = Util::str2int(decimal + 1, 2); + int sec = Util::str2int(decimal + 3, 2); + int f = Util::str2int(decimal + 5, 3); angle = deg + min/60.0 + sec/3600.0 + (f/1000.0)/3600.0; diff --git a/src/map/geotiffmap.cpp b/src/map/geotiffmap.cpp index d89d7287..fee6d176 100644 --- a/src/map/geotiffmap.cpp +++ b/src/map/geotiffmap.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "common/util.h" #include "geotiff.h" #include "image.h" #include "geotiffmap.h" @@ -33,12 +34,6 @@ GeoTIFFMap::~GeoTIFFMap() delete _img; } -QString GeoTIFFMap::name() const -{ - QFileInfo fi(path()); - return fi.fileName(); -} - QPointF GeoTIFFMap::ll2xy(const Coordinates &c) { return QPointF(_transform.proj2img(_projection.ll2xy(c))) / _ratio; diff --git a/src/map/geotiffmap.h b/src/map/geotiffmap.h index 957994f7..85761720 100644 --- a/src/map/geotiffmap.h +++ b/src/map/geotiffmap.h @@ -15,8 +15,6 @@ public: GeoTIFFMap(const QString &fileName, QObject *parent = 0); ~GeoTIFFMap(); - QString name() const; - QRectF bounds(); QPointF ll2xy(const Coordinates &c); Coordinates xy2ll(const QPointF &p); diff --git a/src/map/jnxmap.cpp b/src/map/jnxmap.cpp index 8d425855..d3390c0e 100644 --- a/src/map/jnxmap.cpp +++ b/src/map/jnxmap.cpp @@ -2,7 +2,7 @@ #include #include #include -#include "common/config.h" +#include "common/util.h" #include "rectd.h" #include "gcs.h" #include "pcs.h" @@ -134,7 +134,6 @@ JNXMap::JNXMap(const QString &fileName, QObject *parent) : Map(fileName, parent), _file(fileName), _zoom(0), _mapRatio(1.0), _valid(false) { - _name = QFileInfo(fileName).fileName(); _projection = Projection(GCS::gcs(4326)); if (!_file.open(QIODevice::ReadOnly)) { diff --git a/src/map/jnxmap.h b/src/map/jnxmap.h index 5b5e5fe7..39063215 100644 --- a/src/map/jnxmap.h +++ b/src/map/jnxmap.h @@ -17,8 +17,6 @@ public: public: JNXMap(const QString &fileName, QObject *parent = 0); - QString name() const {return _name;} - QRectF bounds(); RectC llBounds() {return _bounds;} @@ -66,7 +64,6 @@ private: static bool cb(Tile *tile, void *context); static QPixmap pixmap(const Tile *tile, QFile *file); - QString _name; QFile _file; QVector _zooms; int _zoom; diff --git a/src/map/kmzmap.cpp b/src/map/kmzmap.cpp index 24c59860..1c66c10c 100644 --- a/src/map/kmzmap.cpp +++ b/src/map/kmzmap.cpp @@ -18,6 +18,7 @@ #include #include #include +#include"common/util.h" #include "pcs.h" #include "image.h" #include "kmzmap.h" @@ -339,12 +340,6 @@ KMZMap::KMZMap(const QString &fileName, QObject *parent) _valid = true; } -QString KMZMap::name() const -{ - QFileInfo fi(path()); - return fi.fileName(); -} - QRectF KMZMap::bounds() { QRectF rect; diff --git a/src/map/kmzmap.h b/src/map/kmzmap.h index 325cc972..aee1980e 100644 --- a/src/map/kmzmap.h +++ b/src/map/kmzmap.h @@ -17,8 +17,6 @@ class KMZMap : public Map public: KMZMap(const QString &fileName, QObject *parent = 0); - QString name() const; - QRectF bounds(); int zoom() const {return _zoom;} diff --git a/src/map/map.h b/src/map/map.h index ab172cec..5281808f 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -6,6 +6,7 @@ #include #include #include "common/rectc.h" +#include "common/util.h" class QPainter; @@ -28,7 +29,7 @@ public: virtual ~Map() {} const QString &path() const {return _path;} - virtual QString name() const = 0; + virtual QString name() const {return Util::file2name(path());} virtual RectC llBounds(); virtual QRectF bounds() = 0; diff --git a/src/map/mbtilesmap.cpp b/src/map/mbtilesmap.cpp index 67446389..34a82205 100644 --- a/src/map/mbtilesmap.cpp +++ b/src/map/mbtilesmap.cpp @@ -8,7 +8,7 @@ #include #include #include "common/rectc.h" -#include "common/config.h" +#include "common/util.h" #include "osm.h" #include "mbtilesmap.h" @@ -147,7 +147,7 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent) _name = query.value(0).toString(); else { qWarning("%s: missing map name", qPrintable(fileName)); - _name = QFileInfo(fileName).fileName(); + _name = Util::file2name(fileName); } } diff --git a/src/map/rmap.cpp b/src/map/rmap.cpp index 9e562750..19faadd0 100644 --- a/src/map/rmap.cpp +++ b/src/map/rmap.cpp @@ -256,12 +256,6 @@ RMap::RMap(const QString &fileName, QObject *parent) _file.close(); } -QString RMap::name() const -{ - QFileInfo fi(path()); - return fi.baseName(); -} - QRectF RMap::bounds() { return QRectF(QPointF(0, 0), _zooms.at(_zoom).size / _mapRatio); diff --git a/src/map/rmap.h b/src/map/rmap.h index f3e597e5..5f7fd458 100644 --- a/src/map/rmap.h +++ b/src/map/rmap.h @@ -14,8 +14,6 @@ class RMap : public Map public: RMap(const QString &fileName, QObject *parent = 0); - QString name() const; - QRectF bounds(); int zoom() const {return _zoom;}