From 1921087346c922de202541600f2afe7d5d716c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Fri, 23 Sep 2022 21:36:02 +0200 Subject: [PATCH] Multiple data styles fixes --- src/GUI/areaitem.cpp | 12 ++-- src/data/geojsonparser.cpp | 121 ++++++++++++++++++------------------- src/data/kmlparser.cpp | 56 +++++++++++------ src/data/kmlparser.h | 1 + src/data/style.h | 27 +++++---- 5 files changed, 120 insertions(+), 97 deletions(-) diff --git a/src/GUI/areaitem.cpp b/src/GUI/areaitem.cpp index 7ab686b6..32076e44 100644 --- a/src/GUI/areaitem.cpp +++ b/src/GUI/areaitem.cpp @@ -29,10 +29,12 @@ AreaItem::AreaItem(const Area &area, Map *map, GraphicsItem *parent) _digitalZoom = 0; if (_area.style().isValid()) { - _width = (_area.style().pen().style() == Qt::NoPen) - ? 0 : _area.style().pen().width(); - _pen = _area.style().pen(); - _brush = _area.style().brush(); + _width = (_area.style().width() >= 0) + ? _area.style().width() : 2; + _pen = _area.style().stroke().isValid() + ? QPen(area.style().stroke(), _width) : QPen(Qt::NoPen); + _brush = _area.style().fill().isValid() + ? QBrush(_area.style().fill()) : QBrush(Qt::NoBrush); } else { _width = 2; _opacity = 0.5; @@ -128,7 +130,7 @@ void AreaItem::setOpacity(qreal opacity) void AreaItem::setWidth(qreal width) { - if (_area.style().isValid()) + if (_area.style().width() >= 0) return; if (_width == width) return; diff --git a/src/data/geojsonparser.cpp b/src/data/geojsonparser.cpp index 0046612e..d1ad1e85 100644 --- a/src/data/geojsonparser.cpp +++ b/src/data/geojsonparser.cpp @@ -20,60 +20,59 @@ static int markerSize(const QString &str) static void setAreaProperties(Area &area, const QJsonValue &properties) { - if (!properties.isObject()) - return; - QJsonObject o(properties.toObject()); - - if (o["name"].isString()) - area.setName(o["name"].toString()); - if (o["title"].isString()) - area.setName(o["title"].toString()); - if (o["description"].isString()) - area.setDescription(o["description"].toString()); - QColor strokeColor(0x55, 0x55, 0x55); - QColor fillColor(0x55, 0x55, 0x55); + QColor fillColor(0x55, 0x55, 0x55, 0x99); double strokeWidth = 2; - if (o["stroke"].isString()) - strokeColor = QColor(o["stroke"].toString()); - if (o["stroke-opacity"].isDouble()) - strokeColor.setAlphaF(o["stroke-opacity"].toDouble()); - if (o["stroke-width"].isDouble()) - strokeWidth = o["stroke-width"].toDouble(); - if (o["fill"].isString()) - fillColor = QColor(o["fill"].toString()); - if (o["fill-opacity"].isDouble()) - fillColor.setAlphaF(o["fill-opacity"].toDouble()); - else - fillColor.setAlphaF(0.6); + if (properties.isObject()) { + QJsonObject o(properties.toObject()); - area.setStyle(PolygonStyle(QPen(strokeColor, strokeWidth), - QBrush(fillColor))); + if (o["name"].isString()) + area.setName(o["name"].toString()); + if (o["title"].isString()) + area.setName(o["title"].toString()); + if (o["description"].isString()) + area.setDescription(o["description"].toString()); + + if (o["stroke"].isString()) + strokeColor = QColor(o["stroke"].toString()); + if (o["stroke-opacity"].isDouble()) + strokeColor.setAlphaF(o["stroke-opacity"].toDouble()); + if (o["stroke-width"].isDouble()) + strokeWidth = o["stroke-width"].toDouble(); + if (o["fill"].isString()) + fillColor = QColor(o["fill"].toString()); + if (o["fill-opacity"].isDouble()) + fillColor.setAlphaF(o["fill-opacity"].toDouble()); + else + fillColor.setAlphaF(0.6); + } + + area.setStyle(PolygonStyle(fillColor, strokeColor, strokeWidth)); } static void setTrackProperties(TrackData &track, const QJsonValue &properties) { - if (!properties.isObject()) - return; - QJsonObject o(properties.toObject()); - - if (o["name"].isString()) - track.setName(o["name"].toString()); - if (o["title"].isString()) - track.setName(o["title"].toString()); - if (o["description"].isString()) - track.setDescription(o["description"].toString()); - QColor color(0x55, 0x55, 0x55); double width = 2; - if (o["stroke"].isString()) - color = QColor(o["stroke"].toString()); - if (o["stroke-opacity"].isDouble()) - color.setAlphaF(o["stroke-opacity"].toDouble()); - if (o["stroke-width"].isDouble()) - width = o["stroke-width"].toDouble(); + if (properties.isObject()) { + QJsonObject o(properties.toObject()); + + if (o["name"].isString()) + track.setName(o["name"].toString()); + if (o["title"].isString()) + track.setName(o["title"].toString()); + if (o["description"].isString()) + track.setDescription(o["description"].toString()); + + if (o["stroke"].isString()) + color = QColor(o["stroke"].toString()); + if (o["stroke-opacity"].isDouble()) + color.setAlphaF(o["stroke-opacity"].toDouble()); + if (o["stroke-width"].isDouble()) + width = o["stroke-width"].toDouble(); + } track.setStyle(LineStyle(color, width)); } @@ -81,28 +80,28 @@ static void setTrackProperties(TrackData &track, const QJsonValue &properties) static void setWaypointProperties(Waypoint &waypoint, const QJsonValue &properties) { - if (!properties.isObject()) - return; - QJsonObject o(properties.toObject()); - - if (o["name"].isString()) - waypoint.setName(o["name"].toString()); - if (o["title"].isString()) - waypoint.setName(o["title"].toString()); - if (o["description"].isString()) - waypoint.setDescription(o["description"].toString()); - QColor color(0x7e, 0x7e, 0x7e); int size = MARKER_SIZE_MEDIUM; - if (o["marker-color"].isString()) - color = QColor(o["marker-color"].toString()); - if (o["marker-symbol"].isString()) - waypoint.setSymbol(o["marker-symbol"].toString()); - if (o["marker-size"].isString()) - size = markerSize(o["marker-size"].toString()); + if (!properties.isObject()) { + QJsonObject o(properties.toObject()); - waypoint.setStyle(PointStyle(QPixmap(), color, size)); + if (o["name"].isString()) + waypoint.setName(o["name"].toString()); + if (o["title"].isString()) + waypoint.setName(o["title"].toString()); + if (o["description"].isString()) + waypoint.setDescription(o["description"].toString()); + + if (o["marker-color"].isString()) + color = QColor(o["marker-color"].toString()); + if (o["marker-symbol"].isString()) + waypoint.setSymbol(o["marker-symbol"].toString()); + if (o["marker-size"].isString()) + size = markerSize(o["marker-size"].toString()); + } + + waypoint.setStyle(PointStyle(color, size)); } diff --git a/src/data/kmlparser.cpp b/src/data/kmlparser.cpp index 02a1b583..ad118896 100644 --- a/src/data/kmlparser.cpp +++ b/src/data/kmlparser.cpp @@ -643,15 +643,22 @@ void KMLParser::placemark(const Ctx &ctx, QList &tracks, wp->setTimestamp(timestamp); wp->setAddress(address); wp->setPhone(phone); - wp->setStyle(pointStyles.value(id)); + PointStyleMap::iterator it = pointStyles.find(id); + wp->setStyle(it == pointStyles.end() + ? PointStyle(QColor(0x55, 0x55, 0x55)) : *it); } else if (tp) { tp->setName(name); tp->setDescription(desc); - tp->setStyle(lineStyles.value(id)); + LineStyleMap::iterator it = lineStyles.find(id); + tp->setStyle(it == lineStyles.end() + ? LineStyle(QColor(0x55, 0x55, 0x55), 2) : *it); } else if (ap) { ap->setName(name); ap->setDescription(desc); - ap->setStyle(polyStyles.value(id)); + PolygonStyleMap::iterator it = polyStyles.find(id); + ap->setStyle(it == polyStyles.end() + ? PolygonStyle(QColor(0x55, 0x55, 0x55, 0x99), + QColor(0x55, 0x55, 0x55, 0x99), 2) : *it); } } @@ -669,6 +676,21 @@ QString KMLParser::icon() return path; } +QColor KMLParser::color() +{ + QString str(_reader.readElementText()); + if (str.size() != 8) + return QColor(); + + bool aok, bok, gok, rok; + int a = str.midRef(0, 2).toInt(&aok, 16); + int b = str.midRef(2, 2).toInt(&bok, 16); + int g = str.midRef(4, 2).toInt(&gok, 16); + int r = str.midRef(6, 2).toInt(&rok, 16); + + return (aok && bok && gok && rok) ? QColor(r, g, b, a) : QColor(); +} + QString KMLParser::styleUrl() { QString id(_reader.readElementText()); @@ -679,28 +701,28 @@ void KMLParser::iconStyle(const QDir &dir, const QString &id, PointStyleMap &styles) { QPixmap img; - QColor color(Qt::white); + QColor c(0x55, 0x55, 0x55); while (_reader.readNextStartElement()) { if (_reader.name() == QLatin1String("Icon")) img = QPixmap(dir.absoluteFilePath(icon())); else if (_reader.name() == QLatin1String("color")) - color = QColor("#" + _reader.readElementText()); + c = color(); else _reader.skipCurrentElement(); } - styles.insert(id, PointStyle(img, color)); + styles.insert(id, PointStyle(img, c)); } void KMLParser::polyStyle(const QString &id, PolygonStyleMap &styles) { - QColor color(Qt::white); + QColor c(0x55, 0x55, 0x55, 0x99); uint fill = 1, outline = 1; while (_reader.readNextStartElement()) { if (_reader.name() == QLatin1String("color")) - color = QColor("#" + _reader.readElementText()); + c = color(); else if (_reader.name() == QLatin1String("fill")) fill = _reader.readElementText().toUInt(); else if (_reader.name() == QLatin1String("outline")) @@ -709,29 +731,25 @@ void KMLParser::polyStyle(const QString &id, PolygonStyleMap &styles) _reader.skipCurrentElement(); } - QPen pen = (color.isValid() && outline) - ? QPen(color) : QPen(Qt::NoPen); - QBrush brush = (color.isValid() && fill) - ? QBrush(color) : QBrush(Qt::NoBrush); - - styles.insert(id, PolygonStyle(pen, brush)); + styles.insert(id, PolygonStyle(fill ? c : QColor(), + outline ? c : QColor(), 2)); } void KMLParser::lineStyle(const QString &id, LineStyleMap &styles) { - QColor color(Qt::white); - uint width = 1; + QColor c(0x55, 0x55, 0x55); + double width = 2; while (_reader.readNextStartElement()) { if (_reader.name() == QLatin1String("color")) - color = QColor("#" + _reader.readElementText()); + c = color(); else if (_reader.name() == QLatin1String("width")) - width = _reader.readElementText().toUInt(); + width = _reader.readElementText().toDouble(); else _reader.skipCurrentElement(); } - styles.insert(id, LineStyle(color, width)); + styles.insert(id, LineStyle(c, width)); } void KMLParser::styleMapPair(const QString &id, PointStyleMap &pointStyles, diff --git a/src/data/kmlparser.h b/src/data/kmlparser.h index 5127abd5..233798cd 100644 --- a/src/data/kmlparser.h +++ b/src/data/kmlparser.h @@ -63,6 +63,7 @@ private: QDateTime timeStamp(); qreal number(); QDateTime time(); + QColor color(); QString icon(); QString styleUrl(); void style(const QDir &dir, PointStyleMap &pointStyles, diff --git a/src/data/style.h b/src/data/style.h index d19ba5a9..5b26f4e0 100644 --- a/src/data/style.h +++ b/src/data/style.h @@ -10,6 +10,8 @@ public: PointStyle() : _size(-1) {} PointStyle(const QPixmap &icon, const QColor &color = QColor(), int size = -1) : _icon(icon), _color(color), _size(size) {} + PointStyle(const QColor &color, int size = -1) + : _color(color), _size(size) {} const QColor &color() const {return _color;} const QPixmap &icon() const {return _icon;} @@ -23,34 +25,35 @@ private: class PolygonStyle { public: - PolygonStyle() - : _pen(QPen(Qt::NoPen)), _brush(QBrush(Qt::NoBrush)) {} - PolygonStyle(const QPen &pen, const QBrush &brush) - : _pen(pen), _brush(brush) {} + PolygonStyle() : _width(-1) {} + PolygonStyle(const QColor &fill, const QColor &stroke = QColor(), + qreal width = -1) : _fill(fill), _stroke(stroke), _width(width) {} - const QPen &pen() const {return _pen;} - const QBrush &brush() const {return _brush;} + const QColor &fill() const {return _fill;} + const QColor &stroke() const {return _stroke;} + qreal width() const {return _width;} bool isValid() const - {return _pen.style() != Qt::NoPen || _brush.style() != Qt::NoBrush;} + {return _fill.isValid() || _stroke.isValid();} private: - QPen _pen; - QBrush _brush; + QColor _fill; + QColor _stroke; + qreal _width; }; class LineStyle { public: LineStyle() : _width(-1) {} - LineStyle(const QColor &color, int width = -1) + LineStyle(const QColor &color, qreal width = -1) : _color(color), _width(width) {} const QColor &color() const {return _color;} - int width() const {return _width;} + qreal width() const {return _width;} private: QColor _color; - int _width; + qreal _width; }; #endif // STYLE_H