KOSMIndoorMap

openinghourscache.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "config-kosmindoormap.h"
8 #include "openinghourscache_p.h"
9 #include "logging.h"
10 
11 #ifdef HAVE_KOPENINGHOURS
12 #include <KOpeningHours/Interval>
13 #include <KOpeningHours/OpeningHours>
14 #endif
15 
16 #include <QTimeZone>
17 
18 using namespace KOSMIndoorMap;
19 
20 OpeningHoursCache::OpeningHoursCache() = default;
21 OpeningHoursCache::~OpeningHoursCache() = default;
22 
23 void OpeningHoursCache::setMapData(const MapData &mapData)
24 {
25  if (m_mapData == mapData) {
26  return;
27  }
28  m_mapData = mapData;
29  m_cacheEntries.clear();
30 }
31 
32 void OpeningHoursCache::setTimeRange(const QDateTime &begin, const QDateTime &end)
33 {
34  if (begin == m_begin && end == m_end) {
35  return;
36  }
37 
38  m_begin = begin.isValid() ? begin : QDateTime::currentDateTime();
39  m_end = (end > m_begin) ? end : QDateTime();
40  m_cacheEntries.clear();
41 }
42 
43 bool OpeningHoursCache::isClosed(OSM::Element elem, const QByteArray &oh)
44 {
45 #ifndef HAVE_KOPENINGHOURS
46  Q_UNUSED(elem);
47  Q_UNUSED(oh);
48  return false;
49 #else
50  const auto key = elem.id();
51  const auto it = std::lower_bound(m_cacheEntries.begin(), m_cacheEntries.end(), key, [](auto lhs, auto rhs) {
52  return lhs.key < rhs;
53  });
54  if (it != m_cacheEntries.end() && (*it).key == key) {
55  return (*it).closed;
56  }
57 
58  bool closed = false;
59 
61  expr.setLocation(elem.center().latF(), elem.center().lonF());
62  expr.setRegion(m_mapData.regionCode());
63  expr.setTimeZone(m_mapData.timeZone());
64 
65  if (expr.error() != KOpeningHours::OpeningHours::NoError) {
66  qCDebug(Log) << "opening hours expression error:" << expr.error() << oh << elem.url();
67  } else {
68  auto i = expr.interval(m_begin);
69  while (i.state() == KOpeningHours::Interval::Closed && !i.hasOpenEnd() && (i.end() < m_end || !m_end.isValid())) {
70  i = expr.nextInterval(i);
71  }
72  if (expr.error() != KOpeningHours::OpeningHours::NoError) {
73  qCDebug(Log) << "opening hours expression runtime error:" << expr.error() << oh << i << elem.url();
74  }
75  closed = i.state() == KOpeningHours::Interval::Closed;
76  }
77  m_cacheEntries.insert(it, {key, closed});
78  return closed;
79 #endif
80 }
OSM-based multi-floor indoor maps for buildings.
A reference to any of OSM::Node/OSMWay/OSMRelation.
Definition: element.h:22
QString & insert(int position, QChar ch)
Raw OSM map data, separated by levels.
Definition: mapdata.h:59
bool isValid() const const
QDateTime currentDateTime()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 23:04:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.