Skip to content

Commit

Permalink
New distance units
Browse files Browse the repository at this point in the history
  • Loading branch information
AmonRaNet committed Feb 8, 2022
1 parent dd2aee2 commit a55c59d
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lib/include/QGeoView/QGVGlobal.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ enum class MapState
SelectionRect,
};

enum class DistanceUnits
{
Meters,
Kilometers,
NauticalMiles,
Miles,
};

enum class MouseAction : int
{
Move = 0x1,
Expand Down
28 changes: 28 additions & 0 deletions lib/include/QGeoView/QGVUtils.h
Original file line number Diff line number Diff line change
@@ -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 <QGVGlobal.h>

namespace QGV {

QGV_LIB_DECL double metersToDistance(const double meters, const DistanceUnits unit);
QGV_LIB_DECL QString unitToString(const DistanceUnits unit);

} // namespace QGV
12 changes: 10 additions & 2 deletions lib/include/QGeoView/QGVWidgetScale.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -44,4 +50,6 @@ class QGV_LIB_DECL QGVWidgetScale : public QGVWidget
bool mAutoAdjust;
int mScaleMeters;
int mScalePixels;
QGV::DistanceUnits mDistanceUnits;
bool mUseMetersForSmallDistance;
};
2 changes: 2 additions & 0 deletions lib/lib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
50 changes: 50 additions & 0 deletions lib/src/QGVUtils.cpp
Original file line number Diff line number Diff line change
@@ -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 <QtGlobal>

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
38 changes: 34 additions & 4 deletions lib/src/QGVWidgetScale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "QGVWidgetScale.h"
#include "QGVMapQGView.h"
#include "QGVUtils.h"

#include <QPaintEvent>
#include <QPainter>
Expand All @@ -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;
Expand Down Expand Up @@ -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<double>(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<double>(distanceValue), 'f', accuracy))
.arg(QGV::unitToString(mDistanceUnits));
} else {
return tr("%1 m").arg(QString::number(static_cast<double>(meters), 'f', accuracy));
return tr("%1 %2")
.arg(QString::number(static_cast<double>(meters), 'f', 0))
.arg(QGV::unitToString(QGV::DistanceUnits::Meters));
}
}

Expand Down

0 comments on commit a55c59d

Please sign in to comment.