fime/main.py
2020-02-20 15:22:40 +01:00

99 lines
2.5 KiB
Python

#!/usr/bin/env python3
import sys
import signal
from functools import partial
from PySide2 import QtCore, QtGui, QtWidgets
from data import Tasks, Log
from task_edit import TaskEdit
from report import Report
class App:
def __init__(self):
self.app = QtWidgets.QApplication(sys.argv)
self.tasks = Tasks()
self.log = Log()
self.active_task = self.log.last_log() or "Nothing"
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)
self.report = Report(None)
@QtCore.Slot()
def tasks_edited(self):
self.tasks.tasks = self.taskEdit.tasks
self.update_tray_menu()
def change_task(self, task):
self.active_task = task
self.log.log(task)
self.update_tray_menu()
def update_tray_menu(self):
self.menu.clear()
tasks = list(self.tasks.tasks)
tasks.append("Nothing")
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_()
sys.exit()
@QtCore.Slot()
def report(self):
self.report.set_data(self.log.report())
self.report.show()
@QtCore.Slot()
def edit_tasks(self):
self.taskEdit.tasks = self.tasks.tasks
self.taskEdit.show()
if __name__ == "__main__":
app = App()
app.run()