| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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) |
| 72 |
codecs.lookup(encoding) |
| 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 |
|
| 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 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 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() |