-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathClockWidgetConfigureDialog.cxx
More file actions
90 lines (70 loc) · 2.96 KB
/
ClockWidgetConfigureDialog.cxx
File metadata and controls
90 lines (70 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
This file is part of liquidshell.
SPDX-FileCopyrightText: 2017 - 2024 Martin Koller <martin@kollix.at>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <ClockWidgetConfigureDialog.hxx>
#include <QTreeWidget>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QTimeZone>
#include <QHeaderView>
#include <QStandardPaths>
#include <QPixmap>
#include <KLocalizedString>
#include <KTreeWidgetSearchLine>
//--------------------------------------------------------------------------------
ClockWidgetConfigureDialog::ClockWidgetConfigureDialog(QWidget *parent, const QVector<QByteArray> &timeZoneIds)
: QDialog(parent)
{
tree = new QTreeWidget;
QLineEdit *filter = new KTreeWidgetSearchLine(this, tree);
filter->setClearButtonEnabled(true);
filter->setPlaceholderText(i18n("Filter"));
QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
QVBoxLayout *vbox = new QVBoxLayout(this);
vbox->addWidget(filter);
vbox->addWidget(tree);
vbox->addWidget(buttons);
// fill tree
tree->setHeaderLabels(QStringList() << i18n("Timezone") << i18n("Territory"));
tree->setRootIsDecorated(false);
QList<QByteArray> zones = QTimeZone::availableTimeZoneIds();
for (const QByteArray &zone : zones)
{
QTimeZone timeZone(zone);
QTreeWidgetItem *item = new QTreeWidgetItem(tree);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable |
Qt::ItemIsEnabled | Qt::ItemNeverHasChildren);
item->setCheckState(0, timeZoneIds.contains(zone) ? Qt::Checked : Qt::Unchecked);
item->setText(0, timeZone.id());
item->setText(1, QLocale::territoryToString(timeZone.territory()));
// Locate the flag from share/kf5/locale/countries/%1/flag.png
QList<QLocale> matchingLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, timeZone.territory());
if ( !matchingLocales.isEmpty() )
{
QString flag = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
QString("kf5/locale/countries/%1/flag.png").arg(matchingLocales[0].name().mid(3).toLower()));
if ( !flag.isEmpty() )
item->setIcon(1, QPixmap(flag));
}
}
tree->setSortingEnabled(true);
tree->sortByColumn(0, Qt::AscendingOrder);
tree->header()->resizeSections(QHeaderView::ResizeToContents);
}
//--------------------------------------------------------------------------------
QVector<QByteArray> ClockWidgetConfigureDialog::getSelectedTimeZoneIds() const
{
QVector<QByteArray> timeZoneIds;
for (int i = 0; i < tree->topLevelItemCount(); i++)
{
QTreeWidgetItem *item = tree->topLevelItem(i);
if ( item->checkState(0) == Qt::Checked )
timeZoneIds.append(item->text(0).toUtf8());
}
return timeZoneIds;
}
//--------------------------------------------------------------------------------