<filename>init.py
import sys
import PyQt5
import datetime
from audioDict import audioFiles
from PyQt5.QtWidgets import QWidget
from PyQt5 import QtMultimedia

class Clock(QWidget):

	def __init__(self):
		super(Clock, self).__init__()

		# Initialize Clock
		self.alarm = None
		self.alarmtime = datetime.time()
		#self.alarmtime = self.alarmtime.replace(hour=7, minute=0)
		self.getAlarmTime()
		self.initGui()
		self.showFullScreen()
		self.initClock()

	def getAlarmTime(self):
		f = open ("alarmtime.txt", "r+")
		for line in f:
			line = line.split(':')
			h = line[0]
			m = line[1]
			self.alarmtime = self.alarmtime.replace(hour=int(h), minute=int(m))
		f.close()
	# Function that iniitializes the GUI
	def initGui(self):

		# Set Background Color
		colors = self.palette()
		colors.setColor(self.backgroundRole(), PyQt5.QtGui.QColor('black'))
		self.setPalette(colors)

		# Set custom Fonts
		font_db = PyQt5.QtGui.QFontDatabase()
		font_id = font_db.addApplicationFont("Digital Dismay.otf")
		fontChoice = PyQt5.QtGui.QFont("Digital Dismay", 150)

		# Initialize the time
		self.clockText = PyQt5.QtWidgets.QLabel()
		self.updateTimer()
		self.clockText.setAlignment(PyQt5.QtCore.Qt.AlignCenter)
		self.clockText.setFont(fontChoice)
		self.clockText.setStyleSheet("QLabel { color : #BEAB40; }")

		# Initialize the Vbox
		self.vboxClock = PyQt5.QtWidgets.QVBoxLayout()
		self.vboxClock.addWidget(self.clockText)
		self.setLayout(self.vboxClock)

	# Function that Initializes the timer for the clock
	def initClock(self):
		self.timer = PyQt5.QtCore.QTimer()
		self.timer.timeout.connect(self.updateTimer)
		self.timer.start(1000)

	# Update the clock to current time
	def updateTimer(self):
		now = datetime.datetime.now()
		#now = now.replace(hour=8, minute=0, second=0)
		militaryh = now.strftime("%H")
		h = now.strftime("%I")
		m = now.strftime("%M")
		s = now.strftime("%S")

		self.clockText.setText(h+":"+m+":"+s)

		alarmh = self.alarmtime.strftime("%I")
		alarmm = self.alarmtime.strftime("%M")
		if h == alarmh and int(m) == int(alarmm)-1 and s == "53":
			self.setOffTheAlarm()
		elif militaryh in audioFiles:
			if m == "00" and s == "00":
				self.alarm = QtMultimedia.QSound(audioFiles[militaryh])
				self.alarm.play()
			elif m == "29" and s == "57":
				self.alarm = QtMultimedia.QSound("audio/beeping.wav")
				self.alarm.play()

	def setOffTheAlarm(self):
		self.alarm = QtMultimedia.QSound("audio/mainAlarm.wav")
		self.alarm.play()

	def mousePressEvent(self, event):

		self.press = datetime.datetime.now()

	def mouseReleaseEvent(self, event):
		self.release = datetime.datetime.now()
		
		if (self.release-self.press).total_seconds() < 1:
			if self.alarm != None:
				if self.alarm.isFinished() == False:
					self.alarm.stop()
					return	
			self.initAlarmSetterGUI()

	def alarmClicked(self, event):

		time = self.alarmSetter.clockText.text().split(':')

		h = int(time[0])
		m = int(time[1])

		self.alarmtime = self.alarmtime.replace(hour=h, minute=m)


		f = open("alarmtime.txt", 'w')
		f.write(time[0] + ":" + time[1])
		f.close()
		self.showFullScreen()
		self.alarmSetter.hide()

	def initAlarmSetterGUI(self):
		
		
		self.alarmSetter = QWidget()

		# Set Background Color
		colors = self.palette()
		colors.setColor(self.backgroundRole(), PyQt5.QtGui.QColor('black'))
		self.alarmSetter.setPalette(colors)

		# Set custom Fonts
		font_db = PyQt5.QtGui.QFontDatabase()
		font_id = font_db.addApplicationFont("Digital Dismay.otf")
		fontChoice = PyQt5.QtGui.QFont("Digital Dismay", 150)

		self.alarmSetter.clockText = PyQt5.QtWidgets.QLabel()
		self.alarmSetter.clockText.mousePressEvent = self.alarmClicked
		
		h = self.alarmtime.strftime("%H")
		m = self.alarmtime.strftime("%M")
		self.alarmSetter.clockText.setText(h+":"+m)
		self.alarmSetter.clockText.setAlignment(PyQt5.QtCore.Qt.AlignCenter)

		self.alarmSetter.clockText.setFont(fontChoice)
		self.alarmSetter.clockText.setStyleSheet("QLabel { color : #BEAB40; }")

		self.alarmSetter.vboxClock = PyQt5.QtWidgets.QVBoxLayout()
		
		self.alarmSetter.hboxClockUpButtons = PyQt5.QtWidgets.QHBoxLayout()
		self.alarmSetter.hboxClockDownButtons = PyQt5.QtWidgets.QHBoxLayout()

		self.alarmSetter.hourButtonUp = PyQt5.QtWidgets.QPushButton()
		self.alarmSetter.hourButtonUp.clicked.connect(self.hourUp)
		self.alarmSetter.hourButtonUp.setIcon(PyQt5.QtGui.QIcon(PyQt5.QtGui.QPixmap("buttons/buttonUp.png")))
		self.alarmSetter.hourButtonUp.setStyleSheet("QPushButton { height: 120px; width: 120px; background-color: black;}")

		self.alarmSetter.minuteButtonUp = PyQt5.QtWidgets.QPushButton()
		self.alarmSetter.minuteButtonUp.clicked.connect(self.minuteUp)
		self.alarmSetter.minuteButtonUp.setIcon(PyQt5.QtGui.QIcon(PyQt5.QtGui.QPixmap("buttons/buttonUp.png")))
		self.alarmSetter.minuteButtonUp.setStyleSheet("QPushButton { height: 120px; width: 120px; background-color: black;}")

		self.alarmSetter.hourButtonDown = PyQt5.QtWidgets.QPushButton()
		self.alarmSetter.hourButtonDown.clicked.connect(self.hourDown)
		self.alarmSetter.hourButtonDown.setIcon(PyQt5.QtGui.QIcon(PyQt5.QtGui.QPixmap("buttons/buttonDown.png")))
		self.alarmSetter.hourButtonDown.setStyleSheet("QPushButton { height: 120px; width: 120px; background-color: black;}")

		self.alarmSetter.minuteButtonDown = PyQt5.QtWidgets.QPushButton()
		self.alarmSetter.minuteButtonDown.clicked.connect(self.minuteDown)
		self.alarmSetter.minuteButtonDown.setIcon(PyQt5.QtGui.QIcon(PyQt5.QtGui.QPixmap("buttons/buttonDown.png")))
		self.alarmSetter.minuteButtonDown.setStyleSheet("QPushButton { height: 120px; width: 120px; background-color: black;}")

		self.alarmSetter.hboxClockUpButtons.addWidget(self.alarmSetter.hourButtonUp)
		self.alarmSetter.hboxClockUpButtons.addWidget(self.alarmSetter.minuteButtonUp)

		self.alarmSetter.vboxClock.addLayout(self.alarmSetter.hboxClockUpButtons)

		self.alarmSetter.vboxClock.addWidget(self.alarmSetter.clockText)

		self.alarmSetter.hboxClockDownButtons.addWidget(self.alarmSetter.hourButtonDown)
		self.alarmSetter.hboxClockDownButtons.addWidget(self.alarmSetter.minuteButtonDown)
		
		self.alarmSetter.vboxClock.addLayout(self.alarmSetter.hboxClockDownButtons)

		self.alarmSetter.setLayout(self.alarmSetter.vboxClock)
		self.alarmSetter.showFullScreen()
		self.hide()

	def minuteUp(self):
		time = self.alarmSetter.clockText.text().split(':')
		minute = int(time[1])
		minute += 1
		minute = minute % 60
		self.alarmSetter.clockText.setText(time[0]+":" + '%02d' % minute)
	def minuteDown(self):
		time = self.alarmSetter.clockText.text().split(':')
		minute = int(time[1])
		minute -= 1
		if minute < 0:
			minute = 59
		self.alarmSetter.clockText.setText(time[0]+":" + '%02d' % minute)
	def hourUp(self):
		time = self.alarmSetter.clockText.text().split(':')
		hour = int(time[0])
		hour += 1
		hour = hour % 24
		self.alarmSetter.clockText.setText('%02d' % hour +":" +time[1])
	def hourDown(self):
		time = self.alarmSetter.clockText.text().split(':')
		hour = int(time[0])
		hour -= 1
		if hour < 0:
			hour = 23
		self.alarmSetter.clockText.setText('%02d' % hour +":" +time[1])

def main():
	app = PyQt5.QtWidgets.QApplication(sys.argv)
	myClock = Clock()
    
	sys.exit(app.exec_())


if __name__ == '__main__':
	main()