def __init__(self, timer_values, parent=None): #QtGui.QWidget.__init__(self, parent) #QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint) QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint) self.ui = Ui_Form() self.ui.setupUi(self) # Initialization self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.updateTimerDisplay) self.isPaused = False self.isActive = False self.isDone = False self.isLongRest = False self.alarm_times = [] self.alarm_types = [0,1] #Globally there are 2 alarm times : type 0 - work or long rest, 1 - short rest self.working_type = -1 # -1 (or something else than 0 or 1) - undefined, 0 - working, 1 - rest self.settingsDialog = None if (len(timer_values) > 0): self.setTimer(timer_values) # self.startTimer() else: self.settings() self.blinkTimer = QtCore.QTimer(self) self.blinkTimer.timeout.connect(self.toggleTimerColor) self.color_names = [ "Normal", "Black" ] self.color_idx = 1
def __init__(self, timer_values, parent=None): #QtGui.QWidget.__init__(self, parent) QtGui.QMainWindow.__init__( self, None, QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint) self.ui = Ui_Form() self.ui.setupUi(self) # Initialization self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.updateTimerDisplay) self.isPaused = False self.alarm_times = [] self.settingsDialog = None if (len(timer_values) > 0): self.setTimer(timer_values) self.startTimer() else: self.settings() self.contextMenu = QtGui.QMenu(self) self.trayIcon = QtGui.QSystemTrayIcon(self) self.blinkTimer = QtCore.QTimer(self) self.blinkTimer.timeout.connect(self.toggleTimerColor) self.color_names = ["Normal", "Black"] self.color_idx = 1 # Setup self.createMenu() self.trayIcon.setContextMenu(self.contextMenu) self.trayIcon.setIcon(QtGui.QIcon(':Icons/bell.png')) self.trayIcon.activated.connect(self.iconActivated) # Display self.trayIcon.show()
def __init__(self, timer_values, parent=None): QtGui.QWidget.__init__(self, parent) QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint) self.ui = Ui_Form() self.ui.setupUi(self) # Initialize member variables self.color_names = [ "Normal", "Yellow" ] self.color_idx = 1 self.updateTimers(timer_values) self.cur_timer = self.timer_iter.next() # Current timer value self.snooze_time = 1 * 60 self.show() self.oneSecondCounter = 0 self.timerPause = False # Start a timer for 250ms and call showTimer() timer = QtCore.QTimer(self) timer.timeout.connect(self.showTimer) timer.start(250)
class Timer(QtGui.QMainWindow): """ The Timer class uses the QtTimer to keep count and to display the count-down timer. A systray is also implemented with the option to toggle the timer, pause/play, reset, settings and quit. """ def __init__(self, timer_values, parent=None): #QtGui.QWidget.__init__(self, parent) #QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint) QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint) self.ui = Ui_Form() self.ui.setupUi(self) # Initialization self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.updateTimerDisplay) self.isPaused = False self.isActive = False self.isDone = False self.isLongRest = False self.alarm_times = [] self.alarm_types = [0,1] #Globally there are 2 alarm times : type 0 - work or long rest, 1 - short rest self.working_type = -1 # -1 (or something else than 0 or 1) - undefined, 0 - working, 1 - rest self.settingsDialog = None if (len(timer_values) > 0): self.setTimer(timer_values) # self.startTimer() else: self.settings() self.blinkTimer = QtCore.QTimer(self) self.blinkTimer.timeout.connect(self.toggleTimerColor) self.color_names = [ "Normal", "Black" ] self.color_idx = 1 def createMenu(self): """ This menu will be used as the context menu for the systray and the timer window. """ self.toggleTimerAction = QtGui.QAction("&Toggle Timer", self, triggered=self.toggleTimer) self.pauseTimerAction = QtGui.QAction("&Pause/Play Timer", self, triggered=self.pauseTimer) self.resetTimerAction = QtGui.QAction("&Reset Timer", self, triggered=self.resetTimer) self.settingsAction = QtGui.QAction("&Settings", self, triggered=self.settings) self.quitAction = QtGui.QAction("&Quit", self, triggered=QtGui.qApp.quit) self.contextMenu.addAction(self.toggleTimerAction) self.contextMenu.addAction(self.pauseTimerAction) self.contextMenu.addAction(self.resetTimerAction) self.contextMenu.addSeparator() self.contextMenu.addAction(self.settingsAction) self.contextMenu.addSeparator() self.contextMenu.addAction(self.quitAction) def settings(self): if not self.settingsDialog: if (self.alarm_times): self.settingsDialog = SettingsDialog(self, self.alarm_times) else: self.settingsDialog = SettingsDialog(self) self.settingsDialog.show() self.connect(self.settingsDialog, QtCore.SIGNAL("Accept"),self.pullTimes) def toggleTimer(self): """ Toggles the display of the timer. """ try: if self.ui.lcdNumber.isVisible(): self.hide() else: self.show() except AttributeError: return def pauseTimer(self): if (self.isPaused == False): self.timer.stop() self.blinkTimer.stop() self.isPaused = True else: self.timer.start(1000) self.isActive = True self.isPaused = False self.ui.lcdNumber.setStyleSheet("QWidget { background-color: Normal }" ) def pullTimes(self): self.alarm_times = [int(self.settingsDialog.ui.lineEditAlarm1.text()) * MINUTE_LEN, int(self.settingsDialog.ui.lineEditAlarm2.text()) * MINUTE_LEN] self.resetTimer() def setTimer(self, timer_list): self.alarm_times = [x*MINUTE_LEN for x in timer_list] # self.alarm_types = range(len(self.alarm_times)) # self.timer_iter = cycle(self.alarm_times) # An iterator that cycles through the list self.timer_iter = cycle(self.alarm_types) # An iterator that cycles through the list # self.curTime = self.timer_iter.next() # Current timer value self.working_type = self.timer_iter.next() self.curTime = self.alarm_times[self.working_type] # Current timer value def resetTimer(self): # self.timer_iter = cycle(self.alarm_times) # self.timer_iter = cycle(self.alarm_types) # self.working_type = self.timer_iter.next() # self.curTime = self.alarm_times[self.working_type] self.startTimer() self.blinkTimer.stop() self.ui.lcdNumber.setStyleSheet("QWidget { background-color: Normal }" ) self.isActive = True def startTimer(self): self.timer.start(1000) self.isActive = True self.isDone = False def startJobTimer(self): self.working_type = 0 self.curTime = self.alarm_times[self.working_type] self.startTimer() self.blinkTimer.stop() self.ui.lcdNumber.setStyleSheet("QWidget { background-color: Normal }" ) def stopTimer(self): self.curTime = 0 text = "%d:%02d" % (self.curTime/MINUTE_LEN,self.curTime % MINUTE_LEN) self.ui.lcdNumber.display(text) self.isActive = False self.isDone = False self.isLongRest = False # If LongRest is stopped, it's over self.working_type = -1 self.timer.stop() def showTimer(self): self.show() def updateTimerDisplay(self): text = "%d:%02d" % (self.curTime/MINUTE_LEN,self.curTime % MINUTE_LEN) self.ui.lcdNumber.display(text) if self.isActive : if (self.curTime == 0) : self.timer.stop() self.isDone = True if not self.isLongRest : self.blinkTimer.start(250) else : self.isActive = False self.working_type = -1 else: self.curTime -= 1 def toggleTimerColor(self): self.color_idx = 3 - self.color_idx self.showTimer() self.ui.lcdNumber.setStyleSheet("QWidget { background-color: %s }" % self.color_names[self.color_idx - 1]) def mousePressEvent(self, event): button = event.button() if button == 1: self.dragPosition = event.globalPos() - self.frameGeometry().topLeft(); elif button == 2: self.contextMenu.exec_(QtGui.QCursor.pos()) def mouseReleaseEvent(self, event): button = event.button() if button == 1: # left click if (self.curTime == 0): # blinking timer should be closed on a left click self.blinkTimer.stop() self.isDone = False if self.working_type < 0 : self.isActive = False elif self.working_type != 0 : # we were in the rest mode, but the rest is over, so stop and do nothing self.isActive = False self.working_type = -1 else : # the job is over, so going to rest self.working_type = 1 self.isActive = True self.curTime = self.alarm_times[self.working_type] self.resetTimer() self.ui.lcdNumber.setStyleSheet("QWidget { background-color: Normal }" ) def mouseMoveEvent(self, event): if event.buttons() != QtCore.Qt.LeftButton: # not left click return # self.move(event.globalPos() - self.dragPosition) def iconActivated(self, reason): if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick): self.toggleTimer()
class AlarmTimer(QtGui.QMainWindow): def __init__(self, timer_values, parent=None): QtGui.QWidget.__init__(self, parent) QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint) self.ui = Ui_Form() self.ui.setupUi(self) # Initialize member variables self.color_names = [ "Normal", "Yellow" ] self.color_idx = 1 self.updateTimers(timer_values) self.cur_timer = self.timer_iter.next() # Current timer value self.snooze_time = 1 * 60 self.show() self.oneSecondCounter = 0 self.timerPause = False # Start a timer for 250ms and call showTimer() timer = QtCore.QTimer(self) timer.timeout.connect(self.showTimer) timer.start(250) def showTimer(self): if self.timerPause: return text = "%d:%02d" % (self.cur_timer/60,self.cur_timer % 60) self.ui.lcdNumber.display(text) if (self.cur_timer == 0): self.color_idx = 3 - self.color_idx self.show() self.setStyleSheet("QWidget { background-color: %s }" % self.color_names[self.color_idx - 1]) elif self.oneSecondCounter == 3: self.cur_timer -= 1 self.oneSecondCounter = 0 else: self.oneSecondCounter += 1 def updateTimers(self, timer_list): self.alarm_times = timer_list self.timer_iter = cycle(self.alarm_times) # An iterator that cycles through the list def pauseTimer(self): self.timerPause = not self.timerPause def resetTimer(self): # Reset the timer back to the head of the list self.timer_iter = cycle(self.alarm_times) self.cur_timer = self.timer_iter.next() def mouseReleaseEvent(self, event): button = event.button() if button == 2: self.hide() if (self.cur_timer == 0): self.cur_timer = self.snooze_time # Start the timer with snooze value if teh cur_timer has expired elif button == 1: # left click if (self.cur_timer == 0): # blinking timer should be closed on a left click self.cur_timer = self.timer_iter.next() self.setStyleSheet("QWidget { background-color: Normal }" ) def mousePressEvent(self, event): button = event.button() if button == 1: self.dragPosition = event.globalPos() - self.frameGeometry().topLeft(); def mouseMoveEvent(self, event): if event.buttons() != QtCore.Qt.LeftButton: # not left click return self.move(event.globalPos() - self.dragPosition)
class Timer(QtGui.QMainWindow): """ The Timer class uses the QtTimer to keep count and uses a frameless window to display the count-down timer. A systray is also implemented with the option to toggle the timer, pause/play, reset, settings and quit. """ def __init__(self, timer_values, parent=None): #QtGui.QWidget.__init__(self, parent) QtGui.QMainWindow.__init__( self, None, QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint) self.ui = Ui_Form() self.ui.setupUi(self) # Initialization self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.updateTimerDisplay) self.isPaused = False self.alarm_times = [] self.settingsDialog = None if (len(timer_values) > 0): self.setTimer(timer_values) self.startTimer() else: self.settings() self.contextMenu = QtGui.QMenu(self) self.trayIcon = QtGui.QSystemTrayIcon(self) self.blinkTimer = QtCore.QTimer(self) self.blinkTimer.timeout.connect(self.toggleTimerColor) self.color_names = ["Normal", "Black"] self.color_idx = 1 # Setup self.createMenu() self.trayIcon.setContextMenu(self.contextMenu) self.trayIcon.setIcon(QtGui.QIcon(':Icons/bell.png')) self.trayIcon.activated.connect(self.iconActivated) # Display self.trayIcon.show() def createMenu(self): """ This menu will be used as the context menu for the systray and the timer window. """ self.toggleTimerAction = QtGui.QAction("&Toggle Timer", self, triggered=self.toggleTimer) self.pauseTimerAction = QtGui.QAction("&Pause/Play Timer", self, triggered=self.pauseTimer) self.resetTimerAction = QtGui.QAction("&Reset Timer", self, triggered=self.resetTimer) self.settingsAction = QtGui.QAction("&Settings", self, triggered=self.settings) self.quitAction = QtGui.QAction("&Quit", self, triggered=QtGui.qApp.quit) self.contextMenu.addAction(self.toggleTimerAction) self.contextMenu.addAction(self.pauseTimerAction) self.contextMenu.addAction(self.resetTimerAction) self.contextMenu.addSeparator() self.contextMenu.addAction(self.settingsAction) self.contextMenu.addSeparator() self.contextMenu.addAction(self.quitAction) def toggleTimer(self): """ Toggles the display of the timer. """ try: if self.ui.lcdNumber.isVisible(): self.hide() else: self.show() except AttributeError: return def pauseTimer(self): if (self.isPaused == False): self.timer.stop() self.blinkTimer.stop() self.isPaused = True else: self.timer.start(1000) self.isPaused = False self.ui.lcdNumber.setStyleSheet("QWidget { background-color: Normal }") def resetTimer(self): self.timer_iter = cycle(self.alarm_times) self.curTime = self.timer_iter.next() self.startTimer() self.blinkTimer.stop() self.ui.lcdNumber.setStyleSheet("QWidget { background-color: Normal }") def settings(self): if not self.settingsDialog: if (self.alarm_times): self.settingsDialog = SettingsDialog(self, self.alarm_times) else: self.settingsDialog = SettingsDialog(self) self.settingsDialog.show() self.connect(self.settingsDialog, QtCore.SIGNAL("Accept"), self.pullTimes) def pullTimes(self): self.alarm_times = [ int(self.settingsDialog.ui.lineEditAlarm1.text()) * 60, int(self.settingsDialog.ui.lineEditAlarm2.text()) * 60 ] self.resetTimer() def setTimer(self, timer_list): self.alarm_times = [x * 60 for x in timer_list] self.timer_iter = cycle( self.alarm_times) # An iterator that cycles through the list self.curTime = self.timer_iter.next() # Current timer value def startTimer(self): self.timer.start(1000) def showTimer(self): self.show() def updateTimerDisplay(self): text = "%d:%02d" % (self.curTime / 60, self.curTime % 60) self.ui.lcdNumber.display(text) if self.curTime == 0: self.timer.stop() self.blinkTimer.start(250) else: self.curTime -= 1 def toggleTimerColor(self): self.color_idx = 3 - self.color_idx self.showTimer() self.ui.lcdNumber.setStyleSheet("QWidget { background-color: %s }" % self.color_names[self.color_idx - 1]) def mousePressEvent(self, event): button = event.button() if button == 1: self.dragPosition = event.globalPos() - self.frameGeometry( ).topLeft() elif button == 2: self.contextMenu.exec_(QtGui.QCursor.pos()) def mouseReleaseEvent(self, event): button = event.button() if button == 1: # left click if (self.curTime == 0 ): # blinking timer should be closed on a left click self.blinkTimer.stop() self.timer.start(1000) self.curTime = self.timer_iter.next() self.ui.lcdNumber.setStyleSheet( "QWidget { background-color: Normal }") def mouseMoveEvent(self, event): if event.buttons() != QtCore.Qt.LeftButton: # not left click return self.move(event.globalPos() - self.dragPosition) def iconActivated(self, reason): if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick): self.toggleTimer()