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 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
00063 QStringList cities;
00064 QHash<QString, KTimeZone> zonesByCity;
00065
00066 if (!db) {
00067 db = KSystemTimeZones::timeZones();
00068
00069
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
00097
00098
00099
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 );
00109
00110
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
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
00143
00144
00145
00146
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
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"