fime/main.py

108 lines
2.8 KiB
Python
Raw Normal View History

2020-02-19 22:12:56 +00:00
#!/usr/bin/env python3
import sys
import signal
from functools import partial
from PySide2 import QtCore, QtGui, QtWidgets
from data import Tasks, Log
2020-02-19 22:12:56 +00:00
from task_edit import TaskEdit
from report import Report
2020-02-19 22:12:56 +00:00
class App:
def __init__(self):
self.app = QtWidgets.QApplication(sys.argv)
self.tasks = Tasks()
self.log = Log()
2020-02-24 15:54:28 +00:00
self.active_task = self.log.last_log() or "Nothing"
2020-02-19 22:12:56 +00:00
icon = QtGui.QIcon.fromTheme("appointment-new")
self.menu = QtWidgets.QMenu()
self.tray = QtWidgets.QSystemTrayIcon()
self.tray.setIcon(icon)
self.tray.setContextMenu(self.menu)
self.tray.show()
self.tray.setToolTip("fimefracking")
self.update_tray_menu()
self.taskEdit = TaskEdit(None)
self.taskEdit.accepted.connect(self.tasks_edited)
2020-02-24 12:16:17 +00:00
self.reportDialog = Report(None)
2020-02-24 15:54:28 +00:00
self.reportDialog.accepted.connect(self.report_done)
2020-02-19 22:12:56 +00:00
@QtCore.Slot()
def tasks_edited(self):
self.tasks.tasks = self.taskEdit.tasks
self.update_tray_menu()
@QtCore.Slot()
def report_done(self):
2020-02-24 15:54:28 +00:00
self.active_task = self.log.last_log() or "Nothing"
self.update_tray_menu()
2020-02-19 22:12:56 +00:00
def change_task(self, task):
self.active_task = task
2020-02-24 15:54:28 +00:00
if task == "Nothing":
2020-02-24 15:22:41 +00:00
return
self.log.log(task)
2020-02-19 22:12:56 +00:00
self.update_tray_menu()
def update_tray_menu(self):
self.menu.clear()
tasks = list(self.tasks.tasks)
tasks.append("Pause")
if self.active_task == "Nothing":
tasks.append("Nothing")
2020-02-19 22:12:56 +00:00
for t in tasks:
a = self.menu.addAction(t)
a.triggered.connect(partial(self.change_task, t))
if t == self.active_task:
a.setIcon(QtGui.QIcon.fromTheme("go-next"))
self.menu.addSeparator()
new_action = self.menu.addAction("Edit tasks")
new_action.triggered.connect(self.edit_tasks)
report_action = self.menu.addAction("Report")
report_action.triggered.connect(self.report)
self.menu.addSeparator()
exit_action = self.menu.addAction("Close")
exit_action.triggered.connect(self.app.quit)
def sigterm_handler(self, _signo, _frame):
self.app.quit()
def run(self):
timer = QtCore.QTimer()
# interrupt event loop regularly for signal handling
timer.timeout.connect(lambda: None)
timer.start(500)
signal.signal(signal.SIGTERM, self.sigterm_handler)
signal.signal(signal.SIGINT, self.sigterm_handler)
self.app.exec_()
@QtCore.Slot()
def report(self):
2020-02-24 12:16:17 +00:00
self.reportDialog.set_data(self.log.report())
self.reportDialog.show()
2020-02-19 22:12:56 +00:00
@QtCore.Slot()
def edit_tasks(self):
self.taskEdit.tasks = self.tasks.tasks
self.taskEdit.show()
if __name__ == "__main__":
app = App()
app.run()