diff --git a/lib/include/QGeoView/QGVGlobal.h b/lib/include/QGeoView/QGVGlobal.h old mode 100755 new mode 100644 index fe64c01..2e24441 --- a/lib/include/QGeoView/QGVGlobal.h +++ b/lib/include/QGeoView/QGVGlobal.h @@ -54,6 +54,14 @@ enum class MapState SelectionRect, }; +enum class DistanceUnits +{ + Meters, + Kilometers, + NauticalMiles, + Miles, +}; + enum class MouseAction : int { Move = 0x1, diff --git a/lib/include/QGeoView/QGVUtils.h b/lib/include/QGeoView/QGVUtils.h new file mode 100755 index 0000000..f58eb4b --- /dev/null +++ b/lib/include/QGeoView/QGVUtils.h @@ -0,0 +1,28 @@ +/*************************************************************************** + * QGeoView is a Qt / C ++ widget for visualizing geographic data. + * Copyright (C) 2018-2020 Andrey Yaroshenko. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, see https://www.gnu.org/licenses. + ****************************************************************************/ + +#pragma once + +#include + +namespace QGV { + +QGV_LIB_DECL double metersToDistance(const double meters, const DistanceUnits unit); +QGV_LIB_DECL QString unitToString(const DistanceUnits unit); + +} // namespace QGV diff --git a/lib/include/QGeoView/QGVWidgetScale.h b/lib/include/QGeoView/QGVWidgetScale.h index a7336d5..4c0bcf9 100644 --- a/lib/include/QGeoView/QGVWidgetScale.h +++ b/lib/include/QGeoView/QGVWidgetScale.h @@ -33,9 +33,15 @@ class QGV_LIB_DECL QGVWidgetScale : public QGVWidget void setOrientation(Qt::Orientation orientation); Qt::Orientation getOrientation() const; - QString getDistanceLabel(int meters, int accuracy = 0) const; + void setDistanceUnits(QGV::DistanceUnits distanceUnits); + QGV::DistanceUnits getDistanceUnits() const; + + void setUseMetersForSmallDistance(bool useMetersForSmallDistance); + bool getUseMetersForSmallDistance() const; + +protected: + QString getDistanceLabel(int meters) const; -private: void onCamera(const QGVCameraState& oldState, const QGVCameraState& newState) override; void paintEvent(QPaintEvent* event) override; @@ -44,4 +50,6 @@ class QGV_LIB_DECL QGVWidgetScale : public QGVWidget bool mAutoAdjust; int mScaleMeters; int mScalePixels; + QGV::DistanceUnits mDistanceUnits; + bool mUseMetersForSmallDistance; }; diff --git a/lib/lib.pri b/lib/lib.pri index 81133c1..23b9405 100644 --- a/lib/lib.pri +++ b/lib/lib.pri @@ -6,6 +6,7 @@ SOURCES += \ $$PWD/src/QGVCamera.cpp \ $$PWD/src/QGVDrawItem.cpp \ $$PWD/src/QGVGlobal.cpp \ + $$PWD/src/QGVUtils.cpp \ $$PWD/src/QGVImage.cpp \ $$PWD/src/QGVItem.cpp \ $$PWD/src/QGVLayer.cpp \ @@ -30,6 +31,7 @@ HEADERS += \ $$PWD/include/QGeoView/QGVCamera.h \ $$PWD/include/QGeoView/QGVDrawItem.h \ $$PWD/include/QGeoView/QGVGlobal.h \ + $$PWD/include/QGeoView/QGVUtils.h \ $$PWD/include/QGeoView/QGVImage.h \ $$PWD/include/QGeoView/QGVItem.h \ $$PWD/include/QGeoView/QGVLayer.h \ diff --git a/lib/src/QGVUtils.cpp b/lib/src/QGVUtils.cpp new file mode 100644 index 0000000..9e37d3a --- /dev/null +++ b/lib/src/QGVUtils.cpp @@ -0,0 +1,50 @@ +/*************************************************************************** + * QGeoView is a Qt / C ++ widget for visualizing geographic data. + * Copyright (C) 2018-2020 Andrey Yaroshenko. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, see https://www.gnu.org/licenses. + ****************************************************************************/ + +#include "QGVUtils.h" +#include + +namespace QGV { + +double metersToDistance(const double meters, const DistanceUnits unit) +{ + if (unit == DistanceUnits::Kilometers) { + return meters / 1000; + } else if (unit == DistanceUnits::NauticalMiles) { + return meters / 1852.0; + } else if (unit == DistanceUnits::Miles) { + return meters / 1609.0; + } + return meters; +} + +QString unitToString(const DistanceUnits unit) +{ + if (unit == DistanceUnits::Meters) { + return QT_TRANSLATE_NOOP("QGV", "m"); + } else if (unit == DistanceUnits::Kilometers) { + return QT_TRANSLATE_NOOP("QGV", "km"); + } else if (unit == DistanceUnits::NauticalMiles) { + return QT_TRANSLATE_NOOP("QGV", "nm"); + } else if (unit == DistanceUnits::Miles) { + return QT_TRANSLATE_NOOP("QGV", "mi"); + } + return ""; +} + +} // namespace QGV diff --git a/lib/src/QGVWidgetScale.cpp b/lib/src/QGVWidgetScale.cpp index 8245516..cffc15b 100644 --- a/lib/src/QGVWidgetScale.cpp +++ b/lib/src/QGVWidgetScale.cpp @@ -18,6 +18,7 @@ #include "QGVWidgetScale.h" #include "QGVMapQGView.h" +#include "QGVUtils.h" #include #include @@ -30,6 +31,9 @@ const int minLengthPixel = 130; QGVWidgetScale::QGVWidgetScale(Qt::Orientation orientation) { + mDistanceUnits = QGV::DistanceUnits::Kilometers; + mUseMetersForSmallDistance = true; + mAutoAdjust = true; mScaleMeters = 0; mScalePixels = 0; @@ -75,12 +79,38 @@ Qt::Orientation QGVWidgetScale::getOrientation() const return mOrientation; } -QString QGVWidgetScale::getDistanceLabel(int meters, int accuracy) const +void QGVWidgetScale::setDistanceUnits(QGV::DistanceUnits distanceUnits) +{ + mDistanceUnits = distanceUnits; +} + +QGV::DistanceUnits QGVWidgetScale::getDistanceUnits() const +{ + return mDistanceUnits; +} + +void QGVWidgetScale::setUseMetersForSmallDistance(bool useMetersForSmallDistance) +{ + mUseMetersForSmallDistance = useMetersForSmallDistance; +} + +bool QGVWidgetScale::getUseMetersForSmallDistance() const +{ + return mUseMetersForSmallDistance; +} + +QString QGVWidgetScale::getDistanceLabel(int meters) const { - if (meters > 1000) { - return tr("%1 km").arg(QString::number(static_cast(meters) / 1000, 'f', accuracy)); + const double distanceValue = QGV::metersToDistance(meters, mDistanceUnits); + if (distanceValue >= 1 || (distanceValue < 1 && !mUseMetersForSmallDistance)) { + const int accuracy = distanceValue >= 1 ? 0 : 2; + return tr("%1 %2") + .arg(QString::number(static_cast(distanceValue), 'f', accuracy)) + .arg(QGV::unitToString(mDistanceUnits)); } else { - return tr("%1 m").arg(QString::number(static_cast(meters), 'f', accuracy)); + return tr("%1 %2") + .arg(QString::number(static_cast(meters), 'f', 0)) + .arg(QGV::unitToString(QGV::DistanceUnits::Meters)); } }