KDEUI
ktimezonewidget.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00059 if ( !db )
00060 db = KSystemTimeZones::timeZones();
00061
00062 setRootIsDecorated(false);
00063 setHeaderLabels( QStringList() << i18n( "Area" ) << i18n( "Region" ) << i18n( "Comment" ) );
00064
00065
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
00087
00088
00089
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 );
00099
00100
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
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
00133
00134
00135
00136
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
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"