-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
182 lines (149 loc) · 6.54 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import sys
from PySide2 import QtCore, QtWidgets, QtGui
from PySide2.QtCore import Qt
from cli import CommandHandler
from model.task_handler import TaskHandler
from model.task import Task
from model.project import Project
from uis.ui_main import UiMainWindow
from uis.ui_task_editor import UiTaskEditor
from uis.ui_project_editor import UiProjectEditor
class MainHandler:
"""Sets up the whole GUI and it's functionality"""
def __init__(self):
self.task_handler = TaskHandler()
self.main_ui = UiMainWindow(self)
self.project_editor = UiProjectEditor()
self.task_editor = UiTaskEditor(self.task_handler.get_projects())
self.setup_ui()
self.setup_ui_functions()
self.main_ui.window.show()
self.edited_task = None
self.edited_project = None
def setup_ui(self):
"""Displays all projects and tasks in the project bar and task timeline"""
for project in self.task_handler.get_projects():
self.create_project_item(project)
for task in self.task_handler.get_tasks():
self.create_task_item(task)
def setup_ui_functions(self):
"""Connects main ui buttons and editor buttons to their tasks"""
self.main_ui.window.create_task_btn.clicked.connect(self.task_editor.show_reset)
self.main_ui.window.hide_done_tasks_check.stateChanged.connect(
lambda: self.main_ui.timeline.set_done_tasks_visible(
not self.main_ui.window.hide_done_tasks_check.isChecked()))
self.main_ui.window.create_project_btn.clicked.connect(self.project_editor.show_reset)
self.project_editor.dialog.create_btn.clicked.connect(self.finish_editing_project)
self.task_editor.dialog.create_btn.clicked.connect(self.finish_editing_task)
self.main_ui.window.all_projects_btn.clicked.connect(lambda: self.main_ui.timeline.filter_project(None))
def create_task(self, task_form):
"""Creates a task with the information of the form"""
title = task_form.get_title()
description = task_form.get_description()
date = task_form.get_date()
time = task_form.get_time()
project = task_form.get_project()
new_task = Task(title, description, date, time, project)
self.create_task_item(new_task)
self.task_handler.add_task(new_task)
def create_task_item(self, new_task):
"""creates a ui item in the timeline for a task"""
self.main_ui.window.empty_timeline_label.hide()
self.main_ui.window.timeline_area.show()
self.main_ui.timeline.display_task(new_task)
def create_project(self, project_form):
"""Creates a project with the information of the form"""
name = project_form.get_project_name()
color = project_form.get_selected_color()
new_project = Project(name, color)
self.task_handler.add_project(new_project)
self.create_project_item(new_project)
def create_project_item(self, project):
"""Creates project item in the project bar and gives connects it's buttons functionality"""
project_item = self.main_ui.projects_bar.add_project(project)
project_item.content.delete_btn.clicked.connect(lambda: self.delete_project(project))
project_item.content.edit_btn.clicked.connect(lambda: self.start_editing_project(project))
project_item.connect_click(lambda: self.main_ui.timeline.filter_project(project))
def toggle_task_completion(self, task):
"""Toggles if a task id completed and saves"""
task.toggle_is_done()
task.update_listeners()
self.task_handler.save_tasks()
def start_editing_task(self, task):
"""Opens task editor and fills in details of task to edit"""
self.task_editor.reset()
self.task_editor.fill_in(task)
self.task_editor.dialog.show()
self.edited_task = task
def finish_editing_task(self):
"""Called when "Create" button in task editor is clicked.
If a task was being edited the changes will be saved, otherwise a new task will be created+displayed"""
self.task_editor.dialog.hide()
if not self.edited_task:
self.create_task(self.task_editor)
return
self.edited_task.set_title(self.task_editor.get_title())
self.edited_task.set_description(self.task_editor.get_description())
self.edited_task.set_date(self.task_editor.get_date())
self.edited_task.set_time(self.task_editor.get_time())
self.edited_task.set_project(self.task_editor.get_project())
self.edited_task.update_listeners()
self.task_handler.save_tasks()
self.edited_task = None
def delete_task(self, task):
"""Deletes a task and hides the time ine if no other tasks are left"""
self.main_ui.timeline.remove_task(task)
self.task_handler.delete_task(task)
if len(self.task_handler.get_tasks()) == 0:
self.main_ui.window.timeline_area.hide()
self.main_ui.window.empty_timeline_label.show()
def start_editing_project(self, project):
"""Opens the project editor and fills in the details of this project"""
self.project_editor.fill_in(project)
self.project_editor.dialog.show()
self.edited_project = project
def finish_editing_project(self):
"""Called when the "Create" button in the project editor is hit.
Either a new project will be created or the one that was being edited is being updated"""
try:
if not self.edited_project:
self.create_project(self.project_editor)
self.project_editor.dialog.hide()
return
self.task_handler.rename_project(self.edited_project, self.project_editor.get_project_name())
except Exception as e:
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Critical)
msg.setText(str(e))
msg.setWindowTitle("Duplicate project name")
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg.exec_()
return
self.edited_project.set_color(self.project_editor.get_selected_color())
self.edited_project.update_listeners()
self.task_handler.save_projects()
self.edited_project = None
self.project_editor.dialog.hide()
def delete_project(self, project):
self.main_ui.projects_bar.delete_project(project)
self.task_handler.delete_project(project)
if __name__ == "__main__":
# runs the command handler instead of the GUI if arguments were passed
if len(sys.argv) > 1:
CommandHandler(sys.argv)
exit(-1)
# stops this one warning message from popping up
QtCore.QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
app = QtWidgets.QApplication(sys.argv)
# loads Segoe fonts in other OS other than Windows
if sys.platform != "win32":
import os
app_dir = os.path.dirname(os.path.abspath(__file__))
QtGui.QFontDatabase.addApplicationFont(app_dir + "/res/fonts/segoeui.ttf")
QtGui.QFontDatabase.addApplicationFont(app_dir + "/res/fonts/segoeuil.ttf")
QtGui.QFontDatabase.addApplicationFont(app_dir + "/res/fonts/seguisb.ttf")
# makes application visible in MacOs
if sys.platform == "darwin":
os.environ['QT_MAC_WANTS_LAYER'] = '1'
MainHandler()
sys.exit(app.exec_())