comparison python/codeedit.py @ 161:956f8e5ee48a

Improvements to code edit
author Windel Bouwman
date Sat, 09 Mar 2013 15:52:55 +0100
parents 10330be89bc2
children d8c735dc31f9
comparison
equal deleted inserted replaced
160:10330be89bc2 161:956f8e5ee48a
32 def setSource(self, src): 32 def setSource(self, src):
33 self.src = src 33 self.src = src
34 self.adjust() 34 self.adjust()
35 def setCursorPosition(self, c): 35 def setCursorPosition(self, c):
36 self.cursorPosition = clipVal(c, 0, len(self.src)) 36 self.cursorPosition = clipVal(c, 0, len(self.src))
37 print(self.cursorPosition, self.CursorRow, self.CursorCol)
38 self.cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth 37 self.cursorX = self.CursorCol * self.charWidth + self.xposTXT - self.charWidth
39 self.cursorY = self.CursorRow * self.charHeight - self.charHeight 38 self.cursorY = self.CursorRow * self.charHeight - self.charHeight
39 self.update()
40 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition) 40 CursorPosition = property(lambda self: self.cursorPosition, setCursorPosition)
41 @property
42 def Rows(self):
43 # Make this nicer:
44 return self.src.split('\n')
41 @property 45 @property
42 def CursorRow(self): 46 def CursorRow(self):
43 # TODO: make this nice. 47 # TODO: make this nice.
44 txt = self.src[0:self.cursorPosition] 48 txt = self.src[0:self.cursorPosition]
45 return len(txt.split('\n')) 49 return len(txt.split('\n'))
49 curLine = txt.split('\n')[-1] 53 curLine = txt.split('\n')[-1]
50 return len(curLine) + 1 54 return len(curLine) + 1
51 @property 55 @property
52 def CurrentLine(self): 56 def CurrentLine(self):
53 return self.getRow(self.CursorRow) 57 return self.getRow(self.CursorRow)
58 def setRowCol(self, r, c):
59 prevRows = self.Rows[:r]
60 txt = '\n'.join(prevRows)
61 self.CursorPosition = len(txt) + c
54 def getRow(self, r): 62 def getRow(self, r):
55 rows = self.src.split('\n') 63 rows = self.Rows
56 r = r - 1 64 r = r - 1
57 if r < 0 or r > len(rows) - 1: 65 if r < 0 or r > len(rows) - 1:
58 return '' 66 return ''
59 else: 67 else:
60 return rows[r] 68 return rows[r]
61 def getChar(self, pos): 69 def getChar(self, pos):
62 pass 70 pass
63 def insertText(self, txt): 71 def insertText(self, txt):
64 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:]) 72 self.setSource(self.src[0:self.CursorPosition] + txt + self.src[self.CursorPosition:])
65 self.CursorPosition += len(txt) 73 self.CursorPosition += len(txt)
74 def deleteChar(self):
75 self.setSource(self.src[0:self.CursorPosition] + self.src[self.CursorPosition+1:])
66 def GotoNextChar(self): 76 def GotoNextChar(self):
67 if self.src[self.CursorPosition] != '\n': 77 if self.src[self.CursorPosition] != '\n':
68 self.CursorPosition += 1 78 self.CursorPosition += 1
69 def GotoPrevChar(self): 79 def GotoPrevChar(self):
70 if self.src[self.CursorPosition - 1] != '\n': 80 if self.src[self.CursorPosition - 1] != '\n':
71 self.CursorPosition -= 1 81 self.CursorPosition -= 1
72 def GotoNextLine(self): 82 def GotoNextLine(self):
73 curLine = self.CurrentLine 83 curLine = self.CurrentLine
74 c = self.CursorCol 84 c = self.CursorCol
75 self.CursorPosition += len(curLine) - c + c + 1 # line break char! 85 self.CursorPosition += len(curLine) - c + 1 # line break char!
86 curLine = self.CurrentLine
87 if len(curLine) < c:
88 self.CursorPosition += len(curLine)
89 else:
90 self.CursorPosition += c
76 def GotoPrevLine(self): 91 def GotoPrevLine(self):
92 c = self.CursorCol
93 self.CursorPosition -= c + 1 # line break char!
77 curLine = self.CurrentLine 94 curLine = self.CurrentLine
78 c = self.CursorCol 95 if len(curLine) > c:
79 self.CursorPosition -= len(curLine) + c + 1 # line break char! 96 self.CursorPosition -= len(curLine) - c
80 def paintEvent(self, event): 97 def paintEvent(self, event):
81 # Helper variables: 98 # Helper variables:
82 er = event.rect() 99 er = event.rect()
83 chw, chh = self.charWidth, self.charHeight 100 chw, chh = self.charWidth, self.charHeight
84 painter = QPainter(self) 101 painter = QPainter(self)
85 # Background: 102 # Background:
86 painter.fillRect(er, self.palette().color(QPalette.Base)) 103 painter.fillRect(er, self.palette().color(QPalette.Base))
87 painter.fillRect(QRect(self.xposLNA, er.top(), 8 * chw, er.bottom() + 1), Qt.gray) 104 painter.fillRect(QRect(self.xposLNA, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
88 painter.fillRect(self.xposTXT, (self.CursorRow - 1) * chh, er.width(), chh, Qt.yellow) 105 painter.fillRect(er.left(), (self.CursorRow - 1) * chh, er.width(), chh, Qt.yellow)
89 painter.setPen(Qt.gray) 106 painter.setPen(Qt.gray)
90 # first and last row: 107 # first and last row:
91 row1 = max(int(er.top() / chh) - 1, 0) 108 row1 = max(int(er.top() / chh) - 1, 1)
92 row2 = max(int(er.bottom() / chh) + 1, 0) 109 row2 = max(int(er.bottom() / chh) + 1, 1)
93 # Draw contents: 110 # Draw contents:
94 txt = self.src.split('\n')
95 print('update', row1, row2)
96 for row in range(row1, row2 + 1): 111 for row in range(row1, row2 + 1):
97 ypos = row * chh 112 ypos = row * chh - self.charDescent
98 painter.setPen(Qt.black) 113 painter.setPen(Qt.black)
99 painter.drawText(self.xposLNA, ypos, 'R ={0}'.format(row)) 114 painter.drawText(self.xposLNA, ypos, 'R ={0}'.format(row))
100 xpos = self.xposTXT 115 xpos = self.xposTXT
101 if row - 1 < len(txt): 116 painter.drawText(xpos, ypos, self.getRow(row))
102 painter.drawText(xpos, ypos, txt[row - 1])
103 # cursor 117 # cursor
104 if self.blinkcursor: 118 if self.blinkcursor:
105 painter.fillRect(self.cursorX, self.cursorY, 2, chh, Qt.black) 119 painter.fillRect(self.cursorX, self.cursorY, 2, chh, Qt.black)
106 def keyPressEvent(self, event): 120 def keyPressEvent(self, event):
107 if event.matches(QKeySequence.MoveToNextChar): 121 if event.matches(QKeySequence.MoveToNextChar):
108 self.GotoNextChar() 122 self.GotoNextChar()
109 if event.matches(QKeySequence.MoveToPreviousChar): 123 elif event.matches(QKeySequence.MoveToPreviousChar):
110 self.GotoPrevChar() 124 self.GotoPrevChar()
111 if event.matches(QKeySequence.MoveToNextLine): 125 elif event.matches(QKeySequence.MoveToNextLine):
112 self.GotoNextLine() 126 self.GotoNextLine()
113 if event.matches(QKeySequence.MoveToPreviousLine): 127 elif event.matches(QKeySequence.MoveToPreviousLine):
114 self.GotoPrevLine() 128 self.GotoPrevLine()
115 if event.matches(QKeySequence.MoveToNextPage): 129 elif event.matches(QKeySequence.MoveToNextPage):
116 for i in range(5): 130 for i in range(5):
117 self.GotoNextLine() 131 self.GotoNextLine()
118 if event.matches(QKeySequence.MoveToPreviousPage): 132 elif event.matches(QKeySequence.MoveToPreviousPage):
119 for i in range(5): 133 for i in range(5):
120 self.GotoPrevLine() 134 self.GotoPrevLine()
121 if event.matches(QKeySequence.MoveToEndOfLine): 135 elif event.matches(QKeySequence.MoveToEndOfLine):
122 self.CursorPosition += len(self.CurrentLine) - self.CursorCol + 1 136 self.CursorPosition += len(self.CurrentLine) - self.CursorCol + 1
123 if event.matches(QKeySequence.MoveToStartOfLine): 137 elif event.matches(QKeySequence.MoveToStartOfLine):
124 self.CursorPosition -= self.CursorCol - 1 138 self.CursorPosition -= self.CursorCol - 1
125 char = event.text().lower() 139 elif event.matches(QKeySequence.Delete):
126 if char: 140 self.deleteChar()
127 print('ins', char) 141 elif event.matches(QKeySequence.InsertParagraphSeparator):
128 self.insertText(char) 142 self.insertText('\n')
143 elif event.key() == Qt.Key_Backspace:
144 self.CursorPosition -= 1
145 self.deleteChar()
146 else:
147 char = event.text()
148 if char:
149 self.insertText(char)
129 self.update() 150 self.update()
151 def mousePressEvent(self, event):
152 pos = event.pos()
153 if pos.x() > self.xposTXT and pos.x():
154 c = round((pos.x() - self.xposTXT) / self.charWidth)
155 r = int(pos.y() / self.charHeight)
156 self.setRowCol(r, c)
130 def adjust(self): 157 def adjust(self):
131 self.charHeight = self.fontMetrics().height() 158 metrics = self.fontMetrics()
132 self.charWidth = self.fontMetrics().width('x') 159 self.charHeight = metrics.height()
160 self.charWidth = metrics.width('x')
161 self.charDescent = metrics.descent()
133 self.xposLNA = GAP 162 self.xposLNA = GAP
134 self.xposTXT = self.xposLNA + 8 * self.charWidth + GAP 163 self.xposTXT = self.xposLNA + 8 * self.charWidth + GAP
135 self.xposEnd = self.xposTXT + self.charWidth * 80 164 self.xposEnd = self.xposTXT + self.charWidth * 80
136 self.setMinimumWidth(self.xposEnd) 165 self.setMinimumWidth(self.xposEnd)
137 txt = self.src.split('\n') 166 txt = self.src.split('\n')
152 app = QApplication(sys.argv) 181 app = QApplication(sys.argv)
153 ce = CodeEdit() 182 ce = CodeEdit()
154 ce.show() 183 ce.show()
155 src = ''.join(inspect.getsourcelines(InnerCode)[0]) 184 src = ''.join(inspect.getsourcelines(InnerCode)[0])
156 ce.Source = src 185 ce.Source = src
157 ce.resize(600, 500) 186 ce.resize(600, 800)
158 app.exec() 187 app.exec()
159 188