manuskript/manuskript/ui/editors/locker.py

111 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2016-02-07 00:34:22 +13:00
# --!-- coding: utf8 --!--
from PyQt5.QtCore import pyqtSignal, QTimer
from PyQt5.QtWidgets import QWidget, qApp
from manuskript.ui.editors.locker_ui import Ui_locker
class locker(QWidget, Ui_locker):
locked = pyqtSignal()
unlocked = pyqtSignal()
lockChanged = pyqtSignal(bool)
2016-02-07 00:34:22 +13:00
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
self._btnText = None
self._words = None
self._target = None
self._blackout = []
2016-02-07 00:34:22 +13:00
self.timer = QTimer(self)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.unlock)
self.timer.stop()
self.timerSec = QTimer(self)
self.timerSec.setInterval(500)
self.timerSec.timeout.connect(self.updateBtnText)
self.timerSec.stop()
self.rbtnWordTarget.toggled.connect(self.spnWordTarget.setVisible)
self.rbtnTimeTarget.toggled.connect(self.spnTimeTarget.setVisible)
self.rbtnWordTarget.setChecked(True)
self.spnTimeTarget.setVisible(False)
2016-02-07 00:34:22 +13:00
self.btnLock.clicked.connect(self.lock)
2016-02-07 00:34:22 +13:00
def lock(self):
# Block others screens
desktop = qApp.desktop()
self._blackout.clear()
if desktop.screenCount() > 1:
for d in range(desktop.screenCount()):
if desktop.screenNumber(self) != d:
w = QWidget()
w.setStyleSheet("background: black;")
w.move(desktop.screenGeometry(d).topLeft())
w.showFullScreen()
self._blackout.append(w)
2016-02-07 00:34:22 +13:00
if self.rbtnWordTarget.isChecked():
self._target = self._words + self.spnWordTarget.value()
2016-02-07 00:34:22 +13:00
elif self.rbtnTimeTarget.isChecked():
self.timer.setInterval(self.spnTimeTarget.value() * 1000 * 60)
self.timer.start()
self.timerSec.start()
self.updateBtnText()
2016-02-07 00:34:22 +13:00
self.setEnabled(False)
self.locked.emit()
self.lockChanged.emit(True)
2016-02-07 00:34:22 +13:00
def unlock(self):
# Remove black screens
self._blackout.clear()
2016-02-07 00:34:22 +13:00
self.setEnabled(True)
self.btnLock.setText(self._btnText)
self.timer.stop()
self.timerSec.stop()
self._target = None
self.unlocked.emit()
self.lockChanged.emit(False)
2016-02-07 00:34:22 +13:00
def isLocked(self):
return not self.isEnabled()
2016-02-07 00:34:22 +13:00
def setWordCount(self, wc):
self._words = wc
if self.isLocked():
self.updateBtnText()
if self._target and self._words >= self._target:
self.unlock()
2016-02-07 00:34:22 +13:00
def updateBtnText(self):
if not self._btnText:
self._btnText = self.btnLock.text()
2016-02-07 00:34:22 +13:00
# Time locked
if self.timer.remainingTime() != -1:
t = self.timer.remainingTime()
t = int(t / 1000)
if t > 60 * 60:
text = self.tr("~{} h.").format(str(int(t / 60 / 60)))
elif t > 60 * 5:
text = self.tr("~{} mn.").format(str(int(t / 60)))
elif t > 60:
mn = int(t / 60)
sec = t - 60 * mn
text = self.tr("{}:{}").format(str(mn), str(sec))
else:
text = self.tr("{} s.").format(str(t))
2016-02-07 00:34:22 +13:00
self.btnLock.setText(self.tr("{} remaining").format(
2016-02-07 00:34:22 +13:00
text))
# Word locked
2016-02-07 00:34:22 +13:00
elif self._target is not None:
self.btnLock.setText(self.tr("{} words remaining").format(
2016-02-07 00:34:22 +13:00
self._target - self._words))