def __init__( self, app, title ):
super().__init__( app, title )
self.code_font = self.app.getCodeFont()
self.text_edit = QtWidgets.QTextEdit()
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget( self.text_edit )
self.setLayout( self.layout )
self.all_text_formats = {}
for style, fg_colour, bg_colour in self.all_style_colours:
char_format = QtGui.QTextCharFormat()
char_format.setFont( self.code_font )
char_format.setForeground( QtGui.QBrush( QtGui.QColor( str(fg_colour) ) ) )
char_format.setBackground( QtGui.QBrush( QtGui.QColor( str(bg_colour) ) ) )
self.all_text_formats[ style ] = char_format
self.text_edit.setReadOnly( True )
em = self.app.fontMetrics().width( 'm' )
ex = self.app.fontMetrics().lineSpacing()
self.resize( 130*em, 45*ex )
python类QTextCharFormat()的实例源码
def insertTextObject(self):
fileName = self.fileNameLineEdit.text()
file = QFile(fileName)
if not file.open(QIODevice.ReadOnly):
QMessageBox.warning(self, "Error Opening File",
"Could not open '%s'" % fileName)
svgData = file.readAll()
svgCharFormat = QTextCharFormat()
svgCharFormat.setObjectType(Window.SvgTextFormat)
svgCharFormat.setProperty(Window.SvgData, svgData)
try:
# Python v2.
orc = unichr(0xfffc)
except NameError:
# Python v3.
orc = chr(0xfffc)
cursor = self.textEdit.textCursor()
cursor.insertText(orc, svgCharFormat)
self.textEdit.setTextCursor(cursor)
def insertTextObject(self):
fileName = self.fileNameLineEdit.text()
file = QFile(fileName)
if not file.open(QIODevice.ReadOnly):
QMessageBox.warning(self, "Error Opening File",
"Could not open '%s'" % fileName)
svgData = file.readAll()
svgCharFormat = QTextCharFormat()
svgCharFormat.setObjectType(Window.SvgTextFormat)
svgCharFormat.setProperty(Window.SvgData, svgData)
try:
# Python v2.
orc = unichr(0xfffc)
except NameError:
# Python v3.
orc = chr(0xfffc)
cursor = self.textEdit.textCursor()
cursor.insertText(orc, svgCharFormat)
self.textEdit.setTextCursor(cursor)
def __init__(self, api, *args):
super().__init__(*args)
spell_check_lang = api.opt.general['spell_check']
try:
self._dictionary = (
enchant.Dict(spell_check_lang)
if spell_check_lang
else None)
except enchant.errors.DictNotFoundError:
self._dictionary = None
api.log.warn(f'dictionary {spell_check_lang} not installed.')
self._fmt = QtGui.QTextCharFormat()
self._fmt.setUnderlineColor(QtCore.Qt.red)
self._fmt.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline)
self._fmt.setFontUnderline(True)
def insertTextObject(self):
fileName = self.fileNameLineEdit.text()
file = QFile(fileName)
if not file.open(QIODevice.ReadOnly):
QMessageBox.warning(self, "Error Opening File",
"Could not open '%s'" % fileName)
svgData = file.readAll()
svgCharFormat = QTextCharFormat()
svgCharFormat.setObjectType(Window.SvgTextFormat)
svgCharFormat.setProperty(Window.SvgData, svgData)
try:
# Python v2.
orc = unichr(0xfffc)
except NameError:
# Python v3.
orc = chr(0xfffc)
cursor = self.textEdit.textCursor()
cursor.insertText(orc, svgCharFormat)
self.textEdit.setTextCursor(cursor)
def format(color, style=''):
word_color = QColor()
word_color.setNamedColor(color)
word_format = QTextCharFormat()
word_format.setForeground(word_color)
if 'italic' in style:
word_format.setFontItalic(True)
elif 'bold' in style:
word_format.setFontWeight(QFont.Bold)
return word_format
def highlight_matches(self, results):
print(results)
pass
#class Match()
#selected = QtGui.QTextCharFormat()
# ACTIONS
def __init__( self, app ):
self.app = app
self.all_text_formats = {}
for style, fg_colour, bg_colour in self.all_style_colours:
format = QtGui.QTextCharFormat()
format.setForeground( QtGui.QBrush( QtGui.QColor( fg_colour ) ) )
format.setBackground( QtGui.QBrush( QtGui.QColor( bg_colour ) ) )
self.all_text_formats[ style ] = format
super().__init__()
self.setReadOnly( True )
self.setTextInteractionFlags( QtCore.Qt.TextSelectableByMouse|QtCore.Qt.TextSelectableByKeyboard )
def _prepareTheme(self):
for tag, fg_bg in self.theme.items():
fg = QtGui.QColor(fg_bg[0])
bg = QtGui.QColor(fg_bg[1])
self._colors[tag] = (fg, bg)
f = QtGui.QTextCharFormat()
f.setForeground(fg)
f.setBackground(bg)
self._formats[tag] = f
def getFormat(self, tag: ContentTagsEnum) -> QtGui.QTextCharFormat:
if tag not in self._colors:
return None
fg, bg = self._colors[tag]
f = QtGui.QTextCharFormat()
f.setForeground(fg)
f.setBackground(bg)
f.setProperty(VivTextProperties.vivTag, tag)
return f
def __init__(self, parent=None):
super(FeatureCodeHighlighter, self).__init__(parent)
keywordFormat = QTextCharFormat()
keywordFormat.setForeground(QColor(45, 95, 235))
self.addRule("\\b(?<!\\\\)(%s)\\b" % ("|".join(keywordPatterns)),
keywordFormat)
singleLineCommentFormat = QTextCharFormat()
singleLineCommentFormat.setForeground(QColor(112, 128, 144))
self.addRule("#[^\n]*", singleLineCommentFormat)
groupFormat = QTextCharFormat()
groupFormat.setForeground(QColor(255, 27, 147))
self.addRule("@[A-Za-z0-9_.]+", groupFormat)
def search_highlight(self):
cursor = self.text_field.textCursor()
b_format = cursor.blockFormat()
b_format.setBackground(QBrush(QColor('white')))
cursor.setBlockFormat(b_format)
format = QTextCharFormat()
format.setBackground(QBrush(QColor('yellow')))
regex = QRegularExpression(self.search_field.text())
matches = regex.globalMatch(self.text_field.toPlainText())
_matches = []
while matches.hasNext():
_matches.append(matches.next())
self.search_matches = _matches
self.search_field_matches.setText('Matches: ' + str(len(self.search_matches)))
self.search_field_matches.show()
self.search_field_progress.setRange(0, len(self.search_matches))
if len(self.search_matches) > 100:
self.search_field_progress.show()
match_count = 1
for match in self.search_matches:
if match_count > 150:
# TODO: implement proper handling of > 1000 matches
break
self.search_field_progress.setValue(match_count)
match_count += 1
cursor.setPosition(match.capturedStart())
cursor.setPosition(match.capturedEnd(), QTextCursor.KeepAnchor)
cursor.mergeCharFormat(format)
#self.field.moveCursor(QTextCursor.Start)
#self.field.moveCursor(F)
#self.field.ensureCursorVisible()
def weekdayFormatChanged(self):
format = QTextCharFormat()
format.setForeground(
Qt.GlobalColor(
self.weekdayColorCombo.itemData(
self.weekdayColorCombo.currentIndex())))
self.calendar.setWeekdayTextFormat(Qt.Monday, format)
self.calendar.setWeekdayTextFormat(Qt.Tuesday, format)
self.calendar.setWeekdayTextFormat(Qt.Wednesday, format)
self.calendar.setWeekdayTextFormat(Qt.Thursday, format)
self.calendar.setWeekdayTextFormat(Qt.Friday, format)
def weekendFormatChanged(self):
format = QTextCharFormat()
format.setForeground(
Qt.GlobalColor(
self.weekendColorCombo.itemData(
self.weekendColorCombo.currentIndex())))
self.calendar.setWeekdayTextFormat(Qt.Saturday, format)
self.calendar.setWeekdayTextFormat(Qt.Sunday, format)
def reformatHeaders(self):
text = self.headerTextFormatCombo.currentText()
format = QTextCharFormat()
if text == "Bold":
format.setFontWeight(QFont.Bold)
elif text == "Italic":
format.setFontItalic(True)
elif text == "Green":
format.setForeground(Qt.green)
self.calendar.setHeaderTextFormat(format)
def weekdayFormatChanged(self):
format = QTextCharFormat()
format.setForeground(
Qt.GlobalColor(
self.weekdayColorCombo.itemData(
self.weekdayColorCombo.currentIndex())))
self.calendar.setWeekdayTextFormat(Qt.Monday, format)
self.calendar.setWeekdayTextFormat(Qt.Tuesday, format)
self.calendar.setWeekdayTextFormat(Qt.Wednesday, format)
self.calendar.setWeekdayTextFormat(Qt.Thursday, format)
self.calendar.setWeekdayTextFormat(Qt.Friday, format)
def weekendFormatChanged(self):
format = QTextCharFormat()
format.setForeground(
Qt.GlobalColor(
self.weekendColorCombo.itemData(
self.weekendColorCombo.currentIndex())))
self.calendar.setWeekdayTextFormat(Qt.Saturday, format)
self.calendar.setWeekdayTextFormat(Qt.Sunday, format)
def reformatHeaders(self):
text = self.headerTextFormatCombo.currentText()
format = QTextCharFormat()
if text == "Bold":
format.setFontWeight(QFont.Bold)
elif text == "Italic":
format.setFontItalic(True)
elif text == "Green":
format.setForeground(Qt.green)
self.calendar.setHeaderTextFormat(format)
def update_gui(self):
self.calendar_widget.setDateTextFormat(QtCore.QDate(), QtGui.QTextCharFormat())
# -using the "null date" to clear
date_qtextcharformat = QtGui.QTextCharFormat()
date_qtextcharformat.setFontWeight(QtGui.QFont.Bold)
for diarym in wbd.model.DiaryEntryM.get_all():
qdatetime = QtCore.QDateTime.fromMSecsSinceEpoch(diarym.date_added_it * 1000)
self.calendar_widget.setDateTextFormat(qdatetime.date(), date_qtextcharformat)
def highlight_slipfactor_description(self):
"""Highlight the description of currosponding slipfactor on selection of inputs
Note : This routine is not in use in current version
:return:
"""
slip_factor = str(self.ui.combo_slipfactor.currentText())
self.textCursor = QTextCursor(self.ui.textBrowser.document())
cursor = self.textCursor
# Setup the desired format for matches
format = QTextCharFormat()
format.setBackground(QBrush(QColor("red")))
# Setup the regex engine
pattern = str(slip_factor)
regex = QRegExp(pattern)
# Process the displayed document
pos = 0
index = regex.indexIn(self.ui.textBrowser.toPlainText(), pos)
while (index != -1):
# Select the matched text and apply the desired format
cursor.setPosition(index)
cursor.movePosition(QTextCursor.EndOfLine, 1)
# cursor.movePosition(QTextCursor.EndOfWord, 1)
cursor.mergeCharFormat(format)
# Move to the next match
pos = index + regex.matchedLength()
index = regex.indexIn(self.ui.textBrowser.toPlainText(), pos)
def weekdayFormatChanged(self):
format = QTextCharFormat()
format.setForeground(
Qt.GlobalColor(
self.weekdayColorCombo.itemData(
self.weekdayColorCombo.currentIndex())))
self.calendar.setWeekdayTextFormat(Qt.Monday, format)
self.calendar.setWeekdayTextFormat(Qt.Tuesday, format)
self.calendar.setWeekdayTextFormat(Qt.Wednesday, format)
self.calendar.setWeekdayTextFormat(Qt.Thursday, format)
self.calendar.setWeekdayTextFormat(Qt.Friday, format)
def weekendFormatChanged(self):
format = QTextCharFormat()
format.setForeground(
Qt.GlobalColor(
self.weekendColorCombo.itemData(
self.weekendColorCombo.currentIndex())))
self.calendar.setWeekdayTextFormat(Qt.Saturday, format)
self.calendar.setWeekdayTextFormat(Qt.Sunday, format)
def reformatHeaders(self):
text = self.headerTextFormatCombo.currentText()
format = QTextCharFormat()
if text == "Bold":
format.setFontWeight(QFont.Bold)
elif text == "Italic":
format.setFontItalic(True)
elif text == "Green":
format.setForeground(Qt.green)
self.calendar.setHeaderTextFormat(format)
def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
keywordFormat = QTextCharFormat()
keywordFormat.setForeground(Qt.darkBlue)
keywordFormat.setFontWeight(QFont.Bold)
keywordPatterns = ["\\bchar\\b", "\\bclass\\b", "\\bconst\\b",
"\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b", "\\bfriend\\b",
"\\binline\\b", "\\bint\\b", "\\blong\\b", "\\bnamespace\\b",
"\\boperator\\b", "\\bprivate\\b", "\\bprotected\\b",
"\\bpublic\\b", "\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b",
"\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b",
"\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b",
"\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b", "\\bvoid\\b",
"\\bvolatile\\b"]
self.highlightingRules = [(QRegExp(pattern), keywordFormat)
for pattern in keywordPatterns]
classFormat = QTextCharFormat()
classFormat.setFontWeight(QFont.Bold)
classFormat.setForeground(Qt.darkMagenta)
self.highlightingRules.append((QRegExp("\\bQ[A-Za-z]+\\b"),
classFormat))
singleLineCommentFormat = QTextCharFormat()
singleLineCommentFormat.setForeground(Qt.red)
self.highlightingRules.append((QRegExp("//[^\n]*"),
singleLineCommentFormat))
self.multiLineCommentFormat = QTextCharFormat()
self.multiLineCommentFormat.setForeground(Qt.red)
quotationFormat = QTextCharFormat()
quotationFormat.setForeground(Qt.darkGreen)
self.highlightingRules.append((QRegExp("\".*\""), quotationFormat))
functionFormat = QTextCharFormat()
functionFormat.setFontItalic(True)
functionFormat.setForeground(Qt.blue)
self.highlightingRules.append((QRegExp("\\b[A-Za-z0-9_]+(?=\\()"),
functionFormat))
self.commentStartExpression = QRegExp("/\\*")
self.commentEndExpression = QRegExp("\\*/")
def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
keywordFormat = QTextCharFormat()
keywordFormat.setForeground(Qt.darkBlue)
keywordFormat.setFontWeight(QFont.Bold)
keywordPatterns = ["\\bchar\\b", "\\bclass\\b", "\\bconst\\b",
"\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b", "\\bfriend\\b",
"\\binline\\b", "\\bint\\b", "\\blong\\b", "\\bnamespace\\b",
"\\boperator\\b", "\\bprivate\\b", "\\bprotected\\b",
"\\bpublic\\b", "\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b",
"\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b",
"\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b",
"\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b", "\\bvoid\\b",
"\\bvolatile\\b"]
self.highlightingRules = [(QRegExp(pattern), keywordFormat)
for pattern in keywordPatterns]
classFormat = QTextCharFormat()
classFormat.setFontWeight(QFont.Bold)
classFormat.setForeground(Qt.darkMagenta)
self.highlightingRules.append((QRegExp("\\bQ[A-Za-z]+\\b"),
classFormat))
singleLineCommentFormat = QTextCharFormat()
singleLineCommentFormat.setForeground(Qt.red)
self.highlightingRules.append((QRegExp("//[^\n]*"),
singleLineCommentFormat))
self.multiLineCommentFormat = QTextCharFormat()
self.multiLineCommentFormat.setForeground(Qt.red)
quotationFormat = QTextCharFormat()
quotationFormat.setForeground(Qt.darkGreen)
self.highlightingRules.append((QRegExp("\".*\""), quotationFormat))
functionFormat = QTextCharFormat()
functionFormat.setFontItalic(True)
functionFormat.setForeground(Qt.blue)
self.highlightingRules.append((QRegExp("\\b[A-Za-z0-9_]+(?=\\()"),
functionFormat))
self.commentStartExpression = QRegExp("/\\*")
self.commentEndExpression = QRegExp("\\*/")
def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
keywordFormat = QTextCharFormat()
keywordFormat.setForeground(Qt.darkBlue)
keywordFormat.setFontWeight(QFont.Bold)
keywordPatterns = ["\\bchar\\b", "\\bclass\\b", "\\bconst\\b",
"\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b", "\\bfriend\\b",
"\\binline\\b", "\\bint\\b", "\\blong\\b", "\\bnamespace\\b",
"\\boperator\\b", "\\bprivate\\b", "\\bprotected\\b",
"\\bpublic\\b", "\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b",
"\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b",
"\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b",
"\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b", "\\bvoid\\b",
"\\bvolatile\\b"]
self.highlightingRules = [(QRegExp(pattern), keywordFormat)
for pattern in keywordPatterns]
classFormat = QTextCharFormat()
classFormat.setFontWeight(QFont.Bold)
classFormat.setForeground(Qt.darkMagenta)
self.highlightingRules.append((QRegExp("\\bQ[A-Za-z]+\\b"),
classFormat))
singleLineCommentFormat = QTextCharFormat()
singleLineCommentFormat.setForeground(Qt.red)
self.highlightingRules.append((QRegExp("//[^\n]*"),
singleLineCommentFormat))
self.multiLineCommentFormat = QTextCharFormat()
self.multiLineCommentFormat.setForeground(Qt.red)
quotationFormat = QTextCharFormat()
quotationFormat.setForeground(Qt.darkGreen)
self.highlightingRules.append((QRegExp("\".*\""), quotationFormat))
functionFormat = QTextCharFormat()
functionFormat.setFontItalic(True)
functionFormat.setForeground(Qt.blue)
self.highlightingRules.append((QRegExp("\\b[A-Za-z0-9_]+(?=\\()"),
functionFormat))
self.commentStartExpression = QRegExp("/\\*")
self.commentEndExpression = QRegExp("\\*/")