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
50
54
55void AbstractOverlaySource::hiddenElements([[maybe_unused]] std::vector<OSM::Element> &elems) const
56{
57}
58
59const std::vector<OSM::Node>* AbstractOverlaySource::transientNodes() const
60{
61 return nullptr;
62}
63
64
65ModelOverlaySource::ModelOverlaySource(QAbstractItemModel *model, QObject *parent)
66 : AbstractOverlaySource(new ModelOverlaySourcePrivate, parent)
67{
69 const auto roles = model->roleNames();
70 for (auto it = roles.begin(); it != roles.end(); ++it) {
71 if (it.value() == "osmElement") {
72 d->m_elementRole = it.key();
73 } else if (it.value() == "level") {
74 d->m_floorRole = it.key();
75 } else if (it.value() == "hiddenElement") {
76 d->m_hiddenElementRole = it.key();
77 }
78 }
79 if (d->m_elementRole < 0 || d->m_floorRole < 0) {
80 qWarning() << model << " - model does not provide the required roles!";
81 return;
82 }
83 d->m_model = model;
84
89
91}
92
93ModelOverlaySource::~ModelOverlaySource() = default;
94
95void ModelOverlaySource::forEach(int floorLevel, const std::function<void (OSM::Element, int)> &func) const
96{
98 if (!d->m_model) {
99 return;
100 }
101
102 d->recursiveForEach({}, floorLevel, func);
103}
104
105void ModelOverlaySourcePrivate::recursiveForEach(const QModelIndex &rootIdx, int floorLevel, const std::function<void (OSM::Element, int)> &func) const
106{
107 const auto rows = m_model->rowCount(rootIdx);
108 for (int i = 0; i < rows; ++i) {
109 const auto idx = m_model->index(i, 0, rootIdx);
110 const auto floor = idx.data(m_floorRole).toInt();
111 if (floor != floorLevel) {
112 continue;
113 }
114
115 recursiveForEach(idx, floorLevel, func);
116
117 const auto elem = idx.data(m_elementRole).value<OSM::Element>();
118 func(elem, floor);
119 }
120}
121
122void ModelOverlaySource::hiddenElements(std::vector<OSM::Element> &elems) const
123{
124 Q_D(const ModelOverlaySource);
125 if (!d->m_model || d->m_hiddenElementRole < 0) {
126 return;
127 }
128
129 const auto rows = d->m_model->rowCount();
130 for (int i = 0; i < rows; ++i) {
131 const auto idx = d->m_model->index(i, 0);
132 const auto elem = idx.data(d->m_hiddenElementRole).value<OSM::Element>();
133 if (elem.type() != OSM::Type::Null) {
134 elems.push_back(elem);
135 }
136 }
137}
138
139#include "moc_overlaysource.cpp"
A source for overlay elements, drawn on top of the static map data.
virtual void endSwap()
Indicates the end of a scene graph update.
virtual void beginSwap()
Indicates the being of a scene graph update.
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.
virtual const std::vector< OSM::Node > * transientNodes() const
Nodes for newly created geometry.
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 Oct 4 2024 12:02:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.