KOpeningHours

display.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 "display.h"
8#include "interval.h"
9#include "openinghours.h"
10
11#include <KLocalizedString>
12
13#include <cmath>
14
15using namespace KOpeningHours;
16
18{
19 if (oh.error() != OpeningHours::NoError) {
20 return {};
21 }
22
23 const auto now = QDateTime::currentDateTime();
24 const auto i = oh.interval(now);
25 if (i.hasOpenEnd()) {
26 switch (i.state()) {
27 case Interval::Open:
28 return i.comment().isEmpty() ? i18n("Open") : i18n("Open (%1)", i.comment());
29 case Interval::Closed:
30 return i.comment().isEmpty() ? i18n("Closed") : i18n("Closed (%1)", i.comment());
31 case Interval::Unknown:
32 case Interval::Invalid:
33 return {};
34 }
35 }
36
37 const auto next = oh.nextInterval(i);
38 // common transitions we have texts for
39 if ((i.state() == Interval::Closed && next.state() == Interval::Open)
40 || (i.state() == Interval::Open && next.state() == Interval::Closed)
41 ) {
42
43 // time to change is 90 min or less
44 const auto timeToChange = now.secsTo(i.end());
45 if (timeToChange < 90 * 60) {
46 const int minDiff = std::ceil(timeToChange / 60.0);
47 switch (i.state()) {
48 case Interval::Open:
49 return i.comment().isEmpty()
50 ? i18np("Open for one more minute", "Open for %1 more minutes", minDiff)
51 : i18np("Open for one more minute (%2)", "Open for %1 more minutes (%2)", minDiff, i.comment());
52 case Interval::Closed:
53 return i.comment().isEmpty()
54 ? i18np("Currently closed, opens in one minute", "Currently closed, opens in %1 minutes", minDiff)
55 : i18np("Currently closed (%2), opens in one minute", "Currently closed (%2), opens in %1 minutes", minDiff, i.comment());
56 case Interval::Unknown:
57 case Interval::Invalid:
58 return {};
59 }
60 }
61
62 // time to change is 24h or less
63 if (timeToChange < 24 * 60 * 60) {
64 const int hourDiff = std::round(timeToChange / (60.0 * 60.0));
65 switch (i.state()) {
66 case Interval::Open:
67 return i.comment().isEmpty()
68 ? i18np("Open for one more hour", "Open for %1 more hours", hourDiff)
69 : i18np("Open for one more hour (%2)", "Open for %1 more hours (%2)", hourDiff, i.comment());
70 case Interval::Closed:
71 return i.comment().isEmpty()
72 ? i18np("Currently closed, opens in one hour", "Currently closed, opens in %1 hours", hourDiff)
73 : i18np("Currently closed (%2), opens in one hour", "Currently closed (%2), opens in %1 hours", hourDiff, i.comment());
74 case Interval::Unknown:
75 case Interval::Invalid:
76 return {};
77 }
78 }
79
80 // time to change is 7 days or less
81 if (timeToChange < 7 * 24 * 60 * 60) {
82 const int dayDiff = std::round(timeToChange / (24.0 * 60.0 * 60.0));
83 switch (i.state()) {
84 case Interval::Open:
85 return i.comment().isEmpty()
86 ? i18np("Open for one more day", "Open for %1 more days", dayDiff)
87 : i18np("Open for one more day (%2)", "Open for %1 more days (%2)", dayDiff, i.comment());
88 case Interval::Closed:
89 return i.comment().isEmpty()
90 ? i18np("Currently closed, opens in one day", "Currently closed, opens in %1 days", dayDiff)
91 : i18np("Currently closed (%2), opens in one day", "Currently closed (%2), opens in %1 days", dayDiff, i.comment());
92 case Interval::Unknown:
93 case Interval::Invalid:
94 return {};
95 }
96 }
97 }
98
99 // next change is further ahead than one week, or we are transitioning in a way we don't handle above
100 switch (i.state()) {
101 case Interval::Open:
102 return i.comment().isEmpty() ? i18n("Currently open") : i18n("Currently open (%1)", i.comment());
103 case Interval::Closed:
104 return i.comment().isEmpty() ? i18n("Currently closed") : i18n("Currently closed (%1)", i.comment());
105 case Interval::Unknown:
106 return i.comment();
107 case Interval::Invalid:
108 return {};
109 }
110
111 return {};
112}
113
114#include "moc_display.cpp"
static Q_INVOKABLE QString currentState(const KOpeningHours::OpeningHours &oh)
Localized description of the current opening state, and upcoming transitions.
Definition display.cpp:17
An OSM opening hours specification.
Q_INVOKABLE KOpeningHours::Interval interval(const QDateTime &dt) const
Returns the interval containing dt.
Q_INVOKABLE KOpeningHours::Interval nextInterval(const KOpeningHours::Interval &interval) const
Returns the interval immediately following interval.
@ NoError
there is no error, the expression is valid and can be used
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
OSM opening hours parsing and evaluation.
Definition display.h:16
QDateTime currentDateTime()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 11:58:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.