1
# -*- coding: utf-8 -*-
6
Provides various kinds of good-to-have-at-hand tables for charsets, colors
10
Brief application overview:
12
The dialog is wrapped around a QTableView. QAbstractTableModels
13
are dynamically inserted at runtime. Each model is packed in a ModelFactory
14
wich can hold multiple models in multiple categories. All ModelFactories
15
are then handed out to the the controler of the dialog for further processing.
18
The dialog comes equipped to handle the Eric4 special functionality
19
of inserting the contents of a selected cell directly into at the current position
20
of the current editor, but may aswell be used as standalone dialog.
23
Note that chars may or may not get displayed properly. Also some
24
ranges in some encodings are left out for performance reasons.
29
from PyQt4 import QtCore, QtGui
31
from Ui_chartables import Ui_CT_DialogCharTables
33
# import model factories
35
from chartables_model_options import Options
36
from chartables_controler import Controler
37
from chartables_models_charsets import CHARSET_MODEL_FACTORIES
38
from chartables_models_colors import COLOR_MODEL_FACTORIES
39
from chartables_models_entities import ENTITY_MODEL_FACTORIES
40
import chartables_lang
42
from chartables_lib.areatip import AreaTip
43
from chartables_lib.combobox import ComboBoxWithEditTipWrap
47
# in no particular order
49
# - anyone for more tables?
51
# - currently clipboard and drag offers only 'text/plain' format
52
# - some visual feedback would be nice to indicate an item was copied/inserted
54
# - tooltips for truncated items are not 100% accurate. Pretty hard to find out
55
# when exactly Qt truncates strings in combos.
56
# - toltip for edit filed in combobox flaps out a tad to fast. Higher delay
59
# - maybe an option to make the dialog stay topmost?
60
# - maybe a gridlines on/off option?
63
#***********************************************************************
65
#***********************************************************************
66
ICO_APP = 'chartables_small.png'
68
CFG_ORG_NAME = 'jUrner'
69
CFG_APP_NAME = 'CTDialogCharTables'
70
CFG_KEY_STATE = 'State'
71
CFG_KEY_GEOMETRY = 'Geometry'
72
CFG_KEY_FLAGS = 'Flags'
73
CFG_KEY_LOCALE = 'Locale'
77
ID_CHARSETTYPECOMBO = 'cmbCharsetType'
78
ID_CHARSETCOMBO = 'cmbCharset'
79
ID_COLORTYPECOMBO = 'cmbColorType'
80
ID_COLORCOMBO = 'cmbColor'
81
ID_ENTITYTYPECOMBO = 'cmbEntityType'
82
ID_ENTITYCOMBO = 'cmbEntity'
83
ID_TABLE = 'tableView'
85
ID_BUTTON_BOX = 'buttonBox'
87
ID_BTINSERT = 'btInsert' # special button for Eric4
92
(CHARSET_MODEL_FACTORIES, ID_CHARSETTYPECOMBO, ID_CHARSETCOMBO),
93
(COLOR_MODEL_FACTORIES, ID_COLORTYPECOMBO, ID_COLORCOMBO),
94
(ENTITY_MODEL_FACTORIES, ID_ENTITYTYPECOMBO, ID_ENTITYCOMBO),
98
CMB_MIN_CONTENTS_LEN = 5
100
#***********************************************************************
102
#***********************************************************************
103
class DialogCharTables(QtGui.QWidget, Ui_CT_DialogCharTables):
107
def __init__(self, *args, **kwargs):
111
@keyparam flagsEx CTFEX_* runtime flags
112
@keyparam localeName if specified adjusts the language of the dialog
113
to the specified language
115
QtGui.QWidget.__init__(self, *args)
117
self.options = Options(self, flagsEx=kwargs.get('flagsEx', Options.CTFEX_NONE))
118
localeName = kwargs.get('localeName', None)
120
self.setupUi(localeName=localeName)
124
def setupUi(self, localeName=None):
126
overwritten method to setup the user interface
128
@param parent parent of the dialog
131
self.controler = None
135
settings = self.getSettingsObject()
136
settingsState = settings.value(
140
settingsFlags, ok = settings.value(
142
QtCore.QVariant(Options.CTF_MASK_DEFAULT)
145
self.options.flags = settingsFlags
147
self.options.areatips = AreaTip(self)
150
if not self.options.flagsEx & self.options.CTFEX_NOTRANSLATE:
152
settingsLocale = settings.value(
154
QtCore.QVariant(self.options.localeName)
156
localeName = str(settingsLocale)
158
self.options.language = chartables_lang.Language()
159
if not self.options.language.hasLocaleName(localeName):
160
localeName = self.options.language.getDefaultLocaleName()
161
self.options.localeName =localeName
162
self.options.language.installTranslator(self, localeName)
166
Ui_CT_DialogCharTables.setupUi(self, self)
170
table = self.controlById(ID_TABLE)
171
table.verticalHeader().setVisible(False)
172
table.setDragEnabled(True)
173
table.setSelectionMode(table.SingleSelection)
176
QtCore.SIGNAL('activated(const QModelIndex&)'),
181
tab = self.controlById(ID_TAB)
182
self.controler = Controler(table, tab, options=self.options)
185
QtCore.SIGNAL('optionsDisplayed(const QWidget&, bool)'),
186
self._onOptionsDisplayed
190
QtCore.SIGNAL('modelInserted(const QAbstractTableModel&)'),
191
self._onModelInserted
194
for factories, idTypeCmb, idModelCmb in MODEL_FACTORIES:
195
cmbType = self.controlById(idTypeCmb)
196
cmbModel = self.controlById(idModelCmb)
198
# strings may be long here, so truncate if necessary
199
# and show tooltips if necessary
200
cmbModel.setMinimumContentsLength(CMB_MIN_CONTENTS_LEN)
201
cmbModel.setSizeAdjustPolicy(cmbModel.AdjustToMinimumContentsLength)
202
ComboBoxWithEditTipWrap(cmbModel)
204
self.controler.newFactory(factories, cmbType, cmbModel)
208
buttonBox = self.controlById(ID_BUTTON_BOX)
210
btCopy = self.controlById(ID_BTCOPY)
211
buttonBox.addButton(btCopy, buttonBox.ActionRole)
212
btCopy.setEnabled(False)
213
self.connect(btCopy, QtCore.SIGNAL('clicked()'), self._onBtCopyClicked)
215
# init Eric4 special to handle 'Insert At' button and QSettings
216
btInsert = self.controlById(ID_BTINSERT)
217
buttonBox.addButton(btInsert, buttonBox.ActionRole)
218
btInsert.setEnabled(False)
219
btInsert.setVisible(False)
222
# throw application icon on the caption
223
fpath = os.path.join(os.path.split(__file__)[0], ICO_APP)
224
if os.path.isfile(fpath):
225
ico = QtGui.QIcon(fpath)
227
self.setWindowIcon(ico)
231
self.controler.restoreState(str(settingsState))
232
v = settings.value(CFG_KEY_GEOMETRY)
234
self.restoreGeometry(v.toByteArray())
239
def closeEvent(self, evt):
240
"""protected slot called when the dialog is closed"""
241
settings = self.getSettingsObject()
242
settings.setValue(CFG_KEY_STATE, QtCore.QVariant(self.controler.saveState()))
243
settings.setValue(CFG_KEY_FLAGS, QtCore.QVariant(self.options.flags))
244
settings.setValue(CFG_KEY_LOCALE, QtCore.QVariant(self.options.localeName))
245
settings.setValue(CFG_KEY_GEOMETRY, QtCore.QVariant(self.saveGeometry()))
248
def getSettingsObject(self):
249
"""public method to return the QSettings instance for the dialog"""
250
return QtCore.QSettings(
257
def _getCurrentModelCellText(self):
258
"""protected method to return content (string) of the current index in
261
index = self.controlById(ID_TABLE).selectionModel().currentIndex()
269
def _enableSelectionButtons(self, flag):
270
"""protected slot to enable/disable buttons acting on the current selection"""
271
self.controlById(ID_BTCOPY).setEnabled(flag)
274
def _clipCopy(self, text):
275
"""protected method to copy a chunk of text to the clipboard"""
277
clipb = QtGui.QApplication.clipboard()
278
clipb.setText(text, clipb.Clipboard)
279
if clipb.supportsSelection():
280
clipb.setText(text, clipb.Selection)
283
def _onBtCopyClicked(self):
284
"""protected slot called when the Copy button is hit"""
285
self._clipCopy(self._getCurrentModelCellText())
288
def _onActivated(self, index):
289
"""protected slot to copy selected cell to clipboard (enter or doubleclick)"""
290
self._clipCopy(self._getCurrentModelCellText())
293
def _onOptionsDisplayed(self, optionsWidget, flag):
294
"""protected slot called when the options tab is displayed"""
295
method = flag and self.connect or self.disconnect
298
QtCore.SIGNAL('languageChanged(QString&)'),
303
def _onModelInserted(self, model):
304
"""protected slot called when the current model changes"""
306
# we are intersted here in selection changes
307
table = self.controlById(ID_TABLE)
308
selectionModel = table.selectionModel()
311
QtCore.SIGNAL('selectionChanged(const QItemSelection&, const QItemSelection&)'),
312
self._onSelectionChanged
314
self._enableSelectionButtons(False)
317
def _onSelectionChanged(self, selected, deselected):
318
"""protected slot called when the selection of the main table changes"""
320
has_selection = False
321
indexes = selected.indexes()
323
v = indexes[0].data()
327
self._enableSelectionButtons(has_selection)
330
def translate(self, localeName):
331
"""translates the user interface according to the specified localeName
333
@param localeName (string, QString) name of the locale like 'en' or 'de'
335
If no translation is found matching the specified localeName a default
336
the user interface is translated to a default language
338
if not self.options.flagsEx & self.options.CTFEX_NOTRANSLATE:
339
localeName = str(localeName)
340
self.options.localeName = localeName
341
self.options.language.installTranslator(self, localeName)
342
Ui_CT_DialogCharTables.retranslateUi(self, self)
343
self.controler.retranslate()
347
def controlById(self, idControl):
349
convenience method to return a contol given its id
351
@param idControl actually the name of the control as specified in designer
353
return getattr(self, idControl)
358
#***********************************************************************
360
#***********************************************************************
361
if __name__ == '__main__':
362
app = QtGui.QApplication(sys.argv)
363
dlg = DialogCharTables()