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

KDEUI

ktimezonewidget.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2005, S.R.Haque <srhaque@iee.org>.
00003     This file is part of the KDE project
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2, as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "ktimezonewidget.h"
00021 
00022 #include <QtCore/QFile>
00023 #include <QtGui/QPixmap>
00024 
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <kstandarddirs.h>
00028 #include <ksystemtimezone.h>
00029 #include <ktimezone.h>
00030 
00031 class KTimeZoneWidget::Private
00032 {
00033 public:
00034     enum Columns
00035     {
00036         CityColumn = 0,
00037         RegionColumn,
00038         CommentColumn
00039     };
00040 
00041     enum Roles
00042     {
00043         ZoneRole = Qt::UserRole + 0xF3A3CB1
00044     };
00045 };
00046 
00047 #ifndef KDE_USE_FINAL
00048 static bool localeLessThan (const QString &a, const QString &b)
00049 {
00050     return QString::localeAwareCompare(a, b) < 0;
00051 }
00052 #endif
00053 
00054 KTimeZoneWidget::KTimeZoneWidget( QWidget *parent, KTimeZones *db )
00055   : QTreeWidget( parent ),
00056     d( 0 )
00057 {
00058   // If the user did not provide a timezone database, we'll use the system default.
00059   setRootIsDecorated(false);
00060   setHeaderLabels( QStringList() << i18nc("Define an area in the time zone, like a town area", "Area" ) << i18nc( "Time zone", "Region" ) << i18n( "Comment" ) );
00061 
00062   // Collect zones by localized city names, so that they can be sorted properly.
00063   QStringList cities;
00064   QHash<QString, KTimeZone> zonesByCity;
00065 
00066   if (!db) {
00067       db = KSystemTimeZones::timeZones();
00068 
00069       // add UTC to the defaults default
00070       KTimeZone utc = KTimeZone::utc();
00071       cities.append(utc.name());
00072       zonesByCity.insert(utc.name(), utc);
00073   }
00074 
00075   const KTimeZones::ZoneMap zones = db->zones();
00076   for ( KTimeZones::ZoneMap::ConstIterator it = zones.begin(); it != zones.end(); ++it ) {
00077     KTimeZone zone = it.value();
00078     const QString continentCity = displayName( zone );
00079     int separator = continentCity.lastIndexOf('/');
00080     QString city = continentCity.right(continentCity.length() - separator - 1)
00081                    + continentCity.left(separator);
00082 
00083     cities.append( city );
00084     zonesByCity.insert( city, zone );
00085   }
00086   qSort( cities.begin(), cities.end(), localeLessThan );
00087 
00088   foreach ( const QString &city, cities ) {
00089     KTimeZone zone = zonesByCity[city];
00090     QString tzName = zone.name();
00091     QString comment = zone.comment();
00092 
00093     if ( !comment.isEmpty() )
00094       comment = i18n( comment.toUtf8() );
00095 
00096     // Convert:
00097     //
00098     //  "Europe/London", "GB" -> "London", "Europe/GB".
00099     //  "UTC",           ""   -> "UTC",    "".
00100     QStringList continentCity = displayName( zone ).split( '/' );
00101 
00102     QTreeWidgetItem *listItem = new QTreeWidgetItem( this );
00103     listItem->setText( Private::CityColumn, continentCity[ continentCity.count() - 1 ] );
00104     continentCity[ continentCity.count() - 1 ] = zone.countryCode();
00105 
00106     listItem->setText( Private::RegionColumn, continentCity.join( QChar('/') ) );
00107     listItem->setText( Private::CommentColumn, comment );
00108     listItem->setData( Private::CityColumn, Private::ZoneRole, tzName ); // store complete path in custom role
00109 
00110     // Locate the flag from /l10n/%1/flag.png.
00111     QString flag = KStandardDirs::locate( "locale", QString( "l10n/%1/flag.png" ).arg( zone.countryCode().toLower() ) );
00112     if ( QFile::exists( flag ) )
00113       listItem->setIcon( Private::RegionColumn, QPixmap( flag ) );
00114   }
00115 }
00116 
00117 KTimeZoneWidget::~KTimeZoneWidget()
00118 {
00119   delete d;
00120 }
00121 
00122 QString KTimeZoneWidget::displayName( const KTimeZone &zone )
00123 {
00124   return i18n( zone.name().toUtf8() ).replace( '_', ' ' );
00125 }
00126 
00127 QStringList KTimeZoneWidget::selection() const
00128 {
00129     QStringList selection;
00130 
00131     // Loop through all entries.
00132     foreach ( QTreeWidgetItem* listItem, selectedItems() )
00133         selection.append( listItem->data( Private::CityColumn, Private::ZoneRole ).toString() );
00134 
00135     return selection;
00136 }
00137 
00138 void KTimeZoneWidget::setSelected( const QString &zone, bool selected )
00139 {
00140     bool found = false;
00141 
00142     // The code was using findItems( zone, Qt::MatchExactly, Private::ZoneColumn )
00143     // previously, but the underlying model only has 3 columns, the "hidden" column
00144     // wasn't available in there.
00145 
00146     // Loop through all entries.
00147     const int rowCount = model()->rowCount(QModelIndex());
00148     for (int row = 0; row < rowCount; ++row) {
00149         const QModelIndex index = model()->index(row, Private::CityColumn );
00150         const QString tzName = index.data(Private::ZoneRole).toString();
00151         if (tzName == zone) {
00152             selectionModel()->select(index, selected ? (QItemSelectionModel::Select | QItemSelectionModel::Rows) : (QItemSelectionModel::Deselect | QItemSelectionModel::Rows));
00153 
00154             // Ensure the selected item is visible as appropriate.
00155             scrollTo( index );
00156 
00157             found = true;
00158         }
00159     }
00160 
00161     if ( !found )
00162         kDebug() << "No such zone: " << zone;
00163 }
00164 
00165 #include "ktimezonewidget.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal