rendered paste body#!/usr/bin/python
"""Qt shared textpad.
synchronizes the text betweens server and clients.
"""
import sys, urlparse
from qt import *
from twisted.protocols.basic import LineReceiver
from twisted.internet import protocol
import qtreactor
from sharedtextCommon import *
app = QApplication([])
qtreactor.install(app)
class QobbyClientFactory(protocol.ClientFactory):
def __init__(self, qwin):
self.qwin = qwin
print "Client factory created"
def buildProtocol(self, addr):
print "Client Protocol built"
p = QobbyClient(self.qwin)
p.factory = self
return p
class QobbyClient(LineReceiver):
def __init__(self, qwin):
print "Protocol __init__ed"
self.qwin = qwin
app.connect(self.qwin.edit, PYSIGNAL("Changed"), self.keyPress)
self.op_number = 0
def connectionMade(self):
print 'Connected to server'
def lineReceived(self, line):
if len(line)!=0:
if self.connected:
if line.count('~') == 3:
op_number, cursor_line, cursor_char, new_char = line.split("~")
op_number = int(op_number)
cursor_line = int(cursor_line)
cursor_char = int(cursor_char)
new_char = chr(int(new_char))
print "Received:", op_number, cursor_line, cursor_char, new_char
cursor_line_old, cursor_char_old = self.qwin.edit. getCursorPosition()
if new_char == '\x08': # backspace
self.qwin.edit.setCursorPosition(cursor_line, cursor_char)
self.qwin.edit.delChar()
elif new_char == '\x7f': # del
self.qwin.edit.setCursorPosition(cursor_line, cursor_char)
self.qwin.edit.delChar()
elif new_char == '\r': # CRl
self.qwin.edit.setCursorPosition(cursor_line, cursor_char)
self.qwin.edit.insertParagraph("",cursor_line)
else:
self.qwin.edit.setCursorPosition(cursor_line, cursor_char-1)
self.qwin.edit.insert(new_char)
self.qwin.edit.setCursorPosition(cursor_line_old, cursor_char_old)
elif line == "WELCOME-QOBBY":
self.connected = True
def keyPress(self, (cursor_line, cursor_char), new_char):
#Command format:
#OperationNumber~CursorLine~CursorChar~Chars
self.sendLine(str(self.op_number) + '~' + str(cursor_line) + '~' +\
str(cursor_char) + '~' + str(new_char))
class TwistzillaWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.setCaption("Qobby :-)")
vbox = QVBox(self)
vbox.setMargin(2)
vbox.setSpacing(3)
hbox = QHBox(vbox)
label = QLabel("Client. Write here: ", hbox)
self.edit = MyQTextEdit(vbox)
#self.edit.setEdited(0)
#self.connect(self.edit, SIGNAL('textChanged()'), self.updateText)
self.connect(self.edit, PYSIGNAL('Changed'), self.updateText)
self.setCentralWidget(vbox)
self.connectToServer("127.0.0.1",6666)
def updateText(self, a, b):
print a
print b
print "text is:", self.edit.text()
def connectToServer(self, host, port):
print 'Connecting to:', host, port
from twisted.internet import reactor
factory = QobbyClientFactory(self)
reactor.connectTCP(host, port, factory)
def main():
"""Run application."""
# hook up Qt application to Twisted
from twisted.internet import reactor
win = TwistzillaWindow()
win.show()
# make sure stopping twisted event also shuts down QT
reactor.addSystemEventTrigger('after', 'shutdown', app.quit )
# shutdown twisted when window is closed
app.connect(app, SIGNAL("lastWindowClosed()"), reactor.stop)
reactor.run()
if __name__ == '__main__':
main()