KOSMIndoorMap

entities.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "entities.h"
8
9#include <QJsonArray>
10#include <QVariant>
11
12using namespace Qt::Literals::StringLiterals;
13using namespace Wikidata;
14
15Item::Item() = default;
16Item::Item(Wikidata::Q id, const QJsonObject& data)
17 : m_id(id)
18 , m_data(data)
19{
20}
21
22Item::~Item() = default;
23
24std::vector<QVariant> Item::values(P property) const
25{
26 std::vector<QVariant> values;
27 std::vector<bool> prefRank;
28
29 auto propA = m_data.value("claims"_L1).toObject().value(property.toString()).toArray();
30 for (const auto & propV : propA) {
31 const auto propObj = propV.toObject();
32 const auto rank = propObj.value("rank"_L1).toString();
33 if (rank == "deprecated"_L1) {
34 continue;
35 }
36 const auto valueObj = propObj.value("mainsnak"_L1).toObject().value("datavalue"_L1).toObject();
37 const auto type = valueObj.value("type"_L1).toString();
38 if (type == "string"_L1) {
39 values.push_back(valueObj.value("value"_L1).toString());
40 } else if (type == "wikibase-entityid"_L1) {
41 values.push_back(QVariant::fromValue(Wikidata::Q(valueObj.value("value"_L1).toObject().value("id"_L1).toString())));
42 }
43 // TODO other types
44
45 prefRank.push_back(rank == "preferred"_L1);
46 }
47
48 // if there are preferred rank values, take those, otherwise take all normal ranked ones
49 if (std::any_of(prefRank.begin(), prefRank.end(), [](bool v) { return v; })) {
50 int i = 0;
51 for (auto it = values.begin(); it != values.end();) {
52 if (prefRank[i++]) {
53 ++it;
54 } else {
55 it = values.erase(it);
56 }
57 }
58 }
59
60 return values;
61}
62
63QVariant Item::value(Wikidata::P property) const
64{
65 const auto vals = values(property);
66 return vals.empty() ? QVariant() : vals[0];
67}
68
69
70Image::Image() = default;
71Image::Image(const QJsonObject &obj)
72 : m_data(obj)
73{
74}
75
76Image::~Image() = default;
77
78QString Image::name() const
79{
80 return m_data.value("title"_L1).toString().mid(5);
81}
82
83uint64_t Image::fileSize() const
84{
85 return imageInfo().value("size"_L1).toInt();
86}
87
88uint32_t Image::width() const
89{
90 return imageInfo().value("width"_L1).toInt();
91}
92
93uint32_t Image::height() const
94{
95 return imageInfo().value("height"_L1).toInt();
96}
97
98QString Image::mimeType() const
99{
100 return imageInfo().value("mime"_L1).toString();
101}
102
103QString Image::license() const
104{
105 const auto extmeta = imageInfo().value("extmetadata"_L1).toObject();
106 return extmeta.value("LicenseShortName"_L1).toObject().value("value"_L1).toString();
107}
108
109QJsonObject Image::imageInfo() const
110{
111 const auto ii = m_data.value("imageinfo"_L1).toArray();
112 if (ii.isEmpty()) {
113 return {};
114 }
115 return ii.at(0).toObject();
116}
Wikidata property identifier.
Definition entities.h:88
Wikidata item identifier.
Definition entities.h:75
QJsonValue at(qsizetype i) const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
int toInt(int defaultValue) const const
QJsonObject toObject() const const
QString toString() const const
QString mid(qsizetype position, qsizetype n) const const
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.