Menu

[r189]: / trunk / QTextArea.py  Maximize  Restore  History

Download this file

323 lines (308 with data), 12.9 kB

  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# -*- coding: utf-8 -*-
import sys, pluginsystem
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from QCustomTextBrowser import QCustomTextBrowser
class QTextArea(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.parent = parent
self.setAttribute(Qt.WA_DeleteOnClose)
self.commandHistory = []
self.commandHistory.append("")
self.commandHistoryIndex = 0
self.tablayout = QVBoxLayout(self)
self.textbrowser = QCustomTextBrowser(self)
self.textbrowser.setup(self)
self.textbrowser.setAcceptRichText(True)
self.textbrowser.setReadOnly(True)
self.textbrowser.setOpenExternalLinks(True)
self.tablayout.addWidget(self.textbrowser)
self.setLayout(self.tablayout)
self.cursor = QTextCursor(self.textbrowser.document())
self.scrollBar = self.textbrowser.verticalScrollBar()
self.textbrowser.installEventFilter(self)
if len(self.parent.gconfig_others[2]) > 0:
self.printString(self.parent.gconfig_others[2], False, False)
self.printString("", True, len(self.parent.gconfig_others[2]) > 0)
def closeEvent(self, event):
for child in self.children():
try:
child.deleteLater()
except:
pass
self.setParent(None)
self.deleteLater()
def eventFilter(self, obj, e):
try:
if e.type() == QEvent.KeyPress:
if e.key() == Qt.Key_Up or e.key() == Qt.Key_Down or e.key() == Qt.Key_Left or e.key() == Qt.Key_Right:
self.keyPressEvent(e)
return True
elif e.key() == Qt.Key_Space or e.key() == Qt.Key_Tab:
self.keyPressEvent(e)
return True
else:
return False
else:
return False
except:
return False
def keyPressEvent(self, e):
cmd = "" #Don't touch this!
if e.key() == Qt.Key_Return or e.key() == Qt.Key_Enter:
if not self.cursor.atEnd():
self.cursor.movePosition(QTextCursor.End)
self.cursor.insertHtml(self.parent.lastChar)
self.cursor.select(QTextCursor.LineUnderCursor)
if not self.cursor.selectedText() == self.toPlainText(self.parent.prompt + self.parent.lastChar):
selectedtext = str(self.cursor.selectedText())
if selectedtext.startswith(self.toPlainText(self.parent.prompt)) and selectedtext.endswith(self.toPlainText(self.parent.lastChar)):
if len(self.toPlainText(self.parent.lastChar)) == 0:
cmd = self.cursor.selectedText()[len(self.toPlainText(self.parent.prompt)):]
else:
cmd = self.cursor.selectedText()[len(self.toPlainText(self.parent.prompt)):-len(self.toPlainText(self.parent.lastChar))]
self.cursor.clearSelection()
else:
self.cursor.clearSelection()
self.cursor.movePosition(QTextCursor.PreviousCharacter)
while not self.cursorAtStartOfNewPromptLine():
cmd += self.previousChar()
self.cursor.movePosition(QTextCursor.PreviousCharacter)
cmd = cmd[::-1]
cmd = cmd[len(self.parent.prompt):]
print cmd
self.commandHistory.append(cmd)
self.printString(cmd + "<br>", True, False)
self.analyzeCommand(self.toPlainText(cmd))
else:
self.cursor.clearSelection()
self.printString(cmd + "<br>", True, False)
self.printString("", True, not cmd == "")
self.cursor.movePosition(QTextCursor.Up)
self.cursor.select(QTextCursor.LineUnderCursor)
if self.cursor.selectedText() == "":
self.cursor.clearSelection()
self.cursor.movePosition(QTextCursor.Down)
self.cursor.deletePreviousChar()
self.cursor.movePosition(QTextCursor.End)
else:
self.cursor.clearSelection()
self.cursor.movePosition(QTextCursor.End)
self.commandHistoryIndex = len(self.commandHistory)
self.scrollBar.setValue(self.scrollBar.maximum())
elif e.key() == Qt.Key_X and (e.modifiers() & Qt.ControlModifier):
self.textbrowser.cmcut()
elif e.key() == Qt.Key_C and (e.modifiers() & Qt.ControlModifier):
self.textbrowser.cmcopy()
elif e.key() == Qt.Key_V and (e.modifiers() & Qt.ControlModifier):
self.textbrowser.cmpaste()
elif e.key() == Qt.Key_Left and (e.modifiers() & Qt.ControlModifier):
if self.parent.tabwidget.currentIndex() == 0:
self.parent.tabwidget.setCurrentIndex(self.parent.tabwidget.count()-1)
else:
self.parent.tabwidget.setCurrentIndex(self.parent.tabwidget.currentIndex()-1)
elif e.key() == Qt.Key_Right and (e.modifiers() & Qt.ControlModifier):
if self.parent.tabwidget.currentIndex() == self.parent.tabwidget.count()-1:
self.parent.tabwidget.setCurrentIndex(0)
else:
self.parent.tabwidget.setCurrentIndex(self.parent.tabwidget.currentIndex()+1)
elif e.key() == Qt.Key_Backspace:
if self.cursor.atEnd():
if self.cursorCanMoveLeft():
self.cursor.deletePreviousChar()
self.cursor.deletePreviousChar()
self.cursor.insertHtml(self.parent.lastChar)
else:
if self.cursorCanMoveLeft():
self.cursor.movePosition(QTextCursor.PreviousCharacter)
self.cursor.deletePreviousChar()
self.cursor.movePosition(QTextCursor.NextCharacter)
elif e.key() == Qt.Key_Delete:
if not self.cursor.atEnd():
self.cursor.deletePreviousChar()
char = self.nextChar()
self.cursor.insertHtml("<u>" + char + "</u>")
self.cursor.deleteChar()
elif self.cursor.atEnd() and not self.previousChar() == self.parent.lastChar:
self.cursor.deletePreviousChar()
self.cursor.insertHtml(self.parent.lastChar)
elif e.key() == Qt.Key_Space:
self.cursor.movePosition(QTextCursor.PreviousCharacter)
self.cursor.insertHtml("&nbsp;")
self.cursor.movePosition(QTextCursor.NextCharacter)
elif e.key() == Qt.Key_Up:
self.cursor.select(QTextCursor.LineUnderCursor)
self.cursor.removeSelectedText()
if self.commandHistoryIndex == 0:
self.commandHistoryIndex = len(self.commandHistory)-1
else:
self.commandHistoryIndex -= 1
self.printCommandIndex(self.commandHistoryIndex)
elif e.key() == Qt.Key_Down:
self.cursor.select(QTextCursor.LineUnderCursor)
self.cursor.removeSelectedText()
if self.commandHistoryIndex == len(self.commandHistory):
self.commandHistoryIndex = 1
else:
if self.commandHistoryIndex == len(self.commandHistory)-1:
self.commandHistoryIndex = 0
else:
self.commandHistoryIndex += 1
self.printCommandIndex(self.commandHistoryIndex)
elif e.key() == Qt.Key_Left:
if self.cursorCanMoveLeft():
if self.cursor.atEnd() and self.previousChar() == self.parent.lastChar:
self.deleteCursorChar()
else:
char = self.previousChar()
self.cursor.deletePreviousChar()
self.cursor.insertHtml(char)
self.cursor.movePosition(QTextCursor.PreviousCharacter)
char = self.previousChar()
self.cursor.deletePreviousChar()
self.cursor.insertHtml("<u>"+char+"</u>")
elif e.key() == Qt.Key_Right:
char = self.previousChar()
self.cursor.deletePreviousChar()
self.cursor.insertHtml(char)
if self.cursorCanMoveRight():
char = self.nextChar()
self.cursor.deleteChar()
self.cursor.insertHtml("<u>" + char + "</u>")
elif self.cursor.atEnd() and not self.previousChar() == self.parent.lastChar:
self.cursor.insertHtml(self.parent.lastChar)
elif e.key() == Qt.Key_Tab:
self.cursor.select(QTextCursor.LineUnderCursor)
if len(self.toPlainText(self.parent.lastChar)) == 0:
cmd = str(self.cursor.selectedText()[len(self.toPlainText(self.parent.prompt)):])
else:
cmd = str(self.cursor.selectedText()[len(self.toPlainText(self.parent.prompt)):-len(self.toPlainText(self.parent.lastChar))])
self.cursor.clearSelection()
if not len(cmd) == 0:
capabilities = []
for capability in pluginsystem.capabilities:
if str(capability).startswith(self.toPlainText(cmd)):
capabilities.append(str(capability))
capabilities.sort(key=str.lower)
if len(capabilities) == 0:
pass
elif len(capabilities) == 1:
self.printString(capabilities[0], True, False)
else:
sw = True
autocompletecmd = ""
possibleautocompletestring = []
prefix = capabilities[0]
for s in capabilities:
if len(s) < len(prefix):
prefix = prefix[:len(s)]
if not prefix:
autocompletecmd = ''
break
for i in range(len(prefix)):
if prefix[i] != s[i]:
prefix = prefix[:i]
break
autocompletecmd = prefix
if autocompletecmd == cmd:
self.printAutocomplete(cmd, capabilities)
else:
self.printString(autocompletecmd, True, False)
else:
self.printAutocomplete(cmd, pluginsystem.capabilities)
elif e.key() == Qt.Key_Escape:
pluginsystem.trigger_event("quit", data="", obj=self)
self.parent.hms.stopHM()
sys.exit(0)
else:
if not self.cursor.atEnd():
char = self.previousChar()
self.cursor.deletePreviousChar()
self.cursor.insertHtml(e.text())
self.cursor.insertHtml("<u>" + char + "</u>")
else:
self.printString(e.text(), False, False)
def printAutocomplete(self, cmd, capabilities):
fm = QFontMetrics(self.font())
maxlenstr = max(capabilities, key=len)
maxlen = fm.width(maxlenstr) + 40
maxncols = self.width() / maxlen
i = 0
output = "<table><tr>"
for capability in capabilities:
output += "<td width='" + str(maxlen) + "'>" + capability + "</td>"
i += 1
if i == maxncols:
i = 0
output += "</tr><tr>"
output += "</tr></table>"
self.printString(output, False, False)
self.printString(cmd, True, True)
def cursorAtStartOfNewPromptLine(self):
pos = self.cursor.position()
self.cursor.movePosition(QTextCursor.StartOfLine)
if pos == self.cursor.position():
self.cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, len(self.parent.prompt))
cmd = self.cursor.selectedText()
self.cursor.clearSelection()
self.cursor.movePosition(QTextCursor.StartOfLine)
if cmd == self.parent.prompt:
return True
else:
return False
else:
self.cursor.setPosition(pos)
return False
def cursorCanMoveLeft(self):
pos = self.cursor.position()
self.cursor.movePosition(QTextCursor.StartOfLine, QTextCursor.KeepAnchor)
cmd = self.cursor.selectedText()
self.cursor.clearSelection()
self.cursor.setPosition(pos)
if not cmd[:-1] == self.toPlainText(self.parent.prompt):
return True
else:
return False
def cursorCanMoveRight(self):
if not self.cursor.atEnd():
return True
else:
return False
def previousChar(self):
self.cursor.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
char = self.cursor.selectedText()
self.cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
return char
def nextChar(self):
self.cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
char = self.cursor.selectedText()
self.cursor.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
return char
def deleteCursorChar(self):
if len(self.toPlainText(self.parent.lastChar)) == 0:
return
for char in range(len(self.toPlainText(self.parent.lastChar))):
self.cursor.deletePreviousChar()
def printCommandIndex(self, i):
self.printString(self.commandHistory[i], True, False)
def printString(self, s, withPrompt=False, newLine=False):
if withPrompt == False and newLine == False:
self.deleteCursorChar()
elif withPrompt == True and newLine == False:
self.cursor.select(QTextCursor.LineUnderCursor)
self.cursor.removeSelectedText()
self.cursor.insertHtml(self.parent.prompt)
elif withPrompt == False and newLine == True:
self.deleteCursorChar()
self.cursor.insertHtml("<br>")
elif withPrompt == True and newLine == True:
self.deleteCursorChar()
self.cursor.insertHtml("<br>" + self.parent.prompt)
self.cursor.insertHtml(QString.fromUtf8(s) + self.parent.lastChar)
self.scrollBar.setValue(self.scrollBar.maximum())
def analyzeCommand(self, cmd):
pluginsystem.trigger_event("analyze_command", data=cmd, obj=self)
def toPlainText(self, s):
return QTextEdit(s).toPlainText()