Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # This is based on Baz Walter's example code for the QsciLexerCustom class.
- # The added code producing a segmentation fault is marked below in the
- # __init__ method of the MainWindow class.
- import sys
- from PyQt5 import QtCore, QtGui, QtWidgets, Qsci
- class MainWindow(QtWidgets.QMainWindow):
- def __init__(self):
- QtWidgets.QMainWindow.__init__(self)
- self.setWindowTitle('Custom Lexer Example')
- self.setGeometry(QtCore.QRect(50,200,400,400))
- self.editor = Qsci.QsciScintilla(self)
- self.editor.setUtf8(True)
- self.editor.setMarginWidth(2, 15)
- self.editor.setFolding(True)
- # The following three lines of code cause this program to generate a
- # segfault for me upon closing the main window. The original code it
- # replaces is:
- #
- # self.setCentralWidget(self.editor)
- self.tabWidget = QtWidgets.QTabWidget()
- self.tabWidget.addTab(self.editor, 'Untitled')
- self.setCentralWidget(self.tabWidget)
- self.lexer = CustomLexer(self.editor)
- self.editor.setLexer(self.lexer)
- self.editor.setText('\n# sample source\n\nfoo = 1\nbar = 2\n')
- class CustomLexer(Qsci.QsciLexerCustom):
- def __init__(self, parent):
- Qsci.QsciLexerCustom.__init__(self, parent)
- self._styles = {
- 0: 'Default',
- 1: 'Comment',
- 2: 'Key',
- 3: 'Assignment',
- 4: 'Value',
- }
- for key in self._styles:
- setattr(self, self._styles[key], key)
- def description(self, style):
- return self._styles.get(style, '')
- def defaultColor(self, style):
- if style == self.Default:
- return QtGui.QColor('#000000')
- elif style == self.Comment:
- return QtGui.QColor('#C0C0C0')
- elif style == self.Key:
- return QtGui.QColor('#0000CC')
- elif style == self.Assignment:
- return QtGui.QColor('#CC0000')
- elif style == self.Value:
- return QtGui.QColor('#00CC00')
- return Qsci.QsciLexerCustom.defaultColor(self, style)
- def styleText(self, start, end):
- editor = self.editor()
- if editor is None:
- return
- # scintilla works with encoded bytes, not decoded characters.
- # this matters if the source contains non-ascii characters and
- # a multi-byte encoding is used (e.g. utf-8)
- source = b''
- if end > editor.length():
- end = editor.length()
- if end > start:
- source = editor.text().encode('utf-8')[start:end]
- if not source:
- return
- # the line index will also be needed to implement folding
- index = editor.SendScintilla(editor.SCI_LINEFROMPOSITION, start)
- if index > 0:
- # the previous state may be needed for multi-line styling
- pos = editor.SendScintilla(
- editor.SCI_GETLINEENDPOSITION, index - 1)
- state = editor.SendScintilla(editor.SCI_GETSTYLEAT, pos)
- else:
- state = self.Default
- set_style = self.setStyling
- self.startStyling(start, 0x1f)
- # scintilla always asks to style whole lines
- for line in source.splitlines(True):
- length = len(line)
- if line.startswith(b'#'):
- state = self.Comment
- else:
- # the following will style lines like "x = 0"
- pos = line.find(b'=')
- if pos > 0:
- set_style(pos, self.Key)
- set_style(1, self.Assignment)
- length = length - pos - 1
- state = self.Value
- else:
- state = self.Default
- set_style(length, state)
- # folding implementation goes here
- index += 1
- if __name__ == "__main__":
- app = QtWidgets.QApplication(sys.argv)
- app.lastWindowClosed.connect(app.quit)
- win = MainWindow()
- win.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement