#!/usr/bin/python

# QStopWatch -- a very simple stopwatch

# Copyright (C) 2006 Dominic Battre <dominic {at} battre {dot} de>
# Copyright (C) 2013-2021 ActivityWorkshop.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Versions:
# 2006-11-18  version 1.0   - simple but fully functional stop watch
# 2006-11-18  version 1.0.1 - fixed problem with QString vs. Python string
# Obtained from http://kde-apps.org/content/show.php/QStopWatch?content=48810
# 2013-06-19  version 1.1   - ported to python 2.7, qt4; used QLCDNumber for time display
# 2021-12-11  version 1.2   - ported to python 3, qt5

import sys
from PyQt5 import QtGui, QtCore, QtWidgets

class MainWindow(QtWidgets.QMainWindow):

	DEFAULT_WINDOW_TITLE = "Stopwatch"

	def __init__(self, *args):
		QtWidgets.QMainWindow.__init__(self)
		self.mainWidget = QtWidgets.QWidget(self)
		self.setCentralWidget(self.mainWidget)
		self.mainLayout = QtWidgets.QVBoxLayout(self.mainWidget)

		self.buttonsWidget = QtWidgets.QWidget(self.mainWidget)
		self.buttonsLayout = QtWidgets.QHBoxLayout(self.buttonsWidget)

		self.txTime = QtWidgets.QLCDNumber(self.mainWidget)
		self.txTime.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
		self.bnStart = QtWidgets.QPushButton("&Start", self.buttonsWidget)
		self.bnClear = QtWidgets.QPushButton("&Clear", self.buttonsWidget)

		self.bnStart.clicked.connect(self.slotBnStartClicked)
		self.bnClear.clicked.connect(self.slotBnClearClicked)

		self.buttonsLayout.addWidget(self.bnStart)
		self.buttonsLayout.addWidget(self.bnClear)

		self.mainLayout.addWidget(self.txTime)
		self.mainLayout.addWidget(self.buttonsWidget)

		self.counting = False
		self.msInLastLaps = 0
		self.timer = QtCore.QTimer(self)
		self.timer.timeout.connect(self.slotTimerEvent)

		self.displayTime(0, False)
		self.setWindowTitle(self.DEFAULT_WINDOW_TITLE)

		# reduce to minimum size
		self.resize(self.minimumSizeHint())

	def displayTime(self, msecs, exact):
		ms = msecs % 1000
		msecs /= 1000
		sec = msecs % 60
		msecs /= 60
		min = msecs % 60
		msecs /= 60
		hours = msecs
		if exact: 
			timestring = '%02d:%02d:%02d.%03d' % (hours, min, sec, ms)
			self.txTime.setNumDigits(12 if hours > 0 else 9)
			self.txTime.display(timestring)
			self.setWindowTitle(self.DEFAULT_WINDOW_TITLE)
		else:
			timestring = '%02d:%02d:%02d' % (hours, min, sec)
			self.txTime.setNumDigits(8 if hours > 0 else 5)
			self.txTime.display(timestring)
			self.setWindowTitle(timestring)

	def slotBnStartClicked(self):
		if ( self.counting ) :
			print("stop  ", str(QtCore.QTime.currentTime().toString()))
			self.timer.stop()
			self.msInLastLaps += self.startTime.msecsTo(QtCore.QTime.currentTime())
			self.displayTime(self.msInLastLaps, True)
			self.bnStart.setText("&Start")
		else:
			self.startTime = QtCore.QTime.currentTime()
			print("start ", str(self.startTime.toString()))
			self.timer.start(500)
			self.bnStart.setText("&Stop")
			self.slotTimerEvent()

		self.counting = not self.counting

	def slotBnClearClicked(self):
		print("clear")
		self.msInLastLaps = 0
		self.startTime = QtCore.QTime.currentTime()
		self.displayTime(0, not self.counting)

	def slotTimerEvent(self):
		self.displayTime(self.msInLastLaps + self.startTime.msecsTo(QtCore.QTime.currentTime()), False)

# Main method as the program entry point
def main(args):
	app = QtWidgets.QApplication(args)
	win = MainWindow()
	win.show()
	app.exec_()

if __name__=="__main__":
	main(sys.argv)

