KOpeningHours

rule.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 "rule_p.h"
8#include "logging.h"
9#include "openinghours_p.h"
10
11
12using namespace KOpeningHours;
13
14Interval::State Rule::state() const
15{
16 if (m_state == Interval::Invalid) { // implicitly defined state
17 return m_comment.isEmpty() ? Interval::Open : Interval::Unknown;
18 }
19 return m_state;
20}
21
22bool Rule::hasImplicitState() const
23{
24 return m_state == Interval::Invalid;
25}
26
27void Rule::setState(State state)
28{
29 if (state == State::Off) {
30 m_state = Interval::Closed;
31 m_stateFlags = Off;
32 } else {
33 m_state = static_cast<Interval::State>(state);
34 }
35}
36
37void Rule::copyStateFrom(const Rule &otherRule)
38{
39 m_state = otherRule.m_state;
40 m_stateFlags = otherRule.m_stateFlags;
41}
42
43bool Rule::hasComment() const
44{
45 return !m_comment.isEmpty();
46}
47
48void Rule::setComment(const char *str, int len)
49{
50 m_comment = QString::fromUtf8(str, len);
51}
52
53int Rule::requiredCapabilities() const
54{
55 int c = Capability::None;
56 c |= m_timeSelector ? m_timeSelector->requiredCapabilities() : Capability::None;
57 c |= m_weekdaySelector ? m_weekdaySelector->requiredCapabilities() : Capability::None;
58 c |= m_weekSelector ? m_weekSelector->requiredCapabilities() : Capability::None;
59 c |= m_monthdaySelector ? m_monthdaySelector->requiredCapabilities() : Capability::None;
60 c |= m_yearSelector ? m_yearSelector->requiredCapabilities() : Capability::None;
61 c |= m_wideRangeSelectorComment.isEmpty() ? Capability::None : Capability::NotImplemented;
62
63 return c;
64}
65
66bool Rule::isEmpty() const
67{
68 return !hasComment()
69 && hasImplicitState()
70 && (selectorCount() == 0)
71 && !m_seen_24_7
72 && m_wideRangeSelectorComment.isEmpty();
73}
74
75bool Rule::hasSmallRangeSelector() const
76{
77 return m_weekdaySelector || m_timeSelector;
78}
79
80bool Rule::hasWideRangeSelector() const
81{
82 return m_yearSelector || m_weekSelector || m_monthdaySelector || !m_wideRangeSelectorComment.isEmpty();
83}
84
85QByteArray Rule::toExpression() const
86{
87 QByteArray expr;
88 if (!m_timeSelector && !m_weekdaySelector && !m_monthdaySelector && !m_weekSelector && !m_yearSelector) {
89 if (m_seen_24_7) {
90 expr = "24/7";
91 }
92 }
93 auto maybeSpace = [&]() {
94 if (!expr.isEmpty()) {
95 expr += ' ';
96 }
97 };
98 if (m_yearSelector) {
99 expr = m_yearSelector->toExpression();
100 }
101 if (m_monthdaySelector) {
102 maybeSpace();
103 expr += m_monthdaySelector->toExpression({});
104 }
105 if (m_weekSelector) {
106 maybeSpace();
107 expr += "week " + m_weekSelector->toExpression();
108 }
109 if (!m_wideRangeSelectorComment.isEmpty()) {
110 expr += '"' + m_wideRangeSelectorComment.toUtf8() + '"';
111 }
112 if (m_colonAfterWideRangeSelector) {
113 expr += ':';
114 }
115 if (m_weekdaySelector) {
116 maybeSpace();
117 expr += m_weekdaySelector->toExpression();
118 }
119 if (m_timeSelector) {
120 maybeSpace();
121 expr += m_timeSelector->toExpression();
122 }
123 switch (m_state) {
124 case Interval::Open:
125 maybeSpace();
126 expr += "open";
127 break;
128 case Interval::Closed:
129 maybeSpace();
130 expr += m_stateFlags & Off ? "off" : "closed";
131 break;
132 case Interval::Unknown:
133 maybeSpace();
134 expr += "unknown";
135 break;
136 case Interval::Invalid:
137 break;
138 }
139 if (!m_comment.isEmpty()) {
140 if (!expr.isEmpty()) {
141 expr += ' ';
142 }
143 expr += '"' + m_comment.toUtf8() + '"';
144 }
145 return expr;
146}
147
148int Rule::selectorCount() const
149{
150 const auto selectors = { (bool)m_yearSelector, (bool)m_monthdaySelector, (bool)m_weekSelector, (bool)m_weekdaySelector, (bool)m_timeSelector };
151 return std::count(std::begin(selectors), std::end(selectors), true);
152}
State
Opening state during a time interval.
Definition interval.h:97
OSM opening hours parsing and evaluation.
Definition display.h:16
bool isEmpty() const const
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.