-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
144 lines (111 loc) · 4.4 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
import sys
import re
from PyQt4 import QtCore, QtGui
from pygments import highlight
from pygments.lexers import *
from pygments.formatter import Formatter
import time
from overlay import PMXMessageOverlay
# Copyright (C) 2008 Christophe Kibleur <[email protected]>
#
# This file is part of WikiParser (http://thewikiblog.appspot.com/).
#
def hex2QColor(c):
r=int(c[0:2],16)
g=int(c[2:4],16)
b=int(c[4:6],16)
return QtGui.QColor(r,g,b)
class QFormatter(Formatter):
def __init__(self):
Formatter.__init__(self)
self.data=[]
# Create a dictionary of text styles, indexed
# by pygments token names, containing QTextCharFormat
# instances according to pygments' description
# of each style
self.styles={}
for token, style in self.style:
qtf=QtGui.QTextCharFormat()
if style['color']:
qtf.setForeground(hex2QColor(style['color']))
if style['bgcolor']:
qtf.setBackground(hex2QColor(style['bgcolor']))
if style['bold']:
qtf.setFontWeight(QtGui.QFont.Bold)
if style['italic']:
qtf.setFontItalic(True)
if style['underline']:
qtf.setFontUnderline(True)
self.styles[str(token)]=qtf
def format(self, tokensource, outfile):
global styles
# We ignore outfile, keep output in a buffer
self.data=[]
# Just store a list of styles, one for each character
# in the input. Obviously a smarter thing with
# offsets and lengths is a good idea!
for ttype, value in tokensource:
l=len(value)
t=str(ttype)
self.data.extend([self.styles[t],]*l)
class Highlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent, mode):
QtGui.QSyntaxHighlighter.__init__(self, parent)
self.tstamp=time.time()
# Keep the formatter and lexer, initializing them
# may be costly.
self.formatter=QFormatter()
self.lexer=get_lexer_by_name(mode)
def highlightBlock(self, text):
"""Takes a block, applies format to the document.
according to what's in it.
"""
# I need to know where in the document we are,
# because our formatting info is global to
# the document
cb = self.currentBlock()
p = cb.position()
# The \n is not really needed, but sometimes
# you are in an empty last block, so your position is
# **after** the end of the document.
text=unicode(self.document().toPlainText())+'\n'
# Yes, re-highlight the whole document.
# There **must** be some optimizacion possibilities
# but it seems fast enough.
highlight(text,self.lexer,self.formatter)
# Just apply the formatting to this block.
# For titles, it may be necessary to backtrack
# and format a couple of blocks **earlier**.
for i in range(len(unicode(text))):
try:
self.setFormat(i,1,self.formatter.data[p+i])
except IndexError:
pass
# I may need to do something about this being called
# too quickly.
self.tstamp=time.time()
class ExampleOverlayedText(QtGui.QPlainTextEdit, PMXMessageOverlay):
FULL_THERESHOLD = 0.4
def __init__(self, parent = None):
QtGui.QTextEdit.__init__(self, parent)
PMXMessageOverlay.__init__(self)
self.blockCountChanged.connect(self.showBlockCount)
def resizeEvent(self, event):
super(ExampleOverlayedText, self).resizeEvent(event)
self.updateMessagePosition()
def messageLinkActivated(self, link):
QtGui.QMessageBox.information(self, "Link activated", "You just clicked on %s" % link)
def showBlockCount(self, newCount):
self.showMessage("Block count changed to <i><b>%s</b></i>" % newCount)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
rst = ExampleOverlayedText()
rst.setWindowTitle('reSt')
hl=Highlighter(rst.document(),"rest")
rst.show()
python = ExampleOverlayedText()
python.setWindowTitle('python')
hl=Highlighter(python.document(),"python")
python.show()
sys.exit(app.exec_())