Menu

[r343]: / trunk / QCustomTextBrowser.py  Maximize  Restore  History

Download this file

132 lines (125 with data), 5.0 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
# -*- coding: utf-8 -*-
import sys, urllib, pluginsystem, locale
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from QCustomTextCursor import QCustomTextCursor
import globalvars as g
import utils as u
#Locale support
try:
locale.setlocale(locale.LC_ALL, "")
t = gettext.translation("main", "locale", languages=[locale.getlocale()[0].split("_")[0]])
_ = t.ugettext
except:
_ = lambda s: unicode(s)
class QCustomTextBrowser(QTextBrowser):
def __init__(self, parent=None):
QTextBrowser.__init__(self, parent)
self.parent = parent
self.setObjectName("textBrowser")
self.setAttribute(Qt.WA_DeleteOnClose)
self.commandHistory = []
self.commandHistory.append("")
self.commandHistoryIndex = 0
self.setAcceptRichText(True)
self.setReadOnly(True)
self.setOpenExternalLinks(True)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.cursor = QCustomTextCursor(self)
self.scrollBar = self.verticalScrollBar()
self.installEventFilter(self)
if len(g.welcomemsg) > 0:
self.printString(g.welcomemsg, False, False)
self.printString("", True, len(g.welcomemsg) > 0)
self.cursor.updateLastEnterPos()
def getCursor(self):
return self.cursor
def keyPressEvent(self, e):
if (e.key() == Qt.Key_Return or e.key() == Qt.Key_Enter) and (e.modifiers() & Qt.ControlModifier):
self.printString("", False, True)
elif e.key() == Qt.Key_Return or e.key() == Qt.Key_Enter:
self.cursor.moveToEnd()
self.cursor.setPosition(self.cursor.lastEnterPos)
self.cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)
cmd = unicode(self.cursor.selection().toPlainText()[:-len(g.cursorChar)])
self.cursor.clearSelection()
self.printString("", False, True)
pluginsystem.trigger_event("analyze_command", data=cmd, obj=self)
self.printString("", True, True)
self.cursor.deleteUpperEmptyLine()
self.cursor.updateLastEnterPos()
self.commandHistory.append(cmd)
self.commandHistoryIndex = len(self.commandHistory)
self.scrollBar.setValue(self.scrollBar.maximum())
elif e.key() == Qt.Key_Backspace:
self.cursor.keyBackspacePreviousCharacter()
elif e.key() == Qt.Key_Delete:
self.cursor.keyDeletePreviousCharacter()
elif e.key() == Qt.Key_Space:
self.cursor.keySpace()
elif e.key() == Qt.Key_Up:
self.cursor.selectAll()
self.cursor.removeSelectedText()
if self.commandHistoryIndex == 0:
self.commandHistoryIndex = len(self.commandHistory)-1
else:
self.commandHistoryIndex -= 1
self.printString(self.commandHistory[self.commandHistoryIndex], True, False)
elif e.key() == Qt.Key_Down:
self.cursor.selectAll()
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.printString(self.commandHistory[self.commandHistoryIndex], True, False)
elif e.key() == Qt.Key_Left:
self.cursor.moveLeft()
elif e.key() == Qt.Key_Right:
self.cursor.moveRight()
elif e.key() == Qt.Key_Tab:
text = self.cursor.findAllMinusLastWord()
cmd = self.cursor.findLastWord()
if not len(cmd) == 0:
capabilities = []
for capability in pluginsystem.capabilities:
if capability.startswith(cmd):
capabilities.append(unicode(capability))
capabilities.sort(key=unicode.lower)
if len(capabilities) == 1:
self.printString(text + capabilities[0], True, False)
elif len(capabilities) > 1:
autocompletecmd = u.findAutocompleteCommand(capabilities)
if autocompletecmd == cmd:
output = u.commands2table(self, capabilities)
self.printString(output, False, False)
self.cursor.updateLastEnterPosWithLen(len(g.prompt))
self.printString(cmd, True, True)
else:
self.printString(text + autocompletecmd, True, False)
else:
output = u.commands2table(self, pluginsystem.capabilities)
self.printString(output, False, False)
self.cursor.updateLastEnterPosWithLen(len(g.prompt))
self.printString(cmd, True, True)
else:
self.cursor.insertCharacter(e.text())
def printString(self, s, withPrompt=False, newLine=False):
self.cursor.printString(s, withPrompt, newLine)
def loadResource(self, type, name):
url = str(name.toString())
ret = QVariant()
if url.startswith('http://'):
tmpFile = QTemporaryFile()
tmpFile.setAutoRemove(False)
tmpFile.open()
tmpFile = str(tmpFile.fileName())
urllib.urlretrieve(url, tmpFile)
ret = QTextBrowser.loadResource(self, type, QUrl(tmpFile))
else:
ret = QTextBrowser.loadResource(self, type, name)
return ret