-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner_gui.py
executable file
·213 lines (181 loc) · 7.1 KB
/
planner_gui.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Small task managment program
:wq!
"""
import sys
import json
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton
from PyQt5.QtWidgets import QListWidget, QMessageBox, QDateEdit, QListWidgetItem, QLineEdit
from PyQt5.QtGui import QColor
from PyQt5.QtCore import QDate
class ToDoList:
"""
A class to manage a list of tasks.
Attributes:
tasks (list): A list of tasks where each task is represented by a dictionary.
"""
def __init__(self):
"""
Initializes an empty list of tasks.
"""
self.tasks = []
def add_task(self, title, description="", due_date=None):
"""
Adds a new task to the list.
Args:
title (str): The title of the task.
description (str, optional): The description of the task. Defaults to an empty string.
due_date (str, optional): The due date of the task in 'yyyy-MM-dd' format. Defaults to None.
"""
self.tasks.append({"title": title, "description": description, "completed": False, "due_date": due_date})
def view_tasks(self):
"""
Returns the list of tasks.
Returns:
list: The list of tasks.
"""
return self.tasks
def mark_completed(self, task_index):
"""
Marks a task as completed based on its index.
Args:
task_index (int): The index of the task to mark as completed.
"""
if 0 <= task_index < len(self.tasks):
self.tasks[task_index]["completed"] = True
def delete_task(self, task_index):
"""
Deletes a task from the list based on its index.
Args:
task_index (int): The index of the task to delete.
"""
if 0 <= task_index < len(self.tasks):
self.tasks.pop(task_index)
def save_tasks(self, filename="tasks.json"):
"""
Saves the current list of tasks to a JSON file.
Args:
filename (str, optional): The name of the file to save tasks to. Defaults to 'tasks.json'.
"""
with open(filename, "w", encoding="utf-8") as file:
json.dump(self.tasks, file)
def load_tasks(self, filename="tasks.json"):
"""
Loads tasks from a JSON file.
Args:
filename (str, optional): The name of the file to load tasks from. Defaults to 'tasks.json'.
"""
try:
with open(filename, "r", encoding="utf-8") as file:
self.tasks = json.load(file)
except FileNotFoundError:
self.tasks = []
class ToDoApp(QWidget):
"""
A PyQt5 widget for managing and displaying tasks in a to-do list application.
Attributes:
todo_list (ToDoList): An instance of the ToDoList class.
task_input (QLineEdit): Input field for task title.
desc_input (QLineEdit): Input field for task description.
date_input (QDateEdit): Input field for task due date.
task_list (QListWidget): Widget to display the list of tasks.
"""
def __init__(self):
"""
Initializes the application, loads tasks, and sets up the user interface.
"""
super().__init__()
self.todo_list = ToDoList()
self.todo_list.load_tasks()
self.init_UI()
def init_UI(self):
"""
Sets up the user interface including input fields, buttons, and layout.
"""
self.setWindowTitle('To-Do List App')
self.setGeometry(100, 100, 400, 300)
layout = QVBoxLayout()
self.task_input = QLineEdit(self)
self.task_input.setPlaceholderText('Enter task title')
layout.addWidget(self.task_input)
self.desc_input = QLineEdit(self)
self.desc_input.setPlaceholderText('Enter task description (optional)')
layout.addWidget(self.desc_input)
self.date_input = QDateEdit(self)
self.date_input.setCalendarPopup(True)
self.date_input.setDate(QDate.currentDate())
layout.addWidget(self.date_input)
add_button = QPushButton('Add Task', self)
add_button.clicked.connect(self.add_task)
layout.addWidget(add_button)
self.task_list = QListWidget(self)
layout.addWidget(self.task_list)
button_layout = QHBoxLayout()
complete_button = QPushButton('Mark Completed', self)
complete_button.clicked.connect(self.mark_completed)
button_layout.addWidget(complete_button)
delete_button = QPushButton('Delete Task', self)
delete_button.clicked.connect(self.delete_task)
button_layout.addWidget(delete_button)
layout.addLayout(button_layout)
self.setLayout(layout)
self.update_task_list()
def add_task(self):
"""
Adds a new task to the list with the provided title, description, and due date.
Clears the input fields and saves the updated task list.
"""
title = self.task_input.text()
description = self.desc_input.text()
due_date = self.date_input.date().toString("yyyy-MM-dd")
if title:
self.todo_list.add_task(title, description, due_date)
self.update_task_list()
self.task_input.clear()
self.desc_input.clear()
self.todo_list.save_tasks()
else:
QMessageBox.warning(self, 'Error', 'Task title cannot be empty')
def update_task_list(self):
"""
Updates the displayed list of tasks based on the current task list.
Tasks that are not completed are displayed in red.
"""
self.task_list.clear()
for idx, task in enumerate(self.todo_list.view_tasks()):
status = "Done" if task["completed"] else "Not Done"
due_date = task.get("due_date", "No due date")
item_text = f"{idx + 1}. {task['title']} - {status}\n {task['description']}\n Due: {due_date}"
item = QListWidgetItem(item_text)
if not task["completed"]:
item.setForeground(QColor('red'))
self.task_list.addItem(item)
def mark_completed(self):
"""
Marks the selected task as completed and updates the task list.
Saves the updated task list to the file.
"""
selected_items = self.task_list.selectedItems()
if selected_items:
selected_item = selected_items[0]
task_index = self.task_list.row(selected_item)
self.todo_list.mark_completed(task_index)
self.update_task_list()
self.todo_list.save_tasks()
def delete_task(self):
"""
Deletes the selected task from the list and updates the task list.
Saves the updated task list to the file.
"""
selected_items = self.task_list.selectedItems()
if selected_items:
selected_item = selected_items[0]
task_index = self.task_list.row(selected_item)
self.todo_list.delete_task(task_index)
self.update_task_list()
self.todo_list.save_tasks()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ToDoApp()
ex.show()
sys.exit(app.exec_())