Last active
October 20, 2021 03:13
-
-
Save StephanieSunshine/8844503f82d424d5d19f24f26fc188c6 to your computer and use it in GitHub Desktop.
dabQt -- The perfect timer for taking dabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# dabQt -- The python 3 / Qt 5 clock for dabbing | |
# | |
# | |
# usage ./dabQt <burn time> <wait time> | |
# | |
# space bar to start | |
# r to reset the timer | |
# q to quit | |
# | |
# MIT License -- Stephanie Sunshine | |
# analogclock.py from the pyqt5 examples was used as a starting point | |
# | |
from PyQt5.QtCore import QPoint, Qt, QTime, QTimer, QRect | |
from PyQt5.QtGui import QColor, QPainter, QPolygon, QFont | |
from PyQt5.QtWidgets import QApplication, QWidget | |
from sys import argv, exit | |
#from time import perf_counter | |
import time | |
# this controls the width of the seconds and the tick width | |
DEG_SPACE = 3.0 | |
class AnalogClock(QWidget): | |
burnHand = QPolygon([ | |
QPoint(7, 8), | |
QPoint(-7, 8), | |
QPoint(0, -88) | |
]) | |
waitHand = QPolygon([ | |
QPoint(7, 8), | |
QPoint(-7, 8), | |
QPoint(0, -88) | |
]) | |
burnColor = QColor(255, 64, 64, 127) | |
waitColor = QColor(255, 255, 255, 127) | |
start = False | |
burning = True | |
startTime = 0.0 | |
burnTime = -1 | |
waitTime = -1 | |
# burnTime and waitTime counters | |
bT = burnTime | |
wT = 0 | |
def __init__(self, parent=None): | |
super(AnalogClock, self).__init__(parent) | |
timer = QTimer(self) | |
timer.timeout.connect(self.update) | |
# increase this number for slower machines | |
timer.start(50) | |
self.setWindowTitle("Dab Timer") | |
self.resize(600, 600) | |
self.burnTime = int(argv[1]) | |
self.waitTime = int(argv[2]) | |
self.bT = self.burnTime | |
self.initUI() | |
def initUI(self): | |
self.setAutoFillBackground(True) | |
p = self.palette() | |
p.setColor(self.backgroundRole(), Qt.black) | |
self.setPalette(p) | |
# this is a specific special name that the library is looking for | |
def keyPressEvent(self, event): | |
# space for start | |
if event.key() == Qt.Key_Space: | |
self.start = True | |
self.startTime = time.perf_counter() | |
# r for reset | |
elif event.key() == 82: | |
self.start = False | |
self.burning = True | |
self.bT = self.burnTime | |
self.wT = 0 | |
# q for quit | |
elif event.key() == 81: | |
exit() | |
# debug | |
# else: | |
# print(event.key()) | |
# this is a specific special name that the library is looking for | |
def paintEvent(self, event): | |
if self.start: | |
dur = time.perf_counter() - self.startTime | |
#print(self.burnTime-dur) | |
#` | |
# if | |
# | |
if self.burning: | |
if self.burnTime-dur <= 0: | |
self.burning = False | |
self.bT = 0 | |
self.startTime = time.perf_counter() | |
elif self.burnTime-dur > 0: | |
self.bT = self.burnTime-dur | |
else: | |
if dur < self.waitTime: | |
self.wT = dur | |
else: | |
self.wT = self.waitTime | |
# QPainter init | |
side = min(self.width(), self.height()) | |
painter = QPainter(self) | |
painter.setRenderHint(QPainter.Antialiasing) | |
painter.translate(self.width() / 2, self.height() / 2) | |
painter.scale(side / 200.0, side / 200.0) | |
painter.setFont(QFont('Futura', 20)) | |
painter.setPen(AnalogClock.burnColor) | |
offset = round(self.height()/11) | |
painter.drawText(QRect(0-offset, 0, offset, offset), | |
Qt.AlignCenter, f"{round(self.bT)}B") | |
painter.setPen(AnalogClock.waitColor) | |
painter.drawText(QRect(0, 0, offset, offset), | |
Qt.AlignCenter, f"W{round(self.waitTime-self.wT)}") | |
# draw burnHand | |
painter.setPen(Qt.NoPen) | |
painter.setBrush(AnalogClock.burnColor) | |
painter.save() | |
painter.rotate(0-(self.bT*DEG_SPACE)) | |
painter.drawConvexPolygon(AnalogClock.burnHand) | |
painter.restore() | |
# painter.setPen(AnalogClock.burnColor) | |
# draw hash marks | |
for i in range(round(360/DEG_SPACE)): | |
painter.setPen(AnalogClock.burnColor) if i % 2 else painter.setPen( | |
AnalogClock.waitColor) | |
# 88, 0, 96, 0 | |
painter.drawLine(89, 0, 95, 0) | |
painter.rotate(DEG_SPACE) | |
# draw waitHand | |
painter.setPen(Qt.NoPen) | |
painter.setBrush(AnalogClock.waitColor) | |
painter.save() | |
painter.rotate((self.waitTime-self.wT)*DEG_SPACE) | |
painter.drawConvexPolygon(AnalogClock.waitHand) | |
painter.restore() | |
if __name__ == '__main__': | |
if len(argv) != 3: | |
print("dabQt\nusage: dabQt <Burn Time> <Wait Time>\n") | |
exit() | |
app = QApplication(argv) | |
clock = AnalogClock() | |
clock.show() | |
exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment