KOSMIndoorMap

overlaysource.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 "overlaysource.h"
8
9#include <QAbstractItemModel>
10#include <QDebug>
11
12using namespace KOSMIndoorMap;
13
14namespace KOSMIndoorMap
15{
16class AbstractOverlaySourcePrivate {
17public:
18 virtual ~AbstractOverlaySourcePrivate() = default;
19};
20
21class ModelOverlaySourcePrivate : public AbstractOverlaySourcePrivate {
22public:
23 ~ModelOverlaySourcePrivate() override = default;
24 void recursiveForEach(const QModelIndex &rootIdx, int floorLevel, const std::function<void (OSM::Element, int)> &func) const;
25
27 int m_elementRole = -1;
28 int m_floorRole = -1;
29 int m_hiddenElementRole = -1;
30};
31
32}
33
34AbstractOverlaySource::AbstractOverlaySource(QObject *parent)
35 : AbstractOverlaySource(new AbstractOverlaySourcePrivate, parent)
36{
37}
38
39AbstractOverlaySource::AbstractOverlaySource(AbstractOverlaySourcePrivate *dd, QObject *parent)
40 : QObject(parent)
41 , d_ptr(dd)
42{
43}
44
45AbstractOverlaySource::~AbstractOverlaySource() = default;
46
47void AbstractOverlaySource::hiddenElements([[maybe_unused]] std::vector<OSM::Element> &elems) const
48{
49}
50
51
52ModelOverlaySource::ModelOverlaySource(QAbstractItemModel *model, QObject *parent)
53 : AbstractOverlaySource(new ModelOverlaySourcePrivate, parent)
54{
56 const auto roles = model->roleNames();
57 for (auto it = roles.begin(); it != roles.end(); ++it) {
58 if (it.value() == "osmElement") {
59 d->m_elementRole = it.key();
60 } else if (it.value() == "level") {
61 d->m_floorRole = it.key();
62 } else if (it.value() == "hiddenElement") {
63 d->m_hiddenElementRole = it.key();
64 }
65 }
66 if (d->m_elementRole < 0 || d->m_floorRole < 0) {
67 qWarning() << model << " - model does not provide the required roles!";
68 return;
69 }
70 d->m_model = model;
71
76
78}
79
80ModelOverlaySource::~ModelOverlaySource() = default;
81
82void ModelOverlaySource::forEach(int floorLevel, const std::function<void (OSM::Element, int)> &func) const
83{
85 if (!d->m_model) {
86 return;
87 }
88
89 d->recursiveForEach({}, floorLevel, func);
90}
91
92void ModelOverlaySourcePrivate::recursiveForEach(const QModelIndex &rootIdx, int floorLevel, const std::function<void (OSM::Element, int)> &func) const
93{
94 const auto rows = m_model->rowCount(rootIdx);
95 for (int i = 0; i < rows; ++i) {
96 const auto idx = m_model->index(i, 0, rootIdx);
97 const auto floor = idx.data(m_floorRole).toInt();
98 if (floor != floorLevel) {
99 continue;
100 }
101
102 recursiveForEach(idx, floorLevel, func);
103
104 const auto elem = idx.data(m_elementRole).value<OSM::Element>();
105 func(elem, floor);
106 }
107}
108
109void ModelOverlaySource::hiddenElements(std::vector<OSM::Element> &elems) const
110{
111 Q_D(const ModelOverlaySource);
112 if (!d->m_model || d->m_hiddenElementRole < 0) {
113 return;
114 }
115
116 const auto rows = d->m_model->rowCount();
117 for (int i = 0; i < rows; ++i) {
118 const auto idx = d->m_model->index(i, 0);
119 const auto elem = idx.data(d->m_hiddenElementRole).value<OSM::Element>();
120 if (elem.type() != OSM::Type::Null) {
121 elems.push_back(elem);
122 }
123 }
124}
125
126#include "moc_overlaysource.cpp"
A source for overlay elements, drawn on top of the static map data.
void reset()
Trigger style re-compilation.
void update()
Trigger map re-rendering when the source changes.
virtual void hiddenElements(std::vector< OSM::Element > &elems) const
Adds hidden elements to.
A source for overlay elements, based on a QAbstractItemModel as input.
void hiddenElements(std::vector< OSM::Element > &elems) const override
Adds hidden elements to.
void forEach(int floorLevel, const std::function< void(OSM::Element, int)> &func) const override
Iteration interface with floor level filtering.
A reference to any of OSM::Node/OSM::Way/OSM::Relation.
Definition element.h:24
OSM-based multi-floor indoor maps for buildings.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QHash< int, QByteArray > roleNames() const const
void rowsInserted(const QModelIndex &parent, int first, int last)
void rowsRemoved(const QModelIndex &parent, int first, int last)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:53:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.