/
ClockWidgetConfigureDialog.cxx
101 lines (79 loc) · 3.5 KB
/
ClockWidgetConfigureDialog.cxx
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
91
92
93
94
95
96
97
98
99
100
101
/*
Copyright 2017 Martin Koller, kollix@aon.at
This file is part of liquidshell.
liquidshell is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
liquidshell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with liquidshell. If not, see <http://www.gnu.org/licenses/>.
*/
#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("Country"));
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::countryToString(timeZone.country()));
// Locate the flag from share/kf5/locale/countries/%1/flag.png
QList<QLocale> matchingLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, timeZone.country());
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;
}
//--------------------------------------------------------------------------------