annotate python/codeeditor.py @ 97:5a965e9664f2

Movage
author windel
date Mon, 24 Dec 2012 13:32:54 +0100
parents python/libs/widgets/codeeditor.py@4a27c28c7d0f
children fe145e42259d
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 from PyQt4.QtCore import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 from PyQt4.QtGui import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
3 import compiler.lexer
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
4 import os.path
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
5
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
6 class MySyntaxHighlighter(QSyntaxHighlighter):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
7 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8 super(MySyntaxHighlighter, self).__init__(parent)
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
9 # Syntax highlighting:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 self.rules = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 fmt = QTextCharFormat()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12 fmt.setForeground(Qt.darkBlue)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 fmt.setFontWeight(QFont.Bold)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 for kw in compiler.lexer.keywords:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 pattern = '\\b'+kw+'\\b'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16 self.rules.append( (pattern, fmt) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18 # Comments:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19 fmt = QTextCharFormat()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 fmt.setForeground(Qt.gray)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 fmt.setFontItalic(True)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 pattern = '\{.*\}'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 self.rules.append( (pattern, fmt) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 # Procedure:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 fmt = QTextCharFormat()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 fmt.setForeground(Qt.blue)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 fmt.setFontItalic(True)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 #pattern = '(?<=procedure )[A-Za-z]'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 # TODO lookbehind does not work, think something else
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 #self.rules.append( (pattern, fmt) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 def highlightBlock(self, text):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 for pattern, fmt in self.rules:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 expression = QRegExp(pattern)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 index = expression.indexIn(text)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 while index >= 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 length = expression.matchedLength()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 self.setFormat(index, length, fmt)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 index = expression.indexIn(text, index + length)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 class LineNumberArea(QWidget):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 def __init__(self, codeedit):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 super(LineNumberArea, self).__init__(codeedit)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45 self.codeedit = codeedit
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 # TODO: display error in this: self.setToolTip('hello world')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47 def sizeHint(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
48 return QSize(self.codeedit.lineNumberAreaWidth(), 0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
49 def paintEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
50 self.codeedit.lineNumberAreaPaintEvent(ev)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
51
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 class CodeEdit(QPlainTextEdit):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
53 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
54 super(CodeEdit, self).__init__(parent)
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
55 # members:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
56 self.isUntitled = True
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
57 self.filename = None
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
58 self.setFont(QFont('Courier'))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
59 self.lineNumberArea = LineNumberArea(self)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
60
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
61 self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
62 self.updateRequest.connect(self.updateLineNumberArea)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
63
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
64 # Syntax highlighter:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
65 self.highlighter = MySyntaxHighlighter(self.document())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
66
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
67 def setFileName(self, filename):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
68 self.filename = filename
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
69 self.isUntitled = False
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
70 self.setWindowTitle(filename)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
71 def setSource(self, source):
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
72 self.setPlainText(source)
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 9
diff changeset
73
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
74 def save(self):
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
75 pass
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
76 def saveAs(self):
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
77 pass
8
edd70006d3e4 Started with MDI functions
windel
parents: 1
diff changeset
78
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
79 def saveFile(self):
9
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
80 if self.isUntitled:
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
81 self.saveAs()
92ace1ca50a8 64 bits kernel without interrupts but with printf in C
windel
parents: 8
diff changeset
82 else:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
83 source = str(self.toPlainText())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
84 f = open(self.filename, 'w')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
85 f.write(source)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
86 f.close()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
87
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
88 def highlightErrorLocation(self, row, col):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
89 tc = QTextCursor(self.document())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
90 tc.clearSelection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
91 tc.movePosition(tc.Down, tc.MoveAnchor, row - 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
92 tc.movePosition(tc.Right, tc.MoveAnchor, col - 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
93 tc.movePosition(tc.NextCharacter, tc.KeepAnchor) # Select 1 character
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
94 selection = QTextEdit.ExtraSelection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
95 lineColor = QColor(Qt.red).lighter(160)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
96 selection.format.setBackground(lineColor)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
97 #selection.format.setProperty(QTextFormat.FullWidthSelection, True)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
98 selection.cursor = tc
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
99 self.setExtraSelections( [ selection ] )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
100 def clearErrors(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
101 self.setExtraSelections( [ ] )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
102
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
103 def lineNumberAreaWidth(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
104 digits = 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
105 mx = max(1, self.blockCount())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
106 while mx >= 10:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
107 mx = mx / 10
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
108 digits += 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
109 space = 3 + self.fontMetrics().width('8') * digits
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
110 return space
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
111 def lineNumberAreaPaintEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
112 painter = QPainter(self.lineNumberArea)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
113 painter.fillRect(ev.rect(), Qt.lightGray)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
114 block = self.firstVisibleBlock()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
115 blockNumber = block.blockNumber()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
116 top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
117 bottom = top + self.blockBoundingRect(block).height()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
118 while block.isValid() and top <= ev.rect().bottom():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
119 if block.isVisible() and bottom >= ev.rect().top():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
120 num = str(blockNumber + 1)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
121 painter.setPen(Qt.black)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
122 painter.drawText(0, top, self.lineNumberArea.width(), self.fontMetrics().height(), Qt.AlignRight, num)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
123 block = block.next()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
124 top = bottom
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
125 bottom = top + self.blockBoundingRect(block).height()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
126 blockNumber += 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
127 def resizeEvent(self, ev):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
128 super(CodeEdit, self).resizeEvent(ev)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
129 cr = self.contentsRect()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
130 self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height() ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
131 def updateLineNumberAreaWidth(self, newBlockCount):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
132 self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
133 def updateLineNumberArea(self, rect, dy):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
134 if dy > 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
135 self.lineNumberArea.scroll(0, dy)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
136 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
137 self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
138 if rect.contains(self.viewport().rect()):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
139 self.updateLineNumberAreaWidth(0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
140