All pastes #450295 Raw Edit

Mine

public text v1 · immutable
·published 2007-04-20 19:04 UTC
rendered paste body
#!/usr/bin/python
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
# This file is licensed under GPLv2


"""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 QobbyServerFactory(protocol.Factory):
    def __init__(self, qwin):
        self.qwin = qwin
        print "Factory created"
        
    def buildProtocol(self, addr):
        print "Protocol built"
        p = QobbyServer(self.qwin)
        p.factory = self
        return p

class QobbyServer(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 '####Someone Connected Us.'
        self.sendLine('WELCOME-QOBBY')
        self.sendLine(str(self.qwin.edit.text()))

    def lineReceived(self, line):
        if len(line)!=0:
            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 "RX:", op_number, "-", cursor_line, "-", cursor_char, "-", new_char, "-", str(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()
                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)

    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("Server. 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)

    def updateText(self, a, b):
        print a
        print b
        print "text is:", self.edit.text()

def main():
    """Run application."""
    # hook up Qt application to Twisted
    from twisted.internet import reactor
    
    win = TwistzillaWindow()
    win.show()

    factory = QobbyServerFactory(win)
    reactor.listenTCP(6666, factory)

    # 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()