KOSMIndoorMap

openinghourscache.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 "openinghourscache_p.h"
8#include "logging.h"
9
10#include <KOpeningHours/Interval>
11#include <KOpeningHours/OpeningHours>
12
13#include <QTimeZone>
14
15using namespace KOSMIndoorMap;
16
17OpeningHoursCache::OpeningHoursCache()
18{
19 setTimeRange({}, {});
20}
21
22OpeningHoursCache::~OpeningHoursCache() = default;
23
24void OpeningHoursCache::setMapData(const MapData &mapData)
25{
26 if (m_mapData == mapData) {
27 return;
28 }
29 m_mapData = mapData;
30 m_cacheEntries.clear();
31}
32
33void OpeningHoursCache::setTimeRange(const QDateTime &begin, const QDateTime &end)
34{
35 const auto actualBegin = begin.isValid() ? begin : QDateTime::currentDateTime();
36 const auto actualEnd = (end.isValid() && end > m_begin) ? end : actualBegin.addYears(1);
37
38 if (actualBegin == m_begin && actualEnd == m_end) {
39 return;
40 }
41
42 m_begin = actualBegin;
43 m_end = actualEnd;
44 m_cacheEntries.clear();
45}
46
47bool OpeningHoursCache::isClosed(OSM::Element elem, const QByteArray &oh)
48{
49 const auto key = elem.id();
50 const auto it = std::lower_bound(m_cacheEntries.begin(), m_cacheEntries.end(), key, [](auto lhs, auto rhs) {
51 return lhs.key < rhs;
52 });
53 if (it != m_cacheEntries.end() && (*it).key == key) {
54 return (*it).closed;
55 }
56
57 bool closed = false;
58
60 expr.setLocation(elem.center().latF(), elem.center().lonF());
61 expr.setRegion(m_mapData.regionCode());
62 expr.setTimeZone(m_mapData.timeZone());
63
64 if (expr.error() != KOpeningHours::OpeningHours::NoError) {
65 qCDebug(Log) << "opening hours expression error:" << expr.error() << oh << elem.url();
66 } else {
67 auto i = expr.interval(m_begin);
68 while (i.state() == KOpeningHours::Interval::Closed && !i.hasOpenEnd() && (i.end() < m_end || !m_end.isValid())) {
69 i = expr.nextInterval(i);
70 }
71 if (expr.error() != KOpeningHours::OpeningHours::NoError) {
72 qCDebug(Log) << "opening hours expression runtime error:" << expr.error() << oh << i << elem.url();
73 }
74 closed = i.state() == KOpeningHours::Interval::Closed;
75 }
76 m_cacheEntries.insert(it, {key, closed});
77 return closed;
78}
Raw OSM map data, separated by levels.
Definition mapdata.h:60
A reference to any of OSM::Node/OSM::Way/OSM::Relation.
Definition element.h:24
OSM-based multi-floor indoor maps for buildings.
const QList< QKeySequence > & begin()
const QList< QKeySequence > & end()
QDateTime currentDateTime()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:03 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.