manuskript/manuskript/ui/highlighters/basicHighlighter.py

146 lines
5.4 KiB
Python
Raw Normal View History

#!/usr/bin/python
# -*- coding: utf8 -*-
import re
2016-02-07 00:34:22 +13:00
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush, QTextCursor, QColor, QFont, QSyntaxHighlighter
from PyQt5.QtGui import QTextBlockFormat, QTextCharFormat
2016-02-07 00:34:22 +13:00
import manuskript.models.references as Ref
import manuskript.ui.style as S
from manuskript import settings
from manuskript import functions as F
2016-02-07 00:34:22 +13:00
class BasicHighlighter(QSyntaxHighlighter):
def __init__(self, editor):
QSyntaxHighlighter.__init__(self, editor.document())
self.editor = editor
self._misspelledColor = Qt.red
2015-06-25 23:41:55 +12:00
self._defaultBlockFormat = QTextBlockFormat()
self._defaultCharFormat = QTextCharFormat()
self.defaultTextColor = QColor(S.text)
self.backgroundColor = QColor(S.base)
self.markupColor = QColor(S.textLight)
self.linkColor = QColor(S.link)
self.spellingErrorColor = QColor(Qt.red)
def setDefaultBlockFormat(self, bf):
self._defaultBlockFormat = bf
self.rehighlight()
2016-02-07 00:34:22 +13:00
2015-06-25 23:41:55 +12:00
def setDefaultCharFormat(self, cf):
self._defaultCharFormat = cf
self.rehighlight()
2016-02-07 00:34:22 +13:00
def setMisspelledColor(self, color):
self._misspelledColor = color
2016-02-07 00:34:22 +13:00
def updateColorScheme(self, rehighlight=True):
"""
Generates a base set of colors that will take account of user
preferences, and use system style.
"""
# Reading user settings
opt = settings.textEditor
self.defaultTextColor = QColor(opt["fontColor"])
self.backgroundColor = (QColor(opt["background"])
if not opt["backgroundTransparent"]
else QColor(S.window))
self.markupColor = F.mixColors(self.defaultTextColor,
self.backgroundColor,
.3)
self.linkColor = QColor(S.link)
self.spellingErrorColor = QColor(opt["misspelled"])
self._defaultCharFormat.setForeground(QBrush(self.defaultTextColor))
if rehighlight:
self.rehighlight()
def highlightBlock(self, text):
"""Apply syntax highlighting to the given block of text.
"""
self.highlightBlockBefore(text)
self.doHighlightBlock(text)
self.highlightBlockAfter(text)
2016-02-07 00:34:22 +13:00
def doHighlightBlock(self, text):
"""
Virtual funtion to subclass.
"""
pass
def highlightBlockBefore(self, text):
"""Highlighting to do before anything else.
2017-11-15 03:22:16 +13:00
When subclassing BasicHighlighter, you must call highlightBlockBefore
before you do any custom highlighting. Or implement doHighlightBlock.
"""
#print(">", self.currentBlock().document().availableUndoSteps())
c = QTextCursor(self.currentBlock())
#c.joinPreviousEditBlock()
2015-06-25 23:57:41 +12:00
bf = QTextBlockFormat(self._defaultBlockFormat)
if bf != c.blockFormat():
c.setBlockFormat(bf)
#c.endEditBlock()
#print(" ", self.currentBlock().document().availableUndoSteps())
2016-02-07 00:34:22 +13:00
# self.setFormat(0, len(text), self._defaultCharFormat)
def highlightBlockAfter(self, text):
"""Highlighting to do after everything else.
2017-11-15 03:22:16 +13:00
When subclassing BasicHighlighter, you must call highlightBlockAfter
after your custom highlighting. Or implement doHighlightBlock.
"""
2016-02-07 00:34:22 +13:00
2015-06-27 20:27:52 +12:00
# References
2015-07-06 19:45:28 +12:00
for txt in re.finditer(Ref.RegEx, text):
fmt = self.format(txt.start())
2015-06-27 20:27:52 +12:00
fmt.setFontFixedPitch(True)
fmt.setFontWeight(QFont.DemiBold)
2017-11-15 03:22:16 +13:00
2015-07-06 19:45:28 +12:00
if txt.group(1) == Ref.TextLetter:
2017-11-15 03:22:16 +13:00
fmt.setBackground(QBrush(Ref.TextHighlightColor))
elif txt.group(1) == Ref.CharacterLetter:
2017-11-15 03:22:16 +13:00
fmt.setBackground(QBrush(Ref.CharacterHighlightColor))
2015-07-06 19:45:28 +12:00
elif txt.group(1) == Ref.PlotLetter:
2017-11-15 03:22:16 +13:00
fmt.setBackground(QBrush(Ref.PlotHighlightColor))
elif txt.group(1) == Ref.WorldLetter:
2017-11-15 03:22:16 +13:00
fmt.setBackground(QBrush(Ref.WorldHighlightColor))
2016-02-07 00:34:22 +13:00
self.setFormat(txt.start(),
2016-02-07 00:34:22 +13:00
txt.end() - txt.start(),
fmt)
# Spell checking
2017-11-15 03:22:16 +13:00
# Following algorithm would not check words at the end of line.
# This hacks adds a space to every line where the text cursor is not
# So that it doesn't spellcheck while typing, but still spellchecks at
# end of lines. See github's issue #166.
textedText = text
if self.currentBlock().position() + len(text) != \
self.editor.textCursor().position():
textedText = text + " "
2017-11-15 03:22:16 +13:00
# Based on http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
WORDS = r'(?iu)([\w\']+)[^\'\w]'
# (?iu) means case insensitive and unicode
2016-04-07 23:17:14 +12:00
if hasattr(self.editor, "spellcheck") and self.editor.spellcheck:
for word_object in re.finditer(WORDS, textedText):
if (self.editor._dict
and not self.editor._dict.check(word_object.group(1))):
format = self.format(word_object.start(1))
format.setUnderlineColor(self._misspelledColor)
# SpellCheckUnderline fails with some fonts
format.setUnderlineStyle(QTextCharFormat.WaveUnderline)
self.setFormat(word_object.start(1),
word_object.end(1) - word_object.start(1),
format)