|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
Copyright (C) 2005, S.R.Haque <srhaque@iee.org>. |
| 3 |
Copyright (C) 2009, David Faure <faure@kde.org> |
| 4 |
This file is part of the KDE project |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public |
| 8 |
License version 2, as published by the Free Software Foundation. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Library General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Library General Public License |
| 16 |
along with this library; see the file COPYING.LIB. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
Boston, MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "k4timezonewidget.h" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <memory> |
| 25 |
|
| 26 |
#include <KCountry> |
| 27 |
#include <KCountryFlagEmojiIconEngine> |
| 28 |
#include <KLocalizedString> |
| 29 |
|
| 30 |
#include <QDebug> |
| 31 |
#include <QFile> |
| 32 |
#include <QPixmap> |
| 33 |
#include <QTimeZone> |
| 34 |
|
| 35 |
#include <unicode/localebuilder.h> |
| 36 |
#include <unicode/tznames.h> |
| 37 |
#include <unicode/unistr.h> |
| 38 |
|
| 39 |
class Q_DECL_HIDDEN K4TimeZoneWidget::Private |
| 40 |
{ |
| 41 |
public: |
| 42 |
Private() |
| 43 |
: itemsCheckable(false) |
| 44 |
, singleSelection(true) |
| 45 |
{ |
| 46 |
} |
| 47 |
|
| 48 |
enum Columns { |
| 49 |
CityColumn = 0, |
| 50 |
RegionColumn, |
| 51 |
CommentColumn, |
| 52 |
}; |
| 53 |
|
| 54 |
enum Roles { |
| 55 |
ZoneRole = Qt::UserRole + 0xF3A3CB1, |
| 56 |
}; |
| 57 |
|
| 58 |
bool itemsCheckable; |
| 59 |
bool singleSelection; |
| 60 |
}; |
| 61 |
|
| 62 |
static bool localeLessThan(const QString &a, const QString &b) |
| 63 |
{ |
| 64 |
return QString::localeAwareCompare(a, b) < 0; |
| 65 |
} |
| 66 |
|
| 67 |
K4TimeZoneWidget::K4TimeZoneWidget(QWidget *parent) |
| 68 |
: QTreeWidget(parent) |
| 69 |
, d(new K4TimeZoneWidget::Private) |
| 70 |
{ |
| 71 |
// If the user did not provide a timezone database, we'll use the system default. |
| 72 |
setRootIsDecorated(false); |
| 73 |
setHeaderLabels(QStringList() << i18nc("Define an area in the time zone, like a town area", "Area") << i18nc("Time zone", "Region") << i18n("Comment")); |
| 74 |
|
| 75 |
const auto locale = icu::Locale(QLocale::system().name().toLatin1()); |
| 76 |
UErrorCode error = U_ZERO_ERROR; |
| 77 |
std::unique_ptr<icu::TimeZoneNames> tzNames(icu::TimeZoneNames::createInstance(locale, error)); |
| 78 |
if (!U_SUCCESS(error)) { |
| 79 |
qWarning() << "failed to create timezone names" << u_errorName(error); |
| 80 |
return; |
| 81 |
}; |
| 82 |
icu::UnicodeString result; |
| 83 |
|
| 84 |
const auto timezones = QTimeZone::availableTimeZoneIds(); |
| 85 |
|
| 86 |
struct TimeZoneInfo { |
| 87 |
QTimeZone zone; |
| 88 |
QString region; |
| 89 |
QString city; |
| 90 |
}; |
| 91 |
|
| 92 |
std::vector<TimeZoneInfo> zones; |
| 93 |
zones.reserve(timezones.size()); |
| 94 |
for (const auto &id : timezones) { |
| 95 |
const int separator = id.lastIndexOf('/'); |
| 96 |
// Make up the localized key that will be used for sorting. |
| 97 |
// Example: Asia/Tokyo -> key = "i18n(Tokyo)|i18n(Asia)|Asia/Tokyo" |
| 98 |
// The zone name is appended to ensure unicity even with equal translations (#174918) |
| 99 |
// since there is no API continent names, we use the translation catalog of the digitalclock applet which contain these |
| 100 |
const char *domain = "plasma_applet_org.kde.plasma.digitalclock.mo"; |
| 101 |
const QString continent = separator > 0 ? i18nd(domain, id.left(separator)) : QString(); |
| 102 |
const icu::UnicodeString &exemplarCity = tzNames->getExemplarLocationName(icu::UnicodeString::fromUTF8(icu::StringPiece(id.data(), id.size())), result); |
| 103 |
zones.push_back( |
| 104 |
{QTimeZone(id), continent, exemplarCity.isBogus() ? QString::fromLatin1(id) : QString::fromUtf16(exemplarCity.getBuffer(), exemplarCity.length())}); |
| 105 |
} |
| 106 |
std::sort(zones.begin(), zones.end(), [](const TimeZoneInfo &left, const TimeZoneInfo &right) { |
| 107 |
if (auto result = QString::localeAwareCompare(left.city, right.city); result != 0) { |
| 108 |
return result < 0; |
| 109 |
} |
| 110 |
if (auto result = QString::localeAwareCompare(left.region, right.region); result != 0) { |
| 111 |
return result < 0; |
| 112 |
} |
| 113 |
return QString::localeAwareCompare(left.zone.id(), right.zone.id()) < 0; |
| 114 |
}); |
| 115 |
|
| 116 |
for (const auto &zoneInfo : zones) { |
| 117 |
const QTimeZone &zone = zoneInfo.zone; |
| 118 |
const QString tzName = zone.id(); |
| 119 |
QString comment = zone.comment(); |
| 120 |
|
| 121 |
if (!comment.isEmpty()) { |
| 122 |
comment = i18n(comment.toUtf8()); |
| 123 |
} |
| 124 |
|
| 125 |
QTreeWidgetItem *listItem = new QTreeWidgetItem(this); |
| 126 |
listItem->setText(Private::CityColumn, zoneInfo.city); |
| 127 |
QString countryName = KCountry::fromQLocale(zone.territory()).name(); |
| 128 |
if (countryName.isEmpty()) { |
| 129 |
countryName = QLocale::territoryToCode(zone.territory()); |
| 130 |
} |
| 131 |
QString regionLabel = zoneInfo.region; |
| 132 |
if (!regionLabel.isEmpty() && !countryName.isEmpty()) { |
| 133 |
regionLabel += '/'; |
| 134 |
} |
| 135 |
regionLabel += countryName; |
| 136 |
|
| 137 |
listItem->setText(Private::RegionColumn, regionLabel); |
| 138 |
listItem->setText(Private::CommentColumn, comment); |
| 139 |
listItem->setData(Private::CityColumn, Private::ZoneRole, tzName); // store complete path in custom role |
| 140 |
|
| 141 |
listItem->setIcon(Private::RegionColumn, QIcon(new KCountryFlagEmojiIconEngine(QLocale::territoryToCode(zone.territory())))); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
K4TimeZoneWidget::~K4TimeZoneWidget() |
| 146 |
{ |
| 147 |
delete d; |
| 148 |
} |
| 149 |
|
| 150 |
void K4TimeZoneWidget::setItemsCheckable(bool enable) |
| 151 |
{ |
| 152 |
d->itemsCheckable = enable; |
| 153 |
const int count = topLevelItemCount(); |
| 154 |
for (int row = 0; row < count; ++row) { |
| 155 |
QTreeWidgetItem *listItem = topLevelItem(row); |
| 156 |
listItem->setCheckState(Private::CityColumn, Qt::Unchecked); |
| 157 |
} |
| 158 |
QTreeWidget::setSelectionMode(QAbstractItemView::NoSelection); |
| 159 |
} |
| 160 |
|
| 161 |
bool K4TimeZoneWidget::itemsCheckable() const |
| 162 |
{ |
| 163 |
return d->itemsCheckable; |
| 164 |
} |
| 165 |
|
| 166 |
QStringList K4TimeZoneWidget::selection() const |
| 167 |
{ |
| 168 |
QStringList selection; |
| 169 |
|
| 170 |
// Loop through all entries. |
| 171 |
// Do not use selectedItems() because it skips hidden items, making it |
| 172 |
// impossible to use a KTreeWidgetSearchLine. |
| 173 |
// There is no QTreeWidgetItemConstIterator, hence the const_cast :/ |
| 174 |
QTreeWidgetItemIterator it(const_cast<K4TimeZoneWidget *>(this), d->itemsCheckable ? QTreeWidgetItemIterator::Checked : QTreeWidgetItemIterator::Selected); |
| 175 |
for (; *it; ++it) { |
| 176 |
selection.append((*it)->data(Private::CityColumn, Private::ZoneRole).toString()); |
| 177 |
} |
| 178 |
|
| 179 |
return selection; |
| 180 |
} |
| 181 |
|
| 182 |
void K4TimeZoneWidget::setSelected(const QString &zone, bool selected) |
| 183 |
{ |
| 184 |
bool found = false; |
| 185 |
|
| 186 |
// The code was using findItems( zone, Qt::MatchExactly, Private::ZoneColumn ) |
| 187 |
// previously, but the underlying model only has 3 columns, the "hidden" column |
| 188 |
// wasn't available in there. |
| 189 |
|
| 190 |
if (!d->itemsCheckable) { |
| 191 |
// Runtime compatibility for < 4.3 apps, which don't call the setMultiSelection reimplementation. |
| 192 |
d->singleSelection = (QTreeWidget::selectionMode() == QAbstractItemView::SingleSelection); |
| 193 |
} |
| 194 |
|
| 195 |
// Loop through all entries. |
| 196 |
const int rowCount = model()->rowCount(QModelIndex()); |
| 197 |
for (int row = 0; row < rowCount; ++row) { |
| 198 |
const QModelIndex index = model()->index(row, Private::CityColumn); |
| 199 |
const QString tzName = index.data(Private::ZoneRole).toString(); |
| 200 |
if (tzName == zone) { |
| 201 |
if (d->singleSelection && selected) { |
| 202 |
clearSelection(); |
| 203 |
} |
| 204 |
|
| 205 |
if (d->itemsCheckable) { |
| 206 |
QTreeWidgetItem *listItem = itemFromIndex(index); |
| 207 |
listItem->setCheckState(Private::CityColumn, selected ? Qt::Checked : Qt::Unchecked); |
| 208 |
} else { |
| 209 |
selectionModel()->select(index, |
| 210 |
selected ? (QItemSelectionModel::Select | QItemSelectionModel::Rows) |
| 211 |
: (QItemSelectionModel::Deselect | QItemSelectionModel::Rows)); |
| 212 |
} |
| 213 |
|
| 214 |
// Ensure the selected item is visible as appropriate. |
| 215 |
scrollTo(index); |
| 216 |
|
| 217 |
found = true; |
| 218 |
|
| 219 |
if (d->singleSelection && selected) { |
| 220 |
break; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if (!found) { |
| 226 |
qDebug() << "No such zone: " << zone; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
void K4TimeZoneWidget::clearSelection() |
| 231 |
{ |
| 232 |
if (d->itemsCheckable) { |
| 233 |
// Un-select all items |
| 234 |
const int rowCount = model()->rowCount(QModelIndex()); |
| 235 |
for (int row = 0; row < rowCount; ++row) { |
| 236 |
const QModelIndex index = model()->index(row, 0); |
| 237 |
QTreeWidgetItem *listItem = itemFromIndex(index); |
| 238 |
listItem->setCheckState(Private::CityColumn, Qt::Unchecked); |
| 239 |
} |
| 240 |
} else { |
| 241 |
QTreeWidget::clearSelection(); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
void K4TimeZoneWidget::setSelectionMode(QAbstractItemView::SelectionMode mode) |
| 246 |
{ |
| 247 |
d->singleSelection = (mode == QAbstractItemView::SingleSelection); |
| 248 |
if (!d->itemsCheckable) { |
| 249 |
QTreeWidget::setSelectionMode(mode); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
QAbstractItemView::SelectionMode K4TimeZoneWidget::selectionMode() const |
| 254 |
{ |
| 255 |
if (d->itemsCheckable) { |
| 256 |
return d->singleSelection ? QTreeWidget::SingleSelection : QTreeWidget::MultiSelection; |
| 257 |
} else { |
| 258 |
return QTreeWidget::selectionMode(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
#include "moc_k4timezonewidget.cpp" |