18
18
19
19
TODO:
20
20
- configuration - init commands, font, ...
21
- - syntax highlighting
21
+ - python code highlighting
22
22
23
23
"""
24
24
@@ -92,6 +92,29 @@ def closeEvent(self, event):
92
92
QWidget .closeEvent (self , event )
93
93
94
94
95
+ class ConsoleHighlighter (QSyntaxHighlighter ):
96
+ EDIT_LINE , ERROR , OUTPUT , INIT = range (4 )
97
+ def __init__ (self , doc ):
98
+ QSyntaxHighlighter .__init__ (self ,doc )
99
+ formats = { self .OUTPUT : Qt .black ,
100
+ self .ERROR : Qt .red ,
101
+ self .EDIT_LINE : Qt .darkGreen ,
102
+ self .INIT : Qt .gray }
103
+ self .f = {}
104
+ for tag , color in formats .iteritems ():
105
+ self .f [tag ] = QTextCharFormat ()
106
+ self .f [tag ].setForeground (color )
107
+
108
+ def highlightBlock (self , txt ):
109
+ size = txt .length ()
110
+ state = self .currentBlockState ()
111
+ if state == self .OUTPUT or state == self .ERROR or state == self .INIT :
112
+ self .setFormat (0 ,size , self .f [state ])
113
+ # highlight prompt only
114
+ if state == self .EDIT_LINE :
115
+ self .setFormat (0 ,3 , self .f [self .EDIT_LINE ])
116
+
117
+
95
118
class PythonEdit (QTextEdit , code .InteractiveInterpreter ):
96
119
97
120
def __init__ (self ,parent = None ):
@@ -108,9 +131,9 @@ def __init__(self,parent=None):
108
131
109
132
self .buffer = []
110
133
111
- self .insertPlainText ("To access Quantum GIS environment from this console\n "
112
- "use qgis.utils.iface object (instance of QgisInterface class).\n "
113
- "\n " )
134
+ self .insertTaggedText ("To access Quantum GIS environment from this console\n "
135
+ "use qgis.utils.iface object (instance of QgisInterface class).\n "
136
+ "\n " , ConsoleHighlighter . INIT )
114
137
115
138
for line in _init_commands :
116
139
self .runsource (line )
@@ -120,13 +143,12 @@ def __init__(self,parent=None):
120
143
self .history = QStringList ()
121
144
self .historyIndex = 0
122
145
123
- #from pythonhigh import PythonHighlighter
124
- #self.high = PythonHighlighter(self)
146
+ self .high = ConsoleHighlighter (self )
125
147
126
148
def displayPrompt (self , more = False ):
127
149
self .currentPrompt = "... " if more else ">>> "
128
150
self .currentPromptLength = len (self .currentPrompt )
129
- self .insertPlainText (self .currentPrompt )
151
+ self .insertTaggedLine (self .currentPrompt , ConsoleHighlighter . EDIT_LINE )
130
152
self .moveCursor (QTextCursor .End , QTextCursor .MoveAnchor )
131
153
132
154
def isCursorInEditionZone (self ):
@@ -240,6 +262,24 @@ def entered(self):
240
262
self .setTextCursor (self .cursor )
241
263
self .runCommand ( unicode (self .currentCommand ()) )
242
264
265
+ def insertTaggedText (self , txt , tag ):
266
+
267
+ if len (txt ) > 0 and txt [- 1 ] == '\n ' : # remove trailing newline to avoid one more empty line
268
+ txt = txt [0 :- 1 ]
269
+
270
+ c = self .textCursor ()
271
+ for line in txt .split ('\n ' ):
272
+ b = c .block ()
273
+ b .setUserState (tag )
274
+ c .insertText (line )
275
+ c .insertBlock ()
276
+
277
+ def insertTaggedLine (self , txt , tag ):
278
+ c = self .textCursor ()
279
+ b = c .block ()
280
+ b .setUserState (tag )
281
+ c .insertText (txt )
282
+
243
283
def runCommand (self , cmd ):
244
284
245
285
self .updateHistory (cmd )
@@ -254,13 +294,12 @@ def runCommand(self, cmd):
254
294
255
295
output = sys .stdout .get_and_clean_data ()
256
296
if output :
257
- self .insertPlainText (output )
297
+ self .insertTaggedText (output , ConsoleHighlighter . OUTPUT )
258
298
self .displayPrompt (more )
259
299
260
300
def write (self , txt ):
261
301
""" reimplementation from code.InteractiveInterpreter """
262
- self .insertPlainText (txt )
263
-
302
+ self .insertTaggedText (txt , ConsoleHighlighter .ERROR )
264
303
265
304
if __name__ == '__main__' :
266
305
a = QApplication (sys .argv )
0 commit comments