1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/GUI/scaleitem.cpp

152 lines
3.6 KiB
C++
Raw Normal View History

#include <cmath>
2016-01-14 00:37:51 +01:00
#include <QPainter>
#include "config.h"
2017-11-26 18:54:03 +01:00
#include "nicenum.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
#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;
_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()
{
qreal res = _res * pow(2, -_digitalZoom);
2016-01-14 00:37:51 +01:00
if (_units == Imperial) {
_length = niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, 1);
2016-01-14 00:37:51 +01:00
if (_length >= MIINFT) {
2018-02-11 23:51:57 +01:00
_length = niceNum((res * M2MI * SCALE_WIDTH) / SEGMENTS, 1);
_width = (_length / (res * M2MI));
_scale = true;
} else {
_width = (_length / (res * M2FT));
_scale = false;
}
} else if (_units == Nautical) {
_length = niceNum((res * M2FT * SCALE_WIDTH) / SEGMENTS, 1);
if (_length >= NMIINFT) {
_length = niceNum((res * M2NMI * SCALE_WIDTH) / SEGMENTS, 1);
_width = (_length / (res * M2NMI));
2016-01-14 00:37:51 +01:00
_scale = true;
} else {
_width = (_length / (res * M2FT));
2016-01-14 00:37:51 +01:00
_scale = false;
}
} else {
_length = niceNum((res * SCALE_WIDTH) / SEGMENTS, 1);
2016-01-14 00:37:51 +01:00
if (_length >= KMINM) {
_length *= M2KM;
_width = (_length / (res * M2KM));
2016-01-14 00:37:51 +01:00
_scale = true;
} else {
_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-08-31 16:28:37 +02:00
void ScaleItem::setDigitalZoom(qreal zoom)
{
prepareGeometryChange();
_digitalZoom = zoom;
computeScale();
2018-05-18 20:08:52 +02:00
updateCache();
update();
setScale(pow(2, -_digitalZoom));
}