KPublicTransport

line.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "line.h"
8 #include "lineutil_p.h"
9 #include "location.h"
10 #include "datatypes_p.h"
11 #include "json_p.h"
12 #include "mergeutil_p.h"
13 #include "assetrepository_p.h"
14 
15 #include "knowledgedb/linemetadata.h"
16 
17 #include <QColor>
18 #include <QDebug>
19 #include <QUrl>
20 
21 using namespace KPublicTransport;
22 
23 namespace KPublicTransport {
24 class LinePrivate : public QSharedData {
25 public:
26  Line::Mode mode = Line::Unknown;
27  QString modeString;
28  QString name;
29  QColor color;
30  QColor textColor;
31  LineMetaData metaData;
32 };
33 
34 class RoutePrivate : public QSharedData {
35 public:
36  Line line;
37  QString direction;
38  Location destination;
39 };
40 
41 }
42 
43 KPUBLICTRANSPORT_MAKE_GADGET(Line)
44 KPUBLICTRANSPORT_MAKE_PROPERTY(Line, QString, name, setName)
45 KPUBLICTRANSPORT_MAKE_PROPERTY(Line, QColor, textColor, setTextColor)
46 KPUBLICTRANSPORT_MAKE_PROPERTY(Line, Line::Mode, mode, setMode)
47 KPUBLICTRANSPORT_MAKE_PROPERTY(Line, QString, modeString, setModeString)
48 
49 QColor Line::color() const
50 {
51  return d->metaData.color().isValid() ? d->metaData.color() : d->color;
52 }
53 
54 void Line::setColor(const QColor &value)
55 {
56  d.detach();
57  d->color = value;
58 }
59 
60 bool Line::hasColor() const
61 {
62  return d->color.isValid() || d->metaData.color().isValid();
63 }
64 
65 bool Line::hasTextColor() const
66 {
67  return d->textColor.isValid();
68 }
69 
70 void Line::setMetaData(const LineMetaData &metaData)
71 {
72  d->metaData = metaData;
73 }
74 
75 QString Line::logo() const
76 {
77  return AssetRepository::localFile(d->metaData.logoUrl());
78 }
79 
80 bool Line::hasLogo() const
81 {
82  return !logo().isEmpty();
83 }
84 
85 QString Line::modeLogo() const
86 {
87  return AssetRepository::localFile(d->metaData.modeLogoUrl());
88 }
89 
90 bool Line::hasModeLogo() const
91 {
92  return !modeLogo().isEmpty();
93 }
94 
95 bool Line::isSame(const Line &lhs, const Line &rhs)
96 {
97  if (!LineUtil::isCompatibleMode(lhs.mode(), rhs.mode())) {
98  return false;
99  }
100 
101  // ### not really the most efficient way of doing this...
102  using namespace LineUtil;
103  return isSameLineNameFuzzy(lhs.name(), rhs.name())
104  || isSameLineNameFuzzy(QString(lhs.modeString() + QLatin1Char(' ') + lhs.name()).trimmed(), rhs.name())
105  || isSameLineNameFuzzy(lhs.name(), QString(rhs.modeString() + QLatin1Char(' ') + rhs.name()).trimmed())
106  || isSameLineNameFuzzy(QString(lhs.modeString() + QLatin1Char(' ') + lhs.name()).trimmed(), QString(rhs.modeString() + QLatin1Char(' ') + rhs.name()).trimmed());
107 }
108 
109 static QColor mergeColor(const QColor &lhs, const QColor &rhs)
110 {
111  if (!lhs.isValid()) {
112  return rhs;
113  }
114  if (!rhs.isValid()) {
115  return lhs;
116  }
117 
118  const auto lh = lhs.hue(), ls = lhs.saturation(), lv = lhs.value();
119  const auto rh = rhs.hue(), rs = rhs.saturation(), rv = rhs.value();
120  return std::tie(ls, lv, lh) < std::tie(rs, rv, rh) ? rhs : lhs;
121 }
122 
123 Line Line::merge(const Line &lhs, const Line &rhs)
124 {
125  Line l(lhs);
126 
127  l.setModeString(MergeUtil::mergeString(lhs.modeString(), rhs.modeString()));
128  l.setName(MergeUtil::mergeString(lhs.name(), rhs.name()));
130  l.setName(QStringView(l.name()).mid(l.modeString().size()).trimmed().toString());
131  }
132 
133  l.setColor(mergeColor(lhs.color(), rhs.color()));
134  if (!l.textColor().isValid() && rhs.textColor().isValid()) {
135  l.setTextColor(rhs.textColor());
136  }
137  if (l.mode() == Unknown || (l.mode() == Train && rhs.mode() != Unknown)) {
138  l.setMode(rhs.mode());
139  }
140  return l;
141 }
142 
143 QJsonObject Line::toJson(const Line &l)
144 {
145  auto obj = Json::toJson(l);
146  if (l.mode() == Unknown) {
147  obj.remove(QLatin1String("mode"));
148  }
149  return obj;
150 }
151 
152 Line Line::fromJson(const QJsonObject &obj)
153 {
154  auto l = Json::fromJson<Line>(obj);
155  return l;
156 }
157 
158 
159 KPUBLICTRANSPORT_MAKE_GADGET(Route)
160 KPUBLICTRANSPORT_MAKE_PROPERTY(Route, Line, line, setLine)
161 KPUBLICTRANSPORT_MAKE_PROPERTY(Route, Location, destination, setDestination)
162 
163 QString Route::direction() const
164 {
165  if (d->direction.isEmpty() && !d->destination.isEmpty()) {
166  return d->destination.name();
167  }
168  return d->direction;
169 }
170 
171 void Route::setDirection(const QString &value)
172 {
173  d.detach();
174  d->direction = value;
175 }
176 
177 bool Route::isSame(const Route &lhs, const Route &rhs)
178 {
179  const auto matchingDirection = (!lhs.destination().isEmpty() && !rhs.destination().isEmpty() && Location::isSame(lhs.destination(), rhs.destination()))
180  || Location::isSameName(lhs.direction(), rhs.direction());
181 
182  return matchingDirection && Line::isSame(lhs.line(), rhs.line());
183 }
184 
185 Route Route::merge(const Route &lhs, const Route &rhs)
186 {
187  Route r(lhs);
188  r.setLine(Line::merge(lhs.line(), rhs.line()));
189  r.setDirection(MergeUtil::mergeString(lhs.direction(), rhs.direction()));
190  r.setDestination(Location::merge(lhs.destination(), rhs.destination()));
191  return r;
192 }
193 
195 {
196  auto obj = Json::toJson(r);
197  const auto lineObj = Line::toJson(r.line());
198  if (!lineObj.empty()) {
199  obj.insert(QStringLiteral("line"), lineObj);
200  }
201  if (!r.destination().isEmpty()) {
202  obj.insert(QStringLiteral("destination"), Location::toJson(r.destination()));
203  }
204  return obj;
205 }
206 
208 {
209  auto r = Json::fromJson<Route>(obj);
210  r.setLine(Line::fromJson(obj.value(QLatin1String("line")).toObject()));
211  r.setDestination(Location::fromJson(obj.value(QLatin1String("destination")).toObject()));
212  return r;
213 }
214 
215 #include "moc_line.cpp"
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
int value() const const
QString modeString
Human readable representation of the type of transport.
Definition: line.h:69
int size() const const
bool isEmpty() const
Returns true if this is an default-constructed location object not specifying any location...
Definition: location.cpp:76
static bool isSame(const Route &lhs, const Route &rhs)
Checks if to instances refer to the same route (which does not necessarily mean they are exactly equa...
Definition: line.cpp:177
static QJsonObject toJson(const Route &r)
Serializes one object to JSON.
Definition: line.cpp:194
QColor color
Color of the line.
Definition: line.h:56
int hue() const const
static bool isSame(const Location &lhs, const Location &rhs)
Checks if to instances refer to the same location (which does not necessarily mean they are exactly e...
Definition: location.cpp:262
A route of a public transport line.
Definition: line.h:120
QString name
Name of the line.
Definition: line.h:54
Mode
Mode of transportation.
Definition: line.h:31
static bool isSameName(const QString &lhs, const QString &rhs)
Checks if two location names refer to the same location.
Definition: location.cpp:321
KPublicTransport::Location destination
Destination of the route.
Definition: line.h:135
static Location fromJson(const QJsonObject &obj)
Deserialize a Location object from JSON.
Definition: location.cpp:485
QJsonObject toObject() const const
CaseInsensitive
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
static Location merge(const Location &lhs, const Location &rhs)
Merge two departure instances.
Definition: location.cpp:368
static bool isSame(const Line &lhs, const Line &rhs)
Checks if to instances refer to the same line (which does not necessarily mean they are exactly equal...
Definition: line.cpp:95
static Route merge(const Route &lhs, const Route &rhs)
Merge two Route instances.
Definition: line.cpp:185
static QJsonObject toJson(const Line &l)
Serializes one object to JSON.
Definition: line.cpp:143
static Line fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition: line.cpp:152
static Route fromJson(const QJsonObject &obj)
Deserialize an object from JSON.
Definition: line.cpp:207
QString toString() const const
KPublicTransport::Line::Mode mode
Type of transport.
Definition: line.h:64
static Line merge(const Line &lhs, const Line &rhs)
Merge two Line instances.
Definition: line.cpp:123
static QJsonObject toJson(const Location &loc)
Serializes one Location object to JSON.
Definition: location.cpp:444
QColor textColor
Text color to use on top of the line color.
Definition: line.h:60
QJsonValue value(const QString &key) const const
KPublicTransport::Line line
Line this route belongs to.
Definition: line.h:124
int saturation() const const
Static information about a public transport line.
Definition: linemetadata.h:25
A public transport line.
Definition: line.h:23
QJsonObject::iterator insert(const QString &key, const QJsonValue &value)
bool isValid() const const
QString direction
Direction of the route.
Definition: line.h:130
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.