-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_list_sidebar.py
104 lines (84 loc) · 3.78 KB
/
task_list_sidebar.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
from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
QAbstractItemView,
QListWidget,
QTableWidgetItem,
)
class TaskListSidebar(QListWidget):
"""A sidebar widget for displaying multiple Google Task lists."""
def __init__(self, parent=None, window=None):
"""
Initialize the TaskListSidebar.
:param parent: Optional parent widget.
:param window: Reference to the main application window.
"""
super().__init__(parent)
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.itemClicked.connect(self.load_tasks_by_task_list)
self.window = window
def fetch_tasks_by_task_list(self, item):
"""
Fetch tasks for the given task list item.
:param item: A QListWidgetItem containing task list info.
:return: List of tasks from the selected task list.
"""
if isinstance(item, str):
task_list_id = item # Set the current task list ID
else:
task_list_id = item.data(Qt.UserRole)
self.current_tasklist_id = task_list_id # Set the current task list ID
# Fetch tasks for the selected task list
tasks_response = (
self.window.tasks_service.tasks()
.list(tasklist=task_list_id, maxResults=1000)
.execute()
)
tasks = tasks_response.get("items", [])
# Add task list ID to each task
for task in tasks:
task["task_list_id"] = task_list_id
return tasks
def render_tasks(self, tasks):
"""
Clear and reload the main task table with the given tasks.
:param tasks: List of task dictionaries to display.
"""
self.window.task_table.setRowCount(0)
for task in tasks:
row_position = self.window.task_table.rowCount()
self.window.task_table.insertRow(row_position)
# Title column (index 0)
title_item = QTableWidgetItem(task["title"] or "")
self.window.task_table.setItem(row_position, 0, title_item)
# Store all task data in user roles
title_item.setData(Qt.UserRole, task["id"])
title_item.setData(Qt.UserRole + 1, task.get("task_list_id"))
title_item.setData(Qt.UserRole + 2, task.get("updated", ""))
title_item.setData(Qt.UserRole + 3, task.get("notes", ""))
title_item.setData(Qt.UserRole + 4, task.get("webViewLink", ""))
title_item.setData(Qt.UserRole + 5, task.get("due", ""))
title_item.setData(Qt.UserRole + 6, task.get("completed", ""))
title_item.setData(Qt.UserRole + 7, task.get("status", ""))
# Display date column (index 1)
display_date = ""
if task.get("status") == "completed" and "completed" in task:
display_date = task["completed"].split("T")[0] # Just the date part
elif "updated" in task:
display_date = task["updated"].split("T")[0] # Just the date part
self.window.task_table.setItem(
row_position, 1, QTableWidgetItem(display_date)
)
self.window.task_table.clearSelection() # Clear table selection to hide details pane when none is selected
def load_tasks_by_task_list(self, item):
"""
Load tasks for the given item and render them.
:param item: A QListWidgetItem containing task list info.
"""
tasks = self.fetch_tasks_by_task_list(item)
self.render_tasks(tasks)
self.window.refresh_button.setEnabled(True)
def refresh_tasks(self):
"""Refresh tasks by reloading for the currently selected item."""
current_item = self.currentItem()
if current_item:
self.load_tasks_by_task_list(current_item)