Advertisement
Guest User

Khertan

a guest
Apr 12th, 2010
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.78 KB | None | 0 0
  1. '''
  2. Python Syntax Highlighting Example
  3.  
  4. Copyright (C) 2009 Carson J. Q. Farmer
  5.  
  6. This program is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public Licence as published by the Free Software
  8. Foundation; either version 2 of the Licence, or (at your option) any later
  9. version.
  10.  
  11. This program is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. FOR A PARTICULAR PURPOSE.  See the GNU General Public Licence for more
  14. details.
  15.  
  16. You should have received a copy of the GNU General Public Licence along with
  17. this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
  18. Street, Fifth Floor, Boston, MA  02110-1301, USA
  19. '''
  20.  
  21. import sys
  22. from PySide.QtGui import *
  23. from PySide.QtCore import *
  24.  
  25. class MyHighlighter( QSyntaxHighlighter ):
  26.  
  27.     def __init__( self, parent, theme ):
  28.       QSyntaxHighlighter.__init__( self, parent )
  29.       self.parent = parent
  30.       keyword = QTextCharFormat()
  31.       reservedClasses = QTextCharFormat()
  32.       assignmentOperator = QTextCharFormat()
  33.       delimiter = QTextCharFormat()
  34.       specialConstant = QTextCharFormat()
  35.       boolean = QTextCharFormat()
  36.       number = QTextCharFormat()
  37.       comment = QTextCharFormat()
  38.       string = QTextCharFormat()
  39.       singleQuotedString = QTextCharFormat()
  40.  
  41.       self.highlightingRules = []
  42.  
  43.       # keyword
  44.       brush = QBrush( Qt.darkBlue, Qt.SolidPattern )
  45.       keyword.setForeground( brush )
  46.       keyword.setFontWeight( QFont.Bold )
  47.       keywords = QStringList( [ "break", "else", "for", "if", "in",
  48.                                 "next", "repeat", "return", "switch",
  49.                                 "try", "while" ] )
  50.       for word in keywords:
  51.         pattern = QRegExp("\\b" + word + "\\b")
  52.         rule = HighlightingRule( pattern, keyword )
  53.         self.highlightingRules.append( rule )
  54.  
  55.       # reservedClasses
  56.       reservedClasses.setForeground( brush )
  57.       reservedClasses.setFontWeight( QFont.Bold )
  58.       keywords = QStringList( [ "array", "character", "complex",
  59.                                 "data.frame", "double", "factor",
  60.                                 "function", "integer", "list",
  61.                                 "logical", "matrix", "numeric",
  62.                                 "vector" ] )
  63.       for word in keywords:
  64.         pattern = QRegExp("\\b" + word + "\\b")
  65.         rule = HighlightingRule( pattern, reservedClasses )
  66.         self.highlightingRules.append( rule )
  67.  
  68.  
  69.       # assignmentOperator
  70.       brush = QBrush( Qt.yellow, Qt.SolidPattern )
  71.       pattern = QRegExp( "(<){1,2}-" )
  72.       assignmentOperator.setForeground( brush )
  73.       assignmentOperator.setFontWeight( QFont.Bold )
  74.       rule = HighlightingRule( pattern, assignmentOperator )
  75.       self.highlightingRules.append( rule )
  76.  
  77.       # delimiter
  78.       pattern = QRegExp( "[\)\(]+|[\{\}]+|[][]+" )
  79.       delimiter.setForeground( brush )
  80.       delimiter.setFontWeight( QFont.Bold )
  81.       rule = HighlightingRule( pattern, delimiter )
  82.       self.highlightingRules.append( rule )
  83.  
  84.       # specialConstant
  85.       brush = QBrush( Qt.green, Qt.SolidPattern )
  86.       specialConstant.setForeground( brush )
  87.       keywords = QStringList( [ "Inf", "NA", "NaN", "NULL" ] )
  88.       for word in keywords:
  89.         pattern = QRegExp("\\b" + word + "\\b")
  90.         rule = HighlightingRule( pattern, specialConstant )
  91.         self.highlightingRules.append( rule )
  92.  
  93.       # boolean
  94.       boolean.setForeground( brush )
  95.       keywords = QStringList( [ "TRUE", "FALSE" ] )
  96.       for word in keywords:
  97.         pattern = QRegExp("\\b" + word + "\\b")
  98.         rule = HighlightingRule( pattern, boolean )
  99.         self.highlightingRules.append( rule )
  100.  
  101.       # number
  102.       pattern = QRegExp( "[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?" )
  103.       pattern.setMinimal( True )
  104.       number.setForeground( brush )
  105.       rule = HighlightingRule( pattern, number )
  106.       self.highlightingRules.append( rule )
  107.  
  108.       # comment
  109.       brush = QBrush( Qt.blue, Qt.SolidPattern )
  110.       pattern = QRegExp( "#[^\n]*" )
  111.       comment.setForeground( brush )
  112.       rule = HighlightingRule( pattern, comment )
  113.       self.highlightingRules.append( rule )
  114.  
  115.       # string
  116.       brush = QBrush( Qt.red, Qt.SolidPattern )
  117.       pattern = QRegExp( "\".*\"" )
  118.       pattern.setMinimal( True )
  119.       string.setForeground( brush )
  120.       rule = HighlightingRule( pattern, string )
  121.       self.highlightingRules.append( rule )
  122.  
  123.       # singleQuotedString
  124.       pattern = QRegExp( "\'.*\'" )
  125.       pattern.setMinimal( True )
  126.       singleQuotedString.setForeground( brush )
  127.       rule = HighlightingRule( pattern, singleQuotedString )
  128.       self.highlightingRules.append( rule )
  129.  
  130.     def highlightBlock( self, text ):
  131.       for rule in self.highlightingRules:
  132.         expression = QRegExp( rule.pattern )
  133.         index = expression.indexIn( text )
  134.         while index >= 0:
  135.           length = expression.matchedLength()
  136.           self.setFormat( index, length, rule.format )
  137.           index = text.indexOf( expression, index + length )
  138.       self.setCurrentBlockState( 0 )
  139.  
  140. class HighlightingRule():
  141.   def __init__( self, pattern, format ):
  142.     self.pattern = pattern
  143.     self.format = format
  144.  
  145. class TestApp( QMainWindow ):
  146.   def __init__(self):
  147.     QMainWindow.__init__(self)
  148.     font = QFont()
  149.     font.setFamily( "Courier" )
  150.     font.setFixedPitch( True )
  151.     font.setPointSize( 10 )
  152.     editor = QTextEdit()
  153.     editor.setFont( font )
  154.     highlighter = MyHighlighter( editor, "Classic" )
  155.     self.setCentralWidget( editor )
  156.     self.setWindowTitle( "Syntax Highlighter" )
  157.  
  158.  
  159. if __name__ == "__main__":
  160.   app = QApplication( sys.argv )
  161.   window = TestApp()
  162.   window.show()
  163.   sys.exit( app.exec_() )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement