From 1acdb4b60a7f4d76f5c9c6c94be113abd00e68ac Mon Sep 17 00:00:00 2001 From: Balthasar Reuter Date: Fri, 11 May 2018 23:35:18 +0200 Subject: [PATCH] Rudimentary interactivity for printing and other gui-based postprocessing --- photobooth/Photobooth.py | 3 ++- photobooth/gui/GuiPostprocess.py | 27 ++++++++++++++------- photobooth/gui/GuiState.py | 27 +++++++++++++++++++-- photobooth/gui/PyQt5Gui.py | 41 +++++++++++++++++++++++--------- photobooth/main.py | 2 +- 5 files changed, 76 insertions(+), 24 deletions(-) diff --git a/photobooth/Photobooth.py b/photobooth/Photobooth.py index 3d73b9d..70e118b 100644 --- a/photobooth/Photobooth.py +++ b/photobooth/Photobooth.py @@ -141,7 +141,8 @@ class Photobooth: self.recvAck() except TeardownException: - return -1 + self.teardown() + return 123 def setCameraActive(self): diff --git a/photobooth/gui/GuiPostprocess.py b/photobooth/gui/GuiPostprocess.py index 9fd9d7b..9143406 100644 --- a/photobooth/gui/GuiPostprocess.py +++ b/photobooth/gui/GuiPostprocess.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from PyQt5.QtWidgets import QMessageBox + from .. import printer from ..util import lookup_and_import - -from PyQt5.QtWidgets import QMessageBox +from .GuiState import PrintState class GuiPostprocess: @@ -13,7 +14,7 @@ class GuiPostprocess: assert not kwargs - def do(self, parent, picture): + def get(self, picture): raise NotImplementedError() @@ -30,11 +31,19 @@ class PrintPostprocess(GuiPostprocess): self._printer = Printer(page_size, True) - def do(self, parent, picture): + def get(self, picture): - reply = QMessageBox.question(parent, 'Print?', - 'Do you want to print the picture?', - QMessageBox.Yes | QMessageBox.No, QMessageBox.No) + return PrintState(lambda : self.do(picture)) - if reply == QMessageBox.Yes: - self._printer.print(picture) + # reply = QMessageBox.question(parent, 'Print?', + # 'Do you want to print the picture?', + # QMessageBox.Yes | QMessageBox.No, QMessageBox.No) + + # if reply == QMessageBox.Yes: + # self._printer.print(picture) + + + def do(self, picture): + + print('Printing') + self._printer.print(picture) diff --git a/photobooth/gui/GuiState.py b/photobooth/gui/GuiState.py index 4b83f2d..e006cff 100644 --- a/photobooth/gui/GuiState.py +++ b/photobooth/gui/GuiState.py @@ -51,7 +51,6 @@ class IdleState(GuiState): super().__init__(**kwargs) - class PictureState(GuiState): def __init__(self, picture, **kwargs): @@ -97,7 +96,6 @@ class MessageState(GuiState): self._msg = message - class TriggerState(GuiState): def __init__(self, **kwargs): @@ -139,8 +137,33 @@ class PreviewState(PictureState): super().__init__(**kwargs) + class TeardownState(GuiState): def __init__(self, **kwargs): super().__init__(**kwargs) + + +class PrintState(GuiState): + + def __init__(self, handler, **kwargs): + + super().__init__(**kwargs) + + self.handler = handler + + + @property + def handler(self): + + return self._handler + + + @handler.setter + def handler(self, handler): + + if not callable(handler): + raise ValueError('handler must be callable') + + self._handler = handler \ No newline at end of file diff --git a/photobooth/gui/PyQt5Gui.py b/photobooth/gui/PyQt5Gui.py index 1352d94..a7d720b 100644 --- a/photobooth/gui/PyQt5Gui.py +++ b/photobooth/gui/PyQt5Gui.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import multiprocessing as mp +import queue from PIL import ImageQt @@ -10,7 +11,7 @@ from PyQt5.QtWidgets import (QApplication, QCheckBox, QComboBox, QFormLayout, QF from PyQt5.QtGui import QImage, QPainter, QPixmap import math -from PyQt5.QtGui import QBrush, QPen, QColor +from PyQt5.QtGui import QBrush, QPen, QColor, QFont from PyQt5.QtCore import QRect from .PyQt5GuiHelpers import QRoundProgressBar @@ -35,6 +36,7 @@ class PyQt5Gui(Gui): self._lastState = self.showStart self._postprocessList = [] + self._postprocessQueue = queue.Queue() if cfg.getBool('Printer', 'enable'): self._postprocessList.append( PrintPostprocess( cfg.get('Printer', 'module'), @@ -143,8 +145,6 @@ class PyQt5Gui(Gui): QTimer.singleShot(cfg.getInt('Photobooth', 'display_time') * 1000, lambda : self.postprocessPicture(state.picture)) - # self._printer.print(state.picture) - elif isinstance(state, TeardownState): self._conn.send('teardown') self.showStart() @@ -159,9 +159,28 @@ class PyQt5Gui(Gui): def postprocessPicture(self, picture): for task in self._postprocessList: - task.do(self._p, picture) - - self.sendAck() + self._postprocessQueue.put(task.get(picture)) + + self.handleQueue() + + + def handleQueue(self): + + while True: + try: + task = self._postprocessQueue.get(block = False) + except queue.Empty: + self.sendAck() + break + else: + if isinstance(task, PrintState): + reply = QMessageBox.question(self._p, 'Print picture?', + 'Do you want to print the picture?', + QMessageBox.Yes | QMessageBox.No, QMessageBox.No) + if reply == QMessageBox.Yes: + task.handler() + else: + raise ValueError('Unknown task') def showStart(self): @@ -625,7 +644,7 @@ class PyQt5WaitMessage(QFrame): def initFrame(self): - self.setStyleSheet('background-color: white;') + self.setStyleSheet('background-color: black; color: white;') def paintEvent(self, event): @@ -677,7 +696,7 @@ class PyQt5CountdownMessage(QFrame): super().__init__() - self._step_size = 100 + self._step_size = 50 self._counter = time * (1000 // self._step_size) self._action = action self._picture = None @@ -688,7 +707,7 @@ class PyQt5CountdownMessage(QFrame): def initFrame(self): - self.setStyleSheet('background-color: white;') + self.setStyleSheet('background-color: black; color: white;') def initProgressBar(self, time): @@ -744,7 +763,6 @@ class PyQt5CountdownMessage(QFrame): (self.height() - pix.height()) // 2 ) painter.drawPixmap(QPoint(*origin), pix) - # painter.drawText(event.rect(), Qt.AlignCenter, str(self.counter)) painter.end() offset = ( (self.width() - self._bar.width()) // 2, @@ -783,7 +801,7 @@ class PyQt5PictureMessage(QFrame): def initFrame(self): - self.setStyleSheet('background-color: white;') + self.setStyleSheet('background-color: black; color: white') def paintEvent(self, event): @@ -803,3 +821,4 @@ class PyQt5PictureMessage(QFrame): painter.drawText(event.rect(), Qt.AlignCenter, self._message) painter.end() + diff --git a/photobooth/main.py b/photobooth/main.py index df2e557..05b0cc1 100644 --- a/photobooth/main.py +++ b/photobooth/main.py @@ -58,7 +58,7 @@ class CameraProcess(mp.Process): continue status_code = self.run_camera() - print('Camera exit') + print('Camera exit: ', str(status_code)) sys.exit(status_code)