-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpet.py
More file actions
461 lines (399 loc) · 15.1 KB
/
Copy pathpet.py
File metadata and controls
461 lines (399 loc) · 15.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# pet.py
import threading
from PyQt6.QtWidgets import (
QWidget, QVBoxLayout, QPushButton, QLabel,
QFrame, QMessageBox, QProgressBar, QDialog,
QSpinBox, QHBoxLayout
)
from PyQt6.QtCore import Qt, QMetaObject, Q_ARG, pyqtSlot
from PyQt6.QtGui import QFont
from get import generate_timetable_async
from tw import generate_teacherwise_pdf
from dw import generate_daywise_pdf
# ----------------------------------------------------------------
# ATTEMPTS DIALOG
# ----------------------------------------------------------------
class AttemptsDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Generation Settings")
self.setMinimumWidth(480)
self.setWindowFlag(Qt.WindowType.WindowContextHelpButtonHint, False)
self.chosen_attempts = None
root = QVBoxLayout()
root.setSpacing(28)
root.setContentsMargins(48, 44, 48, 40)
# Title
title = QLabel("Class-wise Timetable")
title.setAlignment(Qt.AlignmentFlag.AlignCenter)
title.setFont(QFont("Arial", 18, QFont.Weight.Bold))
title.setStyleSheet("color: #ff4ecd;")
root.addWidget(title)
# Description
desc = QLabel(
"How many generation attempts should the engine run?\n\n"
"Each attempt tries a fresh random schedule and keeps\n"
"the best result found so far."
)
desc.setAlignment(Qt.AlignmentFlag.AlignCenter)
desc.setFont(QFont("Arial", 12))
desc.setStyleSheet("color: #c0c0c0; line-height: 1.6;")
desc.setWordWrap(True)
root.addWidget(desc)
# Spin box row
spin_row = QHBoxLayout()
spin_row.setSpacing(16)
spin_label = QLabel("Attempts:")
spin_label.setFont(QFont("Arial", 13))
spin_label.setStyleSheet("color: #e0e0e0;")
spin_row.addWidget(spin_label)
self.spinbox = QSpinBox()
self.spinbox.setRange(1, 9999)
self.spinbox.setValue(100)
self.spinbox.setSingleStep(10)
self.spinbox.setFont(QFont("Arial", 14))
self.spinbox.setFixedHeight(44)
self.spinbox.setStyleSheet("""
QSpinBox {
background-color: #1e1e1e;
color: white;
border: 2px solid #3a3a3a;
border-radius: 10px;
padding: 4px 12px;
}
QSpinBox:focus {
border: 2px solid #a855f7;
}
QSpinBox::up-button, QSpinBox::down-button {
width: 28px;
background-color: #2a2a2a;
border: none;
}
QSpinBox::up-button:hover, QSpinBox::down-button:hover {
background-color: #3a3a3a;
}
""")
spin_row.addWidget(self.spinbox)
root.addLayout(spin_row)
# Hint
hint = QLabel("Suggested: 100 · Higher = better results, longer wait")
hint.setAlignment(Qt.AlignmentFlag.AlignCenter)
hint.setFont(QFont("Arial", 10))
hint.setStyleSheet("color: #666666;")
root.addWidget(hint)
# Buttons
btn_row = QHBoxLayout()
btn_row.setSpacing(16)
self.btn_cancel = QPushButton("Cancel")
self.btn_cancel.setFixedHeight(46)
self.btn_cancel.setFont(QFont("Arial", 12))
self.btn_cancel.clicked.connect(self.reject)
self.btn_cancel.setStyleSheet("""
QPushButton {
background-color: #2a2a2a;
color: #aaaaaa;
border: 2px solid #3a3a3a;
border-radius: 12px;
}
QPushButton:hover {
background-color: #333333;
color: white;
}
""")
self.btn_generate = QPushButton("Generate")
self.btn_generate.setFixedHeight(46)
self.btn_generate.setFont(QFont("Arial", 12, QFont.Weight.Bold))
self.btn_generate.clicked.connect(self._confirm)
self.btn_generate.setDefault(True)
self.btn_generate.setStyleSheet("""
QPushButton {
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:1,
stop:0 #0f3d2e, stop:1 #1b2a4a
);
color: white;
border: 2px solid #2f2f2f;
border-radius: 12px;
}
QPushButton:hover {
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:1,
stop:0 #145c43, stop:1 #243b6b
);
}
""")
btn_row.addWidget(self.btn_cancel)
btn_row.addWidget(self.btn_generate)
root.addLayout(btn_row)
self.setLayout(root)
self.setStyleSheet("QDialog { background-color: #141414; }")
def _confirm(self):
self.chosen_attempts = self.spinbox.value()
self.accept()
class PDFExporterPage(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.parent_window = parent
self._generating = False
self.main_layout = QVBoxLayout()
self.main_layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.main_layout.setSpacing(30)
self.main_layout.setContentsMargins(60, 40, 60, 40)
# Title
self.title = QLabel("Excel Exporter")
self.title.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.main_layout.addWidget(self.title)
# Status label
self.status_label = QLabel("")
self.status_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.status_label.setStyleSheet("color: #a0a0a0; font-size: 16px;")
self.main_layout.addWidget(self.status_label)
# Progress bar — hidden until generation starts
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 120)
self.progress_bar.setValue(0)
self.progress_bar.setTextVisible(False)
self.progress_bar.setFixedHeight(18)
self.progress_bar.hide()
self.progress_bar.setStyleSheet("""
QProgressBar {
background-color: #2a2a2a;
border: 1px solid #3a3a3a;
border-radius: 9px;
}
QProgressBar::chunk {
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:0,
stop:0 #ff4ecd, stop:1 #a855f7
);
border-radius: 9px;
}
""")
self.main_layout.addWidget(self.progress_bar)
# Button container
self.button_box = QFrame()
self.box_layout = QVBoxLayout()
self.box_layout.setSpacing(25)
self.buttons = []
self.btn_classwise = QPushButton("Generate Class-wise Excel")
self.btn_teacherwise = QPushButton("Generate Teacher-wise Excel")
self.btn_daywise = QPushButton("Generate Day-wise Excel")
self.btn_back = QPushButton("Back to Home")
self.btn_classwise.clicked.connect(self.run_classwise_pdf)
self.btn_teacherwise.clicked.connect(self.run_teacherwise_pdf)
self.btn_daywise.clicked.connect(self.run_daywise_pdf)
self.btn_back.clicked.connect(self.go_back)
self.buttons.extend([
self.btn_classwise,
self.btn_teacherwise,
self.btn_daywise,
self.btn_back
])
for btn in self.buttons:
self.box_layout.addWidget(btn)
self.button_box.setLayout(self.box_layout)
self.main_layout.addWidget(self.button_box)
self.setLayout(self.main_layout)
self.apply_base_style()
self.dynamic_scaling()
# ---------------- Styles ----------------
def apply_base_style(self):
self.setStyleSheet("""
QWidget {
background-color: #121212;
}
QFrame {
background-color: #1c1c1c;
border: 3px solid #2a2a2a;
border-radius: 20px;
}
QPushButton {
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:1,
stop:0 #0f3d2e, stop:1 #1b2a4a
);
color: white;
border: 2px solid #2f2f2f;
border-radius: 15px;
}
QPushButton:hover {
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:1,
stop:0 #145c43, stop:1 #243b6b
);
}
QPushButton:disabled {
background-color: #2a2a2a;
color: #555555;
border: 2px solid #1a1a1a;
}
""")
# ---------------- Dynamic scaling ----------------
def dynamic_scaling(self):
if not hasattr(self, "title"):
return
width = self.width()
title_size = max(40, width // 20)
self.title.setFont(QFont("Arial", title_size, QFont.Weight.Bold))
self.title.setStyleSheet("""
QLabel {
color: qlineargradient(
x1:0, y1:0, x2:1, y2:0,
stop:0 #ff4ecd, stop:1 #a855f7
);
margin-bottom: 50px;
}
""")
button_font_size = max(18, width // 60)
padding = max(15, width // 120)
for btn in self.buttons:
btn.setFont(QFont("Arial", button_font_size))
btn.setStyleSheet(f"""
QPushButton {{
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:1,
stop:0 #0f3d2e, stop:1 #1b2a4a
);
color: white;
border: 2px solid #2f2f2f;
border-radius: 20px;
padding: {padding}px;
}}
QPushButton:hover {{
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:1,
stop:0 #145c43, stop:1 #243b6b
);
}}
QPushButton:disabled {{
background-color: #2a2a2a;
color: #555555;
border: 2px solid #1a1a1a;
}}
""")
def resizeEvent(self, event):
self.dynamic_scaling()
super().resizeEvent(event)
# ---------------- Busy helpers ----------------
def _set_busy(self, status_text, total=120):
self._generating = True
self.status_label.setText(status_text)
self.progress_bar.setRange(0, total)
self.progress_bar.setValue(0)
self.progress_bar.show()
for btn in self.buttons:
btn.setEnabled(False)
def _set_idle(self):
self._generating = False
self.status_label.setText("")
self.progress_bar.hide()
self.progress_bar.setValue(0)
for btn in self.buttons:
btn.setEnabled(True)
self.dynamic_scaling()
# ---------------- Qt slots (always called on main thread) ----------------
@pyqtSlot(int, int, int)
def _on_progress(self, attempt, total, best_empty):
self.progress_bar.setRange(0, total)
self.progress_bar.setValue(attempt)
self.status_label.setText(
f"Attempt {attempt} / {total} — best empty slots so far: {best_empty}"
)
@pyqtSlot(str)
def _on_generation_done(self, message):
self._set_idle()
QMessageBox.information(self, "Done", message)
@pyqtSlot(str)
def _on_generation_error(self, error_text):
self._set_idle()
QMessageBox.critical(self, "Error", error_text)
# ---------------- Button actions ----------------
def run_classwise_pdf(self):
if self._generating:
return
# Show the attempts dialog first
dialog = AttemptsDialog(self)
if dialog.exec() != QDialog.DialogCode.Accepted:
return
max_attempts = dialog.chosen_attempts
self._set_busy(
f"Generating class-wise timetable… please wait.",
total=max_attempts
)
def on_progress(attempt, total, best_empty):
QMetaObject.invokeMethod(
self, "_on_progress",
Qt.ConnectionType.QueuedConnection,
Q_ARG(int, attempt),
Q_ARG(int, total),
Q_ARG(int, best_empty),
)
def on_done(msg):
QMetaObject.invokeMethod(
self, "_on_generation_done",
Qt.ConnectionType.QueuedConnection,
Q_ARG(str, str(msg))
)
def on_error(err):
QMetaObject.invokeMethod(
self, "_on_generation_error",
Qt.ConnectionType.QueuedConnection,
Q_ARG(str, f"Error generating class-wise Excel:\n{err}")
)
generate_timetable_async(
on_progress=on_progress,
on_done=on_done,
on_error=on_error,
max_attempts=max_attempts
)
def run_teacherwise_pdf(self):
if self._generating:
return
self._set_busy("Generating teacher-wise timetable… please wait.", total=0)
# total=0 → indeterminate progress bar (pulse effect not in Qt natively,
# but setting range 0,0 shows a "busy" bar on most platforms)
self.progress_bar.setRange(0, 0)
def _worker():
try:
msg = generate_teacherwise_pdf()
QMetaObject.invokeMethod(
self, "_on_generation_done",
Qt.ConnectionType.QueuedConnection,
Q_ARG(str, str(msg))
)
except Exception as exc:
QMetaObject.invokeMethod(
self, "_on_generation_error",
Qt.ConnectionType.QueuedConnection,
Q_ARG(str, f"Error generating teacher-wise PDF:\n{exc}")
)
threading.Thread(target=_worker, daemon=True).start()
def run_daywise_pdf(self):
if self._generating:
return
self._set_busy("Generating day-wise timetable… please wait.", total=0)
self.progress_bar.setRange(0, 0)
def _worker():
try:
msg = generate_daywise_pdf()
QMetaObject.invokeMethod(
self, "_on_generation_done",
Qt.ConnectionType.QueuedConnection,
Q_ARG(str, str(msg))
)
except Exception as exc:
QMetaObject.invokeMethod(
self, "_on_generation_error",
Qt.ConnectionType.QueuedConnection,
Q_ARG(str, f"Error generating day-wise Excel:\n{exc}")
)
threading.Thread(target=_worker, daemon=True).start()
def go_back(self):
if self._generating:
QMessageBox.warning(
self, "Busy",
"Please wait for the current generation to finish before going back."
)
return
if self.parent_window:
self.parent_window.go_home()