2017-05-01 12:59:56 +02:00
|
|
|
#include <cmath>
|
2018-11-02 20:01:19 +01:00
|
|
|
#include <QApplication>
|
2016-01-14 00:37:51 +01:00
|
|
|
#include <QPainter>
|
2018-10-11 18:19:35 +02:00
|
|
|
#include "common/util.h"
|
2018-11-02 20:01:19 +01:00
|
|
|
#include "font.h"
|
2016-01-14 00:37:51 +01:00
|
|
|
#include "scaleitem.h"
|
|
|
|
|
|
|
|
|
2016-09-25 18:08:39 +02:00
|
|
|
#define BORDER_WIDTH 1
|
2018-09-10 21:22:01 +02:00
|
|
|
#define SCALE_WIDTH 135
|
2016-10-24 00:21:40 +02:00
|
|
|
#define SCALE_HEIGHT 5
|
|
|
|
#define SEGMENTS 3
|
|
|
|
#define PADDING 4
|
2016-01-14 00:37:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
ScaleItem::ScaleItem(QGraphicsItem *parent) : QGraphicsItem(parent)
|
|
|
|
{
|
|
|
|
_units = Metric;
|
2017-01-16 09:54:12 +01:00
|
|
|
_res = 1.0;
|
2017-05-01 12:59:56 +02:00
|
|
|
_digitalZoom = 0;
|
2016-09-11 17:15:23 +02:00
|
|
|
|
2018-05-18 01:38:33 +02:00
|
|
|
_font.setPixelSize(FONT_SIZE);
|
|
|
|
_font.setFamily(FONT_FAMILY);
|
2016-01-14 00:37:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScaleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
2018-05-18 01:38:33 +02:00
|
|
|
QFontMetrics fm(_font);
|
2016-02-02 01:10:05 +01:00
|
|
|
QRect br;
|
2016-02-28 10:58:25 +01:00
|
|
|
|
|
|
|
|
2016-10-17 23:14:07 +02:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing, false);
|
2018-05-18 01:38:33 +02:00
|
|
|
painter->setFont(_font);
|
|
|
|
painter->setPen(QPen(Qt::black, BORDER_WIDTH));
|
2016-01-14 00:37:51 +01:00
|
|
|
|
2018-05-18 20:08:52 +02:00
|
|
|
for (int i = 0; i < _ticks.size(); i++) {
|
|
|
|
br = _ticks.at(i).boundingBox;
|
|
|
|
painter->drawText(_width * i - br.width()/2, br.height() + 1,
|
|
|
|
QString::number(_ticks.at(i).value));
|
2016-01-14 00:37:51 +01:00
|
|
|
}
|
2016-02-02 01:10:05 +01:00
|
|
|
painter->drawText(_width * SEGMENTS + PADDING, SCALE_HEIGHT + PADDING
|
2018-05-18 20:08:52 +02:00
|
|
|
+ br.height() + fm.descent(), _unitsStr);
|
2016-01-14 00:37:51 +01:00
|
|
|
|
2016-02-02 01:10:05 +01:00
|
|
|
painter->drawRect(QRectF(0, br.height() + PADDING, SEGMENTS * _width,
|
|
|
|
SCALE_HEIGHT));
|
2016-01-14 00:37:51 +01:00
|
|
|
for (int i = 0; i < SEGMENTS; i += 2)
|
2016-02-02 01:10:05 +01:00
|
|
|
painter->fillRect(QRectF(i * _width, br.height() + PADDING, _width,
|
|
|
|
SCALE_HEIGHT), Qt::black);
|
2016-09-12 23:53:14 +02:00
|
|
|
|
2016-01-14 00:37:51 +01:00
|
|
|
/*
|
|
|
|
painter->setPen(Qt::red);
|
|
|
|
painter->drawRect(boundingRect());
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScaleItem::computeScale()
|
|
|
|
{
|
2017-05-01 12:59:56 +02:00
|
|
|
qreal res = _res * pow(2, -_digitalZoom);
|
|
|
|
|
2016-01-14 00:37:51 +01:00
|
|
|
if (_units == Imperial) {
|
2021-01-17 19:33:06 +01:00
|
|
|
_length = Util::niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, true);
|
2016-01-14 00:37:51 +01:00
|
|
|
if (_length >= MIINFT) {
|
2021-01-17 19:33:06 +01:00
|
|
|
_length = Util::niceNum((res * M2MI * SCALE_WIDTH) / SEGMENTS, true);
|
2018-02-11 23:51:57 +01:00
|
|
|
_width = (_length / (res * M2MI));
|
|
|
|
_scale = true;
|
|
|
|
} else {
|
|
|
|
_width = (_length / (res * M2FT));
|
|
|
|
_scale = false;
|
|
|
|
}
|
|
|
|
} else if (_units == Nautical) {
|
2021-01-17 19:33:06 +01:00
|
|
|
_length = Util::niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, true);
|
2018-02-11 23:51:57 +01:00
|
|
|
if (_length >= NMIINFT) {
|
2021-01-17 19:33:06 +01:00
|
|
|
_length = Util::niceNum((res * M2NMI * SCALE_WIDTH) / SEGMENTS, true);
|
2018-02-11 23:51:57 +01:00
|
|
|
_width = (_length / (res * M2NMI));
|
2016-01-14 00:37:51 +01:00
|
|
|
_scale = true;
|
|
|
|
} else {
|
2017-05-01 12:59:56 +02:00
|
|
|
_width = (_length / (res * M2FT));
|
2016-01-14 00:37:51 +01:00
|
|
|
_scale = false;
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-17 19:33:06 +01:00
|
|
|
_length = Util::niceNum((res * SCALE_WIDTH) / SEGMENTS, true);
|
2016-01-14 00:37:51 +01:00
|
|
|
if (_length >= KMINM) {
|
|
|
|
_length *= M2KM;
|
2017-05-01 12:59:56 +02:00
|
|
|
_width = (_length / (res * M2KM));
|
2016-01-14 00:37:51 +01:00
|
|
|
_scale = true;
|
|
|
|
} else {
|
2017-05-01 12:59:56 +02:00
|
|
|
_width = (_length / res);
|
2016-01-14 00:37:51 +01:00
|
|
|
_scale = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 20:08:52 +02:00
|
|
|
void ScaleItem::updateCache()
|
|
|
|
{
|
|
|
|
QFontMetrics fm(_font);
|
|
|
|
|
|
|
|
_ticks = QVector<Tick>(SEGMENTS + 1);
|
|
|
|
for (int i = 0; i < _ticks.size(); i++) {
|
|
|
|
Tick &t = _ticks[i];
|
|
|
|
t.value = _length * i;
|
|
|
|
t.boundingBox = fm.tightBoundingRect(QString::number(t.value));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_units == Imperial)
|
|
|
|
_unitsStr = _scale ? qApp->translate("ScaleItem", "mi")
|
|
|
|
: qApp->translate("ScaleItem", "ft");
|
|
|
|
else if (_units == Nautical)
|
|
|
|
_unitsStr = _scale ? qApp->translate("ScaleItem", "nmi")
|
|
|
|
: qApp->translate("ScaleItem", "ft");
|
|
|
|
else
|
|
|
|
_unitsStr = _scale ? qApp->translate("ScaleItem", "km")
|
|
|
|
: qApp->translate("ScaleItem", "m");
|
|
|
|
_unitsBB = fm.tightBoundingRect(_unitsStr);
|
|
|
|
|
|
|
|
QRect ss = _ticks.isEmpty() ? QRect() : _ticks.first().boundingBox;
|
|
|
|
QRect es = _ticks.isEmpty() ? QRect() : _ticks.last().boundingBox;
|
|
|
|
_boundingRect = QRectF(-ss.width()/2, 0, _width * SEGMENTS + ss.width()/2
|
|
|
|
+ qMax(_unitsBB.width() + PADDING, es.width()/2) + 1, SCALE_HEIGHT
|
|
|
|
+ PADDING + ss.height() + 2*fm.descent());
|
|
|
|
}
|
|
|
|
|
2017-01-16 09:54:12 +01:00
|
|
|
void ScaleItem::setResolution(qreal res)
|
2016-01-14 00:37:51 +01:00
|
|
|
{
|
2016-04-01 21:20:23 +02:00
|
|
|
prepareGeometryChange();
|
2017-01-16 09:54:12 +01:00
|
|
|
_res = res;
|
2016-01-14 00:37:51 +01:00
|
|
|
computeScale();
|
2018-05-18 20:08:52 +02:00
|
|
|
updateCache();
|
2016-09-12 02:01:13 +02:00
|
|
|
update();
|
2016-01-14 00:37:51 +01:00
|
|
|
}
|
|
|
|
|
2018-05-18 20:08:52 +02:00
|
|
|
void ScaleItem::setUnits(Units units)
|
2016-01-14 00:37:51 +01:00
|
|
|
{
|
2016-04-01 21:20:23 +02:00
|
|
|
prepareGeometryChange();
|
2016-01-14 00:37:51 +01:00
|
|
|
_units = units;
|
|
|
|
computeScale();
|
2018-05-18 20:08:52 +02:00
|
|
|
updateCache();
|
2016-09-12 02:01:13 +02:00
|
|
|
update();
|
2016-01-14 00:37:51 +01:00
|
|
|
}
|
2017-05-01 12:59:56 +02:00
|
|
|
|
2017-08-31 16:28:37 +02:00
|
|
|
void ScaleItem::setDigitalZoom(qreal zoom)
|
2017-05-01 12:59:56 +02:00
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
_digitalZoom = zoom;
|
|
|
|
computeScale();
|
2018-05-18 20:08:52 +02:00
|
|
|
updateCache();
|
2017-05-01 12:59:56 +02:00
|
|
|
update();
|
|
|
|
|
|
|
|
setScale(pow(2, -_digitalZoom));
|
|
|
|
}
|