def accept(self): nick = self.nickname if not nick: return error_dialog(self, _('Must specify nickname'), _( 'You must specify a nickname for this dictionary'), show=True) if nick in {d.name for d in custom_dictionaries()}: return error_dialog(self, _('Nickname already used'), _( 'A dictionary with the nick name "%s" already exists.') % nick, show=True) oxt = unicode(self.path.text()) try: num = import_from_oxt(oxt, nick) except: import traceback return error_dialog(self, _('Failed to import dictionaries'), _( 'Failed to import dictionaries from %s. Click "Show Details" for more information') % oxt, det_msg=traceback.format_exc(), show=True) if num == 0: return error_dialog(self, _('No dictionaries'), _( 'No dictionaries were found in %s') % oxt, show=True) QDialog.accept(self)
def build_dictionaries(self, reread=False): all_dictionaries = builtin_dictionaries() | custom_dictionaries(reread=reread) languages = defaultdict(lambda : defaultdict(set)) for d in all_dictionaries: for locale in d.locales | {d.primary_locale}: languages[locale.langcode][locale.countrycode].add(d) bf = QFont(self.dictionaries.font()) bf.setBold(True) itf = QFont(self.dictionaries.font()) itf.setItalic(True) self.dictionaries.clear() for lc in sorted(languages, key=lambda x:sort_key(calibre_langcode_to_name(x))): i = QTreeWidgetItem(self.dictionaries, LANG) i.setText(0, calibre_langcode_to_name(lc)) i.setData(0, Qt.UserRole, lc) best_country = getattr(best_locale_for_language(lc), 'countrycode', None) for countrycode in sorted(languages[lc], key=lambda x: country_map()['names'].get(x, x)): j = QTreeWidgetItem(i, COUNTRY) j.setText(0, country_map()['names'].get(countrycode, countrycode)) j.setData(0, Qt.UserRole, countrycode) if countrycode == best_country: j.setData(0, Qt.FontRole, bf) pd = get_dictionary(DictionaryLocale(lc, countrycode)) for dictionary in sorted(languages[lc][countrycode], key=lambda d:d.name): k = QTreeWidgetItem(j, DICTIONARY) pl = calibre_langcode_to_name(dictionary.primary_locale.langcode) if dictionary.primary_locale.countrycode: pl += '-' + dictionary.primary_locale.countrycode.upper() k.setText(0, dictionary.name or (_('<Builtin dictionary for {0}>').format(pl))) k.setData(0, Qt.UserRole, dictionary) if dictionary.name: k.setFlags(k.flags() | Qt.ItemIsEditable) if pd == dictionary: k.setData(0, Qt.FontRole, itf) self.dictionaries.expandAll()