More robust key press handling

This commit is contained in:
Balthasar Reuter
2018-03-29 23:00:26 +02:00
parent 3e27a46618
commit 2c279cbce2
2 changed files with 26 additions and 33 deletions

View File

@@ -8,7 +8,7 @@ from CameraOpenCV import CameraOpenCV as Camera
from multiprocessing import Pipe, Process from multiprocessing import Pipe, Process
from time import clock, sleep from time import time, sleep
class Photobooth: class Photobooth:
@@ -38,14 +38,13 @@ class Photobooth:
sleep(2) sleep(2)
if self._cap.hasPreview: if self._cap.hasPreview:
tic = clock() tic, toc = time(), 0
toc = clock() - tic
while toc < 3: while toc < 3:
send.send( Gui.PreviewState( send.send( Gui.PreviewState(
message = str(3 - int(toc)), message = str(3 - int(toc)),
picture = self._cap.getPreview() ) ) picture = self._cap.getPreview() ) )
toc = clock() - tic toc = time() - tic
else: else:
for i in range(3): for i in range(3):
send.send( Gui.PreviewState(str(i)) ) send.send( Gui.PreviewState(str(i)) )

View File

@@ -26,8 +26,8 @@ class PyQt5Gui(Gui.Gui):
receiver.notify.connect(self.handleState) receiver.notify.connect(self.handleState)
receiver.start() receiver.start()
self._transport = send
self._p.transport = send self._p.transport = send
self._p.handleEscape = self.showStart
self.showStart() self.showStart()
@@ -39,6 +39,15 @@ class PyQt5Gui(Gui.Gui):
self._p.close() self._p.close()
def handleKeypressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.showStart()
elif event.key() == Qt.Key_Space:
self._p.handleKeypressEvent = lambda event : None
self._transport.send('triggered')
def handleState(self, state): def handleState(self, state):
if not isinstance(state, Gui.GuiState): if not isinstance(state, Gui.GuiState):
@@ -60,16 +69,19 @@ class PyQt5Gui(Gui.Gui):
def showStart(self): def showStart(self):
self._p.handleKeypressEvent = lambda event : None
self._p.setCentralWidget(PyQt5Start(self)) self._p.setCentralWidget(PyQt5Start(self))
def showSettings(self): def showSettings(self):
self._p.handleKeypressEvent = lambda event : None
self._p.setCentralWidget(PyQt5Settings(self)) self._p.setCentralWidget(PyQt5Settings(self))
def showIdle(self): def showIdle(self):
self._p.handleKeypressEvent = self.handleKeypressEvent
self._p.setCentralWidget(PyQt5PictureMessage('Hit the button!', 'homer.jpg')) self._p.setCentralWidget(PyQt5PictureMessage('Hit the button!', 'homer.jpg'))
@@ -107,6 +119,8 @@ class PyQt5MainWindow(QMainWindow):
super().__init__() super().__init__()
self.handleKeypressEvent = lambda event : None
self.initUI() self.initUI()
@@ -125,24 +139,24 @@ class PyQt5MainWindow(QMainWindow):
self._transport = new_transport self._transport = new_transport
@property @property
def handleEscape(self): def handleKeypressEvent(self):
return self._handle_escape return self._handle_key
@handleEscape.setter
def handleEscape(self, func): @handleKeypressEvent.setter
def handleKeypressEvent(self, func):
if not callable(func): if not callable(func):
raise ValueError('Escape key handler must be callable') raise ValueError('Keypress event handler must be callable')
self._handle_escape = func self._handle_key = func
def initUI(self): def initUI(self):
global cfg global cfg
# self.showStart()
self.setWindowTitle('Photobooth') self.setWindowTitle('Photobooth')
if cfg.getBool('Gui', 'fullscreen'): if cfg.getBool('Gui', 'fullscreen'):
@@ -153,23 +167,6 @@ class PyQt5MainWindow(QMainWindow):
self.show() self.show()
# def showStart(self):
# content = PyQt5Start(self)
# self.setCentralWidget(content)
# def showSettings(self):
# content = PyQt5Settings(self)
# self.setCentralWidget(content)
# def showIdle(self):
# self.showMessage('Hit the button!', 'homer.jpg')
def showMessage(self, message, picture=None): def showMessage(self, message, picture=None):
content = PyQt5PictureMessage(message, picture) content = PyQt5PictureMessage(message, picture)
@@ -190,10 +187,7 @@ class PyQt5MainWindow(QMainWindow):
def keyPressEvent(self, event): def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape: self.handleKeypressEvent(event)
self.handleEscape()
elif event.key() == Qt.Key_Space:
self.transport.send('triggered')