-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_editor_widget.py
53 lines (42 loc) · 1.73 KB
/
code_editor_widget.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
# System library imports
from PyQt4 import QtGui
# Local imports
from syntax_highlighter import PythonHighlighter
class CodeWidget(QtGui.QPlainTextEdit):
def __init__(self, parent, filename, font=None):
super(CodeWidget, self).__init__(parent)
self.set_font(font)
self.setWordWrapMode(QtGui.QTextOption.NoWrap)
self.filename = filename
self.highlighter = PythonHighlighter(self.document())
self.is_zoomed = True
def set_font(self, font):
if font is None:
font = QtGui.QFont('Consolas', 11)
self.document().setDefaultFont(font)
def color_line(self, line, color):
selection = QtGui.QTextEdit.ExtraSelection()
selection.format.setBackground(color)
selection.format.setProperty(QtGui.QTextFormat.FullWidthSelection, True)
selection.cursor = self.textCursor()
selection.cursor.movePosition(QtGui.QTextCursor.Start)
selection.cursor.movePosition(QtGui.QTextCursor.Down,
QtGui.QTextCursor.MoveAnchor,
line-1)
extraSelections = self.extraSelections()
if extraSelections is None:
extraSelections = [selection]
else:
extraSelections.append(selection)
self.setExtraSelections(extraSelections)
def clear_line_coloring(self):
self.setExtraSelections([])
def toggle_zoom(self):
font = self.document().defaultFont()
if self.is_zoomed:
font.setPointSize(4)
self.setFont(font)
else:
font.setPointSize(11)
self.setFont(font)
self.is_zoomed = not self.is_zoomed