2015-06-07 02:21:08 +12:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#--!-- coding: utf8 --!--
|
|
|
|
|
|
|
|
from qt import *
|
|
|
|
from enums import *
|
|
|
|
from ui.editors.t2tHighlighter import *
|
2015-06-25 23:41:55 +12:00
|
|
|
from ui.editors.t2tFunctions import *
|
2015-06-25 01:43:35 +12:00
|
|
|
from ui.editors.basicHighlighter import *
|
2015-06-25 04:39:38 +12:00
|
|
|
from ui.editors.textFormat import *
|
2015-06-25 01:43:35 +12:00
|
|
|
from models.outlineModel import *
|
2015-06-15 22:18:42 +12:00
|
|
|
from functions import *
|
2015-06-26 03:06:07 +12:00
|
|
|
import settings
|
2015-06-25 23:41:55 +12:00
|
|
|
import re
|
2015-06-15 22:18:42 +12:00
|
|
|
|
2015-06-07 02:21:08 +12:00
|
|
|
try:
|
|
|
|
import enchant
|
|
|
|
except ImportError:
|
|
|
|
enchant = None
|
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
class textEditView(QTextEdit):
|
2015-06-07 02:21:08 +12:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def __init__(self, parent=None, index=None, html=None, spellcheck=True, highlighting=False, dict="", autoResize=False):
|
2015-06-07 02:21:08 +12:00
|
|
|
QTextEdit.__init__(self, parent)
|
2015-06-07 06:29:02 +12:00
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
self._column = Outline.text.value
|
|
|
|
self._index = None
|
|
|
|
self._indexes = None
|
2015-06-27 02:00:59 +12:00
|
|
|
self._placeholderText = self.placeholderText()
|
2015-06-15 22:18:42 +12:00
|
|
|
self._updating = False
|
|
|
|
self._item = None
|
2015-06-25 01:43:35 +12:00
|
|
|
self._highlighting = highlighting
|
2015-06-25 05:53:51 +12:00
|
|
|
self._textFormat = "text"
|
2015-06-25 01:43:35 +12:00
|
|
|
self.setAcceptRichText(False)
|
2015-06-26 03:06:07 +12:00
|
|
|
# When setting up a theme, this becomes true.
|
|
|
|
self._fromTheme = False
|
2015-06-15 22:18:42 +12:00
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
self.spellcheck = spellcheck
|
2015-06-30 00:21:57 +12:00
|
|
|
self.currentDict = dict if dict else settings.dict
|
2015-06-08 12:07:08 +12:00
|
|
|
self.highlighter = None
|
2015-06-15 22:18:42 +12:00
|
|
|
self._autoResize = autoResize
|
2015-06-20 04:47:45 +12:00
|
|
|
self._defaultBlockFormat = QTextBlockFormat()
|
2015-06-26 03:06:07 +12:00
|
|
|
self._defaultCharFormat = QTextCharFormat()
|
2015-06-20 04:47:45 +12:00
|
|
|
self.highlightWord = ""
|
|
|
|
self.highligtCS = False
|
|
|
|
self.defaultFontPointSize = qApp.font().pointSize()
|
|
|
|
self._dict = None
|
2015-06-25 04:39:38 +12:00
|
|
|
#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)
|
2015-06-25 10:05:01 +12:00
|
|
|
#self.updateTimer.timeout.connect(lambda: print("Timeout"))
|
|
|
|
|
2015-06-25 05:53:51 +12:00
|
|
|
self.updateTimer.stop()
|
2015-06-25 04:39:38 +12:00
|
|
|
self.document().contentsChanged.connect(self.updateTimer.start, AUC)
|
2015-06-25 10:05:01 +12:00
|
|
|
#self.document().contentsChanged.connect(lambda: print("Document changed"))
|
|
|
|
|
2015-06-25 05:53:51 +12:00
|
|
|
#self.document().contentsChanged.connect(lambda: print(self.objectName(), "Contents changed"))
|
2015-06-07 05:10:44 +12:00
|
|
|
|
|
|
|
if index:
|
|
|
|
self.setCurrentModelIndex(index)
|
|
|
|
|
|
|
|
elif html:
|
|
|
|
self.document().setHtml(html)
|
|
|
|
self.setReadOnly(True)
|
2015-06-15 22:18:42 +12:00
|
|
|
|
|
|
|
self.setAutoResize(self._autoResize)
|
2015-06-20 04:47:45 +12:00
|
|
|
|
|
|
|
# Spellchecking
|
|
|
|
if enchant and self.spellcheck:
|
|
|
|
self._dict = enchant.Dict(self.currentDict if self.currentDict else enchant.get_default_language())
|
|
|
|
else:
|
|
|
|
self.spellcheck = False
|
|
|
|
|
2015-06-30 00:21:57 +12:00
|
|
|
if self._highlighting and not self.highlighter:
|
|
|
|
self.highlighter = t2tHighlighter(self)
|
|
|
|
self.highlighter.setDefaultBlockFormat(self._defaultBlockFormat)
|
2015-06-15 22:18:42 +12:00
|
|
|
|
|
|
|
def setModel(self, model):
|
|
|
|
self._model = model
|
2015-06-25 10:05:01 +12:00
|
|
|
#self._model.dataChanged.connect(self.update, AUC)
|
2015-06-07 08:39:43 +12:00
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
def setColumn(self, col):
|
|
|
|
self._column = col
|
|
|
|
|
|
|
|
def setHighlighting(self, val):
|
|
|
|
self._highlighting = val
|
2015-06-07 05:10:44 +12:00
|
|
|
|
2015-06-20 04:47:45 +12:00
|
|
|
def setDefaultBlockFormat(self, bf):
|
|
|
|
self._defaultBlockFormat = bf
|
|
|
|
if self.highlighter:
|
|
|
|
self.highlighter.setDefaultBlockFormat(bf)
|
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
def setCurrentModelIndex(self, index):
|
2015-06-15 22:18:42 +12:00
|
|
|
self._indexes = None
|
2015-06-07 05:10:44 +12:00
|
|
|
if index.isValid():
|
2015-06-15 22:18:42 +12:00
|
|
|
if index.column() != self._column:
|
|
|
|
index = index.sibling(index.row(), self._column)
|
|
|
|
self._index = index
|
2015-06-27 02:00:59 +12:00
|
|
|
|
|
|
|
self.setPlaceholderText(self._placeholderText)
|
2015-06-15 22:18:42 +12:00
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
self._model = index.model()
|
2015-06-25 01:43:35 +12:00
|
|
|
try:
|
|
|
|
self._model.dataChanged.connect(self.update, AUC)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2015-06-25 05:53:51 +12:00
|
|
|
self.setupEditorForIndex(self._index)
|
2015-06-26 03:06:07 +12:00
|
|
|
self.loadFontSettings()
|
2015-06-25 01:43:35 +12:00
|
|
|
#self.document().contentsChanged.connect(self.submit, AUC)
|
2015-06-07 05:10:44 +12:00
|
|
|
self.updateText()
|
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
|
2015-06-22 23:11:45 +12:00
|
|
|
else:
|
|
|
|
self._index = QModelIndex()
|
|
|
|
try:
|
|
|
|
self.document().contentsChanged.disconnect(self.submit)
|
2015-06-25 01:43:35 +12:00
|
|
|
#self._model.dataChanged.disconnect(self.update)
|
2015-06-22 23:11:45 +12:00
|
|
|
except:
|
|
|
|
pass
|
2015-06-25 04:39:38 +12:00
|
|
|
|
2015-06-22 23:11:45 +12:00
|
|
|
self.setPlainText("")
|
2015-06-07 05:10:44 +12:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def setupEditorForIndex(self, index):
|
2015-06-25 05:53:51 +12:00
|
|
|
|
|
|
|
# what type of text are we editing?
|
|
|
|
if type(index.model()) != outlineModel:
|
|
|
|
self._textFormat = "text"
|
|
|
|
return
|
|
|
|
|
2015-06-30 00:33:30 +12:00
|
|
|
if self._column != Outline.text.value:
|
2015-06-25 05:53:51 +12:00
|
|
|
self._textFormat = "text"
|
2015-06-30 00:33:30 +12:00
|
|
|
|
|
|
|
else:
|
|
|
|
item = index.internalPointer()
|
|
|
|
if item.isHTML():
|
|
|
|
self._textFormat = "html"
|
|
|
|
elif item.isT2T():
|
|
|
|
self._textFormat = "t2t"
|
|
|
|
else:
|
|
|
|
self._textFormat = "text"
|
2015-06-25 05:53:51 +12:00
|
|
|
|
2015-06-25 20:50:30 +12:00
|
|
|
# Accept richtext maybe
|
|
|
|
if self._textFormat == "html":
|
|
|
|
self.setAcceptRichText(True)
|
|
|
|
else:
|
|
|
|
self.setAcceptRichText(False)
|
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
# Setting highlighter
|
|
|
|
if self._highlighting:
|
|
|
|
item = index.internalPointer()
|
|
|
|
if self._column == Outline.text.value and not item.isT2T():
|
|
|
|
self.highlighter = basicHighlighter(self)
|
|
|
|
else:
|
|
|
|
self.highlighter = t2tHighlighter(self)
|
|
|
|
|
|
|
|
self.highlighter.setDefaultBlockFormat(self._defaultBlockFormat)
|
|
|
|
|
2015-06-26 03:06:07 +12:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
cf = QTextCharFormat()
|
|
|
|
cf.setFont(f)
|
|
|
|
cf.setForeground(QColor(opt["fontColor"]))
|
|
|
|
|
|
|
|
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"])
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
def setCurrentModelIndexes(self, indexes):
|
|
|
|
self._index = None
|
|
|
|
self._indexes = []
|
|
|
|
|
|
|
|
for i in indexes:
|
|
|
|
if i.isValid():
|
|
|
|
if i.column() != self._column:
|
|
|
|
i = i.sibling(i.row(), self._column)
|
|
|
|
self._indexes.append(i)
|
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
#self.document().contentsChanged.connect(self.submit)
|
2015-06-15 22:18:42 +12:00
|
|
|
self.updateText()
|
2015-06-07 02:21:08 +12:00
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
def update(self, topLeft, bottomRight):
|
2015-06-15 22:38:38 +12:00
|
|
|
if self._updating:
|
2015-06-15 22:18:42 +12:00
|
|
|
return
|
|
|
|
|
|
|
|
elif self._index:
|
2015-06-25 10:05:01 +12:00
|
|
|
|
|
|
|
if topLeft.parent() != self._index.parent():
|
|
|
|
return
|
|
|
|
|
2015-06-25 20:50:30 +12:00
|
|
|
#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()))
|
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
if topLeft.row() <= self._index.row() <= bottomRight.row():
|
2015-06-25 20:50:30 +12:00
|
|
|
if self._column == Outline.text.value and \
|
|
|
|
topLeft.column() <= Outline.type.value <= bottomRight.column():
|
|
|
|
# If item type change, and we display the main text,
|
|
|
|
# we reset the index to set the proper
|
2015-06-25 01:43:35 +12:00
|
|
|
# highlighter and other defaults
|
|
|
|
self.setupEditorForIndex(self._index)
|
2015-06-25 04:39:38 +12:00
|
|
|
self.updateText()
|
2015-06-25 01:43:35 +12:00
|
|
|
|
2015-06-25 04:39:38 +12:00
|
|
|
elif topLeft.column() <= self._column <= bottomRight.column():
|
|
|
|
self.updateText()
|
2015-06-15 22:18:42 +12:00
|
|
|
|
|
|
|
elif self._indexes:
|
|
|
|
update = False
|
|
|
|
for i in self._indexes:
|
|
|
|
if topLeft.row() <= i.row() <= bottomRight.row():
|
|
|
|
update = True
|
|
|
|
if update:
|
|
|
|
self.updateText()
|
2015-06-07 02:21:08 +12:00
|
|
|
|
2015-06-25 05:53:51 +12:00
|
|
|
def disconnectDocument(self):
|
|
|
|
try:
|
|
|
|
self.document().contentsChanged.disconnect(self.updateTimer.start)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def reconnectDocument(self):
|
|
|
|
self.document().contentsChanged.connect(self.updateTimer.start, AUC)
|
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
def updateText(self):
|
2015-06-25 01:43:35 +12:00
|
|
|
if self._updating:
|
|
|
|
return
|
2015-06-25 10:05:01 +12:00
|
|
|
#print("Updating", self.objectName())
|
2015-06-15 22:38:38 +12:00
|
|
|
self._updating = True
|
2015-06-15 22:18:42 +12:00
|
|
|
if self._index:
|
2015-06-25 05:53:51 +12:00
|
|
|
self.disconnectDocument()
|
|
|
|
if self._textFormat == "html":
|
2015-06-25 01:43:35 +12:00
|
|
|
if self.toHtml() != toString(self._model.data(self._index)):
|
2015-06-25 10:05:01 +12:00
|
|
|
#print(" Updating html")
|
2015-06-25 23:41:55 +12:00
|
|
|
html = self._model.data(self._index)
|
|
|
|
self.document().setHtml(toString(html))
|
2015-06-25 01:43:35 +12:00
|
|
|
else:
|
|
|
|
if self.toPlainText() != toString(self._model.data(self._index)):
|
2015-06-25 10:05:01 +12:00
|
|
|
#print(" Updating plaintext")
|
2015-06-25 01:43:35 +12:00
|
|
|
self.document().setPlainText(toString(self._model.data(self._index)))
|
2015-06-25 05:53:51 +12:00
|
|
|
self.reconnectDocument()
|
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
elif self._indexes:
|
2015-06-27 02:00:59 +12:00
|
|
|
self.disconnectDocument()
|
2015-06-15 22:18:42 +12:00
|
|
|
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:
|
2015-06-25 01:43:35 +12:00
|
|
|
# Assuming that we don't use HTML with multiple items
|
2015-06-15 22:18:42 +12:00
|
|
|
self.document().setPlainText(t[0])
|
|
|
|
else:
|
|
|
|
self.document().setPlainText("")
|
|
|
|
|
|
|
|
if not self._placeholderText:
|
|
|
|
self._placeholderText = self.placeholderText()
|
|
|
|
|
|
|
|
self.setPlaceholderText(self.tr("Various"))
|
2015-06-27 02:00:59 +12:00
|
|
|
self.reconnectDocument()
|
2015-06-15 22:38:38 +12:00
|
|
|
self._updating = False
|
2015-06-07 05:10:44 +12:00
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
def submit(self):
|
2015-06-15 22:38:38 +12:00
|
|
|
if self._updating:
|
|
|
|
return
|
2015-06-25 10:05:01 +12:00
|
|
|
#print("Submitting", self.objectName())
|
2015-06-15 22:18:42 +12:00
|
|
|
if self._index:
|
2015-06-25 01:43:35 +12:00
|
|
|
item = self._index.internalPointer()
|
2015-06-25 05:53:51 +12:00
|
|
|
if self._textFormat == "html":
|
2015-06-25 01:43:35 +12:00
|
|
|
if self.toHtml() != self._model.data(self._index):
|
2015-06-25 10:05:01 +12:00
|
|
|
#print(" Submitting html")
|
2015-06-25 01:43:35 +12:00
|
|
|
self._updating = True
|
2015-06-25 23:41:55 +12:00
|
|
|
html = self.toHtml()
|
|
|
|
# We don't store font settings
|
|
|
|
html = re.sub(r"font-family:.*?;", "", html)
|
|
|
|
html = re.sub(r"font-size:.*?;", "", html)
|
|
|
|
self._model.setData(self._index, html)
|
2015-06-25 01:43:35 +12:00
|
|
|
self._updating = False
|
|
|
|
else:
|
|
|
|
if self.toPlainText() != self._model.data(self._index):
|
2015-06-25 10:05:01 +12:00
|
|
|
#print(" Submitting plain text")
|
2015-06-25 01:43:35 +12:00
|
|
|
self._updating = True
|
|
|
|
self._model.setData(self._index, self.toPlainText())
|
|
|
|
self._updating = False
|
2015-06-15 22:18:42 +12:00
|
|
|
|
|
|
|
elif self._indexes:
|
2015-06-27 02:00:59 +12:00
|
|
|
print("Submitting many indexes")
|
2015-06-15 22:18:42 +12:00
|
|
|
self._updating = True
|
|
|
|
for i in self._indexes:
|
|
|
|
item = i.internalPointer()
|
|
|
|
if self.toPlainText() != toString(item.data(self._column)):
|
|
|
|
self._model.setData(i, self.toPlainText())
|
|
|
|
self._updating = False
|
|
|
|
|
2015-06-28 00:11:26 +12:00
|
|
|
def keyPressEvent(self, event):
|
|
|
|
if event.key() == Qt.Key_Space:
|
|
|
|
self.submit()
|
|
|
|
QTextEdit.keyPressEvent(self, event)
|
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
# -----------------------------------------------------------------------------------------------------
|
|
|
|
# Resize stuff
|
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
def resizeEvent(self, e):
|
|
|
|
QTextEdit.resizeEvent(self, e)
|
2015-06-15 22:18:42 +12:00
|
|
|
if self._autoResize:
|
2015-06-07 06:29:02 +12:00
|
|
|
self.sizeChange()
|
2015-06-07 05:10:44 +12:00
|
|
|
|
|
|
|
def sizeChange(self):
|
|
|
|
docHeight = self.document().size().height()
|
|
|
|
if self.heightMin <= docHeight <= self.heightMax:
|
|
|
|
self.setMinimumHeight(docHeight)
|
|
|
|
|
2015-06-15 22:18:42 +12:00
|
|
|
def setAutoResize(self, val):
|
|
|
|
self._autoResize = val
|
|
|
|
if self._autoResize:
|
|
|
|
self.document().contentsChanged.connect(self.sizeChange)
|
|
|
|
self.heightMin = 0
|
|
|
|
self.heightMax = 65000
|
|
|
|
self.sizeChange()
|
2015-06-07 05:10:44 +12:00
|
|
|
|
2015-06-25 04:39:38 +12:00
|
|
|
###############################################################################
|
|
|
|
# SPELLCHECKING
|
|
|
|
###############################################################################
|
|
|
|
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
|
2015-06-07 05:10:44 +12:00
|
|
|
|
|
|
|
def setDict(self, d):
|
2015-06-16 06:09:16 +12:00
|
|
|
self.currentDict = d
|
2015-06-20 04:47:45 +12:00
|
|
|
self._dict = enchant.Dict(d)
|
2015-06-16 06:09:16 +12:00
|
|
|
if self.highlighter:
|
|
|
|
self.highlighter.rehighlight()
|
2015-06-07 05:10:44 +12:00
|
|
|
|
|
|
|
def toggleSpellcheck(self, v):
|
|
|
|
self.spellcheck = v
|
2015-06-20 04:47:45 +12:00
|
|
|
if enchant and self.spellcheck and not self._dict:
|
|
|
|
self._dict = enchant.Dict(self.currentDict if self.currentDict else enchant.get_default_language())
|
2015-06-15 22:18:42 +12:00
|
|
|
if self.highlighter:
|
|
|
|
self.highlighter.rehighlight()
|
2015-06-20 04:47:45 +12:00
|
|
|
else:
|
|
|
|
self.spellcheck = False
|
2015-06-07 05:10:44 +12:00
|
|
|
|
|
|
|
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."
|
|
|
|
|
2015-06-08 08:06:57 +12:00
|
|
|
correct = pyqtSignal(str)
|
2015-06-07 05:10:44 +12:00
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
QAction.__init__(self, *args)
|
|
|
|
|
|
|
|
self.triggered.connect(lambda x: self.correct.emit(
|
2015-06-08 08:06:57 +12:00
|
|
|
str(self.text())))
|
2015-06-07 05:10:44 +12:00
|
|
|
|
|
|
|
def contextMenuEvent(self, event):
|
|
|
|
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
|
|
|
|
|
|
|
|
if not self.spellcheck:
|
|
|
|
QTextEdit.contextMenuEvent(self, event)
|
|
|
|
return
|
2015-06-27 20:27:52 +12:00
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
popup_menu = self.createStandardContextMenu()
|
2015-06-27 20:27:52 +12:00
|
|
|
popup_menu.exec_(event.globalPos())
|
|
|
|
|
|
|
|
def createStandardContextMenu(self):
|
|
|
|
popup_menu = QTextEdit.createStandardContextMenu(self)
|
|
|
|
|
|
|
|
if not self.spellcheck:
|
|
|
|
return popup_menu
|
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
# Select the word under the cursor.
|
|
|
|
cursor = self.textCursor()
|
2015-06-27 20:27:52 +12:00
|
|
|
#cursor = self.cursorForPosition(pos)
|
2015-06-07 05:10:44 +12:00
|
|
|
cursor.select(QTextCursor.WordUnderCursor)
|
|
|
|
self.setTextCursor(cursor)
|
|
|
|
# Check if the selected word is misspelled and offer spelling
|
|
|
|
# suggestions if it is.
|
2015-06-27 20:27:52 +12:00
|
|
|
if cursor.hasSelection():
|
|
|
|
text = str(cursor.selectedText())
|
2015-06-20 04:47:45 +12:00
|
|
|
if not self._dict.check(text):
|
2015-06-27 20:27:52 +12:00
|
|
|
spell_menu = QMenu(self.tr('Spelling Suggestions'), self)
|
2015-06-20 04:47:45 +12:00
|
|
|
for word in self._dict.suggest(text):
|
2015-06-07 05:10:44 +12:00
|
|
|
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])
|
|
|
|
popup_menu.insertMenu(popup_menu.actions()[0], spell_menu)
|
2015-06-27 20:27:52 +12:00
|
|
|
|
|
|
|
return popup_menu
|
|
|
|
|
2015-06-07 05:10:44 +12:00
|
|
|
def correctWord(self, word):
|
|
|
|
'''
|
|
|
|
Replaces the selected text with word.
|
|
|
|
'''
|
|
|
|
cursor = self.textCursor()
|
|
|
|
cursor.beginEditBlock()
|
|
|
|
|
|
|
|
cursor.removeSelectedText()
|
|
|
|
cursor.insertText(word)
|
|
|
|
|
|
|
|
cursor.endEditBlock()
|
|
|
|
|
2015-06-25 04:39:38 +12:00
|
|
|
###############################################################################
|
|
|
|
# FORMATTING
|
|
|
|
###############################################################################
|
|
|
|
|
2015-06-25 06:41:23 +12:00
|
|
|
def focusOutEvent(self, event):
|
|
|
|
"Submit changes just before focusing out."
|
|
|
|
QTextEdit.focusOutEvent(self, event)
|
|
|
|
self.submit()
|
|
|
|
|
2015-06-25 04:39:38 +12:00
|
|
|
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):
|
2015-06-25 23:41:55 +12:00
|
|
|
|
|
|
|
if self._textFormat == "html":
|
|
|
|
|
|
|
|
if _format == "Clear":
|
|
|
|
|
|
|
|
cursor = self.textCursor()
|
|
|
|
|
|
|
|
if _format == "Clear":
|
2015-06-26 03:06:07 +12:00
|
|
|
fmt = self._defaultCharFormat
|
2015-06-25 23:41:55 +12:00
|
|
|
cursor.setCharFormat(fmt)
|
2015-06-26 03:06:07 +12:00
|
|
|
bf = self._defaultBlockFormat
|
2015-06-25 23:41:55 +12:00
|
|
|
cursor.setBlockFormat(bf)
|
|
|
|
|
|
|
|
elif _format in ["Bold", "Italic", "Underline"]:
|
|
|
|
|
|
|
|
cursor = self.textCursor()
|
|
|
|
|
|
|
|
# If no selection, selects the word in which the cursor is now
|
|
|
|
if not cursor.hasSelection():
|
|
|
|
cursor.movePosition(QTextCursor.StartOfWord,
|
|
|
|
QTextCursor.MoveAnchor)
|
|
|
|
cursor.movePosition(QTextCursor.EndOfWord,
|
|
|
|
QTextCursor.KeepAnchor)
|
|
|
|
|
|
|
|
fmt = cursor.charFormat()
|
|
|
|
|
|
|
|
if _format == "Bold":
|
|
|
|
fmt.setFontWeight(QFont.Bold if fmt.fontWeight() != QFont.Bold else QFont.Normal)
|
|
|
|
elif _format == "Italic":
|
|
|
|
fmt.setFontItalic(not fmt.fontItalic())
|
|
|
|
elif _format == "Underline":
|
|
|
|
fmt.setFontUnderline(not fmt.fontUnderline())
|
|
|
|
|
2015-06-26 03:06:07 +12:00
|
|
|
fmt2 = self._defaultCharFormat
|
2015-06-25 23:41:55 +12:00
|
|
|
fmt2.setFontWeight(fmt.fontWeight())
|
|
|
|
fmt2.setFontItalic(fmt.fontItalic())
|
|
|
|
fmt2.setFontUnderline(fmt.fontUnderline())
|
|
|
|
|
|
|
|
cursor.mergeCharFormat(fmt2)
|
|
|
|
|
|
|
|
elif _format in ["Left", "Center", "Right", "Justify"]:
|
|
|
|
|
|
|
|
cursor = self.textCursor()
|
|
|
|
|
2015-06-25 23:57:41 +12:00
|
|
|
#bf = cursor.blockFormat()
|
|
|
|
bf = QTextBlockFormat()
|
2015-06-25 23:41:55 +12:00
|
|
|
bf.setAlignment(
|
|
|
|
Qt.AlignLeft if _format == "Left" else
|
|
|
|
Qt.AlignHCenter if _format == "Center" else
|
|
|
|
Qt.AlignRight if _format == "Right" else
|
|
|
|
Qt.AlignJustify)
|
|
|
|
|
2015-06-25 23:57:41 +12:00
|
|
|
cursor.setBlockFormat(bf)
|
2015-06-25 23:41:55 +12:00
|
|
|
self.setTextCursor(cursor)
|
|
|
|
|
|
|
|
elif self._textFormat == "t2t":
|
|
|
|
if _format == "Bold":
|
|
|
|
t2tFormatSelection(self, 0)
|
|
|
|
elif _format == "Italic":
|
|
|
|
t2tFormatSelection(self, 1)
|
|
|
|
elif _format == "Underline":
|
|
|
|
t2tFormatSelection(self, 2)
|
|
|
|
elif _format == "Clear":
|
|
|
|
t2tClearFormat(self)
|
|
|
|
|
|
|
|
|
2015-06-25 20:50:30 +12:00
|
|
|
|