Advertisement
Guest User

QsciLexerCustom example for PyQt5

a guest
Nov 2nd, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Baz Walter's QSciLexerCustom example code, adapted for PyQt5.
  4.  
  5. import sys
  6. from PyQt5 import QtCore, QtGui, QtWidgets, Qsci
  7.  
  8.  
  9. class MainWindow(QtWidgets.QMainWindow):
  10.      def __init__(self):
  11.          QtWidgets.QMainWindow.__init__(self)
  12.          self.setWindowTitle('Custom Lexer Example')
  13.          self.setGeometry(QtCore.QRect(50,200,400,400))
  14.          self.editor = Qsci.QsciScintilla(self)
  15.          self.editor.setUtf8(True)
  16.          self.editor.setMarginWidth(2, 15)
  17.          self.editor.setFolding(True)
  18.          self.setCentralWidget(self.editor)
  19.          self.lexer = CustomLexer(self.editor)
  20.          self.editor.setLexer(self.lexer)
  21.          self.editor.setText('\n# sample source\n\nfoo = 1\nbar = 2\n')
  22.  
  23.  
  24. class CustomLexer(Qsci.QsciLexerCustom):
  25.      def __init__(self, parent):
  26.          Qsci.QsciLexerCustom.__init__(self, parent)
  27.          self._styles = {
  28.              0: 'Default',
  29.              1: 'Comment',
  30.              2: 'Key',
  31.              3: 'Assignment',
  32.              4: 'Value',
  33.              }
  34.          for key in self._styles:
  35.              setattr(self, self._styles[key], key)
  36.  
  37.      def description(self, style):
  38.          return self._styles.get(style, '')
  39.  
  40.      def defaultColor(self, style):
  41.          if style == self.Default:
  42.              return QtGui.QColor('#000000')
  43.          elif style == self.Comment:
  44.              return QtGui.QColor('#C0C0C0')
  45.          elif style == self.Key:
  46.              return QtGui.QColor('#0000CC')
  47.          elif style == self.Assignment:
  48.              return QtGui.QColor('#CC0000')
  49.          elif style == self.Value:
  50.              return QtGui.QColor('#00CC00')
  51.          return Qsci.QsciLexerCustom.defaultColor(self, style)
  52.  
  53.      def styleText(self, start, end):
  54.          editor = self.editor()
  55.          if editor is None:
  56.              return
  57.  
  58.          # scintilla works with encoded bytes, not decoded characters.
  59.          # this matters if the source contains non-ascii characters and
  60.          # a multi-byte encoding is used (e.g. utf-8)
  61.          source = b''
  62.          if end > editor.length():
  63.              end = editor.length()
  64.          if end > start:
  65.              source = editor.text().encode('utf-8')[start:end]
  66.          if not source:
  67.              return
  68.  
  69.          # the line index will also be needed to implement folding
  70.          index = editor.SendScintilla(editor.SCI_LINEFROMPOSITION, start)
  71.          if index > 0:
  72.              # the previous state may be needed for multi-line styling
  73.              pos = editor.SendScintilla(
  74.                        editor.SCI_GETLINEENDPOSITION, index - 1)
  75.              state = editor.SendScintilla(editor.SCI_GETSTYLEAT, pos)
  76.          else:
  77.              state = self.Default
  78.  
  79.          set_style = self.setStyling
  80.          self.startStyling(start, 0x1f)
  81.  
  82.          # scintilla always asks to style whole lines
  83.          for line in source.splitlines(True):
  84.              length = len(line)
  85.              if line.startswith(b'#'):
  86.                  state = self.Comment
  87.              else:
  88.                  # the following will style lines like "x = 0"
  89.                  pos = line.find(b'=')
  90.                  if pos > 0:
  91.                      set_style(pos, self.Key)
  92.                      set_style(1, self.Assignment)
  93.                      length = length - pos - 1
  94.                      state = self.Value
  95.                  else:
  96.                      state = self.Default
  97.              set_style(length, state)
  98.              # folding implementation goes here
  99.              index += 1
  100.  
  101.  
  102. if __name__ == "__main__":
  103.      app = QtWidgets.QApplication(sys.argv)
  104.      app.lastWindowClosed.connect(app.quit)
  105.      win = MainWindow()
  106.      win.show()
  107.      sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement