-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtodo_app.py
More file actions
256 lines (222 loc) · 11.5 KB
/
todo_app.py
File metadata and controls
256 lines (222 loc) · 11.5 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import json
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QPushButton, QCheckBox,
QMessageBox, QLabel, QProgressBar, QGroupBox
)
from PyQt5.QtCore import Qt
from task_details_dialog import TaskDetailsDialog
from file_manager import FileManager
from styles import *
class ToDoApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("To-Do List App")
self.setGeometry(100, 100, 800, 600)
self.setStyleSheet(f"background-color: {BACKGROUND_COLOR}; color: {TEXT_COLOR}; border-radius: 15px;")
layout = QVBoxLayout()
header = QLabel("My To-Do List")
header.setStyleSheet(f"font-size: {HEADER_FONT_SIZE}px; font-weight: bold; color: {PRIMARY_COLOR};")
header.setAlignment(Qt.AlignCenter)
layout.addWidget(header)
input_layout = QHBoxLayout()
self.task_input = QLineEdit(self)
self.task_input.setPlaceholderText("Enter a new task...")
self.task_input.setStyleSheet(INPUT_STYLE)
input_layout.addWidget(self.task_input)
self.add_button = QPushButton("➕ Add Task", self)
self.add_button.setStyleSheet(BUTTON_STYLE)
self.add_button.clicked.connect(self.add_task)
input_layout.addWidget(self.add_button)
layout.addLayout(input_layout)
self.task_sections = QVBoxLayout()
self.active_tasks_group = QGroupBox("Active Tasks")
self.active_tasks_group.setStyleSheet(f"color: {TEXT_COLOR}; font-size: {FONT_SIZE}px;")
self.active_tasks_layout = QVBoxLayout()
self.active_tasks_layout.setAlignment(Qt.AlignTop)
self.active_tasks_group.setLayout(self.active_tasks_layout)
self.task_sections.addWidget(self.active_tasks_group)
self.completed_tasks_group = QGroupBox("Completed Tasks")
self.completed_tasks_group.setStyleSheet(f"color: {TEXT_COLOR}; font-size: {FONT_SIZE}px;")
self.completed_tasks_layout = QVBoxLayout()
self.completed_tasks_layout.setAlignment(Qt.AlignTop)
self.completed_tasks_group.setLayout(self.completed_tasks_layout)
self.task_sections.addWidget(self.completed_tasks_group)
layout.addLayout(self.task_sections)
self.progress_bar = QProgressBar(self)
self.progress_bar.setStyleSheet(f"""
QProgressBar {{
background-color: {CARD_COLOR};
color: {TEXT_COLOR};
border-radius: 5px;
}}
QProgressBar::chunk {{
background-color: {PRIMARY_COLOR};
border-radius: 5px;
}}
""")
layout.addWidget(self.progress_bar)
button_layout = QHBoxLayout()
self.clear_completed_button = QPushButton("🗑️ Clear Completed")
self.clear_completed_button.setStyleSheet(DELETE_BUTTON_STYLE)
self.clear_completed_button.clicked.connect(self.clear_completed_tasks)
button_layout.addWidget(self.clear_completed_button)
self.file_manager_button = QPushButton("📂 File Manager")
self.file_manager_button.setStyleSheet(SECONDARY_BUTTON_STYLE)
self.file_manager_button.clicked.connect(self.open_file_manager)
button_layout.addWidget(self.file_manager_button)
layout.addLayout(button_layout)
self.load_tasks()
self.setLayout(layout)
def add_task(self):
try:
task_text = self.task_input.text().strip()
if task_text:
details_dialog = TaskDetailsDialog()
if details_dialog.exec_() == QDialog.Accepted:
details = details_dialog.get_details()
self.create_task_item(task_text, details)
self.task_input.clear()
self.save_tasks()
else:
QMessageBox.warning(self, "Warning", "Please enter a task!")
except Exception as e:
print(f"Error in add_task: {e}")
QMessageBox.critical(self, "Error", f"Failed to add task: {e}")
def create_task_item(self, task_text, details):
try:
task_widget = QWidget()
task_layout = QHBoxLayout()
checkbox = QCheckBox()
checkbox.setStyleSheet(f"color: {TEXT_COLOR};")
checkbox.stateChanged.connect(lambda: self.mark_task_completed(task_widget, checkbox))
task_layout.addWidget(checkbox)
task_label = QLineEdit(task_text)
task_label.setReadOnly(True)
task_label.setStyleSheet(f"background-color: transparent; border: none; color: {TEXT_COLOR}; font-size: {FONT_SIZE}px;")
task_layout.addWidget(task_label)
priority_label = QLabel(details["priority"])
priority_label.setStyleSheet(
f"color: {PRIMARY_COLOR if details['priority'] == 'High' else SECONDARY_COLOR if details['priority'] == 'Medium' else TEXT_COLOR}; font-size: 12px;"
)
task_layout.addWidget(priority_label)
due_date_label = QLabel(details["due_date"])
due_date_label.setStyleSheet(f"color: {SECONDARY_COLOR}; font-size: 12px;")
task_layout.addWidget(due_date_label)
tags_label = QLabel(details["tags"])
tags_label.setStyleSheet(f"color: {TEXT_COLOR}; font-size: 12px;")
task_layout.addWidget(tags_label)
delete_button = QPushButton("🗑️")
delete_button.setStyleSheet(f"background-color: transparent; border: none; color: {DANGER_COLOR}; font-size: 14px;")
delete_button.clicked.connect(lambda: self.delete_task(task_widget))
task_layout.addWidget(delete_button)
task_widget.setLayout(task_layout)
self.active_tasks_layout.addWidget(task_widget)
except Exception as e:
print(f"Error in create_task_item: {e}")
QMessageBox.critical(self, "Error", f"Failed to create task: {e}")
def mark_task_completed(self, task_widget, checkbox):
try:
if checkbox.isChecked():
self.active_tasks_layout.removeWidget(task_widget)
self.completed_tasks_layout.addWidget(task_widget)
task_widget.setStyleSheet(f"background-color: {CARD_COLOR}; color: {TEXT_COLOR}; border-radius: 10px; padding: 8px;")
self.save_tasks()
self.update_progress()
except Exception as e:
print(f"Error in mark_task_completed: {e}")
QMessageBox.critical(self, "Error", f"Failed to mark task as completed: {e}")
def delete_task(self, task_widget):
try:
self.active_tasks_layout.removeWidget(task_widget)
self.completed_tasks_layout.removeWidget(task_widget)
task_widget.deleteLater()
self.save_tasks()
self.update_progress()
except Exception as e:
print(f"Error in delete_task: {e}")
QMessageBox.critical(self, "Error", f"Failed to delete task: {e}")
def clear_completed_tasks(self):
try:
for i in reversed(range(self.completed_tasks_layout.count())):
widget = self.completed_tasks_layout.itemAt(i).widget()
if widget:
widget.deleteLater()
self.save_tasks()
self.update_progress()
except Exception as e:
print(f"Error in clear_completed_tasks: {e}")
QMessageBox.critical(self, "Error", f"Failed to clear completed tasks: {e}")
def open_file_manager(self):
try:
self.file_manager = FileManager()
self.file_manager.show()
except Exception as e:
print(f"Error in open_file_manager: {e}")
QMessageBox.critical(self, "Error", f"Failed to open file manager: {e}")
def update_progress(self):
try:
total_tasks = self.active_tasks_layout.count() + self.completed_tasks_layout.count()
completed_tasks = self.completed_tasks_layout.count()
self.progress_bar.setValue(int((completed_tasks / total_tasks) * 100) if total_tasks > 0 else 0)
except Exception as e:
print(f"Error in update_progress: {e}")
QMessageBox.critical(self, "Error", f"Failed to update progress: {e}")
def save_tasks(self):
try:
tasks = []
for i in range(self.active_tasks_layout.count()):
widget = self.active_tasks_layout.itemAt(i).widget()
if widget:
task_text = widget.findChild(QLineEdit).text()
checkbox = widget.findChild(QCheckBox)
priority_label = widget.findChild(QLabel)
due_date_label = widget.findChildren(QLabel)[1]
tags_label = widget.findChildren(QLabel)[2]
tasks.append(
f"{task_text},{checkbox.isChecked()},{priority_label.text()},{due_date_label.text()},{tags_label.text()}")
for i in range(self.completed_tasks_layout.count()):
widget = self.completed_tasks_layout.itemAt(i).widget()
if widget:
task_text = widget.findChild(QLineEdit).text()
checkbox = widget.findChild(QCheckBox)
priority_label = widget.findChild(QLabel)
due_date_label = widget.findChildren(QLabel)[1]
tags_label = widget.findChildren(QLabel)[2]
tasks.append(
f"{task_text},{checkbox.isChecked()},{priority_label.text()},{due_date_label.text()},{tags_label.text()}")
with open("todo_qt_advanced.txt", "w") as file:
file.write("\n".join(tasks))
print("Tasks saved successfully.")
except Exception as e:
print(f"Error in save_tasks: {e}")
QMessageBox.critical(self, "Error", f"Failed to save tasks: {e}")
def load_tasks(self):
try:
with open("todo_qt_advanced.txt", "r") as file:
tasks = file.read().splitlines()
for task in tasks:
if task:
parts = task.split(",")
if len(parts) < 5:
print(f"Skipping invalid task: {task}")
continue
task_text = parts[0]
completed = parts[1]
priority = parts[2]
due_date = parts[3]
tags = parts[4]
details = {"priority": priority, "due_date": due_date, "tags": tags}
self.create_task_item(task_text, details)
if completed == "True":
task_widget = self.active_tasks_layout.itemAt(self.active_tasks_layout.count() - 1).widget()
checkbox = task_widget.findChild(QCheckBox)
checkbox.setChecked(True)
self.mark_task_completed(task_widget, checkbox)
except FileNotFoundError:
print("No saved tasks found.")
except Exception as e:
print(f"Error in load_tasks: {e}")
QMessageBox.critical(self, "Error", f"Failed to load tasks: {e}")