/
textEditView.py
508 lines (415 loc) · 17.9 KB
/
textEditView.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env python
# --!-- coding: utf8 --!--
import re
from PyQt5.QtCore import QTimer, QModelIndex, Qt, QEvent, pyqtSignal, QRegExp
from PyQt5.QtGui import QTextBlockFormat, QTextCharFormat, QFont, QColor, QIcon, QMouseEvent, QTextCursor
from PyQt5.QtWidgets import QTextEdit, qApp, QAction, QMenu
from manuskript import settings
from manuskript.enums import Outline
from manuskript.functions import AUC, themeIcon
from manuskript.functions import toString
from manuskript.models.outlineModel import outlineModel
from manuskript.ui.editors.MDFunctions import MDFormatSelection
from manuskript.ui.editors.MMDHighlighter import MMDHighlighter
from manuskript.ui.editors.basicHighlighter import basicHighlighter
from manuskript.ui.editors.textFormat import textFormat
try:
import enchant
except ImportError:
enchant = None
class textEditView(QTextEdit):
def __init__(self, parent=None, index=None, html=None, spellcheck=True, highlighting=False, dict="",
autoResize=False):
QTextEdit.__init__(self, parent)
self._column = Outline.text.value
self._index = None
self._indexes = None
self._model = None
self._placeholderText = self.placeholderText()
self._updating = False
self._item = None
self._highlighting = highlighting
self._textFormat = "text"
self.setAcceptRichText(False)
# When setting up a theme, this becomes true.
self._fromTheme = False
self.spellcheck = spellcheck
self.currentDict = dict if dict else settings.dict
self.highlighter = None
self.setAutoResize(autoResize)
self._defaultBlockFormat = QTextBlockFormat()
self._defaultCharFormat = QTextCharFormat()
self.highlightWord = ""
self.highligtCS = False
self.defaultFontPointSize = qApp.font().pointSize()
self._dict = None
# self.document().contentsChanged.connect(self.submit, AUC)
# Submit text changed only after 500ms without modifications
self.updateTimer = QTimer()
self.updateTimer.setInterval(500)
self.updateTimer.setSingleShot(True)
self.updateTimer.timeout.connect(self.submit)
# self.updateTimer.timeout.connect(lambda: print("Timeout"))
self.updateTimer.stop()
self.document().contentsChanged.connect(self.updateTimer.start, AUC)
# self.document().contentsChanged.connect(lambda: print("Document changed"))
# self.document().contentsChanged.connect(lambda: print(self.objectName(), "Contents changed"))
self.setEnabled(False)
if index:
self.setCurrentModelIndex(index)
elif html:
self.document().setHtml(html)
self.setReadOnly(True)
# Spellchecking
if enchant and self.spellcheck:
try:
self._dict = enchant.Dict(self.currentDict if self.currentDict else enchant.get_default_language())
except enchant.errors.DictNotFoundError:
self.spellcheck = False
else:
self.spellcheck = False
if self._highlighting and not self.highlighter:
self.highlighter = basicHighlighter(self)
self.highlighter.setDefaultBlockFormat(self._defaultBlockFormat)
def setModel(self, model):
self._model = model
try:
self._model.dataChanged.connect(self.update, AUC)
except TypeError:
pass
try:
self._model.rowsAboutToBeRemoved.connect(self.rowsAboutToBeRemoved, AUC)
except TypeError:
pass
def setColumn(self, col):
self._column = col
def setHighlighting(self, val):
self._highlighting = val
def setDefaultBlockFormat(self, bf):
self._defaultBlockFormat = bf
if self.highlighter:
self.highlighter.setDefaultBlockFormat(bf)
def setCurrentModelIndex(self, index):
self._indexes = None
if index.isValid():
self.setEnabled(True)
if index.column() != self._column:
index = index.sibling(index.row(), self._column)
self._index = index
self.setPlaceholderText(self._placeholderText)
if not self._model:
self.setModel(index.model())
self.setupEditorForIndex(self._index)
self.loadFontSettings()
self.updateText()
else:
self._index = QModelIndex()
self.setPlainText("")
self.setEnabled(False)
def setCurrentModelIndexes(self, indexes):
self._index = None
self._indexes = []
for i in indexes:
if i.isValid():
self.setEnabled(True)
if i.column() != self._column:
i = i.sibling(i.row(), self._column)
self._indexes.append(i)
if not self._model:
self.setModel(i.model())
self.updateText()
def setupEditorForIndex(self, index):
# In which model are we editing?
if type(index.model()) != outlineModel:
self._textFormat = "text"
return
# what type of text are we editing?
if self._column not in [Outline.text.value, Outline.notes.value]:
self._textFormat = "text"
else:
self._textFormat = "md"
# Setting highlighter
if self._highlighting:
item = index.internalPointer()
if self._column in [Outline.text.value, Outline.notes.value]:
self.highlighter = MMDHighlighter(self)
else:
self.highlighter = basicHighlighter(self)
self.highlighter.setDefaultBlockFormat(self._defaultBlockFormat)
def loadFontSettings(self):
if self._fromTheme or \
not self._index or \
type(self._index.model()) != outlineModel or \
self._column != Outline.text.value:
return
opt = settings.textEditor
f = QFont()
f.fromString(opt["font"])
# self.setFont(f)
self.setStyleSheet("""QTextEdit{{
background: {bg};
color: {foreground};
font-family: {ff};
font-size: {fs};
}}
""".format(
bg=opt["background"],
foreground=opt["fontColor"],
ff=f.family(),
fs="{}pt".format(str(f.pointSize()))))
cf = QTextCharFormat()
# cf.setFont(f)
# cf.setForeground(QColor(opt["fontColor"]))
self.setCursorWidth(opt["cursorWidth"])
bf = QTextBlockFormat()
bf.setLineHeight(opt["lineSpacing"], bf.ProportionalHeight)
bf.setTextIndent(opt["tabWidth"] * 1 if opt["indent"] else 0)
bf.setTopMargin(opt["spacingAbove"])
bf.setBottomMargin(opt["spacingBelow"])
bf.setAlignment(Qt.AlignLeft if opt["textAlignment"] == 0 else
Qt.AlignCenter if opt["textAlignment"] == 1 else
Qt.AlignRight if opt["textAlignment"] == 2 else
Qt.AlignJustify)
self._defaultCharFormat = cf
self._defaultBlockFormat = bf
if self.highlighter:
self.highlighter.setMisspelledColor(QColor(opt["misspelled"]))
self.highlighter.setDefaultCharFormat(self._defaultCharFormat)
self.highlighter.setDefaultBlockFormat(self._defaultBlockFormat)
def update(self, topLeft, bottomRight):
if self._updating:
return
elif self._index:
if topLeft.parent() != self._index.parent():
return
# print("Model changed: ({}:{}), ({}:{}/{}), ({}:{}) for {} of {}".format(
# topLeft.row(), topLeft.column(),
# self._index.row(), self._index.row(), self._column,
# bottomRight.row(), bottomRight.column(),
# self.objectName(), self.parent().objectName()))
if topLeft.row() <= self._index.row() <= bottomRight.row():
if topLeft.column() <= self._column <= bottomRight.column():
self.updateText()
elif self._indexes:
update = False
for i in self._indexes:
if topLeft.row() <= i.row() <= bottomRight.row():
update = True
if update:
self.updateText()
def rowsAboutToBeRemoved(self, parent, first, last):
if self._index:
if self._index.parent() == parent and \
first <= self._index.row() <= last:
self._index = None
self.setEnabled(False)
# FIXME: self._indexes
def disconnectDocument(self):
try:
self.document().contentsChanged.disconnect(self.updateTimer.start)
except:
pass
def reconnectDocument(self):
self.document().contentsChanged.connect(self.updateTimer.start, AUC)
def updateText(self):
if self._updating:
return
# print("Updating", self.objectName())
self._updating = True
if self._index:
self.disconnectDocument()
if self.toPlainText() != toString(self._model.data(self._index)):
# print(" Updating plaintext")
self.document().setPlainText(toString(self._model.data(self._index)))
self.reconnectDocument()
elif self._indexes:
self.disconnectDocument()
t = []
same = True
for i in self._indexes:
item = i.internalPointer()
t.append(toString(item.data(self._column)))
for t2 in t[1:]:
if t2 != t[0]:
same = False
break
if same:
self.document().setPlainText(t[0])
else:
self.document().setPlainText("")
if not self._placeholderText:
self._placeholderText = self.placeholderText()
self.setPlaceholderText(self.tr("Various"))
self.reconnectDocument()
self._updating = False
def submit(self):
self.updateTimer.stop()
if self._updating:
return
# print("Submitting", self.objectName())
if self._index and self._index.isValid():
# item = self._index.internalPointer()
if self.toPlainText() != self._model.data(self._index):
# print(" Submitting plain text")
self._updating = True
self._model.setData(self._index, self.toPlainText())
self._updating = False
elif self._indexes:
self._updating = True
for i in self._indexes:
item = i.internalPointer()
if self.toPlainText() != toString(item.data(self._column)):
print("Submitting many indexes")
self._model.setData(i, self.toPlainText())
self._updating = False
def keyPressEvent(self, event):
QTextEdit.keyPressEvent(self, event)
if event.key() == Qt.Key_Space:
self.submit()
# -----------------------------------------------------------------------------------------------------
# Resize stuff
def resizeEvent(self, e):
QTextEdit.resizeEvent(self, e)
if self._autoResize:
self.sizeChange()
def sizeChange(self):
docHeight = self.document().size().height()
if self.heightMin <= docHeight <= self.heightMax:
self.setMinimumHeight(docHeight)
def setAutoResize(self, val):
self._autoResize = val
if self._autoResize:
self.document().contentsChanged.connect(self.sizeChange)
self.heightMin = 0
self.heightMax = 65000
self.sizeChange()
###############################################################################
# SPELLCHECKING
###############################################################################
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
def setDict(self, d):
self.currentDict = d
self._dict = enchant.Dict(d)
if self.highlighter:
self.highlighter.rehighlight()
def toggleSpellcheck(self, v):
self.spellcheck = v
if enchant and self.spellcheck and not self._dict:
if self.currentDict:
self._dict = enchant.Dict(self.currentDict)
elif enchant.dict_exists(enchant.get_default_language()):
self._dict = enchant.Dict(enchant.get_default_language())
else:
self.spellcheck = False
if self.highlighter:
self.highlighter.rehighlight()
else:
self.spellcheck = False
def mousePressEvent(self, event):
if event.button() == Qt.RightButton:
# Rewrite the mouse event to a left button event so the cursor is
# moved to the location of the pointer.
event = QMouseEvent(QEvent.MouseButtonPress, event.pos(),
Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
QTextEdit.mousePressEvent(self, event)
class SpellAction(QAction):
"""A special QAction that returns the text in a signal. Used for spellckech."""
correct = pyqtSignal(str)
def __init__(self, *args):
QAction.__init__(self, *args)
self.triggered.connect(lambda x: self.correct.emit(
str(self.text())))
def contextMenuEvent(self, event):
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
popup_menu = self.createStandardContextMenu()
popup_menu.exec_(event.globalPos())
def createStandardContextMenu(self):
popup_menu = QTextEdit.createStandardContextMenu(self)
if not self.spellcheck:
return popup_menu
# Select the word under the cursor.
# But only if there is no selection (otherwise it's impossible to select more text to copy/cut)
cursor = self.textCursor()
if not cursor.hasSelection():
cursor.select(QTextCursor.WordUnderCursor)
self.setTextCursor(cursor)
# Check if the selected word is misspelled and offer spelling
# suggestions if it is.
if cursor.hasSelection():
text = str(cursor.selectedText())
valid = self._dict.check(text)
selectedWord = cursor.selectedText()
if not valid:
spell_menu = QMenu(self.tr('Spelling Suggestions'), self)
spell_menu.setIcon(themeIcon("spelling"))
for word in self._dict.suggest(text):
action = self.SpellAction(word, spell_menu)
action.correct.connect(self.correctWord)
spell_menu.addAction(action)
# Only add the spelling suggests to the menu if there are
# suggestions.
if len(spell_menu.actions()) != 0:
popup_menu.insertSeparator(popup_menu.actions()[0])
# Adds: add to dictionary
addAction = QAction(self.tr("&Add to dictionary"), popup_menu)
addAction.setIcon(QIcon.fromTheme("list-add"))
addAction.triggered.connect(self.addWordToDict)
addAction.setData(selectedWord)
popup_menu.insertAction(popup_menu.actions()[0], addAction)
# Adds: suggestions
popup_menu.insertMenu(popup_menu.actions()[0], spell_menu)
# popup_menu.insertSeparator(popup_menu.actions()[0])
# If word was added to custom dict, give the possibility to remove it
elif valid and self._dict.is_added(selectedWord):
popup_menu.insertSeparator(popup_menu.actions()[0])
# Adds: remove from dictionary
rmAction = QAction(self.tr("&Remove from custom dictionary"), popup_menu)
rmAction.setIcon(QIcon.fromTheme("list-remove"))
rmAction.triggered.connect(self.rmWordFromDict)
rmAction.setData(selectedWord)
popup_menu.insertAction(popup_menu.actions()[0], rmAction)
return popup_menu
def correctWord(self, word):
"""
Replaces the selected text with word.
"""
cursor = self.textCursor()
cursor.beginEditBlock()
cursor.removeSelectedText()
cursor.insertText(word)
cursor.endEditBlock()
def addWordToDict(self):
word = self.sender().data()
self._dict.add(word)
self.highlighter.rehighlight()
def rmWordFromDict(self):
word = self.sender().data()
self._dict.remove(word)
self.highlighter.rehighlight()
###############################################################################
# FORMATTING
###############################################################################
def focusOutEvent(self, event):
"""Submit changes just before focusing out."""
QTextEdit.focusOutEvent(self, event)
self.submit()
def focusInEvent(self, event):
"""Finds textFormatter and attach them to that view."""
QTextEdit.focusInEvent(self, event)
p = self.parent()
while p.parent():
p = p.parent()
if self._index:
for tF in p.findChildren(textFormat, QRegExp(".*"), Qt.FindChildrenRecursively):
tF.updateFromIndex(self._index)
tF.setTextEdit(self)
def applyFormat(self, _format):
if self._textFormat == "md":
if _format == "Bold":
MDFormatSelection(self, 0)
elif _format == "Italic":
MDFormatSelection(self, 1)
elif _format == "Code":
MDFormatSelection(self, 2)
elif _format == "Clear":
MDFormatSelection(self)