forked from tallforasmurf/PPQT2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charview.py
366 lines (335 loc) · 14.4 KB
/
charview.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
__license__ = '''
License (GPL-3.0) :
This file is part of PPQT Version 2.
PPQT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You can find a copy of the GNU General Public License in the file
extras/COPYING.TXT included in the distribution of this program, or see:
<http://www.gnu.org/licenses/>.
'''
__version__ = "2.0.0"
__author__ = "David Cortesi"
__copyright__ = "Copyright 2013, 2014 David Cortesi"
__maintainer__ = "David Cortesi"
__email__ = "tallforasmurf@yahoo.com"
'''
charview.py
Define classes to implement the Chars panel.
CharModel(QAbstractTableModel) interfaces to chardata.py to get the actual
data, and also supplies such user-visible items as column headers and
tooltips, which need to be translated and localized. It calls on the Python
standard module unicodedata for character class names and character names.
CharSortFilter(QSortFilterProxyModel) implements sort/filter proxy
based on a test function set by the parent.
CharView(QWidget) implements a panel consisting of a top row containing
a Refresh button the left, and a filter combobox on the right. Below and
filling the rest of the widget is a 6-column table headed (in English)
* Glyph, the character symbol
* Value, the character value in hex
* Count, the number times it appears in the document
* Entity, the HTML named or numeric entity value
* Category, unicode category in words.
* Name, official unicode name for that character
'''
import constants as C
import unicodedata
from PyQt5.QtCore import (
Qt,
QAbstractTableModel,
QCoreApplication,
QSortFilterProxyModel
)
_TR = QCoreApplication.translate
from PyQt5.QtWidgets import (
QWidget,
QComboBox,
QHBoxLayout,
QVBoxLayout,
QPushButton,
QTableView
)
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Dictionary to translate Unicode category abbreviations such as "Sc"
# returned by unicodedata.category() to phrases such as "Symbol, currency".
# The key is lowercased because we don't trust unicodedata to be 100%
# consistent with the unicode standard names.
#
# Not making any attempt to translate these, as unicode.org doesn't.
UC_CAT_EXPAND = {
'cc' : 'Other, Control',
'cf' : 'Other, Format',
'cn' : 'Other, Not Assigned',
'co' : 'Other, Private Use',
'cs' : 'Other, Surrogate',
'lc' : 'Letter, Cased',
'll' : 'Letter, Lowercase',
'lm' : 'Letter, Modifier',
'lo' : 'Letter, Other',
'lt' : 'Letter, Titlecase',
'lu' : 'Letter, Uppercase',
'mc' : 'Mark, Spacing Combining',
'me' : 'Mark, Enclosing',
'mn' : 'Mark, Nonspacing',
'nd' : 'Number, Decimal Digit',
'nl' : 'Number, Letter',
'no' : 'Number, Other',
'pc' : 'Punctuation, Connector',
'pd' : 'Punctuation, Dash',
'pe' : 'Punctuation, Close',
'pf' : 'Punctuation, Final quote ',
'pi' : 'Punctuation, Initial quote ',
'po' : 'Puntuation, Other',
'ps' : 'Punctuation, Open',
'sc' : 'Symbol, Currency',
'sk' : 'Symbol, Modifier',
'sm' : 'Symbol, Math',
'so' : 'Symbol, Other',
'zl' : 'Separator, Line',
'zp' : 'Separator, Paragraph',
'zs' : 'Separator, Space'
}
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Dictionaries of translated column header strings, column tool-tip strings,
# and column text alignment. Key is the column number 0..5.
COL_HEADERS = {
0 : _TR('character table column head',
'Symbol'),
1 : _TR('character table column head',
'Value'),
2 : _TR('character table column head',
'Count'),
3 : _TR('character table column head',
'Entity'),
4 : _TR('character table column head',
'Unicode category'),
5 : _TR('character table column head',
'Unicode name')
}
COL_TOOLTIPS = {
0: _TR('char. table column tooltip',
'Character symbol'),
1: _TR('char. table column tooltip',
'Numeric value in hexadecimal'),
2: _TR('char. table column tooltip',
'Number in the document'),
3: _TR('char. table column tooltip',
'HTML/XML Entity code'),
4: _TR('char. table column tooltip',
'Unicode category'),
5: _TR('char. table column tooltip',
'Official Unicode character name')
}
COL_ALIGNMENT = {
0: Qt.AlignHCenter,
1: Qt.AlignRight,
2: Qt.AlignRight,
3: Qt.AlignLeft,
4:Qt.AlignLeft,
5:Qt.AlignLeft
}
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Define the table model, implementing those methods required to make an
# abstract table, concrete:
#
# flags : the base class returns Qt.itemIsSelectable|Qt.itemIsEnabled.
# columnCount : return the number of columns (6, but not hard-coded)
# rowCount : return number of chars in the database
# headerData : return the column header name or tooltip string.
# data : return the actual data, or various helpful info about a column.
class CharModel(QAbstractTableModel):
def __init__(self, chardata, parent):
super().__init__(parent)
self.chardata = chardata # save access to CharData object
def columnCount(self,index):
global COL_HEADERS
if index.isValid() : return 0 # we don't have a tree here
return len(COL_HEADERS)
def rowCount(self,index):
if index.isValid() : return 0 # we don't have a tree here
return self.chardata.char_count()
def headerData(self, col, axis, role):
global COL_HEADERS, COL_TOOLTIPS, COL_ALIGNMENT
if (axis == Qt.Horizontal) and (col >= 0):
if role == Qt.DisplayRole : # request for actual text
return COL_HEADERS[col]
if (role == Qt.ToolTipRole) or (role == Qt.StatusTipRole) :
return COL_TOOLTIPS[col]
if (role == Qt.TextAlignmentRole) :
return COL_ALIGNMENT[col]
return None # whatever you said, we don't have it
def data(self, index, role ):
global UC_CAT_EXPAND, COL_ALIGNMENT, COL_TOOLTIPS
(char, count) = self.chardata.get_tuple(index.row())
if role == Qt.DisplayRole : # request for actual data
if 0 == index.column():
return char
elif 1 == index.column():
return '0x{0:04x}'.format(ord(char))
elif 2 == index.column():
return count
elif 3 == index.column():
if char in C.NAMED_ENTITIES :
return '&' + C.NAMED_ENTITIES[char] + ';'
else:
return '&#{0:d};'.format(ord(char))
elif 4 == index.column():
return UC_CAT_EXPAND[unicodedata.category(char).lower()]
else: # assuming column is 5, unicode name
return unicodedata.name(char,'no name?').title()
elif (role == Qt.TextAlignmentRole) :
return COL_ALIGNMENT[index.column()]
elif (role == Qt.ToolTipRole) or (role == Qt.StatusTipRole) :
if index.column() < 5 :
return COL_TOOLTIPS[index.column()]
# For column 5, the tooltip is the name string, because a narrow
# column may not expose the entire name any other way.
return unicodedata.name(char,'no name?').title()
# Sorry, we don't support other roles
return None
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Define the sort/filter proxy, used by the table view to sort and filter
# the rows to be displayed. We don't re-implement the sort() method, that
# just goes to the default which sorts columns on their character values
# as returned by the data() method above.
#
# We do implement filterAcceptsRow(). This tests the given row's character
# value in a Lambda expression selected by the user. The lambdas are defined
# in this class, and the current lambda is selected upon an activated(row)
# signal from the filter combobox.
class CharFilter(QSortFilterProxyModel):
def __init__(self, chardata, parent):
super().__init__(parent)
self.chardata = chardata # save pointer to the chardata object
self.lambda_all = lambda char : True
self.lambda_not_ascii = lambda char : (ord(char) > 126) or (ord(char) < 32)
self.lambda_not_latin = lambda char : (ord(char) > 255) or (not (ord(char) & 0x60))
self.test = self.lambda_all
# To implement filterAcceptsRow we take the row number and go to the
# database to get that character. We pass the character into one of the
# above lambda expressions and return that result, True for accept and
# False for reject.
def filterAcceptsRow(self, row, parent_index):
return self.test(self.chardata.get_char(row))
# The parent calls this slot to set the test lambda when the user chooses
# one. It is up to the parent widget to cause a redisplay of the table.
def set_filter(self, row):
if row == 1 : self.test = self.lambda_not_ascii
elif row == 2 : self.test = self.lambda_not_latin
else : self.test = self.lambda_all
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Define the CharView object that is displayed in the Chars panel.
#
class CharView(QWidget):
def __init__(self, my_book):
super().__init__(None) # no parent, the tabset will parent us later
# save access to the book and other important objects.
self.my_book = my_book
self.chardata = my_book.get_char_model()
self.findpanel = my_book.get_find_panel()
# Instantiate our layout and subwidgets. This creates:
# self.view, QTableView
# self.refresh, QPushButton
# self.popup, QComboBox
self._uic()
# Set up the table model/view. Pass to the model a pointer
# to the view so it can query the row under the mouse.
self.model = CharModel(self.chardata,self)
#Interpose a sort filter proxy between the view and the model.
self.proxy = CharFilter(self.chardata,self)
self.proxy.setSourceModel(self.model)
self.view.setModel(self.proxy)
# Hook up some signals.
# Connect the double-click signal to find_this.
self.view.doubleClicked.connect(self.find_this)
# Connect the CharsLoaded from the chardata object to our slot.
self.chardata.CharsLoaded.connect(self.chars_loaded)
# Connect the popup activated signal to our slot.
self.popup.activated.connect(self.new_filter)
# Connect the refresh button clicked signal to refresh below
self.refresh.clicked.connect(self.do_refresh)
# Connect the modelReset signal to our slot.
self.model.modelReset.connect(self.set_up_view)
# Slot to receive doubleClicked(index) from the table view, and
# convert that into a Find for that character.
def find_this(self, index):
repl = None
if index.column() == 3 :
# doubleclick was in the HTML entity column. Put the entity
# string from column 3 in the replace-1 field
repl = index.data(Qt.DisplayRole)
if index.column() != 0 :
# dblclick on some column other than 0. We need a reference to
# column 0, and we get it from the index.
index = index.sibling(index.row(),0)
what = index.data(Qt.DisplayRole) # get the character as a string
# Call for a find with respect case on, whole word and regex off
self.findpanel.find_this(what,case=True,word=False,regex=False,repl=repl)
# Slot to receive the CharsLoaded() signal from the chardata module
# (metadata has been loaded). Reset the table model.
def chars_loaded(self):
self.model.beginResetModel()
self.model.endResetModel()
# Slot to receive the activated(row) signal from the filter popup. Set
# the filter and reset the table model.
def new_filter(self,row):
self.model.beginResetModel()
self.proxy.set_filter(row)
self.model.endResetModel()
# Slot to receive the clicked() signal from the Refresh button.
# Warn the table model that things be changing, then call the
# database to do a new census, then finish the table reset.
def do_refresh(self):
self.model.beginResetModel()
self.chardata.refresh()
self.model.endResetModel()
# Slot to receive the modelReset() signal from the table model, emitted
# after the endResetModel() call. Set some features of our table view
# that we can't set until some data has been loaded.
def set_up_view(self):
self.view.resizeColumnsToContents()
self.view.horizontalHeader().setStretchLastSection(True)
self.view.resizeRowsToContents()
self.view.setSortingEnabled(True)
# Do all the fiddly UI stuff out of line.
def _uic(self):
mainLayout = QVBoxLayout()
self.setLayout(mainLayout)
topLayout = QHBoxLayout()
topLayout.setContentsMargins(0,0,0,0)
mainLayout.addLayout(topLayout,0)
# Lay out the refresh button and filter popup
self.refresh = QPushButton(
_TR('Button to reload all data in char panel',
'Refresh')
)
topLayout.addWidget(self.refresh,0)
topLayout.addStretch(1) # push filters to the right
self.popup = QComboBox()
# Set choices in popup, must match to the lambdas
# defined in CharFilter.set_filter()
self.popup.addItem(
_TR('char panel: show all characters',
'All')
)
self.popup.addItem(
_TR('char panel: show non-ascii characters',
'not 7-bit')
)
self.popup.addItem(
_TR('char panel: show non-latin-1',
'not Latin-1')
)
topLayout.addWidget(self.popup,0)
# Set up the table view, the actual visible table
self.view = QTableView()
self.view.setCornerButtonEnabled(False)
self.view.setWordWrap(False)
self.view.setAlternatingRowColors(True)
mainLayout.addWidget(self.view,1) # give it all the stretch
# end of _uic()