Commit 79f59fbd authored by Volker Krause's avatar Volker Krause
Browse files

Add line or product logos to the timeline delegate header when available

As those can be non-square that requires proper aspect ratio handling.
Unfortunately Kirigami.Icon hardcodes its implicit size to 32x32, giving
us no way to do this properly. This problem exists in numerous other places
where we use those icons as well, those just happen to be a bit smaller
in height, so the implicitWidth of 32 just happens to work ok-ish.

For the timeline headers this looks broken though, so we need a correct
solution, even if the implementation is ugly. For this we now have a helper
method that peaks into the SVG data to read out the view box size and
computes the aspect ratio from that.

Still needs to be rolled out to all the other places using line icons.
parent 0357c2e9
......@@ -26,7 +26,8 @@ Kirigami.AbstractCard {
property var rangeType
property Item headerItem
property string headerIconSource
property alias headerIcon: _headerIcon
property alias headerIconSource: _headerIcon.source
readonly property double headerFontScale: 1.0
......@@ -71,11 +72,15 @@ Kirigami.AbstractCard {
anchors.margins: Kirigami.Units.largeSpacing
Kirigami.Icon {
source: headerIconSource
Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
Layout.preferredHeight: Layout.preferredWidth
id: _headerIcon
Layout.preferredWidth: Layout.preferredHeight * aspectRatio
Layout.preferredHeight: Kirigami.Units.iconSizes.smallMedium
color: Kirigami.Theme.textColor
isMask: true
// work around the fact that Kirigami.Icon always gives us an implicit size of 32x32 no
// matter the size of the input icon
property real aspectRatio: isMask ? 1.0 : Util.svgAspectRatio(source)
}
}
......
......@@ -14,7 +14,10 @@ import "." as App
App.TimelineDelegate {
id: root
headerIconSource: departure.route.line.mode == Line.Unknown ? "qrc:///images/train.svg" : PublicTransport.lineModeIcon(departure.route.line.mode)
headerIcon {
source: departure.route.line.mode == Line.Unknown ? "qrc:///images/train.svg" : PublicTransport.lineIcon(departure.route.line)
isMask: !departure.route.line.hasLogo
}
headerItem: RowLayout {
QQC2.Label {
text: {
......
/*
SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
SPDX-FileCopyrightText: 2018-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "util.h"
#include "logging.h"
#include <KItinerary/JsonLdDocument>
......@@ -12,7 +13,11 @@
#include <QAbstractItemModel>
#include <QDateTime>
#include <QFile>
#include <QTimeZone>
#include <QXmlStreamReader>
#include <cmath>
using namespace KItinerary;
......@@ -69,4 +74,46 @@ void Util::sortModel(QObject *model, int column, Qt::SortOrder sortOrder) const
}
}
float Util::svgAspectRatio(const QString &svgFilePath) const
{
if (svgFilePath.isEmpty()) {
return 1.0f;
}
QFile file(svgFilePath);
if (!file.open(QFile::ReadOnly)) {
qCWarning(Log) << file.errorString() << svgFilePath;
return 1.0f;
}
QXmlStreamReader reader(&file);
while (!reader.atEnd()) {
if (reader.readNext() != QXmlStreamReader::StartElement) {
continue;
}
if (reader.name() != QLatin1String("svg")) {
qCDebug(Log) << svgFilePath << "not an svg file?";
return 1.0f;
}
const auto viewBox = reader.attributes().value(QLatin1String("viewBox"));
const auto parts = viewBox.split(QLatin1Char(' '), Qt::SkipEmptyParts);
if (parts.size() != 4) {
qCDebug(Log) << "invalid SVG viewBox:" << viewBox;
return 1.0f;
}
bool widthValid, heightValid;
const auto width = parts.at(2).toDouble(&widthValid);
const auto height = parts.at(3).toDouble(&heightValid);
if (!widthValid || !heightValid || height == 0.0f || width == 0.0f) {
qCDebug(Log) << "invalid SVG viewBox:" << viewBox;
return 1.0f;
}
return width / height;
}
return 1.0f;
}
#include "moc_util.cpp"
/*
SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
SPDX-FileCopyrightText: 2018-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -34,6 +34,11 @@ public:
/** Execute the non-exported sort() method on a QAbstractItemModel. */
Q_INVOKABLE void sortModel(QObject *model, int column, Qt::SortOrder sortOrder) const;
/** Determine the aspect ratio of an SVG file.
* This is a dirty workaround for the problem that Kirigami.Icon has its implicit size hardcoded to 32x32...
*/
Q_INVOKABLE float svgAspectRatio(const QString &svgFilePath) const;
};
#endif // UTIL_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment