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() self.recvAck()
except TeardownException: except TeardownException:
return -1 self.teardown()
return 123
def setCameraActive(self): def setCameraActive(self):

View File

@@ -1,10 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QMessageBox
from .. import printer from .. import printer
from ..util import lookup_and_import from ..util import lookup_and_import
from .GuiState import PrintState
from PyQt5.QtWidgets import QMessageBox
class GuiPostprocess: class GuiPostprocess:
@@ -13,7 +14,7 @@ class GuiPostprocess:
assert not kwargs assert not kwargs
def do(self, parent, picture): def get(self, picture):
raise NotImplementedError() raise NotImplementedError()
@@ -30,11 +31,19 @@ class PrintPostprocess(GuiPostprocess):
self._printer = Printer(page_size, True) self._printer = Printer(page_size, True)
def do(self, parent, picture): def get(self, picture):
reply = QMessageBox.question(parent, 'Print?', return PrintState(lambda : self.do(picture))
'Do you want to print the picture?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes: # reply = QMessageBox.question(parent, 'Print?',
self._printer.print(picture) # '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) super().__init__(**kwargs)
class PictureState(GuiState): class PictureState(GuiState):
def __init__(self, picture, **kwargs): def __init__(self, picture, **kwargs):
@@ -97,7 +96,6 @@ class MessageState(GuiState):
self._msg = message self._msg = message
class TriggerState(GuiState): class TriggerState(GuiState):
def __init__(self, **kwargs): def __init__(self, **kwargs):
@@ -139,8 +137,33 @@ class PreviewState(PictureState):
super().__init__(**kwargs) super().__init__(**kwargs)
class TeardownState(GuiState): class TeardownState(GuiState):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**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 -*- # -*- coding: utf-8 -*-
import multiprocessing as mp import multiprocessing as mp
import queue
from PIL import ImageQt from PIL import ImageQt
@@ -10,7 +11,7 @@ from PyQt5.QtWidgets import (QApplication, QCheckBox, QComboBox, QFormLayout, QF
from PyQt5.QtGui import QImage, QPainter, QPixmap from PyQt5.QtGui import QImage, QPainter, QPixmap
import math import math
from PyQt5.QtGui import QBrush, QPen, QColor from PyQt5.QtGui import QBrush, QPen, QColor, QFont
from PyQt5.QtCore import QRect from PyQt5.QtCore import QRect
from .PyQt5GuiHelpers import QRoundProgressBar from .PyQt5GuiHelpers import QRoundProgressBar
@@ -35,6 +36,7 @@ class PyQt5Gui(Gui):
self._lastState = self.showStart self._lastState = self.showStart
self._postprocessList = [] self._postprocessList = []
self._postprocessQueue = queue.Queue()
if cfg.getBool('Printer', 'enable'): if cfg.getBool('Printer', 'enable'):
self._postprocessList.append( PrintPostprocess( cfg.get('Printer', 'module'), self._postprocessList.append( PrintPostprocess( cfg.get('Printer', 'module'),
@@ -143,8 +145,6 @@ class PyQt5Gui(Gui):
QTimer.singleShot(cfg.getInt('Photobooth', 'display_time') * 1000, QTimer.singleShot(cfg.getInt('Photobooth', 'display_time') * 1000,
lambda : self.postprocessPicture(state.picture)) lambda : self.postprocessPicture(state.picture))
# self._printer.print(state.picture)
elif isinstance(state, TeardownState): elif isinstance(state, TeardownState):
self._conn.send('teardown') self._conn.send('teardown')
self.showStart() self.showStart()
@@ -159,9 +159,28 @@ class PyQt5Gui(Gui):
def postprocessPicture(self, picture): def postprocessPicture(self, picture):
for task in self._postprocessList: 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): def showStart(self):
@@ -625,7 +644,7 @@ class PyQt5WaitMessage(QFrame):
def initFrame(self): def initFrame(self):
self.setStyleSheet('background-color: white;') self.setStyleSheet('background-color: black; color: white;')
def paintEvent(self, event): def paintEvent(self, event):
@@ -677,7 +696,7 @@ class PyQt5CountdownMessage(QFrame):
super().__init__() super().__init__()
self._step_size = 100 self._step_size = 50
self._counter = time * (1000 // self._step_size) self._counter = time * (1000 // self._step_size)
self._action = action self._action = action
self._picture = None self._picture = None
@@ -688,7 +707,7 @@ class PyQt5CountdownMessage(QFrame):
def initFrame(self): def initFrame(self):
self.setStyleSheet('background-color: white;') self.setStyleSheet('background-color: black; color: white;')
def initProgressBar(self, time): def initProgressBar(self, time):
@@ -744,7 +763,6 @@ class PyQt5CountdownMessage(QFrame):
(self.height() - pix.height()) // 2 ) (self.height() - pix.height()) // 2 )
painter.drawPixmap(QPoint(*origin), pix) painter.drawPixmap(QPoint(*origin), pix)
# painter.drawText(event.rect(), Qt.AlignCenter, str(self.counter))
painter.end() painter.end()
offset = ( (self.width() - self._bar.width()) // 2, offset = ( (self.width() - self._bar.width()) // 2,
@@ -783,7 +801,7 @@ class PyQt5PictureMessage(QFrame):
def initFrame(self): def initFrame(self):
self.setStyleSheet('background-color: white;') self.setStyleSheet('background-color: black; color: white')
def paintEvent(self, event): def paintEvent(self, event):
@@ -803,3 +821,4 @@ class PyQt5PictureMessage(QFrame):
painter.drawText(event.rect(), Qt.AlignCenter, self._message) painter.drawText(event.rect(), Qt.AlignCenter, self._message)
painter.end() painter.end()

View File

@@ -58,7 +58,7 @@ class CameraProcess(mp.Process):
continue continue
status_code = self.run_camera() status_code = self.run_camera()
print('Camera exit') print('Camera exit: ', str(status_code))
sys.exit(status_code) sys.exit(status_code)