• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • widgets
ktimezonewidget.cpp
Go to the documentation of this file.
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 "ktimezonewidget.h"
22 
23 #include <QtCore/QFile>
24 #include <QtGui/QPixmap>
25 
26 #include <kdebug.h>
27 #include <klocale.h>
28 #include <kstandarddirs.h>
29 #include <ksystemtimezone.h>
30 #include <ktimezone.h>
31 
32 class KTimeZoneWidget::Private
33 {
34 public:
35  Private() : itemsCheckable(false), singleSelection(true) {}
36 
37  enum Columns
38  {
39  CityColumn = 0,
40  RegionColumn,
41  CommentColumn
42  };
43 
44  enum Roles
45  {
46  ZoneRole = Qt::UserRole + 0xF3A3CB1
47  };
48 
49  bool itemsCheckable;
50  bool singleSelection;
51 };
52 
53 #ifndef KDE_USE_FINAL
54 static bool localeLessThan (const QString &a, const QString &b)
55 {
56  return QString::localeAwareCompare(a, b) < 0;
57 }
58 #endif
59 
60 KTimeZoneWidget::KTimeZoneWidget( QWidget *parent, KTimeZones *db )
61  : QTreeWidget( parent ),
62  d(new KTimeZoneWidget::Private)
63 {
64  // If the user did not provide a timezone database, we'll use the system default.
65  setRootIsDecorated(false);
66  setHeaderLabels( QStringList() << i18nc("Define an area in the time zone, like a town area", "Area" ) << i18nc( "Time zone", "Region" ) << i18n( "Comment" ) );
67 
68  // Collect zones by localized city names, so that they can be sorted properly.
69  QStringList cities;
70  QHash<QString, KTimeZone> zonesByCity;
71 
72  if (!db) {
73  db = KSystemTimeZones::timeZones();
74 
75  // add UTC to the defaults default
76  KTimeZone utc = KTimeZone::utc();
77  cities.append(utc.name());
78  zonesByCity.insert(utc.name(), utc);
79  }
80 
81  const KTimeZones::ZoneMap zones = db->zones();
82  for ( KTimeZones::ZoneMap::ConstIterator it = zones.begin(); it != zones.end(); ++it ) {
83  const KTimeZone zone = it.value();
84  const QString continentCity = displayName( zone );
85  const int separator = continentCity.lastIndexOf('/');
86  // Make up the localized key that will be used for sorting.
87  // Example: i18n(Asia/Tokyo) -> key = "i18n(Tokyo)|i18n(Asia)|Asia/Tokyo"
88  // The zone name is appended to ensure unicity even with equal translations (#174918)
89  const QString key = continentCity.mid(separator+1) + '|'
90  + continentCity.left(separator) + '|' + zone.name();
91  cities.append( key );
92  zonesByCity.insert( key, zone );
93  }
94  qSort( cities.begin(), cities.end(), localeLessThan );
95 
96  foreach ( const QString &key, cities ) {
97  const KTimeZone zone = zonesByCity.value(key);
98  const QString tzName = zone.name();
99  QString comment = zone.comment();
100 
101  if ( !comment.isEmpty() )
102  comment = i18n( comment.toUtf8() );
103 
104  // Convert:
105  //
106  // "Europe/London", "GB" -> "London", "Europe/GB".
107  // "UTC", "" -> "UTC", "".
108  QStringList continentCity = displayName( zone ).split( '/' );
109 
110  QTreeWidgetItem *listItem = new QTreeWidgetItem( this );
111  listItem->setText( Private::CityColumn, continentCity[ continentCity.count() - 1 ] );
112  QString countryName = KGlobal::locale()->countryCodeToName( zone.countryCode() );
113  if ( countryName.isEmpty() ) {
114  continentCity[ continentCity.count() - 1 ] = zone.countryCode();
115  } else {
116  continentCity[ continentCity.count() - 1 ] = countryName;
117  }
118 
119  listItem->setText( Private::RegionColumn, continentCity.join( QChar('/') ) );
120  listItem->setText( Private::CommentColumn, comment );
121  listItem->setData( Private::CityColumn, Private::ZoneRole, tzName ); // store complete path in custom role
122 
123  // Locate the flag from /l10n/%1/flag.png.
124  QString flag = KStandardDirs::locate( "locale", QString( "l10n/%1/flag.png" ).arg( zone.countryCode().toLower() ) );
125  if ( QFile::exists( flag ) )
126  listItem->setIcon( Private::RegionColumn, QPixmap( flag ) );
127  }
128 }
129 
130 KTimeZoneWidget::~KTimeZoneWidget()
131 {
132  delete d;
133 }
134 
135 void KTimeZoneWidget::setItemsCheckable(bool enable)
136 {
137  d->itemsCheckable = enable;
138  const int count = topLevelItemCount();
139  for (int row = 0; row < count; ++row) {
140  QTreeWidgetItem* listItem = topLevelItem(row);
141  listItem->setCheckState(Private::CityColumn, Qt::Unchecked);
142  }
143  QTreeWidget::setSelectionMode(QAbstractItemView::NoSelection);
144 }
145 
146 bool KTimeZoneWidget::itemsCheckable() const
147 {
148  return d->itemsCheckable;
149 }
150 
151 QString KTimeZoneWidget::displayName( const KTimeZone &zone )
152 {
153  return i18n( zone.name().toUtf8() ).replace( '_', ' ' );
154 }
155 
156 QStringList KTimeZoneWidget::selection() const
157 {
158  QStringList selection;
159 
160  // Loop through all entries.
161  // Do not use selectedItems() because it skips hidden items, making it
162  // impossible to use a KTreeWidgetSearchLine.
163  // There is no QTreeWidgetItemConstIterator, hence the const_cast :/
164  QTreeWidgetItemIterator it(const_cast<KTimeZoneWidget*>(this), d->itemsCheckable ? QTreeWidgetItemIterator::Checked : QTreeWidgetItemIterator::Selected);
165  for (; *it; ++it) {
166  selection.append( (*it)->data( Private::CityColumn, Private::ZoneRole ).toString() );
167  }
168 
169  return selection;
170 }
171 
172 void KTimeZoneWidget::setSelected( const QString &zone, bool selected )
173 {
174  bool found = false;
175 
176  // The code was using findItems( zone, Qt::MatchExactly, Private::ZoneColumn )
177  // previously, but the underlying model only has 3 columns, the "hidden" column
178  // wasn't available in there.
179 
180  if (!d->itemsCheckable) {
181  // Runtime compatibility for < 4.3 apps, which don't call the setMultiSelection reimplementation.
182  d->singleSelection = (QTreeWidget::selectionMode() == QAbstractItemView::SingleSelection);
183  }
184 
185  // Loop through all entries.
186  const int rowCount = model()->rowCount(QModelIndex());
187  for (int row = 0; row < rowCount; ++row) {
188  const QModelIndex index = model()->index(row, Private::CityColumn);
189  const QString tzName = index.data(Private::ZoneRole).toString();
190  if (tzName == zone) {
191 
192  if (d->singleSelection && selected) {
193  clearSelection();
194  }
195 
196  if (d->itemsCheckable) {
197  QTreeWidgetItem* listItem = itemFromIndex(index);
198  listItem->setCheckState(Private::CityColumn, selected ? Qt::Checked : Qt::Unchecked);
199  } else {
200  selectionModel()->select(index, selected ? (QItemSelectionModel::Select | QItemSelectionModel::Rows) : (QItemSelectionModel::Deselect | QItemSelectionModel::Rows));
201  }
202 
203  // Ensure the selected item is visible as appropriate.
204  scrollTo( index );
205 
206  found = true;
207 
208  if (d->singleSelection && selected) {
209  break;
210  }
211  }
212  }
213 
214  if ( !found )
215  kDebug() << "No such zone: " << zone;
216 }
217 
218 void KTimeZoneWidget::clearSelection()
219 {
220  if (d->itemsCheckable) {
221  // Un-select all items
222  const int rowCount = model()->rowCount(QModelIndex());
223  for (int row = 0; row < rowCount; ++row) {
224  const QModelIndex index = model()->index(row, 0);
225  QTreeWidgetItem* listItem = itemFromIndex(index);
226  listItem->setCheckState(Private::CityColumn, Qt::Unchecked);
227  }
228  } else {
229  QTreeWidget::clearSelection();
230  }
231 }
232 
233 void KTimeZoneWidget::setSelectionMode(QAbstractItemView::SelectionMode mode)
234 {
235  d->singleSelection = (mode == QAbstractItemView::SingleSelection);
236  if (!d->itemsCheckable) {
237  QTreeWidget::setSelectionMode(mode);
238  }
239 }
240 
241 QAbstractItemView::SelectionMode KTimeZoneWidget::selectionMode() const
242 {
243  if (d->itemsCheckable) {
244  return d->singleSelection ? QTreeWidget::SingleSelection : QTreeWidget::MultiSelection;
245  } else {
246  return QTreeWidget::selectionMode();
247  }
248 }
249 
250 #include "ktimezonewidget.moc"
i18n
QString i18n(const char *text)
KTimeZoneWidget::KTimeZoneWidget
KTimeZoneWidget(QWidget *parent=0, KTimeZones *timeZones=0)
Constructs a time zone selection widget.
Definition: ktimezonewidget.cpp:60
KTimeZoneWidget::clearSelection
void clearSelection()
Unselect all timezones.
Definition: ktimezonewidget.cpp:218
kdebug.h
KTimeZoneWidget::~KTimeZoneWidget
virtual ~KTimeZoneWidget()
Destroys the time zone selection widget.
Definition: ktimezonewidget.cpp:130
QTreeWidget
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
KTimeZoneWidget::selection
QStringList selection() const
Returns the currently selected time zones.
Definition: ktimezonewidget.cpp:156
QWidget
KTimeZoneWidget::setSelectionMode
void setSelectionMode(QAbstractItemView::SelectionMode mode)
Allows to select multiple timezones.
Definition: ktimezonewidget.cpp:233
KLocale::countryCodeToName
QString countryCodeToName(const QString &country) const
QString
QHash
KTimeZoneWidget::setItemsCheckable
void setItemsCheckable(bool enable)
Makes all items show a checkbox, so that the user can select multiple timezones by means of checking ...
Definition: ktimezonewidget.cpp:135
KTimeZones
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
localeLessThan
static bool localeLessThan(const QString &a, const QString &b)
Definition: ktimezonewidget.cpp:54
KTimeZoneWidget::itemsCheckable
bool itemsCheckable() const
i18nc
QString i18nc(const char *ctxt, const char *text)
KSystemTimeZones::timeZones
static KTimeZones * timeZones()
KStandardAction::Deselect
Definition: kstandardaction.h:133
ksystemtimezone.h
KTimeZoneWidget::displayName
static QString displayName(const KTimeZone &zone)
Format a time zone name in a standardised manner.
Definition: ktimezonewidget.cpp:151
ktimezonewidget.h
KTimeZoneWidget::selectionMode
QAbstractItemView::SelectionMode selectionMode() const
QStringList
ktimezone.h
KTimeZone
KGlobal::locale
KLocale * locale()
KStandardAction::replace
KAction * replace(const QObject *recvr, const char *slot, QObject *parent)
Find and replace matches.
Definition: kstandardaction.cpp:344
KTimeZoneWidget
A time zone selection widget.
Definition: ktimezonewidget.h:59
KTimeZoneWidget::setSelected
void setSelected(const QString &zone, bool selected)
Select/deselect the named time zone.
Definition: ktimezonewidget.cpp:172
kstandarddirs.h
KTimeZones::zones
const ZoneMap zones() const
QMap
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal