From 2c279cbce2f991f1ea769c621b368972eaf6f213 Mon Sep 17 00:00:00 2001 From: Balthasar Reuter Date: Thu, 29 Mar 2018 23:00:26 +0200 Subject: [PATCH] More robust key press handling --- photobooth/Photobooth.py | 7 +++--- photobooth/PyQt5Gui.py | 52 ++++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/photobooth/Photobooth.py b/photobooth/Photobooth.py index 67c610b..bde307b 100644 --- a/photobooth/Photobooth.py +++ b/photobooth/Photobooth.py @@ -8,7 +8,7 @@ from CameraOpenCV import CameraOpenCV as Camera from multiprocessing import Pipe, Process -from time import clock, sleep +from time import time, sleep class Photobooth: @@ -38,14 +38,13 @@ class Photobooth: sleep(2) if self._cap.hasPreview: - tic = clock() - toc = clock() - tic + tic, toc = time(), 0 while toc < 3: send.send( Gui.PreviewState( message = str(3 - int(toc)), picture = self._cap.getPreview() ) ) - toc = clock() - tic + toc = time() - tic else: for i in range(3): send.send( Gui.PreviewState(str(i)) ) diff --git a/photobooth/PyQt5Gui.py b/photobooth/PyQt5Gui.py index f773018..3a02125 100644 --- a/photobooth/PyQt5Gui.py +++ b/photobooth/PyQt5Gui.py @@ -26,8 +26,8 @@ class PyQt5Gui(Gui.Gui): receiver.notify.connect(self.handleState) receiver.start() + self._transport = send self._p.transport = send - self._p.handleEscape = self.showStart self.showStart() @@ -39,6 +39,15 @@ class PyQt5Gui(Gui.Gui): 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): if not isinstance(state, Gui.GuiState): @@ -60,16 +69,19 @@ class PyQt5Gui(Gui.Gui): def showStart(self): + self._p.handleKeypressEvent = lambda event : None self._p.setCentralWidget(PyQt5Start(self)) def showSettings(self): + self._p.handleKeypressEvent = lambda event : None self._p.setCentralWidget(PyQt5Settings(self)) def showIdle(self): + self._p.handleKeypressEvent = self.handleKeypressEvent self._p.setCentralWidget(PyQt5PictureMessage('Hit the button!', 'homer.jpg')) @@ -107,6 +119,8 @@ class PyQt5MainWindow(QMainWindow): super().__init__() + self.handleKeypressEvent = lambda event : None + self.initUI() @@ -125,24 +139,24 @@ class PyQt5MainWindow(QMainWindow): self._transport = new_transport @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): - 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): global cfg - # self.showStart() self.setWindowTitle('Photobooth') if cfg.getBool('Gui', 'fullscreen'): @@ -153,23 +167,6 @@ class PyQt5MainWindow(QMainWindow): 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): content = PyQt5PictureMessage(message, picture) @@ -190,10 +187,7 @@ class PyQt5MainWindow(QMainWindow): def keyPressEvent(self, event): - if event.key() == Qt.Key_Escape: - self.handleEscape() - elif event.key() == Qt.Key_Space: - self.transport.send('triggered') + self.handleKeypressEvent(event)