File indexing completed on 2024-04-28 05:11:37
0001 /* 0002 SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno.virlet@gmail.com> 0003 SPDX-FileCopyrightText: 2008-2009, 2013 Allen Winter <winter@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "ktimezonecombobox.h" 0009 0010 #include <KLocalizedString> 0011 0012 #include <QList> 0013 0014 using namespace IncidenceEditorNG; 0015 0016 class IncidenceEditorNG::KTimeZoneComboBoxPrivate 0017 { 0018 public: 0019 KTimeZoneComboBoxPrivate(KTimeZoneComboBox *parent) 0020 : mParent(parent) 0021 { 0022 } 0023 0024 void fillComboBox(); 0025 KTimeZoneComboBox *const mParent; 0026 QList<QByteArray> mZones; 0027 }; 0028 0029 void KTimeZoneComboBoxPrivate::fillComboBox() 0030 { 0031 mParent->clear(); 0032 mZones.clear(); 0033 0034 // Read all known time zones. 0035 const QList<QByteArray> lstTimeZoneIds = QTimeZone::availableTimeZoneIds(); 0036 mZones.reserve(lstTimeZoneIds.count() + 3); 0037 0038 // Prepend the system time zone, UTC and Floating, for convenience. 0039 mZones.append(QTimeZone::systemTimeZoneId()); 0040 mZones.append("Floating"); 0041 mZones.append("UTC"); 0042 const auto sortStart = mZones.end(); 0043 0044 std::copy(lstTimeZoneIds.begin(), lstTimeZoneIds.end(), std::back_inserter(mZones)); 0045 std::sort(sortStart, mZones.end()); // clazy:exclude=detaching-member 0046 0047 // Put translated zones into the combobox 0048 for (const auto &z : std::as_const(mZones)) { 0049 mParent->addItem(i18n(z.constData()).replace(QLatin1Char('_'), QLatin1Char(' '))); 0050 } 0051 } 0052 0053 KTimeZoneComboBox::KTimeZoneComboBox(QWidget *parent) 0054 : QComboBox(parent) 0055 , d(new KTimeZoneComboBoxPrivate(this)) 0056 { 0057 d->fillComboBox(); 0058 } 0059 0060 KTimeZoneComboBox::~KTimeZoneComboBox() = default; 0061 0062 void KTimeZoneComboBox::selectTimeZone(const QTimeZone &zone) 0063 { 0064 int nCurrentlySet = -1; 0065 0066 int i = 0; 0067 for (const auto &z : std::as_const(d->mZones)) { 0068 if (z == zone.id()) { 0069 nCurrentlySet = i; 0070 break; 0071 } 0072 ++i; 0073 } 0074 0075 if (nCurrentlySet == -1) { 0076 if (zone == QTimeZone::utc()) { 0077 setCurrentIndex(2); // UTC 0078 } else if (zone == QTimeZone::systemTimeZone()) { 0079 setCurrentIndex(0); // Local 0080 } else { 0081 setCurrentIndex(1); // Floating event 0082 } 0083 } else { 0084 setCurrentIndex(nCurrentlySet); 0085 } 0086 } 0087 0088 void KTimeZoneComboBox::selectTimeZoneFor(const QDateTime &dateTime) 0089 { 0090 if (dateTime.timeSpec() == Qt::LocalTime) 0091 setCurrentIndex(1); // Floating 0092 else 0093 selectTimeZone(dateTime.timeZone()); 0094 } 0095 0096 QTimeZone KTimeZoneComboBox::selectedTimeZone() const 0097 { 0098 QTimeZone zone; 0099 if (currentIndex() >= 0) { 0100 if (currentIndex() == 0) { // Local 0101 zone = QTimeZone::systemTimeZone(); 0102 } else if (currentIndex() == 1) { // Floating event 0103 zone = QTimeZone::systemTimeZone(); 0104 } else if (currentIndex() == 2) { // UTC 0105 zone = QTimeZone::utc(); 0106 } else { 0107 zone = QTimeZone(d->mZones[currentIndex()]); 0108 } 0109 } 0110 0111 return zone; 0112 } 0113 0114 void KTimeZoneComboBox::selectLocalTimeZone() 0115 { 0116 selectTimeZone(QTimeZone::systemTimeZone()); 0117 } 0118 0119 void KTimeZoneComboBox::setFloating(bool floating, const QTimeZone &zone) 0120 { 0121 if (floating) { 0122 setCurrentIndex(1); 0123 } else { 0124 if (zone.isValid()) { 0125 selectTimeZone(zone); 0126 } else { 0127 selectLocalTimeZone(); 0128 } 0129 } 0130 } 0131 0132 void KTimeZoneComboBox::applyTimeZoneTo(QDateTime &dt) const 0133 { 0134 if (isFloating()) { 0135 dt.setTimeZone(QTimeZone::LocalTime); 0136 } else { 0137 dt.setTimeZone(selectedTimeZone()); 0138 } 0139 } 0140 0141 bool KTimeZoneComboBox::isFloating() const 0142 { 0143 return currentIndex() == 1; 0144 } 0145 0146 #include "moc_ktimezonecombobox.cpp"