--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ProjectDjangoTagsMenu/TimezoneSelectionDialog.py Wed Feb 12 19:54:24 2014 +0100 @@ -0,0 +1,581 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a dialog to select a time zone. +""" + +from PyQt4.QtCore import pyqtSlot +from PyQt4.QtGui import QDialog, QDialogButtonBox + +from .Ui_TimezoneSelectionDialog import Ui_TimezoneSelectionDialog + + +class TimezoneSelectionDialog(QDialog, Ui_TimezoneSelectionDialog): + """ + Class implementing a dialog to select a time zone. + """ + Timezones = { + 'Africa': [ + 'Abidjan', + 'Accra', + 'Addis_Ababa', + 'Algiers', + 'Asmara', + 'Asmera', + 'Bamako', + 'Bangui', + 'Banjul', + 'Bissau', + 'Blantyre', + 'Brazzaville', + 'Bujumbura', + 'Cairo', + 'Casablanca', + 'Ceuta', + 'Conakry', + 'Dakar', + 'Dar_es_Salaam', + 'Djibouti', + 'Douala', + 'El_Aaiun', + 'Freetown', + 'Gaborone', + 'Harare', + 'Johannesburg', + 'Juba', + 'Kampala', + 'Khartoum', + 'Kigali', + 'Kinshasa', + 'Lagos', + 'Libreville', + 'Lome', + 'Luanda', + 'Lubumbashi', + 'Lusaka', + 'Malabo', + 'Maputo', + 'Maseru', + 'Mbabane', + 'Mogadishu', + 'Monrovia', + 'Nairobi', + 'Ndjamena', + 'Niamey', + 'Nouakchott', + 'Ouagadougou', + 'Porto-Novo', + 'Sao_Tome', + 'Timbuktu', + 'Tripoli', + 'Tunis', + 'Windhoek', + ], + 'America': [ + 'Adak', + 'Anchorage', + 'Anguilla', + 'Antigua', + 'Araguaina', + 'Argentina/Buenos_Aires', + 'Argentina/Catamarca', + 'Argentina/ComodRivadavia', + 'Argentina/Cordoba', + 'Argentina/Jujuy', + 'Argentina/La_Rioja', + 'Argentina/Mendoza', + 'Argentina/Rio_Gallegos', + 'Argentina/Salta', + 'Argentina/San_Juan', + 'Argentina/San_Luis', + 'Argentina/Tucuman', + 'Argentina/Ushuaia', + 'Aruba', + 'Asuncion', + 'Atikokan', + 'Atka', + 'Bahia', + 'Bahia_Banderas', + 'Barbados', + 'Belem', + 'Belize', + 'Blanc-Sablon', + 'Boa_Vista', + 'Bogota', + 'Boise', + 'Buenos_Aires', + 'Cambridge_Bay', + 'Campo_Grande', + 'Cancun', + 'Caracas', + 'Catamarca', + 'Cayenne', + 'Cayman', + 'Chicago', + 'Chihuahua', + 'Coral_Harbour', + 'Cordoba', + 'Costa_Rica', + 'Creston', + 'Cuiaba', + 'Curacao', + 'Danmarkshavn', + 'Dawson', + 'Dawson_Creek', + 'Denver', + 'Detroit', + 'Dominica', + 'Edmonton', + 'Eirunepe', + 'El_Salvador', + 'Ensenada', + 'Fort_Wayne', + 'Fortaleza', + 'Glace_Bay', + 'Godthab', + 'Goose_Bay', + 'Grand_Turk', + 'Grenada', + 'Guadeloupe', + 'Guatemala', + 'Guayaquil', + 'Guyana', + 'Halifax', + 'Havana', + 'Hermosillo', + 'Indiana/Indianapolis', + 'Indiana/Knox', + 'Indiana/Marengo', + 'Indiana/Petersburg', + 'Indiana/Tell_City', + 'Indiana/Vevay', + 'Indiana/Vincennes', + 'Indiana/Winamac', + 'Indianapolis', + 'Inuvik', + 'Iqaluit', + 'Jamaica', + 'Jujuy', + 'Juneau', + 'Kentucky/Louisville', + 'Kentucky/Monticello', + 'Knox_IN', + 'Kralendijk', + 'La_Paz', + 'Lima', + 'Los_Angeles', + 'Louisville', + 'Lower_Princes', + 'Maceio', + 'Managua', + 'Manaus', + 'Marigot', + 'Martinique', + 'Matamoros', + 'Mazatlan', + 'Mendoza', + 'Menominee', + 'Merida', + 'Metlakatla', + 'Mexico_City', + 'Miquelon', + 'Moncton', + 'Monterrey', + 'Montevideo', + 'Montreal', + 'Montserrat', + 'Nassau', + 'New_York', + 'Nipigon', + 'Nome', + 'Noronha', + 'North_Dakota/Beulah', + 'North_Dakota/Center', + 'North_Dakota/New_Salem', + 'Ojinaga', + 'Panama', + 'Pangnirtung', + 'Paramaribo', + 'Phoenix', + 'Port-au-Prince', + 'Port_of_Spain', + 'Porto_Acre', + 'Porto_Velho', + 'Puerto_Rico', + 'Rainy_River', + 'Rankin_Inlet', + 'Recife', + 'Regina', + 'Resolute', + 'Rio_Branco', + 'Rosario', + 'Santa_Isabel', + 'Santarem', + 'Santiago', + 'Santo_Domingo', + 'Sao_Paulo', + 'Scoresbysund', + 'Shiprock', + 'Sitka', + 'St_Barthelemy', + 'St_Johns', + 'St_Kitts', + 'St_Lucia', + 'St_Thomas', + 'St_Vincent', + 'Swift_Current', + 'Tegucigalpa', + 'Thule', + 'Thunder_Bay', + 'Tijuana', + 'Toronto', + 'Tortola', + 'Vancouver', + 'Virgin', + 'Whitehorse', + 'Winnipeg', + 'Yakutat', + 'Yellowknife', + ], + 'Antarctica':[ + 'Casey', + 'Davis', + 'DumontDUrville', + 'Macquarie', + 'Mawson', + 'McMurdo', + 'Palmer', + 'Rothera', + 'South_Pole', + 'Syowa', + 'Vostok', + ], + 'Arctic':[ + 'Longyearbyen', + ], + 'Asia': [ + 'Aden', + 'Almaty', + 'Amman', + 'Anadyr', + 'Aqtau', + 'Aqtobe', + 'Ashgabat', + 'Ashkhabad', + 'Baghdad', + 'Bahrain', + 'Baku', + 'Bangkok', + 'Beirut', + 'Bishkek', + 'Brunei', + 'Calcutta', + 'Choibalsan', + 'Chongqing', + 'Chungking', + 'Colombo', + 'Dacca', + 'Damascus', + 'Dhaka', + 'Dili', + 'Dubai', + 'Dushanbe', + 'Gaza', + 'Harbin', + 'Hebron', + 'Ho_Chi_Minh', + 'Hong_Kong', + 'Hovd', + 'Irkutsk', + 'Istanbul', + 'Jakarta', + 'Jayapura', + 'Jerusalem', + 'Kabul', + 'Kamchatka', + 'Karachi', + 'Kashgar', + 'Kathmandu', + 'Katmandu', + 'Khandyga', + 'Kolkata', + 'Krasnoyarsk', + 'Kuala_Lumpur', + 'Kuching', + 'Kuwait', + 'Macao', + 'Macau', + 'Magadan', + 'Makassar', + 'Manila', + 'Muscat', + 'Nicosia', + 'Novokuznetsk', + 'Novosibirsk', + 'Omsk', + 'Oral', + 'Phnom_Penh', + 'Pontianak', + 'Pyongyang', + 'Qatar', + 'Qyzylorda', + 'Rangoon', + 'Riyadh', + 'Saigon', + 'Sakhalin', + 'Samarkand', + 'Seoul', + 'Shanghai', + 'Singapore', + 'Taipei', + 'Tashkent', + 'Tbilisi', + 'Tehran', + 'Tel_Aviv', + 'Thimbu', + 'Thimphu', + 'Tokyo', + 'Ujung_Pandang', + 'Ulaanbaatar', + 'Ulan_Bator', + 'Urumqi', + 'Ust-Nera', + 'Vientiane', + 'Vladivostok', + 'Yakutsk', + 'Yekaterinburg', + 'Yerevan', + ], + 'Atlantic': [ + 'Azores', + 'Bermuda', + 'Canary', + 'Cape_Verde', + 'Faeroe', + 'Faroe', + 'Jan_Mayen', + 'Madeira', + 'Reykjavik', + 'South_Georgia', + 'St_Helena', + 'Stanley', + ], + 'Australia': [ + 'ACT', + 'Adelaide', + 'Brisbane', + 'Broken_Hill', + 'Canberra', + 'Currie', + 'Darwin', + 'Eucla', + 'Hobart', + 'LHI', + 'Lindeman', + 'Lord_Howe', + 'Melbourne', + 'NSW', + 'North', + 'Perth', + 'Queensland', + 'South', + 'Sydney', + 'Tasmania', + 'Victoria', + 'West', + 'Yancowinna', + ], + 'Europe': [ + 'Amsterdam', + 'Andorra', + 'Athens', + 'Belfast', + 'Belgrade', + 'Berlin', + 'Bratislava', + 'Brussels', + 'Bucharest', + 'Budapest', + 'Busingen', + 'Chisinau', + 'Copenhagen', + 'Dublin', + 'Gibraltar', + 'Guernsey', + 'Helsinki', + 'Isle_of_Man', + 'Istanbul', + 'Jersey', + 'Kaliningrad', + 'Kiev', + 'Lisbon', + 'Ljubljana', + 'London', + 'Luxembourg', + 'Madrid', + 'Malta', + 'Mariehamn', + 'Minsk', + 'Monaco', + 'Moscow', + 'Nicosia', + 'Oslo', + 'Paris', + 'Podgorica', + 'Prague', + 'Riga', + 'Rome', + 'Samara', + 'San_Marino', + 'Sarajevo', + 'Simferopol', + 'Skopje', + 'Sofia', + 'Stockholm', + 'Tallinn', + 'Tirane', + 'Tiraspol', + 'Uzhgorod', + 'Vaduz', + 'Vatican', + 'Vienna', + 'Vilnius', + 'Volgograd', + 'Warsaw', + 'Zagreb', + 'Zaporozhye', + 'Zurich', + ], + 'Indian': [ + 'Antananarivo', + 'Chagos', + 'Christmas', + 'Cocos', + 'Comoro', + 'Kerguelen', + 'Mahe', + 'Maldives', + 'Mauritius', + 'Mayotte', + 'Reunion', + ], + 'Pacific': [ + 'Apia', + 'Auckland', + 'Chatham', + 'Chuuk', + 'Easter', + 'Efate', + 'Enderbury', + 'Fakaofo', + 'Fiji', + 'Funafuti', + 'Galapagos', + 'Gambier', + 'Guadalcanal', + 'Guam', + 'Honolulu', + 'Johnston', + 'Kiritimati', + 'Kosrae', + 'Kwajalein', + 'Majuro', + 'Marquesas', + 'Midway', + 'Nauru', + 'Niue', + 'Norfolk', + 'Noumea', + 'Pago_Pago', + 'Palau', + 'Pitcairn', + 'Pohnpei', + 'Ponape', + 'Port_Moresby', + 'Rarotonga', + 'Saipan', + 'Samoa', + 'Tahiti', + 'Tarawa', + 'Tongatapu', + 'Truk', + 'Wake', + 'Wallis', + 'Yap', + ], + } + + def __init__(self, parent=None): + """ + Constructor + + @param parent reference to the parent widget (QWidget) + """ + super().__init__(parent) + self.setupUi(self) + + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) + + self.regionCombo.addItems([" "] + sorted(self.Timezones.keys())) + + def __updateOK(self): + """ + Private method to update the OK button. + """ + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( + bool(self.regionCombo.currentText()) and + len(self.cityList.selectedItems()) == 1 + ) + + @pyqtSlot(str) + def on_regionCombo_currentIndexChanged(self, region): + """ + Private slot handling the selection of a time zone region. + + @param region selected region (string) + """ + self.cityList.clear() + if region in self.Timezones: + self.cityList.addItems(sorted(self.Timezones[region])) + + self.__updateOK() + + @pyqtSlot() + def on_cityList_itemSelectionChanged(self): + """ + Private slot handling a change of the city selection. + """ + self.__updateOK() + + def getData(self): + """ + Public method to retrieve the data. + + @return selected time zone (string) + """ + if (self.regionCombo.currentText() and + len(self.cityList.selectedItems()) == 1): + return "{0}/{1}".format( + self.regionCombo.currentText(), + self.cityList.selectedItems()[0].text() + ) + else: + return "" + + @staticmethod + def getTimezone(parent=None): + """ + Static method to select a time zone. + + @param parent reference to the parent widget (QWidget) + @return tuple of selected time zone (string) and flag indicating + the acceptance state (boolean) + """ + dlg = TimezoneSelectionDialog(parent) + tz = "" + if dlg.exec_() == QDialog.Accepted: + tz = dlg.getData() + return tz, bool(tz)