• Skip to content
  • Skip to link menu
KDE 4.1 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   if ( !db )
00060       db = KSystemTimeZones::timeZones();
00061 
00062   setRootIsDecorated(false);
00063   setHeaderLabels( QStringList() << i18n( "Area" ) << i18n( "Region" ) << i18n( "Comment" ) );
00064 
00065   // Collect zones by localized city names, so that they can be sorted properly.
00066   const KTimeZones::ZoneMap zones = db->zones();
00067   QStringList cities;
00068   QHash<QString, KTimeZone> zonesByCity;
00069   for ( KTimeZones::ZoneMap::ConstIterator it = zones.begin(); it != zones.end(); ++it ) {
00070     KTimeZone zone = it.value();
00071     const QStringList continentCity = displayName( zone ).split( '/' );
00072     QString city = continentCity[ continentCity.count() - 1 ];
00073     cities.append( city );
00074     zonesByCity.insert( city, zone );
00075   }
00076   qSort( cities.begin(), cities.end(), localeLessThan );
00077 
00078   foreach ( const QString &city, cities ) {
00079     KTimeZone zone = zonesByCity[city];
00080     QString tzName = zone.name();
00081     QString comment = zone.comment();
00082 
00083     if ( !comment.isEmpty() )
00084       comment = i18n( comment.toUtf8() );
00085 
00086     // Convert:
00087     //
00088     //  "Europe/London", "GB" -> "London", "Europe/GB".
00089     //  "UTC",           ""   -> "UTC",    "".
00090     QStringList continentCity = displayName( zone ).split( '/' );
00091 
00092     QTreeWidgetItem *listItem = new QTreeWidgetItem( this );
00093     listItem->setText( Private::CityColumn, continentCity[ continentCity.count() - 1 ] );
00094     continentCity[ continentCity.count() - 1 ] = zone.countryCode();
00095 
00096     listItem->setText( Private::RegionColumn, continentCity.join( QChar('/') ) );
00097     listItem->setText( Private::CommentColumn, comment );
00098     listItem->setData( Private::CityColumn, Private::ZoneRole, tzName ); // store complete path in custom role
00099 
00100     // Locate the flag from /l10n/%1/flag.png.
00101     QString flag = KStandardDirs::locate( "locale", QString( "l10n/%1/flag.png" ).arg( zone.countryCode().toLower() ) );
00102     if ( QFile::exists( flag ) )
00103       listItem->setIcon( Private::RegionColumn, QPixmap( flag ) );
00104   }
00105 }
00106 
00107 KTimeZoneWidget::~KTimeZoneWidget()
00108 {
00109   delete d;
00110 }
00111 
00112 QString KTimeZoneWidget::displayName( const KTimeZone &zone )
00113 {
00114   return i18n( zone.name().toUtf8() ).replace( '_', ' ' );
00115 }
00116 
00117 QStringList KTimeZoneWidget::selection() const
00118 {
00119     QStringList selection;
00120 
00121     // Loop through all entries.
00122     foreach ( QTreeWidgetItem* listItem, selectedItems() )
00123         selection.append( listItem->data( Private::CityColumn, Private::ZoneRole ).toString() );
00124 
00125     return selection;
00126 }
00127 
00128 void KTimeZoneWidget::setSelected( const QString &zone, bool selected )
00129 {
00130     bool found = false;
00131 
00132     // The code was using findItems( zone, Qt::MatchExactly, Private::ZoneColumn )
00133     // previously, but the underlying model only has 3 columns, the "hidden" column
00134     // wasn't available in there.
00135 
00136     // Loop through all entries.
00137     const int rowCount = model()->rowCount(QModelIndex());
00138     for (int row = 0; row < rowCount; ++row) {
00139         const QModelIndex index = model()->index(row, Private::CityColumn );
00140         const QString tzName = index.data(Private::ZoneRole).toString();
00141         if (tzName == zone) {
00142             selectionModel()->select(index, selected ? (QItemSelectionModel::Select | QItemSelectionModel::Rows) : QItemSelectionModel::Deselect);
00143 
00144             // Ensure the selected item is visible as appropriate.
00145             scrollTo( index );
00146 
00147             found = true;
00148         }
00149     }
00150 
00151     if ( !found )
00152         kDebug() << "No such zone: " << zone;
00153 }
00154 
00155 #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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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