Debian Codegraph @

lib/encoding_selector.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Contributors:
5 # INADA Naoki, 2009
6 # Alexander Belchenko, 2009
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
22 import codecs
23 from PyQt5 import QtCore, QtGui, QtWidgets
24
25 from breezy.plugins.qbrz.lib.i18n import gettext
26 from breezy.plugins.qbrz.lib.util import is_valid_encoding
27 from breezy.osutils import get_user_encoding, get_terminal_encoding
28
29
30 class UniqueList(object):
31 """List-like object with unique-only append."""
32
33 def __init__(self, data=None):
34 self._data = []
35 if data:
36 self._data.extend(data)
37
38 def append(self, item):
39 if item not in self._data:
40 self._data.append(item)
41
42 def __add__(self, other):
43 for item in other:
44 self.append(item)
45 return UniqueList(self._data)
46
47 def __iter__(self):
48 return iter(self._data)
49
50
51 class BaseEncodingSelector(object):
52
53 def init_encodings(self, initial_encoding=None):
54 _encodings = UniqueList()
55 if initial_encoding:
56 _encodings.append(initial_encoding)
57 _encodings.append(get_user_encoding())
58 _encodings.append(get_terminal_encoding())
59 _encodings += python_encodings
60 self.encodings = list(filter(is_valid_encoding, _encodings))
61
62 if initial_encoding:
63 if initial_encoding not in self.encodings:
64 self.encodings.insert(0, initial_encoding)
65 else:
66 initial_encoding = 'utf-8'
67 self._encoding = initial_encoding
68
69 def _encodingChanged(self, encoding):
70 try:
71 encoding = str(encoding) # may raise UnicodeError
72 codecs.lookup(encoding) # may raise LookupError
73 self._encoding = encoding
74 self.onChanged(encoding)
75 except (UnicodeError, LookupError):
76 QtWidgets.QMessageBox.critical(self, gettext("Wrong encoding"),
77 gettext('Encoding "%s" is invalid or not supported.') % str(encoding))
78 self.setEncoding(self._encoding)
79
80 def getEncoding(self):
81 return self._encoding
82
83 def setEncoding(self, encoding):
84 if is_valid_encoding(encoding):
85 self._encoding = encoding
86 self._setEncoding(encoding)
87
88 def _setEncoding(self, encoding):
89 pass
90
91 encoding = property(getEncoding, setEncoding)
92
93
94 class EncodingSelector(QtWidgets.QWidget, BaseEncodingSelector):
95 """Widget to control encoding of text."""
96
97 def __init__(self, initial_encoding=None, label_text=None, onChanged=None,
98 *args):
99 """Create widget: label + combobox.
100 @param initial_encoding: initially selected encoding (default: utf-8).
101 @param label_text: text for label (default: "Encoding:").
102 @param onChanged: callback to processing encoding change.
103 @param args: additional arguments to initialize QWidget.
104 """
105 QtWidgets.QWidget.__init__(self, *args)
106 self.init_encodings(initial_encoding)
107 self.onChanged = onChanged
108 if onChanged is None:
109 self.onChanged = lambda encoding: None
110
111 layout = QtWidgets.QHBoxLayout()
112
113 if label_text is None:
114 label_text = gettext("Encoding:")
115 self._label = QtWidgets.QLabel(label_text)
116 layout.addWidget(self._label)
117
118 self.chooser = QtWidgets.QComboBox()
119 self.chooser.addItems(self.encodings)
120 self.chooser.setEditable(True)
121 self.chooser.setEditText(self.encoding)
122 self.chooser.currentIndexChanged['QString'].connect(self._encodingChanged)
123 self.chooser.focusOutEvent = self._focusOut
124 layout.addWidget(self.chooser)
125
126 self.setLayout(layout)
127
128 def _focusOut(self, ev):
129 encoding = self.chooser.currentText()
130 if self._encoding != encoding:
131 self._encodingChanged(encoding)
132 QtWidgets.QComboBox.focusOutEvent(self.chooser, ev)
133
134 def _setEncoding(self, encoding):
135 self.chooser.setEditText(encoding)
136
137 def getLabel(self):
138 return str(self._label.text())
139
140 def setLabel(self, new_label):
141 self._label.setText(new_label)
142
143 label = property(getLabel, setLabel)
144
145
146 class EncodingMenuSelector(QtWidgets.QMenu, BaseEncodingSelector):
147 """Menu to control encoding of text."""
148
149 def __init__(self, initial_encoding=None, label_text=None, onChanged=None,
150 *args):
151 """Create widget: label + combobox.
152 @param initial_encoding: initially selected encoding (default: utf-8).
153 @param label_text: text for label (default: "Encoding:").
154 @param onChanged: callback to processing encoding change.
155 @param args: additional arguments to initialize QWidget.
156 """
157 QtWidgets.QMenu.__init__(self, *args)
158 self.init_encodings(initial_encoding)
159 self.onChanged = onChanged
160 if onChanged is None:
161 self.onChanged = lambda encoding: None
162
163 self.setTitle(label_text)
164 self.setStyleSheet("QMenu { menu-scrollable: 1; }")
165
166 self.action_group = QtWidgets.QActionGroup(self)
167
168 self.encoding_actions = {}
169 for encoding in self.encodings:
170 action = QtWidgets.QAction(encoding, self.action_group)
171 action.setCheckable(True)
172 action.setData(encoding)
173 self.addAction(action)
174 self.encoding_actions[encoding] = action
175
176 self._setEncoding(self.encoding)
177 # RJLRJL changed triggered to _triggered
178 self.triggered[QtWidgets.QAction].connect(self._triggered)
179
180 def _triggered(self, action):
181 encoding = action.data()
182 self._encodingChanged(encoding)
183
184 def _setEncoding(self, encoding):
185 if encoding in self.encoding_actions:
186 self.encoding_actions[encoding].setChecked(True)
187
188 def getLabel(self):
189 return str(self.title())
190
191 def setLabel(self, new_label):
192 self.setTitle(new_label)
193
194 label = property(getLabel, setLabel)
195
196
197 # The 'python_encodings' horror below changes underscores to dashes and them makes a list.
198 # What you actually get is:
199 #
200 # ['utf-8', 'ascii', 'latin-1', 'big5', 'big5hkscs', 'cp037', 'cp1006', 'cp1026', 'cp1140', 'cp1250', 'cp1251',
201 # 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp424', 'cp437', 'cp500', 'cp737', 'cp775',
202 # 'cp850', 'cp852', 'cp855', 'cp856', 'cp857', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866', 'cp869',
203 # 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'euc-jisx0213', 'euc-jis-2004', 'euc-jp', 'euc-kr', 'gb18030', 'gb2312',
204 # 'gbk', 'hp-roman8', 'hz', 'iso2022-jp', 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3',
205 # 'iso2022-jp-ext', 'iso2022-kr', 'iso8859-1', 'iso8859-10', 'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15',
206 # 'iso8859-16', 'iso8859-2', 'iso8859-3', 'iso8859-4', 'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9',
207 # 'johab', 'koi8-r', 'koi8-u', 'mac-arabic', 'mac-centeuro', 'mac-croatian', 'mac-cyrillic', 'mac-farsi', 'mac-greek',
208 # 'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-romanian', 'mac-turkish', 'ptcp154', 'shift-jis', 'shift-jisx0213',
209 # 'shift-jis-2004', 'tis-620', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-32', 'utf-32-be', 'utf-32-le', 'utf-7', 'utf-8-sig']
210 #
211 # ...and before you ask, no, I don't know why they didn't just do that either. What larks!
212
213 # human encodings found in standard Python encodings package.
214 python_encodings = """
215 utf_8
216 ascii
217 latin_1
218 big5
219 big5hkscs
220 cp037
221 cp1006
222 cp1026
223 cp1140
224 cp1250
225 cp1251
226 cp1252
227 cp1253
228 cp1254
229 cp1255
230 cp1256
231 cp1257
232 cp1258
233 cp424
234 cp437
235 cp500
236 cp737
237 cp775
238 cp850
239 cp852
240 cp855
241 cp856
242 cp857
243 cp860
244 cp861
245 cp862
246 cp863
247 cp864
248 cp865
249 cp866
250 cp869
251 cp874
252 cp875
253 cp932
254 cp949
255 cp950
256 euc_jisx0213
257 euc_jis_2004
258 euc_jp
259 euc_kr
260 gb18030
261 gb2312
262 gbk
263 hp_roman8
264 hz
265 iso2022_jp
266 iso2022_jp_1
267 iso2022_jp_2
268 iso2022_jp_2004
269 iso2022_jp_3
270 iso2022_jp_ext
271 iso2022_kr
272 iso8859_1
273 iso8859_10
274 iso8859_11
275 iso8859_13
276 iso8859_14
277 iso8859_15
278 iso8859_16
279 iso8859_2
280 iso8859_3
281 iso8859_4
282 iso8859_5
283 iso8859_6
284 iso8859_7
285 iso8859_8
286 iso8859_9
287 johab
288 koi8_r
289 koi8_u
290 mac_arabic
291 mac_centeuro
292 mac_croatian
293 mac_cyrillic
294 mac_farsi
295 mac_greek
296 mac_iceland
297 mac_latin2
298 mac_roman
299 mac_romanian
300 mac_turkish
301 ptcp154
302 shift_jis
303 shift_jisx0213
304 shift_jis_2004
305 tis_620
306 utf_16
307 utf_16_be
308 utf_16_le
309 utf_32
310 utf_32_be
311 utf_32_le
312 utf_7
313 utf_8_sig
314 """.replace('_','-').split()