eric6/Helpviewer/HelpLanguagesDialog.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to configure the preferred languages.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot, QByteArray, QLocale, QStringListModel
13 from PyQt5.QtWidgets import QDialog
14
15 from .Ui_HelpLanguagesDialog import Ui_HelpLanguagesDialog
16
17 import Preferences
18
19
20 class HelpLanguagesDialog(QDialog, Ui_HelpLanguagesDialog):
21 """
22 Class implementing a dialog to configure the preferred languages.
23 """
24 def __init__(self, parent=None):
25 """
26 Constructor
27
28 @param parent reference to the parent widget (QWidget)
29 """
30 super(HelpLanguagesDialog, self).__init__(parent)
31 self.setupUi(self)
32
33 self.__model = QStringListModel()
34 self.languagesList.setModel(self.__model)
35 self.languagesList.selectionModel().currentChanged.connect(
36 self.__currentChanged)
37
38 languages = Preferences.toList(Preferences.Prefs.settings.value(
39 "Help/AcceptLanguages", self.defaultAcceptLanguages()))
40 self.__model.setStringList(languages)
41
42 allLanguages = []
43 for index in range(QLocale.C + 1, QLocale.LastLanguage + 1):
44 allLanguages += self.expand(QLocale.Language(index))
45 self.__allLanguagesModel = QStringListModel()
46 self.__allLanguagesModel.setStringList(allLanguages)
47 self.addCombo.setModel(self.__allLanguagesModel)
48
49 def __currentChanged(self, current, previous):
50 """
51 Private slot to handle a change of the current selection.
52
53 @param current index of the currently selected item (QModelIndex)
54 @param previous index of the previously selected item (QModelIndex)
55 """
56 self.removeButton.setEnabled(current.isValid())
57 row = current.row()
58 self.upButton.setEnabled(row > 0)
59 self.downButton.setEnabled(
60 row != -1 and row < self.__model.rowCount() - 1)
61
62 @pyqtSlot()
63 def on_upButton_clicked(self):
64 """
65 Private slot to move a language up.
66 """
67 currentRow = self.languagesList.currentIndex().row()
68 data = self.languagesList.currentIndex().data()
69 self.__model.removeRow(currentRow)
70 self.__model.insertRow(currentRow - 1)
71 self.__model.setData(self.__model.index(currentRow - 1), data)
72 self.languagesList.setCurrentIndex(self.__model.index(currentRow - 1))
73
74 @pyqtSlot()
75 def on_downButton_clicked(self):
76 """
77 Private slot to move a language down.
78 """
79 currentRow = self.languagesList.currentIndex().row()
80 data = self.languagesList.currentIndex().data()
81 self.__model.removeRow(currentRow)
82 self.__model.insertRow(currentRow + 1)
83 self.__model.setData(self.__model.index(currentRow + 1), data)
84 self.languagesList.setCurrentIndex(self.__model.index(currentRow + 1))
85
86 @pyqtSlot()
87 def on_removeButton_clicked(self):
88 """
89 Private slot to remove a language from the list of acceptable
90 languages.
91 """
92 currentRow = self.languagesList.currentIndex().row()
93 self.__model.removeRow(currentRow)
94
95 @pyqtSlot()
96 def on_addButton_clicked(self):
97 """
98 Private slot to add a language to the list of acceptable languages.
99 """
100 language = self.addCombo.currentText()
101 if language in self.__model.stringList():
102 return
103
104 self.__model.insertRow(self.__model.rowCount())
105 self.__model.setData(self.__model.index(self.__model.rowCount() - 1),
106 language)
107 self.languagesList.setCurrentIndex(
108 self.__model.index(self.__model.rowCount() - 1))
109
110 def accept(self):
111 """
112 Public method to accept the data entered.
113 """
114 result = self.__model.stringList()
115 if result == self.defaultAcceptLanguages():
116 Preferences.Prefs.settings.remove("Help/AcceptLanguages")
117 else:
118 Preferences.Prefs.settings.setValue("Help/AcceptLanguages", result)
119 super(HelpLanguagesDialog, self).accept()
120
121 @classmethod
122 def httpString(cls, languages):
123 """
124 Class method to convert a list of acceptable languages into a
125 byte array.
126
127 The byte array can be sent along with the Accept-Language http header
128 (see RFC 2616).
129
130 @param languages list of acceptable languages (list of strings)
131 @return converted list (QByteArray)
132 """
133 processed = []
134 qvalue = 1.0
135 for language in languages:
136 leftBracket = language.find('[')
137 rightBracket = language.find(']')
138 tag = language[leftBracket + 1:rightBracket]
139 if not processed:
140 processed.append(tag)
141 else:
142 processed.append("{0};q={1:.1f}".format(tag, qvalue))
143 if qvalue > 0.1:
144 qvalue -= 0.1
145
146 return QByteArray(", ".join(processed).encode("utf-8"))
147
148 @classmethod
149 def defaultAcceptLanguages(cls):
150 """
151 Class method to get the list of default accept languages.
152
153 @return list of acceptable languages (list of strings)
154 """
155 language = QLocale.system().name()
156 if not language:
157 return []
158 else:
159 return cls.expand(QLocale(language).language())
160
161 @classmethod
162 def expand(cls, language):
163 """
164 Class method to expand a language enum to a readable languages
165 list.
166
167 @param language language number (QLocale.Language)
168 @return list of expanded language names (list of strings)
169 """
170 allLanguages = []
171 countries = [l.country() for l in QLocale.matchingLocales(
172 language, QLocale.AnyScript, QLocale.AnyCountry)]
173 languageString = "{0} [{1}]"\
174 .format(QLocale.languageToString(language),
175 QLocale(language).name().split('_')[0])
176 allLanguages.append(languageString)
177 for country in countries:
178 languageString = "{0}/{1} [{2}]"\
179 .format(QLocale.languageToString(language),
180 QLocale.countryToString(country),
181 '-'.join(QLocale(language, country).name()
182 .split('_')).lower())
183 if languageString not in allLanguages:
184 allLanguages.append(languageString)
185
186 return allLanguages

eric ide

mercurial