Eventviews

timescaleconfigdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno@virlet.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5*/
6#include "timescaleconfigdialog.h"
7#include "prefs.h"
8
9#include <KCalUtils/Stringify>
10
11#include <KLocalizedString>
12
13#include <QDialogButtonBox>
14#include <QIcon>
15#include <QPushButton>
16#include <QTimeZone>
17#include <QVBoxLayout>
18
19using namespace EventViews;
20
21class EventViews::TimeScaleConfigDialogPrivate
22{
23public:
24 TimeScaleConfigDialogPrivate(TimeScaleConfigDialog *parent, const PrefsPtr &preferences)
25 : q(parent)
26 , mPreferences(preferences)
27 {
28 }
29
30public:
31 TimeScaleConfigDialog *const q;
32 PrefsPtr mPreferences;
33};
34
35enum {
36 TimeZoneNameRole = Qt::UserRole
37};
38
39using TimeZoneNamePair = QPair<QString, QByteArray>;
40
41static QString tzWithUTC(const QByteArray &zoneId)
42{
43 auto tz = QTimeZone(zoneId);
44 return QStringLiteral("%1 (%2)").arg(i18n(zoneId.constData()), KCalUtils::Stringify::tzUTCOffsetStr(tz));
45}
46
47TimeScaleConfigDialog::TimeScaleConfigDialog(const PrefsPtr &preferences, QWidget *parent)
48 : QDialog(parent)
49 , d(new TimeScaleConfigDialogPrivate(this, preferences))
50{
51 setWindowTitle(i18nc("@title:window", "Timezone"));
52 auto mainLayout = new QVBoxLayout(this);
53 setModal(true);
54
55 auto mainwidget = new QWidget(this);
56 setupUi(mainwidget);
57
58 mainLayout->addWidget(mainwidget);
59
61 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
62 okButton->setDefault(true);
64 connect(buttonBox, &QDialogButtonBox::rejected, this, &TimeScaleConfigDialog::reject);
65
66 mainLayout->addWidget(buttonBox);
67
68 QStringList shownTimeZones(QString::fromUtf8(d->mPreferences->timeZone().id()));
69 shownTimeZones += d->mPreferences->timeScaleTimezones();
70 shownTimeZones.removeDuplicates();
71
74 const auto zoneIds = QTimeZone::availableTimeZoneIds();
75 for (const auto &zoneId : std::as_const(zoneIds)) {
76 // do not list timezones already shown
77 if (!shownTimeZones.contains(QString::fromUtf8(zoneId))) {
78 availList.append(TimeZoneNamePair(tzWithUTC(zoneId), zoneId));
79 } else {
80 selList.append(TimeZoneNamePair(tzWithUTC(zoneId), zoneId));
81 }
82 }
83 std::sort(availList.begin(), availList.end());
84
85 for (const TimeZoneNamePair &item : std::as_const(availList)) {
86 zoneCombo->addItem(item.first, item.second);
87 }
88 zoneCombo->setCurrentIndex(0);
89
90 addButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
91 removeButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
92 upButton->setIcon(QIcon::fromTheme(QStringLiteral("go-up")));
93 downButton->setIcon(QIcon::fromTheme(QStringLiteral("go-down")));
94
95 connect(addButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::add);
96 connect(removeButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::remove);
97 connect(upButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::up);
98 connect(downButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::down);
99
100 connect(okButton, &QPushButton::clicked, this, &TimeScaleConfigDialog::okClicked);
101 connect(buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &TimeScaleConfigDialog::reject);
102 connect(listWidget, &QListWidget::currentItemChanged, this, &TimeScaleConfigDialog::slotUpdateButton);
103
104 for (const TimeZoneNamePair &item : std::as_const(selList)) {
105 auto widgetItem = new QListWidgetItem(item.first);
106 widgetItem->setData(TimeZoneNameRole, item.second);
107 listWidget->addItem(widgetItem);
108 }
109 slotUpdateButton();
110}
111
112TimeScaleConfigDialog::~TimeScaleConfigDialog() = default;
113
114void TimeScaleConfigDialog::slotUpdateButton()
115{
116 removeButton->setEnabled(listWidget->currentItem());
117 const bool numberElementMoreThanOneElement = (listWidget->count() > 1);
118 upButton->setEnabled(numberElementMoreThanOneElement && (listWidget->currentRow() >= 1));
119 downButton->setEnabled(numberElementMoreThanOneElement && (listWidget->currentRow() < listWidget->count() - 1));
120}
121
122void TimeScaleConfigDialog::okClicked()
123{
124 d->mPreferences->setTimeScaleTimezones(zones());
125 d->mPreferences->writeConfig();
126 accept();
127}
128
129void TimeScaleConfigDialog::add()
130{
131 // Do not add duplicates
132 if (zoneCombo->currentIndex() >= 0) {
133 const int numberItem(listWidget->count());
134 for (int i = 0; i < numberItem; ++i) {
135 if (listWidget->item(i)->data(TimeZoneNameRole).toString() == zoneCombo->itemData(zoneCombo->currentIndex(), TimeZoneNameRole).toString()) {
136 return;
137 }
138 }
139
140 auto item = new QListWidgetItem(zoneCombo->currentText());
141 item->setData(TimeZoneNameRole, zoneCombo->itemData(zoneCombo->currentIndex(), TimeZoneNameRole).toString());
142 listWidget->addItem(item);
143 zoneCombo->removeItem(zoneCombo->currentIndex());
144 }
145 slotUpdateButton();
146}
147
148void TimeScaleConfigDialog::remove()
149{
150 zoneCombo->insertItem(0, listWidget->currentItem()->text(), zoneCombo->itemData(zoneCombo->currentIndex(), TimeZoneNameRole).toString());
151 delete listWidget->takeItem(listWidget->currentRow());
152 slotUpdateButton();
153}
154
155void TimeScaleConfigDialog::up()
156{
157 int row = listWidget->currentRow();
158 QListWidgetItem *item = listWidget->takeItem(row);
159 listWidget->insertItem(qMax(row - 1, 0), item);
160 listWidget->setCurrentRow(qMax(row - 1, 0));
161}
162
163void TimeScaleConfigDialog::down()
164{
165 int row = listWidget->currentRow();
166 QListWidgetItem *item = listWidget->takeItem(row);
167 listWidget->insertItem(qMin(row + 1, listWidget->count()), item);
168 listWidget->setCurrentRow(qMin(row + 1, listWidget->count() - 1));
169}
170
171QStringList TimeScaleConfigDialog::zones() const
172{
174 const int count = listWidget->count();
175 list.reserve(count);
176 for (int i = 0; i < count; ++i) {
177 list << listWidget->item(i)->data(TimeZoneNameRole).toString();
178 }
179 return list;
180}
181
182#include "moc_timescaleconfigdialog.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Namespace EventViews provides facilities for displaying incidences, including events,...
Definition agenda.h:33
KCALUTILS_EXPORT QString tzUTCOffsetStr(const QTimeZone &tz)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & preferences()
void clicked(bool checked)
void setShortcut(const QKeySequence &key)
const char * constData() const const
virtual void accept()
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
iterator begin()
qsizetype count() const const
pointer data()
iterator end()
void reserve(qsizetype size)
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
void setDefault(bool)
QString fromUtf8(QByteArrayView str)
UserRole
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QList< QByteArray > availableTimeZoneIds()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.