-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathTimetable.py
More file actions
99 lines (90 loc) · 3.5 KB
/
Timetable.py
File metadata and controls
99 lines (90 loc) · 3.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
from PyQt6 import QtCore, QtWidgets, QtGui
from components import Settings, TableModel
import json
# Used for displaying toggable timetable
class Timetable:
def __init__(self, table, data=False):
self.table = table
header = [["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]]
with open("timeslots.json") as json_file:
timeslots = json.load(json_file)["timeslots"]
settings = Settings.getSettings()
header.append(
timeslots[settings["starting_time"] : settings["ending_time"] + 1]
)
self.data = data
if not data:
self.data = []
for i in range(settings["ending_time"] + 1 - settings["starting_time"]):
self.data.append(
[
"Available",
"Available",
"Available",
"Available",
"Available",
"Available",
]
)
self.model = TimetableModel(header, self.data)
table.setModel(self.model)
table.horizontalHeader().setSectionResizeMode(
QtWidgets.QHeaderView.ResizeMode.Fixed
)
table.verticalHeader().setSectionResizeMode(
QtWidgets.QHeaderView.ResizeMode.Fixed
)
table.clicked.connect(self.toggleCells)
table.horizontalHeader().sectionClicked.connect(self.toggleCells)
table.verticalHeader().sectionClicked.connect(self.toggleCells)
table.findChild(QtWidgets.QAbstractButton).clicked.connect(self.toggleCells)
# Toggles the availability and changes UI color to appropriate color
def toggleCells(self):
indexes = self.table.selectionModel().selectedIndexes()
for i in indexes:
value = (
"Available"
if self.data[i.row()][i.column()] == "Unavailable"
else "Unavailable"
)
if value == "Available":
self.table.setStyleSheet(
"selection-background-color: rgb(46, 204, 113); selection-color: black;"
)
else:
self.table.setStyleSheet(
"selection-background-color: rgb(231, 76, 60); selection-color: black;"
)
self.model.setData(i, value)
def getData(self):
return self.data
# Timetable model that provides color support for availability status
class TimetableModel(TableModel.TableModel):
def __init__(self, header, data):
super().__init__(header, data)
def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
elif role == QtCore.Qt.ItemDataRole.BackgroundRole:
if self.data[index.row()][index.column()] == "Available":
return QtGui.QBrush(QtGui.QColor(46, 204, 113))
else:
return QtGui.QBrush(QtGui.QColor(231, 76, 60))
elif role != QtCore.Qt.ItemDataRole.DisplayRole:
return QtCore.QVariant()
return self.data[index.row()][index.column()]
def generateRawTable():
settings = Settings.getSettings()
data = []
for i in range(settings["ending_time"] + 1 - settings["starting_time"]):
data.append(
[
"Available",
"Available",
"Available",
"Available",
"Available",
"Available",
]
)
return data