Menu

[r236]: / trunk / QCustomTextCursor.py  Maximize  Restore  History

Download this file

143 lines (123 with data), 4.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
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import globalvars as g
class QCustomTextCursor(QTextCursor):
def __init__(self, parent=None):
QTextCursor.__init__(self, parent.document())
self.parent = parent
self.lastEnterPos = 0
def updateLastEnterPos(self):
self.lastEnterPos = self.position()-len(g.cursorChar)
def updateLastEnterPosWithLen(self, slen):
self.lastEnterPos = self.position()+slen
def previousChar(self):
self.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
char = unicode(self.selection().toPlainText())
self.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
return char
def nextChar(self):
self.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
char = unicode(self.selection().toPlainText())
self.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
return char
def deleteCursorChar(self):
if self.atEnd() and self.previousChar() == g.cursorChar:
self.deletePreviousChar()
def deleteUpperEmptyLine(self):
self.movePosition(QTextCursor.Up)
self.select(QTextCursor.LineUnderCursor)
if len(self.selectedText()) == 0:
self.deleteChar()
self.movePosition(QTextCursor.End)
def insertCharacter(self, c):
self.clearSelection()
if not self.atEnd() or (self.atEnd() and self.previousChar() != g.cursorChar):
self.insertText(c)
self.underlinePreviousChar(True)
else:
self.movePosition(QTextCursor.PreviousCharacter)
self.insertText(c)
self.movePosition(QTextCursor.NextCharacter)
def underlinePreviousChar(self, underline):
self.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
cformat = self.charFormat()
cformat.setFontUnderline(underline)
self.setCharFormat(cformat)
self.movePosition(QTextCursor.NextCharacter)
def underlineNextChar(self, underline):
self.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
cformat = self.charFormat()
cformat.setFontUnderline(underline)
self.setCharFormat(cformat)
self.movePosition(QTextCursor.PreviousCharacter)
def keyBackspacePreviousCharacter(self):
if self.position() > self.lastEnterPos+len(g.cursorChar):
self.movePosition(QTextCursor.PreviousCharacter)
self.deletePreviousChar()
self.movePosition(QTextCursor.NextCharacter)
def keyDeletePreviousCharacter(self):
if not self.movePosition(QTextCursor.NextCharacter):
return
self.movePosition(QTextCursor.PreviousCharacter)
self.deletePreviousChar()
self.movePosition(QTextCursor.NextCharacter)
self.underlinePreviousChar(True)
def keySpace(self):
self.movePosition(QTextCursor.PreviousCharacter)
self.insertText(" ")
self.movePosition(QTextCursor.NextCharacter)
def moveLeft(self):
if self.position() > self.lastEnterPos+len(g.cursorChar):
self.clearSelection()
self.movePosition(QTextCursor.PreviousCharacter)
self.underlineNextChar(False)
self.underlinePreviousChar(True)
def moveRight(self):
if not self.atEnd():
self.clearSelection()
self.underlinePreviousChar(False)
self.underlineNextChar(True)
self.movePosition(QTextCursor.NextCharacter)
def moveToEnd(self):
if not self.atEnd():
char = self.previousChar()
self.deletePreviousChar()
self.insertHtml(char)
self.movePosition(QTextCursor.End)
self.insertHtml(g.cursorChar)
def printString(self, s, withPrompt, newLine):
self.movePosition(QTextCursor.End)
if withPrompt == False and newLine == False:
self.deleteCursorChar()
elif withPrompt == True and newLine == False:
self.select(QTextCursor.LineUnderCursor)
self.removeSelectedText()
self.insertHtml(g.prompt)
elif withPrompt == False and newLine == True:
self.deleteCursorChar()
self.insertHtml("<br>")
elif withPrompt == True and newLine == True:
self.deleteCursorChar()
self.insertHtml("<br>" + g.prompt)
self.insertHtml(QString.fromUtf8(s))
self.insertText(g.cursorChar)
self.underlinePreviousChar(True)
self.parent.scrollBar.setValue(self.parent.scrollBar.maximum())
def selectAll(self):
self.setPosition(self.lastEnterPos)
self.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)
def findAllMinusLastWord(self): ###TODO: This won't preserve more than one space. Fix it somewhen in the future...
self.selectAll()
text = unicode(self.selection().toPlainText()).split()[:-1]
self.movePosition(QTextCursor.End)
if len(text) == 0:
text = ""
else:
text = " ".join(text) + " "
return text
def findLastWord(self): ###TODO: Same as selectAllMinusLastWord()
self.selectAll()
lastword = unicode(self.selection().toPlainText())
return lastword.split()[-1]