KOSMIndoorMap

overlaysource.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "overlaysource.h"
8 
9 #include <QAbstractItemModel>
10 #include <QDebug>
11 
12 using namespace KOSMIndoorMap;
13 
14 namespace KOSMIndoorMap
15 {
16 class AbstractOverlaySourcePrivate {
17 public:
18  virtual ~AbstractOverlaySourcePrivate() = default;
19 };
20 
21 class ModelOverlaySourcePrivate : public AbstractOverlaySourcePrivate {
22 public:
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 
34 AbstractOverlaySource::AbstractOverlaySource(AbstractOverlaySourcePrivate *dd, QObject *parent)
35  : QObject(parent)
36  , d_ptr(dd)
37 {
38 }
39 
40 AbstractOverlaySource::~AbstractOverlaySource() = default;
41 
42 
43 ModelOverlaySource::ModelOverlaySource(QAbstractItemModel *model, QObject *parent)
44  : AbstractOverlaySource(new ModelOverlaySourcePrivate, parent)
45 {
46  Q_D(ModelOverlaySource);
47  const auto roles = model->roleNames();
48  for (auto it = roles.begin(); it != roles.end(); ++it) {
49  if (it.value() == "osmElement") {
50  d->m_elementRole = it.key();
51  } else if (it.value() == "level") {
52  d->m_floorRole = it.key();
53  } else if (it.value() == "hiddenElement") {
54  d->m_hiddenElementRole = it.key();
55  }
56  }
57  if (d->m_elementRole < 0 || d->m_floorRole < 0) {
58  qWarning() << model << " - model does not provide the required roles!";
59  return;
60  }
61  d->m_model = model;
62 
63  connect(model, &QAbstractItemModel::modelReset, this, &ModelOverlaySource::update);
64  connect(model, &QAbstractItemModel::rowsInserted, this, &ModelOverlaySource::update);
65  connect(model, &QAbstractItemModel::rowsRemoved, this, &ModelOverlaySource::update);
66  connect(model, &QAbstractItemModel::dataChanged, this, &ModelOverlaySource::update);
67 
68  connect(model, &QAbstractItemModel::modelReset, this, &ModelOverlaySource::reset);
69 }
70 
71 ModelOverlaySource::~ModelOverlaySource() = default;
72 
73 void ModelOverlaySource::forEach(int floorLevel, const std::function<void (OSM::Element, int)> &func) const
74 {
75  Q_D(const ModelOverlaySource);
76  if (!d->m_model) {
77  return;
78  }
79 
80  d->recursiveForEach({}, floorLevel, func);
81 }
82 
83 void ModelOverlaySourcePrivate::recursiveForEach(const QModelIndex &rootIdx, int floorLevel, const std::function<void (OSM::Element, int)> &func) const
84 {
85  const auto rows = m_model->rowCount(rootIdx);
86  for (int i = 0; i < rows; ++i) {
87  const auto idx = m_model->index(i, 0, rootIdx);
88  const auto floor = idx.data(m_floorRole).toInt();
89  if (floor != floorLevel) {
90  continue;
91  }
92 
93  recursiveForEach(idx, floorLevel, func);
94 
95  const auto elem = idx.data(m_elementRole).value<OSM::Element>();
96  func(elem, floor);
97  }
98 }
99 
100 void ModelOverlaySource::hiddenElements(std::vector<OSM::Element> &elems) const
101 {
102  Q_D(const ModelOverlaySource);
103  if (!d->m_model || d->m_hiddenElementRole < 0) {
104  return;
105  }
106 
107  const auto rows = d->m_model->rowCount();
108  for (int i = 0; i < rows; ++i) {
109  const auto idx = d->m_model->index(i, 0);
110  const auto elem = idx.data(d->m_hiddenElementRole).value<OSM::Element>();
111  if (elem.type() != OSM::Type::Null) {
112  elems.push_back(elem);
113  }
114  }
115 }
A source for overlay elements, based on a QAbstractItemModel as input.
Definition: overlaysource.h:57
OSM-based multi-floor indoor maps for buildings.
virtual QHash< int, QByteArray > roleNames() const const
A reference to any of OSM::Node/OSMWay/OSMRelation.
Definition: element.h:22
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
void rowsRemoved(const QModelIndex &parent, int first, int last)
A source for overlay elements, drawn on top of the static map data.
Definition: overlaysource.h:26
void rowsInserted(const QModelIndex &parent, int first, int last)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Sep 25 2021 23:05:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.