1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 14:53:21 +02:00

Code cleanup

This commit is contained in:
Martin Tůma 2019-07-08 21:19:25 +02:00
parent 86cc30433c
commit 4e9faf3a23
2 changed files with 75 additions and 82 deletions

View File

@ -17,18 +17,18 @@ static void expand(QRect &rect, int width)
TextPointItem::TextPointItem(const QPoint &point, const QString *text,
const QFont *font, const QImage *img, const QColor *color,
const QColor *bgColor) : _text(text), _font(font), _img(img), _color(color),
_bgColor(bgColor)
const QColor *bgColor) : _text(font ? text : 0), _font(font), _img(img),
_color(color), _bgColor(bgColor)
{
if (text) {
QFontMetrics fm(*font);
int limit = font->pixelSize() * MAX_TEXT_WIDTH;
_textRect = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, *text);
if (_text) {
QFontMetrics fm(*_font);
int limit = _font->pixelSize() * MAX_TEXT_WIDTH;
_textRect = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, *_text);
_textRect.adjust(0, 0, 1, 1);
}
if (_bgColor && _textRect.width() < font->pixelSize() * MIN_BOX_WIDTH)
expand(_textRect, font->pixelSize() * MIN_BOX_WIDTH);
if (_bgColor && _textRect.width() < _font->pixelSize() * MIN_BOX_WIDTH)
expand(_textRect, _font->pixelSize() * MIN_BOX_WIDTH);
}
setPos(point);
}
@ -58,6 +58,14 @@ void TextPointItem::paint(QPainter *painter) const
- _img->height()/2), *_img);
if (_text) {
if (_bgColor) {
painter->setPen(*_color);
painter->setBrush(*_bgColor);
painter->drawRect(_textRect);
painter->setBrush(Qt::NoBrush);
painter->setFont(*_font);
painter->drawText(_textRect, FLAGS, *_text);
} else {
QImage img(_textRect.size(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
QPainter ip(&img);
@ -68,18 +76,12 @@ void TextPointItem::paint(QPainter *painter) const
painter->drawImage(_textRect.x() - 1, _textRect.y() - 1, img);
painter->drawImage(_textRect.x() + 1, _textRect.y() + 1, img);
painter->drawImage(_textRect.x() - 1, _textRect.y() + 1, img);
painter->drawImage(_textRect.x() + 1, _textRect.y() - 1, img);
painter->drawImage(_textRect.x(), _textRect.y() - 1, img);
painter->drawImage(_textRect.x(), _textRect.y() + 1, img);
painter->drawImage(_textRect.x() - 1, _textRect.y(), img);
painter->drawImage(_textRect.x() + 1, _textRect.y(), img);
if (_bgColor) {
painter->setPen(*_color);
painter->setBrush(*_bgColor);
painter->drawRect(_textRect);
painter->setBrush(Qt::NoBrush);
}
if (_color) {
painter->setFont(*_font);
painter->setPen(*_color);
@ -95,6 +97,7 @@ void TextPointItem::paint(QPainter *painter) const
#endif // QT >= 5.4
}
}
}
//painter->setPen(Qt::red);
//painter->drawRect(_rect);

View File

@ -73,7 +73,6 @@ static QColor shieldBgColor1("#dd3e3e");
static QColor shieldBgColor2("#379947");
static QColor shieldBgColor3("#4a7fc1");
static QString convertUnits(const QString &str)
{
bool ok;
@ -102,26 +101,47 @@ static int minPOIZoom(Style::POIClass cl)
}
}
static QFont font(int pixelSize)
static QFont pixelSizeFont(int pixelSize)
{
QFont f;
f.setPixelSize(pixelSize);
return f;
}
static QFont *font(Style::FontSize size, Style::FontSize defaultSize
= Style::Normal)
{
/* The fonts must be initialized on first usage (after the QGuiApplication
instance is created) */
#define FONT(name, size) \
static const QFont *name() \
{ \
static QFont f = font(size); \
return &f; \
static QFont large = pixelSizeFont(16);
static QFont normal = pixelSizeFont(14);
static QFont small = pixelSizeFont(12);
switch (size) {
case Style::None:
return 0;
case Style::Large:
return &large;
case Style::Normal:
return &normal;
case Style::Small:
return &small;
default:
return font(defaultSize);
}
}
FONT(largeFont, 16)
FONT(normalFont, 14)
FONT(smallFont, 12)
FONT(poiFont, 10)
static QFont *poiFont(Style::FontSize size = Style::Normal)
{
static QFont poi = pixelSizeFont(10);
switch (size) {
case Style::None:
return 0;
default:
return &poi;
}
}
static const QColor *shieldBgColor(Label::Shield::Type type)
{
@ -347,22 +367,12 @@ void IMGMap::processLines(QList<IMG::Poly> &lines, const QPoint &tile,
if (Style::isContourLine(poly.type))
poly.label.setText(convertUnits(poly.label.text()));
const QFont *font;
switch (style.textFontSize()) {
case Style::Large:
font = largeFont();
break;
case Style::Normal:
font = normalFont();
break;
default:
font = smallFont();
}
const QFont *fnt = font(style.textFontSize(), Style::Small);
const QColor *color = style.textColor().isValid()
? &style.textColor() : 0;
TextPathItem *item = new TextPathItem(poly.points,
&poly.label.text(), tileRect, font, color);
&poly.label.text(), tileRect, fnt, color);
if (item->isValid() && !item->collides(textItems))
textItems.append(item);
else
@ -424,7 +434,6 @@ void IMGMap::processPoints(QList<IMG::Point> &points,
for (int i = 0; i < points.size(); i++) {
IMG::Point &point = points[i];
const Style::Point &style = _img.style()->point(point.type);
if (point.poi && _zoom < minPOIZoom(Style::poiClass(point.type)))
@ -433,31 +442,12 @@ void IMGMap::processPoints(QList<IMG::Point> &points,
const QString *label = point.label.text().isEmpty()
? 0 : &(point.label.text());
const QImage *img = style.img().isNull() ? 0 : &style.img();
const QFont *font = 0;
if (point.poi) {
if (style.textFontSize() == Style::None)
label = 0;
else
font = poiFont();
} else {
switch (style.textFontSize()) {
case Style::None:
label = 0;
break;
case Style::Small:
font = smallFont();
break;
case Style::Large:
font = largeFont();
break;
default:
font = normalFont();
}
}
const QFont *fnt = point.poi
? poiFont(style.textFontSize()) : font(style.textFontSize());
const QColor *color = style.textColor().isValid()
? &style.textColor() : 0;
if (!label && !img)
if ((!label || !fnt) && !img)
continue;
if (Style::isSpot(point.type))
@ -469,7 +459,7 @@ void IMGMap::processPoints(QList<IMG::Point> &points,
}
TextPointItem *item = new TextPointItem(
ll2xy(point.coordinates).toPoint(), label, font, img, color);
ll2xy(point.coordinates).toPoint(), label, fnt, img, color);
if (item->isValid() && !item->collides(textItems))
textItems.append(item);
else