1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
"""
This module contains some dialogs to help you manage encodings in
you application.
"""
import locale
from pyqode.core import icons
from pyqode.core.api import encodings
from pyqode.qt import QtCore, QtWidgets
from pyqode.core.cache import Cache
from pyqode.core._forms import dlg_preferred_encodings_editor_ui
class DlgPreferredEncodingsEditor(QtWidgets.QDialog):
"""
This dialog is used to edit the preferred encodings that appears in
the encodings menu/combo box.
"""
def __init__(self, parent=None):
super(DlgPreferredEncodingsEditor, self).__init__(parent)
self.ui = dlg_preferred_encodings_editor_ui.Ui_Dialog()
self.ui.setupUi(self)
self._load_preferred()
self.ui.pushButtonAdd.clicked.connect(self._add)
self.ui.pushButtonAdd.setIcon(icons.icon(
'go-next', ':/pyqode-icons/rc/go-next.png', 'fa.arrow-right'))
self.ui.pushButtonRemove.setIcon(icons.icon(
'go-previous', ':/pyqode-icons/rc/go-previous.png',
'fa.arrow-left'))
self.ui.pushButtonRemove.clicked.connect(self._remove)
def _load_available(self):
self.ui.tableWidgetAvailable.setColumnCount(2)
self.ui.tableWidgetAvailable.setSelectionMode(
self.ui.tableWidgetAvailable.SingleSelection)
self.ui.tableWidgetAvailable.setSelectionBehavior(
self.ui.tableWidgetAvailable.SelectRows)
self.ui.tableWidgetAvailable.setHorizontalHeaderLabels([
'Encoding', 'Language'])
self.ui.tableWidgetAvailable.verticalHeader().hide()
self.ui.tableWidgetAvailable.setSortingEnabled(True)
preferred = Cache().preferred_encodings
for key in sorted(encodings.ENCODINGS_MAP.keys()):
value = encodings.ENCODINGS_MAP[key]
if key not in preferred:
# lang_item.setData(QtCore.Qt.UserRole, key)
row = self.ui.tableWidgetAvailable.rowCount()
self.ui.tableWidgetAvailable.insertRow(row)
for column in range(2):
item = QtWidgets.QTableWidgetItem(value[column].strip())
item.setData(QtCore.Qt.UserRole, key)
# item.setData(QtCore.Qt.UserRole, key)
self.ui.tableWidgetAvailable.setItem(row, column, item)
self.ui.tableWidgetAvailable.sortByColumn(0, QtCore.Qt.AscendingOrder)
def _load_preferred(self):
self._load_available() # setup preferred encodings
self.ui.tableWidgetPreferred.setColumnCount(2)
self.ui.tableWidgetPreferred.setSelectionMode(
self.ui.tableWidgetPreferred.SingleSelection)
self.ui.tableWidgetPreferred.setSelectionBehavior(
self.ui.tableWidgetPreferred.SelectRows)
self.ui.tableWidgetPreferred.setHorizontalHeaderLabels([
'Encoding', 'Language'])
self.ui.tableWidgetPreferred.verticalHeader().hide()
self.ui.tableWidgetPreferred.setSortingEnabled(True)
for i, encoding in enumerate(Cache().preferred_encodings):
encoding = encodings.convert_to_codec_key(encoding)
value = encodings.ENCODINGS_MAP[encoding]
row = self.ui.tableWidgetPreferred.rowCount()
self.ui.tableWidgetPreferred.insertRow(row)
for column in range(2):
item = QtWidgets.QTableWidgetItem(value[column].strip())
item.setData(QtCore.Qt.UserRole, encoding)
self.ui.tableWidgetPreferred.setItem(row, column, item)
self.ui.tableWidgetPreferred.sortByColumn(0, QtCore.Qt.AscendingOrder)
def _transfer_selected_items(self, source, destination):
# keeping sorting enabled cause bug for the second transferred item
destination.setSortingEnabled(False)
row = source.currentRow()
if row != -1:
# take items from source
items = []
encoding = source.item(row, 0).data(QtCore.Qt.UserRole)
is_locale = encoding == encodings.convert_to_codec_key(
locale.getpreferredencoding())
if source == self.ui.tableWidgetPreferred and is_locale:
destination.setSortingEnabled(True)
return
for i in range(2):
items.append(source.takeItem(row, i))
source.removeRow(row)
# insert a new row in the taken items into destination
row = destination.rowCount()
destination.insertRow(row)
for col, item in enumerate(items):
item = QtWidgets.QTableWidgetItem(item)
destination.setItem(row, col, item)
destination.setSortingEnabled(True)
def _add(self):
self._transfer_selected_items(self.ui.tableWidgetAvailable,
self.ui.tableWidgetPreferred)
def _remove(self):
self._transfer_selected_items(self.ui.tableWidgetPreferred,
self.ui.tableWidgetAvailable)
def get_preferred_encodings(self):
"""
Gets the list of preferred encodings.
:return: list
"""
encodings = []
for row in range(self.ui.tableWidgetPreferred.rowCount()):
item = self.ui.tableWidgetPreferred.item(row, 0)
encodings.append(item.data(QtCore.Qt.UserRole))
return encodings
@classmethod
def edit_encoding(cls, parent):
"""
Static helper method that shows the encoding editor dialog
If the dialog was accepted the new encodings are added to the settings.
:param parent: parent widget
:return: True in case of succes, False otherwise
"""
dlg = cls(parent)
if dlg.exec_() == dlg.Accepted:
settings = Cache()
settings.preferred_encodings = dlg.get_preferred_encodings()
return True
return False
class DlgEncodingsChoice(QtWidgets.QDialog):
"""
This dialogs ask the user to choose an encoding from a combo box.
You can use it if you're not using the encoding panel when there is a
decoding error when opening a file.
"""
def __init__(self, parent, path, encoding):
super(DlgEncodingsChoice, self).__init__(parent)
self.setWindowTitle('Choose encoding')
# avoid circular references with CodeEdit
from pyqode.core._forms import dlg_encodings_ui
self.ui = dlg_encodings_ui.Ui_Dialog()
self.ui.setupUi(self)
self.ui.comboBoxEncodings.current_encoding = encoding
self.ui.lblDescription.setText(
self.ui.lblDescription.text() %
(_('There was a problem opening the file %r with encoding: %s') %
(path, encoding)))
@classmethod
def choose_encoding(cls, parent, path, encoding):
"""
Show the encodings dialog and returns the user choice.
:param parent: parent widget.
:param path: file path
:param encoding: current file encoding
:return: selected encoding
"""
dlg = cls(parent, path, encoding)
dlg.exec_()
return dlg.ui.comboBoxEncodings.current_encoding
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
new_encoding = DlgEncodingsChoice.choose_encoding(None, __file__, 'utf-8')
print(new_encoding)
|