File indexing completed on 2024-04-21 16:30:32

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <ClockWidgetConfigureDialog.hxx>
0022 
0023 #include <QTreeWidget>
0024 #include <QDialogButtonBox>
0025 #include <QVBoxLayout>
0026 #include <QTimeZone>
0027 #include <QHeaderView>
0028 #include <QStandardPaths>
0029 #include <QPixmap>
0030 
0031 #include <KLocalizedString>
0032 #include <KTreeWidgetSearchLine>
0033 
0034 //--------------------------------------------------------------------------------
0035 
0036 ClockWidgetConfigureDialog::ClockWidgetConfigureDialog(QWidget *parent, const QVector<QByteArray> &timeZoneIds)
0037   : QDialog(parent)
0038 {
0039   tree = new QTreeWidget;
0040   QLineEdit *filter = new KTreeWidgetSearchLine(this, tree);
0041   filter->setClearButtonEnabled(true);
0042   filter->setPlaceholderText(i18n("Filter"));
0043   QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0044   connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
0045   connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
0046 
0047   QVBoxLayout *vbox = new QVBoxLayout(this);
0048   vbox->addWidget(filter);
0049   vbox->addWidget(tree);
0050   vbox->addWidget(buttons);
0051 
0052   // fill tree
0053   tree->setHeaderLabels(QStringList() << i18n("Timezone") << i18n("Country"));
0054   tree->setRootIsDecorated(false);
0055 
0056   QList<QByteArray> zones = QTimeZone::availableTimeZoneIds();
0057 
0058   for (const QByteArray &zone : zones)
0059   {
0060     QTimeZone timeZone(zone);
0061     QTreeWidgetItem *item = new QTreeWidgetItem(tree);
0062 
0063     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable |
0064                    Qt::ItemIsEnabled | Qt::ItemNeverHasChildren);
0065 
0066     item->setCheckState(0, timeZoneIds.contains(zone) ? Qt::Checked : Qt::Unchecked);
0067     item->setText(0, timeZone.id());
0068     item->setText(1, QLocale::countryToString(timeZone.country()));
0069 
0070     // Locate the flag from share/kf5/locale/countries/%1/flag.png
0071     QList<QLocale> matchingLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, timeZone.country());
0072     if ( !matchingLocales.isEmpty() )
0073     {
0074       QString flag = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
0075                          QString("kf5/locale/countries/%1/flag.png").arg(matchingLocales[0].name().mid(3).toLower()));
0076       if ( !flag.isEmpty() )
0077         item->setIcon(1, QPixmap(flag));
0078     }
0079   }
0080 
0081   tree->setSortingEnabled(true);
0082   tree->sortByColumn(0, Qt::AscendingOrder);
0083   tree->header()->resizeSections(QHeaderView::ResizeToContents);
0084 }
0085 
0086 //--------------------------------------------------------------------------------
0087 
0088 QVector<QByteArray> ClockWidgetConfigureDialog::getSelectedTimeZoneIds() const
0089 {
0090   QVector<QByteArray> timeZoneIds;
0091 
0092   for (int i = 0; i < tree->topLevelItemCount(); i++)
0093   {
0094     QTreeWidgetItem *item = tree->topLevelItem(i);
0095     if ( item->checkState(0) == Qt::Checked )
0096       timeZoneIds.append(item->text(0).toUtf8());
0097   }
0098 
0099   return timeZoneIds;
0100 }
0101 
0102 //--------------------------------------------------------------------------------