Rudimentary interactivity for printing and other gui-based postprocessing

This commit is contained in:
Balthasar Reuter
2018-05-11 23:35:18 +02:00
parent 62b167ad83
commit 1acdb4b60a
5 changed files with 76 additions and 24 deletions

View File

@@ -141,7 +141,8 @@ class Photobooth:
self.recvAck()
except TeardownException:
return -1
self.teardown()
return 123
def setCameraActive(self):

View File

@@ -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)

View File

@@ -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

View File

@@ -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._postprocessQueue.put(task.get(picture))
self.sendAck()
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()

View File

@@ -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)