3 from PyQt4
.QtCore
import Qt
4 from PyQt4
.QtGui
import QFont
5 from PyQt4
.QtGui
import QSyntaxHighlighter
6 from PyQt4
.QtGui
import QTextCharFormat
8 class GitSyntaxHighlighter(QSyntaxHighlighter
):
10 def __init__(self
, doc
):
11 QSyntaxHighlighter
.__init
__(self
, doc
)
13 begin
= self
.__mkformat
(QFont
.Bold
, Qt
.cyan
)
14 addition
= self
.__mkformat
(QFont
.Bold
, Qt
.green
)
15 removal
= self
.__mkformat
(QFont
.Bold
, Qt
.red
)
16 message
= self
.__mkformat
(QFont
.Bold
, Qt
.yellow
, Qt
.black
)
18 # Catch trailing whitespace
19 bad_ws_format
= self
.__mkformat
(QFont
.Bold
, Qt
.black
, Qt
.red
)
20 self
._bad
_ws
_regex
= re
.compile('(.*?)(\s+)$')
21 self
._bad
_ws
_format
= bad_ws_format
24 ( re
.compile('^(@@|\+\+\+|---)'), begin
),
25 ( re
.compile('^\+'), addition
),
26 ( re
.compile('^-'), removal
),
27 ( re
.compile('^:'), message
),
30 def getFormat(self
, line
):
31 for regex
, rule
in self
._rules
:
36 def highlightBlock(self
, qstr
):
37 ascii
= qstr
.toAscii().data()
39 fmt
= self
.getFormat(ascii
)
41 match
= self
._bad
_ws
_regex
.match(ascii
)
42 if match
and match
.group(2):
43 start
= len(match
.group(1))
44 self
.setFormat(0, start
, fmt
)
45 self
.setFormat(start
, len(ascii
),
48 self
.setFormat(0, len(ascii
), fmt
)
50 def __mkformat(self
, weight
, color
, bgcolor
=None):
51 format
= QTextCharFormat()
52 format
.setFontWeight(weight
)
53 format
.setForeground(color
)
54 if bgcolor
: format
.setBackground(bgcolor
)
58 if __name__
== '__main__':
60 from PyQt4
import QtCore
, QtGui
62 class SyntaxTestDialog(QtGui
.QDialog
):
63 def __init__(self
, parent
):
64 QtGui
.QDialog
.__init
__(self
, parent
)
67 def setupUi(self
, CommandDialog
):
68 CommandDialog
.resize(QtCore
.QSize(QtCore
.QRect(0,0,720,512).size()).expandedTo(CommandDialog
.minimumSizeHint()))
70 self
.vboxlayout
= QtGui
.QVBoxLayout(CommandDialog
)
71 self
.vboxlayout
.setObjectName("vboxlayout")
73 self
.commandText
= QtGui
.QTextEdit(CommandDialog
)
76 font
.setFamily("Monospace")
78 self
.commandText
.setFont(font
)
79 self
.commandText
.setAcceptDrops(False)
80 #self.commandText.setReadOnly(True)
81 self
.vboxlayout
.addWidget(self
.commandText
)
83 GitSyntaxHighlighter(self
.commandText
.document())
86 app
= QtGui
.QApplication(sys
.argv
)
87 dialog
= SyntaxTestDialog(app
.activeWindow())