Last active
November 20, 2018 22:21
-
-
Save tos-kamiya/c2abe44fe47b045b56e0f87dc013cfc5 to your computer and use it in GitHub Desktop.
PyQt analog wall-clock
This file contains 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 | |
# forked from https://github.com/baoboa/pyqt5/blob/master/examples/widgets/analogclock.py | |
############################################################################# | |
## | |
## Copyright (C) 2013 Riverbank Computing Limited. | |
## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | |
## All rights reserved. | |
## | |
## This file is part of the examples of PyQt. | |
## | |
## $QT_BEGIN_LICENSE:BSD$ | |
## You may use this file under the terms of the BSD license as follows: | |
## | |
## "Redistribution and use in source and binary forms, with or without | |
## modification, are permitted provided that the following conditions are | |
## met: | |
## * Redistributions of source code must retain the above copyright | |
## notice, this list of conditions and the following disclaimer. | |
## * Redistributions in binary form must reproduce the above copyright | |
## notice, this list of conditions and the following disclaimer in | |
## the documentation and/or other materials provided with the | |
## distribution. | |
## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | |
## the names of its contributors may be used to endorse or promote | |
## products derived from this software without specific prior written | |
## permission. | |
## | |
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | |
## $QT_END_LICENSE$ | |
## | |
############################################################################# | |
import math | |
from PyQt5.QtCore import QPoint, Qt, QDateTime, QTime, QTimer, QSettings, QRect, QRectF | |
from PyQt5.QtCore import QCoreApplication | |
from PyQt5.QtGui import QColor, QPainter, QPolygon, QIcon, QFont, QPen, QBrush, QPainterPath | |
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget | |
class PyAnalogClock(QMainWindow): | |
hourHand = QPolygon([ | |
QPoint(4, 8), | |
QPoint(-4, 8), | |
QPoint(-4, -40), | |
QPoint(4, -40) | |
]) | |
minuteHand = QPolygon([ | |
QPoint(3, 8), | |
QPoint(-2, 8), | |
QPoint(-2, -70), | |
QPoint(3, -70) | |
]) | |
hourColorPm = QColor(0x94, 0x48, 0x48, 214) | |
hourColorAm = QColor(0x48, 0x94, 0x94, 214) | |
minuteColor = QColor(57, 57, 57, 188) | |
whiteShadowColor = QColor(255, 255, 255, 96) | |
smokeBackgroundColor = QColor(255, 255, 255, 32) | |
rubyColor = QColor(255, 255, 255, 75) | |
textColor = QColor(34, 34, 34, 188) | |
textPanelColor = QColor(255, 255, 255, 121) | |
def setShowFrame(self, showFrame): | |
self.showFrame = showFrame | |
flags = self.windowFlags() | |
if not showFrame: | |
flags |= Qt.WindowStaysOnBottomHint | Qt.FramelessWindowHint | |
else: | |
flags &= ~(Qt.WindowStaysOnBottomHint | Qt.FramelessWindowHint) | |
self.setWindowFlags(flags) | |
def checkUpdate(self): | |
time = QTime.currentTime() | |
if time.second() % 10 == 0: | |
self.update() | |
def rotatedPoint(self, x, y, degr): | |
theta = degr * math.pi / 180 | |
s = math.sin(theta) | |
c = math.cos(theta) | |
return x * c - y * s, x * s + y * c | |
def closeEvent(self, event): | |
geometry = self.saveGeometry() | |
self.settings.setValue('geometry', geometry) | |
self.settings.sync() | |
super(PyAnalogClock, self).closeEvent(event) | |
def mouseDoubleClickEvent(self, event): | |
geometry = self.saveGeometry() | |
self.settings.setValue('geometry', geometry) | |
self.hide() | |
self.setShowFrame(not self.showFrame) | |
self.show() | |
self.restoreGeometry(geometry) | |
def __init__(self, parent=None, showFrame=False, windowSize=None): | |
super(PyAnalogClock, self).__init__(parent) | |
timer = QTimer(self) | |
timer.timeout.connect(self.checkUpdate) | |
timer.start(1000) | |
appIcon = QIcon.fromTheme("applications-accessories") | |
self.setWindowIcon(appIcon) | |
self.setAttribute(Qt.WA_TranslucentBackground) | |
self.settings = QSettings('ToshihiroKamiya', 'Analog Clock') | |
if windowSize is None: | |
geometry = self.settings.value('geometry', None) | |
if geometry is not None: | |
self.restoreGeometry(geometry) | |
else: | |
windowSize = 100 | |
self.resize(windowSize, windowSize) | |
else: | |
if windowSize < 100: | |
windowSize = 100 | |
self.resize(windowSize, windowSize) | |
self.setShowFrame(not not showFrame) | |
self.setWindowTitle("Analog Clock") | |
font = QFont() | |
font.setStyleHint(QFont.SansSerif) | |
font.setFamily('monospace') | |
font.setPointSize(12) | |
self.font = font | |
font = QFont(font) | |
font.setPointSize(13) | |
self.rubyFont = font | |
def paintEvent(self, event): | |
side = min(self.width(), self.height()) | |
timeDate = QDateTime.currentDateTime() | |
timeDateStr = timeDate.toString("HH:mm\nMM/dd") | |
time = timeDate.time() | |
isAm = time.hour() < 12 | |
hourColor = self.hourColorAm if isAm else self.hourColorPm | |
whiteShadowPen = QPen(self.whiteShadowColor) | |
whiteShadowPen.setJoinStyle(Qt.MiterJoin) | |
whiteShadowPen.setWidthF(0.9) | |
y0 = -90 if 15 <= time.minute() < 45 else 20 | |
x0 = -90 if 0 <= time.hour() % 12 < 6 else 20 | |
textPanelRect = QRectF(x0, y0, 69, 69) | |
painter = QPainter() | |
painter.begin(self) | |
painter.setRenderHint(QPainter.Antialiasing) | |
painter.translate(self.width() / 2, self.height() / 2) | |
painter.scale(side / 200.0, side / 200.0) | |
# draw clock frame | |
painter.setClipping(True) | |
p = QPainterPath() | |
p.addRect(QRectF(-100, -100, 200, 200)) | |
p2 = QPainterPath() | |
p2.addRect(QRectF(textPanelRect)) | |
p = p.subtracted(p2) | |
painter.setClipPath(p) | |
painter.setPen(whiteShadowPen) | |
painter.setBrush(QBrush(self.smokeBackgroundColor)) | |
painter.drawEllipse(QPoint(0, 0), 99, 99) | |
painter.setPen(whiteShadowPen) | |
painter.setFont(self.rubyFont) | |
painter.setBrush(QBrush(hourColor)) | |
for i in range(0, 12): | |
x, y = self.rotatedPoint(0, -92, i * 360/12) | |
painter.drawEllipse(x - 3, y - 3, 6, 6) | |
painter.setPen(self.rubyColor) | |
for i in range(0, 12): | |
x, y = self.rotatedPoint(0, -76, i * 360/12) | |
painter.drawText(QRect(x - 10, y - 10, 20, 20), Qt.AlignCenter, "%d" % (i if isAm else i + 12)) | |
painter.setPen(whiteShadowPen) | |
painter.setBrush(QBrush(self.minuteColor)) | |
for j in range(0, 60): | |
if j % 5 != 0: | |
x, y = self.rotatedPoint(0, -92, j * 360/60) | |
painter.drawEllipse(x - 1, y - 1, 2, 2) | |
painter.setClipping(False) | |
# draw digital clock panel | |
painter.setPen(whiteShadowPen) | |
painter.setBrush(QBrush(self.textPanelColor)) | |
painter.drawRect(textPanelRect) | |
texts = timeDateStr.split('\n') | |
painter.setFont(self.font) | |
painter.setPen(self.textColor) | |
h2 = textPanelRect.height() / 2 | |
rect = QRect(textPanelRect.left(), textPanelRect.top() + 5, textPanelRect.width(), h2-5) | |
painter.drawText(rect, Qt.AlignCenter, texts[0]) | |
rect = QRect(textPanelRect.left(), textPanelRect.top() + h2, textPanelRect.width(), h2-5) | |
painter.drawText(rect, Qt.AlignCenter, texts[1]) | |
# draw hands | |
painter.setPen(whiteShadowPen) | |
painter.setBrush(QBrush(hourColor)) | |
painter.save() | |
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) | |
painter.drawConvexPolygon(self.hourHand) | |
painter.restore() | |
painter.setPen(whiteShadowPen) | |
painter.setBrush(QBrush(self.minuteColor)) | |
painter.save() | |
painter.rotate(6.0 * (time.minute() + time.second() / 60.0)) | |
painter.drawConvexPolygon(self.minuteHand) | |
painter.restore() | |
painter.end() | |
if __name__ == "__main__": | |
import sys | |
__doc__ = """Show wall clock. | |
Usage: {argv0} [Options] | |
Options: | |
-f Show window frame. | |
-s SIZE Set window size. | |
""".format(argv0=sys.argv[0]) | |
optionShowFrame = False | |
optionWindowSize = None | |
argv = sys.argv[1:] | |
while argv: | |
if argv[0] == '-f': | |
optionShowFrame = True | |
elif argv[0].startswith('-s'): | |
if len(argv[0]) > 2: | |
s = int(argv[0][2:]) | |
else: | |
s = int(argv[1]) | |
del argv[0] | |
optionWindowSize = s | |
elif argv[0] == '-h': | |
print(__doc__) | |
sys.exit(0) | |
else: | |
sys.exit("error: too many arguments / unknown option: %s" % argv[0]) | |
del argv[0] | |
argv.insert(0, sys.argv[0]) | |
app = QApplication(argv) | |
clock = PyAnalogClock(showFrame=optionShowFrame, windowSize=optionWindowSize) | |
clock.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment