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

145 lines
3.2 KiB
C++
Raw Normal View History

#include <cmath>
2016-01-14 00:37:51 +01:00
#include <QPainter>
#include "config.h"
2016-07-25 19:32:36 +02:00
#include "misc.h"
2016-01-14 00:37:51 +01:00
#include "scaleitem.h"
2016-09-25 18:08:39 +02:00
#define BORDER_WIDTH 1
#define SCALE_WIDTH 132
#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
2016-09-12 23:53:14 +02:00
#ifndef Q_OS_MAC
2016-09-11 17:15:23 +02:00
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
2016-09-12 23:53:14 +02:00
#endif // Q_OS_MAC
2016-01-14 00:37:51 +01:00
}
void ScaleItem::updateBoundingRect()
2016-01-14 00:37:51 +01:00
{
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
QRect ss, es, us;
ss = fm.tightBoundingRect(QString::number(0));
es = fm.tightBoundingRect(QString::number(_length * SEGMENTS));
us = fm.tightBoundingRect(units());
2016-09-12 23:53:14 +02:00
_boundingRect = QRectF(-ss.width()/2, 0, _width * SEGMENTS + ss.width()/2
+ qMax(us.width() + PADDING, es.width()/2) + 1, SCALE_HEIGHT + PADDING
+ ss.height() + 2*fm.descent());
2016-01-14 00:37:51 +01:00
}
void ScaleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
2016-02-02 01:10:05 +01:00
QRect br;
2016-09-25 18:08:39 +02:00
QPen pen = QPen(Qt::black, BORDER_WIDTH);
2016-02-28 10:58:25 +01:00
2016-10-17 23:14:07 +02:00
painter->setRenderHint(QPainter::Antialiasing, false);
2016-01-14 00:37:51 +01:00
painter->setFont(font);
2016-09-25 18:08:39 +02:00
painter->setPen(pen);
2016-01-14 00:37:51 +01:00
for (int i = 0; i <= SEGMENTS; i++) {
QString label = QString::number(_length * i);
2016-02-02 01:10:05 +01:00
br = fm.tightBoundingRect(label);
2016-10-23 14:58:24 +02:00
painter->drawText(_width * i - br.width()/2, br.height() + 1, label);
2016-01-14 00:37:51 +01:00
}
2016-02-02 01:10:05 +01:00
painter->drawText(_width * SEGMENTS + PADDING, SCALE_HEIGHT + PADDING
+ br.height() + fm.descent(), units());
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());
*/
}
QString ScaleItem::units() const
{
if (_units == Imperial)
2016-08-10 08:19:55 +02:00
return _scale ? qApp->translate("ScaleItem", "mi")
: qApp->translate("ScaleItem", "ft");
2016-01-14 00:37:51 +01:00
else
2016-08-10 08:19:55 +02:00
return _scale ? qApp->translate("ScaleItem", "km")
: qApp->translate("ScaleItem", "m");
2016-01-14 00:37:51 +01:00
}
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) {
_length = niceNum((res * M2FT * FT2MI * SCALE_WIDTH) / SEGMENTS, 1);
_width = (_length / (res * M2FT * FT2MI));
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;
}
}
}
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();
updateBoundingRect();
2016-09-12 02:01:13 +02:00
update();
2016-01-14 00:37:51 +01:00
}
void ScaleItem::setUnits(enum Units units)
{
2016-04-01 21:20:23 +02:00
prepareGeometryChange();
2016-01-14 00:37:51 +01:00
_units = units;
computeScale();
updateBoundingRect();
2016-09-12 02:01:13 +02:00
update();
2016-01-14 00:37:51 +01:00
}
void ScaleItem::setDigitalZoom(int zoom)
{
prepareGeometryChange();
_digitalZoom = zoom;
computeScale();
updateBoundingRect();
update();
setScale(pow(2, -_digitalZoom));
}