Incidenceeditor

ktimezonecombobox.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno.virlet@gmail.com>
3 SPDX-FileCopyrightText: 2008-2009, 2013 Allen Winter <winter@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "ktimezonecombobox.h"
9
10#include <KLocalizedString>
11
12#include <QList>
13
14using namespace IncidenceEditorNG;
15
16class IncidenceEditorNG::KTimeZoneComboBoxPrivate
17{
18public:
19 KTimeZoneComboBoxPrivate(KTimeZoneComboBox *parent)
20 : mParent(parent)
21 {
22 }
23
24 void fillComboBox();
25 KTimeZoneComboBox *const mParent;
26 QList<QByteArray> mZones;
27};
28
29void KTimeZoneComboBoxPrivate::fillComboBox()
30{
31 mParent->clear();
32 mZones.clear();
33
34 // Read all known time zones.
36 mZones.reserve(lstTimeZoneIds.count() + 3);
37
38 // Prepend the system time zone, UTC and Floating, for convenience.
40 mZones.append("Floating");
41 mZones.append("UTC");
42 const auto sortStart = mZones.end();
43
44 std::copy(lstTimeZoneIds.begin(), lstTimeZoneIds.end(), std::back_inserter(mZones));
45 std::sort(sortStart, mZones.end()); // clazy:exclude=detaching-member
46
47 // Put translated zones into the combobox
48 for (const auto &z : std::as_const(mZones)) {
49 mParent->addItem(i18n(z.constData()).replace(QLatin1Char('_'), QLatin1Char(' ')));
50 }
51}
52
54 : QComboBox(parent)
55 , d(new KTimeZoneComboBoxPrivate(this))
56{
57 d->fillComboBox();
58}
59
61
63{
64 int nCurrentlySet = -1;
65
66 int i = 0;
67 for (const auto &z : std::as_const(d->mZones)) {
68 if (z == zone.id()) {
70 break;
71 }
72 ++i;
73 }
74
75 if (nCurrentlySet == -1) {
76 if (zone == QTimeZone::utc()) {
77 setCurrentIndex(2); // UTC
78 } else if (zone == QTimeZone::systemTimeZone()) {
79 setCurrentIndex(0); // Local
80 } else {
81 setCurrentIndex(1); // Floating event
82 }
83 } else {
85 }
86}
87
89{
90 if (dateTime.timeSpec() == Qt::LocalTime)
91 setCurrentIndex(1); // Floating
92 else
93 selectTimeZone(dateTime.timeZone());
94}
95
97{
99 if (currentIndex() >= 0) {
100 if (currentIndex() == 0) { // Local
102 } else if (currentIndex() == 1) { // Floating event
104 } else if (currentIndex() == 2) { // UTC
106 } else {
107 zone = QTimeZone(d->mZones[currentIndex()]);
108 }
109 }
110
111 return zone;
112}
113
118
119void KTimeZoneComboBox::setFloating(bool floating, const QTimeZone &zone)
120{
121 if (floating) {
123 } else {
124 if (zone.isValid()) {
126 } else {
128 }
129 }
130}
131
133{
134 if (isFloating()) {
135 dt.setTimeZone(QTimeZone::LocalTime);
136 } else {
137 dt.setTimeZone(selectedTimeZone());
138 }
139}
140
142{
143 return currentIndex() == 1;
144}
145
146#include "moc_ktimezonecombobox.cpp"
A combobox that shows the system timezones available in QTimeZone and provides methods to easily sele...
KTimeZoneComboBox(QWidget *parent=nullptr)
Creates a new time zone combobox.
QTimeZone selectedTimeZone() const
Return the time zone associated with the currently selected item.
void selectTimeZone(const QTimeZone &zone)
Selects the item in the combobox corresponding to the given zone.
void applyTimeZoneTo(QDateTime &dt) const
Applies the selected timezone to the given QDateTime This isn't the same as dt.setTimeZone(selectedTi...
void selectTimeZoneFor(const QDateTime &dateTime)
Selects the item in the combobox corresponding to the zone for the given datetime.
~KTimeZoneComboBox() override
Destroys the time zone combobox.
bool isFloating() const
Returns true if the selecting timezone is the floating time zone.
void setFloating(bool floating, const QTimeZone &zone={})
If floating is true, selects floating time zone, otherwise if @zone is valid, selects @pzone time zon...
void selectLocalTimeZone()
Convenience version of selectTimeZone(const QTimeZone &).
QString i18n(const char *text, const TYPE &arg...)
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
void clear()
void setCurrentIndex(int index)
Qt::TimeSpec timeSpec() const const
QTimeZone timeZone() const const
void append(QList< T > &&value)
iterator begin()
void clear()
qsizetype count() const const
iterator end()
void reserve(qsizetype size)
T qobject_cast(QObject *object)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
LocalTime
QList< QByteArray > availableTimeZoneIds()
QTimeZone systemTimeZone()
QByteArray systemTimeZoneId()
QTimeZone utc()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:37 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.