Files
@ d7e4d5b0d61e
Branch filter:
Location: light9/lib/qt4reactor.py
d7e4d5b0d61e
5.4 KiB
text/x-python
move animatedZoom. other viewstate api fixes.
Ignore-this: 6d1b59e21f5097c50b6c88a87d3e83da
Ignore-this: 6d1b59e21f5097c50b6c88a87d3e83da
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 | # http://twistedmatrix.com/trac/browser/sandbox/therve/qt4reactor.py
# with some fixes by drewp
# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
# See LICENSE for details.
"""
This module provides support for Twisted to interact with the PyQt mainloop.
In order to use this support, simply do the following::
| import qt4reactor
| qt4reactor.install()
Then use twisted.internet APIs as usual. The other methods here are not
intended to be called directly.
API Stability: stable
Maintainer: U{Itamar Shtull-Trauring<mailto:twisted@itamarst.org>}
Port to QT4: U{Gabe Rudy<mailto:rudy@goldenhelix.com>}
"""
__all__ = ['install']
import sys
from zope.interface import implements
from PyQt4.QtCore import QSocketNotifier, QObject, SIGNAL, QTimer
from PyQt4.QtGui import QApplication
from twisted.internet.interfaces import IReactorFDSet
from twisted.python import log
from twisted.internet.posixbase import PosixReactorBase
class TwistedSocketNotifier(QSocketNotifier):
"""
Connection between an fd event and reader/writer callbacks.
"""
def __init__(self, reactor, watcher, type):
QSocketNotifier.__init__(self, watcher.fileno(), type)
self.reactor = reactor
self.watcher = watcher
self.fn = None
if type == QSocketNotifier.Read:
self.fn = self.read
elif type == QSocketNotifier.Write:
self.fn = self.write
QObject.connect(self, SIGNAL("activated(int)"), self.fn)
def shutdown(self):
QObject.disconnect(self, SIGNAL("activated(int)"), self.fn)
self.setEnabled(0)
self.fn = self.watcher = None
def read(self, sock):
w = self.watcher
def _read():
why = None
try:
why = w.doRead()
except:
log.err()
why = sys.exc_info()[1]
if why:
self.reactor._disconnectSelectable(w, why, True)
log.callWithLogger(w, _read)
self.reactor.simulate()
def write(self, sock):
w = self.watcher
def _write():
why = None
self.setEnabled(0)
try:
why = w.doWrite()
except:
log.err()
why = sys.exc_info()[1]
if why:
self.reactor._disconnectSelectable(w, why, False)
elif self.watcher:
self.setEnabled(1)
log.callWithLogger(w, _write)
self.reactor.simulate()
class QTReactor(PosixReactorBase):
"""
Qt based reactor.
"""
implements(IReactorFDSet)
# Reference to a DelayedCall for self.crash() when the reactor is
# entered through .iterate()
_crashCall = None
_timer = None
def __init__(self, app=None):
self._reads = {}
self._writes = {}
if app is None:
app = QApplication([])
self.qApp = app
PosixReactorBase.__init__(self)
self.addSystemEventTrigger('after', 'shutdown', self.cleanup)
def addReader(self, reader):
if not reader in self._reads:
self._reads[reader] = TwistedSocketNotifier(self, reader,
QSocketNotifier.Read)
def addWriter(self, writer):
if not writer in self._writes:
self._writes[writer] = TwistedSocketNotifier(self, writer,
QSocketNotifier.Write)
def removeReader(self, reader):
if reader in self._reads:
self._reads[reader].shutdown()
del self._reads[reader]
def removeWriter(self, writer):
if writer in self._writes:
self._writes[writer].shutdown()
del self._writes[writer]
def removeAll(self):
return self._removeAll(self._reads, self._writes)
def getReaders(self):
return self._reads.keys()
def getWriters(self):
return self._writes.keys()
def simulate(self):
self._lastTimer = self._timer # put off the __del__
if self._timer is not None:
self._timer.stop()
self._timer = None
if not self.running:
self.qApp.exit()
return
self.runUntilCurrent()
if self._crashCall is not None:
self._crashCall.reset(0)
timeout = self.timeout()
if timeout is None:
timeout = 1.0
timeout = min(timeout, 0.01) * 1010
if self._timer is None:
self._timer = QTimer()
self._timer.setObjectName("simulateTimer")
QObject.connect(self._timer, SIGNAL("timeout()"), self.simulate)
self._timer.start(timeout)
def cleanup(self):
if self._timer is not None:
self._timer.stop()
self._timer = None
def iterate(self, delay=0.0):
self._crashCall = self.callLater(delay, self._crash)
self.run()
def mainLoop(self):
self.simulate()
self.qApp.exec_()
def _crash(self):
if self._crashCall is not None:
if self._crashCall.active():
self._crashCall.cancel()
self._crashCall = None
self.running = False
def install(app=None):
"""
Configure the twisted mainloop to be run inside the qt mainloop.
"""
from twisted.internet import main
reactor = QTReactor(app=app)
main.installReactor(reactor)
|