diff --git a/photobooth/gui/Qt5Gui/Frames.py b/photobooth/gui/Qt5Gui/Frames.py index 38c1394..fbe8bc4 100644 --- a/photobooth/gui/Qt5Gui/Frames.py +++ b/photobooth/gui/Qt5Gui/Frames.py @@ -3,8 +3,6 @@ from os.path import expanduser -import math - from PyQt5 import QtCore from PyQt5 import QtGui from PyQt5 import QtWidgets @@ -166,23 +164,21 @@ class PictureMessage(QtWidgets.QFrame): class WaitMessage(QtWidgets.QFrame): - # With spinning wait clock, inspired by - # https://wiki.python.org/moin/PyQt/A%20full%20widget%20waiting%20indicator def __init__(self, message): super().__init__() self._message = message + self._clock = Widgets.SpinningWaitClock() def showEvent(self, event): - self._counter = 0 self.startTimer(100) def timerEvent(self, event): - self._counter += 1 + self._clock.value += 1 self.update() def _paintMessage(self, painter): @@ -191,36 +187,20 @@ class WaitMessage(QtWidgets.QFrame): f.setPixelSize(self.height() / 8) painter.setFont(f) - rect = QtCore.QRect(0, self.height() * 3 / 5, - self.width(), self.height() * 3 / 10) + rect = QtCore.QRect(0, self.height() * 3 / 5, self.width(), + self.height() * 3 / 10) painter.drawText(rect, QtCore.Qt.AlignCenter, self._message) - def _paintClock(self, painter): - - painter.setRenderHint(QtGui.QPainter.Antialiasing) - painter.setPen(QtGui.QPen(QtCore.Qt.NoPen)) - - center = (self.width() / 2, self.height() / 2) - - dots = 8 - pos = self._counter % dots - - for i in range(dots): - - distance = (pos - i) % dots - color = (distance + 1) / (dots + 1) * 255 - painter.setBrush(QtGui.QBrush(QtGui.QColor(color, color, color))) - - painter.drawEllipse( - center[0] + 180 / dots * math.cos(2 * math.pi * i / dots) - 20, - center[1] + 180 / dots * math.sin(2 * math.pi * i / dots) - 20, - 15, 15) - def paintEvent(self, event): + offset = ((self.width() - self._clock.width()) // 2, + (self.height() - self._clock.height()) // 2) + painter = QtGui.QPainter(self) self._paintMessage(painter) - self._paintClock(painter) + self._clock.render(painter, QtCore.QPoint(*offset), + self._clock.visibleRegion(), + QtWidgets.QWidget.DrawChildren) painter.end() @@ -237,15 +217,6 @@ class CountdownMessage(QtWidgets.QFrame): self._initProgressBar(time) - def _initProgressBar(self, time): - - self._bar = Widgets.RoundProgressBar(0, time, time) - self._bar.setFixedSize(200, 200) - - def _updateProgressBar(self): - - self._bar.value = self._value / (1000 // self._step_size) - @property def value(self): @@ -269,26 +240,14 @@ class CountdownMessage(QtWidgets.QFrame): self._picture = picture - def paintEvent(self, event): + def _initProgressBar(self, time): - # background image - if self.picture is not None: - painter = QtGui.QPainter(self) + self._bar = Widgets.RoundProgressBar(0, time, time) + self._bar.setFixedSize(200, 200) - pix = QtGui.QPixmap.fromImage(self.picture) - pix = pix.scaled(self.size(), QtCore.Qt.KeepAspectRatio, - QtCore.Qt.FastTransformation) - origin = ((self.width() - pix.width()) // 2, - (self.height() - pix.height()) // 2) - painter.drawPixmap(QtCore.QPoint(*origin), pix) + def _updateProgressBar(self): - painter.end() - - offset = ((self.width() - self._bar.width()) // 2, - (self.height() - self._bar.height()) // 2) - self._bar.render(self, QtCore.QPoint(*offset), - self._bar.visibleRegion(), - QtWidgets.QWidget.DrawChildren) + self._bar.value = self._value / (1000 // self._step_size) def showEvent(self, event): @@ -305,6 +264,28 @@ class CountdownMessage(QtWidgets.QFrame): self._updateProgressBar() self.update() + def paintEvent(self, event): + + painter = QtGui.QPainter(self) + + # background image + if self.picture is not None: + + pix = QtGui.QPixmap.fromImage(self.picture) + pix = pix.scaled(self.size(), QtCore.Qt.KeepAspectRatio, + QtCore.Qt.FastTransformation) + origin = ((self.width() - pix.width()) // 2, + (self.height() - pix.height()) // 2) + painter.drawPixmap(QtCore.QPoint(*origin), pix) + + offset = ((self.width() - self._bar.width()) // 2, + (self.height() - self._bar.height()) // 2) + self._bar.render(painter, QtCore.QPoint(*offset), + self._bar.visibleRegion(), + QtWidgets.QWidget.DrawChildren) + + painter.end() + class Settings(QtWidgets.QFrame): diff --git a/photobooth/gui/Qt5Gui/Widgets.py b/photobooth/gui/Qt5Gui/Widgets.py index 73d1f20..650f0bb 100644 --- a/photobooth/gui/Qt5Gui/Widgets.py +++ b/photobooth/gui/Qt5Gui/Widgets.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from math import ceil +import math from PyQt5 import Qt from PyQt5 import QtCore @@ -9,6 +9,61 @@ from PyQt5 import QtGui from PyQt5 import QtWidgets +class SpinningWaitClock(QtWidgets.QWidget): + # Spinning wait clock, inspired by + # https://wiki.python.org/moin/PyQt/A%20full%20widget%20waiting%20indicator + + def __init__(self): + + super().__init__() + + self._num_dots = 8 + self._value = 0 + + @property + def value(self): + + return self._value + + @value.setter + def value(self, value): + + if self._value != value: + self._value = value + self.update() + + def showEvent(self, event): + + self.startTimer(100) + + def timerEvent(self, event): + + self.value += 1 + + def paintEvent(self, event): + + painter = QtGui.QPainter(self) + painter.setRenderHint(QtGui.QPainter.Antialiasing) + painter.setPen(QtGui.QPen(QtCore.Qt.NoPen)) + + dots = self._num_dots + center = (self.width() / 2, self.height() / 2) + pos = self.value % dots + + for dot in range(dots): + distance = (pos - dot) % dots + offset = (180 / dots * math.cos(2 * math.pi * dot / dots) - 20, + 180 / dots * math.sin(2 * math.pi * dot / dots) - 20) + + color = (distance + 1) / (dots + 1) * 255 + painter.setBrush(QtGui.QBrush(QtGui.QColor(color, color, color))) + + painter.drawEllipse(center[0] + offset[0], center[1] + offset[1], + 15, 15) + + painter.end() + + class RoundProgressBar(QtWidgets.QWidget): # Adaptation of QRoundProgressBar from # https://sourceforge.net/projects/qroundprogressbar/ @@ -76,7 +131,7 @@ class RoundProgressBar(QtWidgets.QWidget): def _drawText(self, painter, inner_rect, inner_radius): - text = '{}'.format(ceil(self.value)) + text = '{}'.format(math.ceil(self.value)) f = self.font() f.setPixelSize(inner_radius * 0.8 / len(text))