KOSMIndoorMap

entities.h
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef WIKIDATA_ITEM_H
8#define WIKIDATA_ITEM_H
9
10#include <QColor>
11#include <QDebug>
12#include <QJsonObject>
13#include <QMetaType>
14#include <QVariant>
15
16#include <cstdint>
17#include <vector>
18
19namespace Wikidata {
20
21template <typename T, char Prefix>
22class Identifier
23{
24public:
25 explicit inline constexpr Identifier() = default;
26 explicit inline constexpr Identifier(T id) : m_id(id) {}
27 explicit inline Identifier(QStringView id) : m_id(fromString(id).m_id) {}
28 explicit inline Identifier(QByteArrayView id) : m_id(fromString(id).m_id) {}
29
30 [[nodiscard]] inline constexpr bool isValid() const
31 {
32 return m_id > 0;
33 }
34
35 [[nodiscard]] inline constexpr bool operator<(Identifier other) const
36 {
37 return m_id < other.m_id;
38 }
39 [[nodiscard]] inline constexpr bool operator==(Identifier other) const
40 {
41 return m_id == other.m_id;
42 }
43 [[nodiscard]] inline constexpr bool operator!=(Identifier other) const
44 {
45 return m_id != other.m_id;
46 }
47
48 [[nodiscard]] inline QString toString() const
49 {
50 return QLatin1Char(Prefix) + QString::number(m_id);
51 }
52
53protected:
54 T m_id = 0;
55
56private:
57 [[nodiscard]] static inline Identifier fromString(QStringView id)
58 {
59 if (!id.startsWith(QLatin1Char(Prefix))) {
60 return Identifier();
61 }
62 return Identifier(id.mid(1).toULongLong());
63 }
64 [[nodiscard]] static inline Identifier fromString(QByteArrayView id)
65 {
66 if (!id.startsWith(Prefix)) {
67 return Identifier();
68 }
69 return Identifier(id.mid(1).toULongLong());
70 }
71};
72
73/** Wikidata item identifier. */
74class Q : public Identifier<uint64_t, 'Q'>
75{
76public:
77 explicit inline constexpr Q() = default;
78 explicit inline constexpr Q(uint64_t id) : Identifier(id) {}
79 explicit inline Q(QStringView id) : Identifier(id) {}
80 explicit inline Q(QByteArrayView id) : Identifier(id) {}
81
82private:
83 friend class std::hash<Wikidata::Q>;
84};
85
86/** Wikidata property identifier. */
87class P : public Identifier<uint32_t, 'P'>
88{
89public:
90 enum Property : uint32_t {
91 image = 18,
92 instanceOf = 31,
93 logoImage = 154,
94 partOf = 361,
95 nighttimeView = 3451,
96 winterView = 5252,
97 imageOfInterior = 5775,
98 view = 8517,
99 aerialView = 8592,
100 modelImage = 11101,
101 };
102
103 explicit inline constexpr P() = default;
104 /* implicit */ inline constexpr P(Property id) : Identifier(id) {}
105 explicit inline constexpr P(uint32_t id) : Identifier(id) {}
106 explicit inline P(QStringView id) : Identifier(id) {}
107
108private:
109 friend class std::hash<Wikidata::P>;
110};
111
112/** Wikidata item. */
113class Item
114{
115public:
116 explicit Item();
117 explicit Item(Q id, const QJsonObject &data);
118 ~Item();
119
120 [[nodiscard]] inline constexpr bool isValid() const { return m_id.isValid(); }
121 [[nodiscard]] inline constexpr Q id() const { return m_id; }
122
123 template <typename T>
124 [[nodiscard]] inline T value(P property) const
125 {
126 return convertValue<T>(value(property));
127 }
128
129 template <typename T>
130 [[nodiscard]] inline std::vector<T> values(P property) const
131 {
132 const auto vals = values(property);
133 std::vector<T> v;
134 v.reserve(vals.size());
135 std::transform(vals.begin(), vals.end(), std::back_inserter(v), [](const QVariant &value) { return convertValue<T>(value); });
136 return v;
137 }
138
139private:
140 [[nodiscard]] std::vector<QVariant> values(P property) const;
141 [[nodiscard]] QVariant value(P property) const;
142
143 template <typename T>
144 [[nodiscard]] static inline T convertValue(const QVariant &v)
145 {
146 return v.value<T>();
147 }
148
149 Q m_id;
150 QJsonObject m_data;
151};
152
153template <>
154inline QColor Item::convertValue<QColor>(const QVariant &v)
155{
156 const auto s = v.toString();
157 if (s.size() == 6) {
158 return QColor(QLatin1Char('#') + s);
159 }
160 return QColor(s);
161}
162
163/** Commons image metadata. */
164class Image
165{
166public:
167 explicit Image();
168 explicit Image(const QJsonObject &obj);
169 ~Image();
170
171 [[nodiscard]] QString name() const;
172 [[nodiscard]] uint64_t fileSize() const;
173 [[nodiscard]] uint32_t width() const;
174 [[nodiscard]] uint32_t height() const;
175 [[nodiscard]] QString mimeType() const;
176 [[nodiscard]] QString license() const;
177
178private:
179 [[nodiscard]] QJsonObject imageInfo() const;
180 QJsonObject m_data;
181};
182
183}
184
185Q_DECLARE_METATYPE(Wikidata::Q)
186Q_DECLARE_METATYPE(Wikidata::P)
187
188template <typename T, char Prefix>
189inline QDebug operator<<(QDebug debug, Wikidata::Identifier<T, Prefix> id)
190{
191 QDebugStateSaver saver(debug);
192 if (id.isValid()) {
193 debug.noquote().nospace() << QLatin1StringView("https://www.wikidata.org/wiki/") + id.toString();
194 }
195 return debug;
196}
197
198template<>
199struct std::hash<Wikidata::P>
200{
201 std::size_t operator()(Wikidata::P id) const noexcept
202 {
203 return std::hash<uint64_t>{}(id.m_id);
204 }
205};
206template<>
207struct std::hash<Wikidata::Q>
208{
209 std::size_t operator()(Wikidata::Q id) const noexcept
210 {
211 return std::hash<uint64_t>{}(id.m_id);
212 }
213};
214
215#endif // WIKIDATA_ITEM_H
Commons image metadata.
Definition entities.h:165
Wikidata item.
Definition entities.h:114
Wikidata property identifier.
Definition entities.h:88
Wikidata item identifier.
Definition entities.h:75
QDebug & noquote()
QDebug & nospace()
QString toString() const const
QString number(double n, char format, int precision)
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.