-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_window.py
More file actions
549 lines (465 loc) · 19.6 KB
/
Copy pathmain_window.py
File metadata and controls
549 lines (465 loc) · 19.6 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import os
import uuid
from PyQt6.QtCore import Qt, QSize, pyqtSlot
from PyQt6.QtGui import QTextCursor
from PyQt6.QtWidgets import (
QFileDialog,
QFrame,
QHBoxLayout,
QInputDialog,
QLabel,
QListWidget,
QListWidgetItem,
QMainWindow,
QMessageBox,
QPushButton,
QSizePolicy,
QSplitter,
QStatusBar,
QTextEdit,
QVBoxLayout,
QWidget,
)
from change_password_dialog import ChangePasswordDialog
from config_manager import load_config, save_config
from process_worker import MiddlewareProcess, Status
from user_manager_dialog import UserManagerDialog
# ──────────────────────────────────────────────────────────────────────────── #
# Per-service card widget shown inside the list
# ──────────────────────────────────────────────────────────────────────────── #
class ServiceCardWidget(QWidget):
def __init__(self, mid: str, name: str, jar_path: str, parent: QWidget | None = None):
super().__init__(parent)
self.mid = mid
self.setFixedHeight(74)
layout = QHBoxLayout(self)
layout.setContentsMargins(12, 8, 12, 8)
layout.setSpacing(10)
# Status dot
self._dot = QLabel("●")
self._dot.setFixedWidth(20)
self._dot.setAlignment(Qt.AlignmentFlag.AlignCenter)
self._dot.setStyleSheet("color: #808080; font-size: 17px;")
# Info column
info = QVBoxLayout()
info.setSpacing(1)
info.setContentsMargins(0, 0, 0, 0)
self._name_lbl = QLabel(name)
self._name_lbl.setStyleSheet("font-weight: bold; font-size: 13px; color: #cdd6f4;")
self._file_lbl = QLabel(os.path.basename(jar_path))
self._file_lbl.setStyleSheet("font-size: 11px; color: #9a9a9a;")
self._status_lbl = QLabel("Stopped")
self._status_lbl.setStyleSheet("font-size: 11px; color: #9a9a9a;")
info.addWidget(self._name_lbl)
info.addWidget(self._file_lbl)
info.addWidget(self._status_lbl)
# Buttons
self.start_btn = QPushButton("▶")
self.start_btn.setObjectName("startBtn")
self.start_btn.setFixedSize(34, 34)
self.start_btn.setToolTip("Start service")
self.stop_btn = QPushButton("■")
self.stop_btn.setObjectName("stopBtn")
self.stop_btn.setFixedSize(34, 34)
self.stop_btn.setToolTip("Stop service")
self.stop_btn.setEnabled(False)
self.remove_btn = QPushButton("✕")
self.remove_btn.setObjectName("removeBtn")
self.remove_btn.setFixedSize(34, 34)
self.remove_btn.setToolTip("Remove service")
layout.addWidget(self._dot)
layout.addLayout(info, 1)
layout.addWidget(self.start_btn)
layout.addWidget(self.stop_btn)
layout.addWidget(self.remove_btn)
def set_status(self, status: str) -> None:
palette = {
"Running": ("#a6e3a1", "#a6e3a1"),
"Stopped": ("#808080", "#9a9a9a"),
"Crashed": ("#f38ba8", "#f38ba8"),
}
dot_color, text_color = palette.get(status, ("#585b70", "#6c7086"))
self._dot.setStyleSheet(f"color: {dot_color}; font-size: 17px;")
self._status_lbl.setText(status)
self._status_lbl.setStyleSheet(f"font-size: 11px; color: {text_color};")
running = status == "Running"
self.start_btn.setEnabled(not running)
self.stop_btn.setEnabled(running)
# ──────────────────────────────────────────────────────────────────────────── #
# Main application window
# ──────────────────────────────────────────────────────────────────────────── #
class MainWindow(QMainWindow):
def __init__(self, username: str = "", role: str = ""):
super().__init__()
self._username = username
self._role = role
self.setWindowTitle("LIS Middleware Manager")
self.resize(1200, 720)
self.setMinimumSize(860, 520)
self._processes: dict[str, MiddlewareProcess] = {}
self._cards: dict[str, ServiceCardWidget] = {}
self._list_items: dict[str, QListWidgetItem] = {}
self._logs: dict[str, list[str]] = {}
self._selected_id: str | None = None
self._build_ui()
self._load_saved()
# ------------------------------------------------------------------ #
# UI construction
# ------------------------------------------------------------------ #
def _build_ui(self) -> None:
central = QWidget()
self.setCentralWidget(central)
root = QVBoxLayout(central)
root.setContentsMargins(0, 0, 0, 0)
root.setSpacing(0)
root.addWidget(self._make_topbar())
splitter = QSplitter(Qt.Orientation.Horizontal)
splitter.setHandleWidth(1)
splitter.addWidget(self._make_sidebar())
splitter.addWidget(self._make_log_panel())
splitter.setSizes([300, 900])
splitter.setStretchFactor(0, 0)
splitter.setStretchFactor(1, 1)
root.addWidget(splitter, 1)
self._statusbar = QStatusBar()
self.setStatusBar(self._statusbar)
self._refresh_statusbar()
def _make_topbar(self) -> QFrame:
bar = QFrame()
bar.setFixedHeight(56)
bar.setStyleSheet(
"QFrame { background-color: #4a4a4a; border-bottom: 1px solid #5a5a5a; }"
)
layout = QHBoxLayout(bar)
layout.setContentsMargins(20, 0, 20, 0)
layout.setSpacing(10)
icon_lbl = QLabel("🔬")
icon_lbl.setStyleSheet("font-size: 30px; background: transparent;")
layout.addWidget(icon_lbl)
title = QLabel("LIS Middleware Manager")
title.setStyleSheet("font-size: 18px; font-weight: bold; color: #89b4fa;")
layout.addWidget(title)
layout.addStretch()
self._add_btn = QPushButton(" + Add JAR")
self._add_btn.setObjectName("addBtn")
self._add_btn.setFixedHeight(36)
self._add_btn.clicked.connect(self._on_add)
self._start_all_btn = QPushButton("▶ Start All")
self._start_all_btn.setObjectName("startAllBtn")
self._start_all_btn.setFixedHeight(36)
self._start_all_btn.clicked.connect(self._on_start_all)
self._stop_all_btn = QPushButton("■ Stop All")
self._stop_all_btn.setObjectName("stopAllBtn")
self._stop_all_btn.setFixedHeight(36)
self._stop_all_btn.clicked.connect(self._on_stop_all)
if self._role == "Admin":
layout.addWidget(self._add_btn)
layout.addWidget(self._start_all_btn)
layout.addWidget(self._stop_all_btn)
# User badge + manage users (Admin only)
if self._username:
layout.addSpacing(8)
sep = QLabel("|")
sep.setStyleSheet("color: #5a5a5a; font-size: 18px;")
layout.addWidget(sep)
layout.addSpacing(6)
user_lbl = QLabel(self._username)
user_lbl.setStyleSheet("font-size: 13px; color: #cdd6f4;")
layout.addWidget(user_lbl)
layout.addSpacing(4)
role_color = "#89b4fa" if self._role == "Admin" else "#a6e3a1"
role_badge = QLabel(self._role)
role_badge.setStyleSheet(
f"font-size:11px;font-weight:bold;color:{role_color};"
f"background:rgba(255,255,255,0.07);border-radius:4px;padding:2px 8px;"
)
layout.addWidget(role_badge)
if self._role == "Admin":
layout.addSpacing(4)
users_btn = QPushButton("👥 Users")
users_btn.setObjectName("usersBtn")
users_btn.setFixedHeight(34)
users_btn.setCursor(Qt.CursorShape.PointingHandCursor)
users_btn.clicked.connect(self._on_manage_users)
layout.addWidget(users_btn)
return bar
def _make_sidebar(self) -> QFrame:
frame = QFrame()
frame.setObjectName("sidebar")
frame.setMinimumWidth(260)
frame.setMaximumWidth(420)
frame.setStyleSheet(
"QFrame#sidebar { background-color: #4a4a4a; border-right: 1px solid #5a5a5a; }"
)
layout = QVBoxLayout(frame)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
hdr = QLabel(" SERVICES")
hdr.setFixedHeight(36)
hdr.setStyleSheet(
"font-size: 11px; font-weight: bold; color: #9a9a9a;"
"letter-spacing: 1px; border-bottom: 1px solid #5a5a5a;"
"background-color: #4a4a4a; padding-left: 12px;"
)
layout.addWidget(hdr)
self._list = QListWidget()
self._list.setSpacing(0)
self._list.setSelectionMode(QListWidget.SelectionMode.SingleSelection)
self._list.currentItemChanged.connect(self._on_selection_changed)
layout.addWidget(self._list, 1)
return frame
def _make_log_panel(self) -> QFrame:
frame = QFrame()
frame.setObjectName("logPanel")
frame.setStyleSheet("QFrame#logPanel { background-color: #3a3a3a; }")
layout = QVBoxLayout(frame)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Header row
hdr = QFrame()
hdr.setFixedHeight(44)
hdr.setStyleSheet(
"background-color: #4a4a4a; border-bottom: 1px solid #5a5a5a;"
)
hdr_layout = QHBoxLayout(hdr)
hdr_layout.setContentsMargins(16, 6, 16, 6)
self._log_title = QLabel("Select a service to view logs")
self._log_title.setStyleSheet("font-size: 14px; font-weight: bold; color: #cdd6f4;")
self._clear_btn = QPushButton("Clear")
self._clear_btn.setObjectName("clearLogBtn")
self._clear_btn.setFixedHeight(28)
self._clear_btn.clicked.connect(self._on_clear_log)
if self._role != "Admin":
self._clear_btn.hide()
hdr_layout.addWidget(self._log_title)
hdr_layout.addStretch()
hdr_layout.addWidget(self._clear_btn)
layout.addWidget(hdr)
# Log text area
self._log_view = QTextEdit()
self._log_view.setReadOnly(True)
self._log_view.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
self._log_view.document().setDefaultStyleSheet(
"body { margin: 0; padding: 0; }"
"p, span { margin: 0; padding: 0; }"
)
layout.addWidget(self._log_view, 1)
# Empty state overlay
empty_hint = (
"No service selected\n\nClick + Add JAR to get started"
if self._role == "Admin"
else "No service selected"
)
self._empty_lbl = QLabel(empty_hint)
self._empty_lbl.setAlignment(Qt.AlignmentFlag.AlignCenter)
self._empty_lbl.setStyleSheet(
"color: #6a6a6a; font-size: 15px; background-color: #3a3a3a;"
)
self._empty_lbl.setSizePolicy(
QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding
)
layout.addWidget(self._empty_lbl)
self._log_view.hide()
return frame
# ------------------------------------------------------------------ #
# Service registration / removal
# ------------------------------------------------------------------ #
def _register(self, mid: str, name: str, jar_path: str, auto_restart: bool = True) -> None:
proc = MiddlewareProcess(mid, name, jar_path, auto_restart)
proc.status_changed.connect(self._on_status_changed)
proc.log_received.connect(self._on_log_received)
self._processes[mid] = proc
self._logs[mid] = []
card = ServiceCardWidget(mid, name, jar_path)
card.start_btn.clicked.connect(lambda: self._processes[mid].start())
card.stop_btn.clicked.connect(lambda: self._confirm_stop(mid))
card.remove_btn.clicked.connect(lambda: self._remove(mid))
if self._role != "Admin":
card.remove_btn.hide()
self._cards[mid] = card
item = QListWidgetItem(self._list)
item.setData(Qt.ItemDataRole.UserRole, mid)
item.setSizeHint(QSize(0, 78))
self._list.addItem(item)
self._list.setItemWidget(item, card)
self._list_items[mid] = item
if len(self._processes) == 1:
self._list.setCurrentItem(item)
self._refresh_statusbar()
def _confirm_stop(self, mid: str) -> None:
proc = self._processes.get(mid)
if not proc:
return
reply = QMessageBox.warning(
self,
"Stop Service",
f"Are you sure you want to stop <b>{proc.name}</b>?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)
if reply == QMessageBox.StandardButton.Yes:
proc.auto_restart = False
proc.stop()
def _remove(self, mid: str) -> None:
proc = self._processes.get(mid)
if not proc:
return
if proc.status == Status.RUNNING:
msg = (
f"<b>{proc.name}</b> is currently running.<br><br>"
"Stop it and remove from the list?"
)
else:
msg = f"Are you sure you want to remove <b>{proc.name}</b>?"
reply = QMessageBox.warning(
self,
"Remove Service",
msg,
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)
if reply != QMessageBox.StandardButton.Yes:
return
if proc.status == Status.RUNNING:
proc.auto_restart = False
proc.stop()
item = self._list_items.pop(mid, None)
if item:
self._list.takeItem(self._list.row(item))
self._processes.pop(mid, None)
self._cards.pop(mid, None)
self._logs.pop(mid, None)
if self._selected_id == mid:
self._selected_id = None
self._log_title.setText("Select a service to view logs")
self._log_view.clear()
self._log_view.hide()
self._empty_lbl.show()
self._save()
self._refresh_statusbar()
# ------------------------------------------------------------------ #
# UI event handlers
# ------------------------------------------------------------------ #
def _on_change_password(self) -> None:
dlg = ChangePasswordDialog(
username=self._username, require_current=True, parent=self
)
if dlg.exec() == ChangePasswordDialog.DialogCode.Accepted:
from PyQt6.QtWidgets import QMessageBox
QMessageBox.information(
self, "Password Changed", "Your password has been updated successfully."
)
def _on_manage_users(self) -> None:
dlg = UserManagerDialog(current_username=self._username, parent=self)
dlg.exec()
def _on_add(self) -> None:
paths, _ = QFileDialog.getOpenFileNames(
self, "Select JAR File(s)", "", "JAR Files (*.jar);;All Files (*)"
)
for jar_path in paths:
default_name = os.path.splitext(os.path.basename(jar_path))[0]
name, ok = QInputDialog.getText(
self,
"Service Name",
f"Display name for {os.path.basename(jar_path)}:",
text=default_name,
)
if ok and name.strip():
self._register(str(uuid.uuid4()), name.strip(), jar_path)
self._save()
def _on_start_all(self) -> None:
for proc in self._processes.values():
if proc.status == Status.STOPPED:
proc.start()
def _on_stop_all(self) -> None:
active = [p for p in self._processes.values() if p.status != Status.STOPPED]
if not active:
return
count = len(active)
reply = QMessageBox.warning(
self,
"Stop All Services",
f"Are you sure you want to stop all <b>{count}</b> running service(s)?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)
if reply != QMessageBox.StandardButton.Yes:
return
for proc in active:
proc.auto_restart = False
proc.stop()
def _on_clear_log(self) -> None:
if self._selected_id:
self._logs[self._selected_id] = []
self._log_view.clear()
def _on_selection_changed(
self, current: QListWidgetItem, _previous: QListWidgetItem
) -> None:
if not current:
return
mid: str = current.data(Qt.ItemDataRole.UserRole)
self._selected_id = mid
proc = self._processes.get(mid)
self._log_title.setText(f"Logs — {proc.name}" if proc else "Logs")
self._log_view.clear()
logs = self._logs.get(mid, [])
if logs:
self._log_view.setHtml("".join(logs))
sb = self._log_view.verticalScrollBar()
sb.setValue(sb.maximum())
self._log_view.show()
self._empty_lbl.hide()
# ------------------------------------------------------------------ #
# Process signal handlers
# ------------------------------------------------------------------ #
@pyqtSlot(str, str)
def _on_status_changed(self, mid: str, status: str) -> None:
card = self._cards.get(mid)
if card:
card.set_status(status)
self._refresh_statusbar()
@pyqtSlot(str, str)
def _on_log_received(self, mid: str, html: str) -> None:
self._logs.setdefault(mid, []).append(html)
if self._selected_id == mid:
cursor = self._log_view.textCursor()
cursor.movePosition(QTextCursor.MoveOperation.End)
cursor.insertHtml(html)
self._log_view.setTextCursor(cursor)
self._log_view.ensureCursorVisible()
# ------------------------------------------------------------------ #
# Persistence & misc
# ------------------------------------------------------------------ #
def _load_saved(self) -> None:
for entry in load_config():
self._register(
entry["id"],
entry["name"],
entry["jar_path"],
entry.get("auto_restart", True),
)
def _save(self) -> None:
save_config(
[
{
"id": mid,
"name": p.name,
"jar_path": p.jar_path,
"auto_restart": p.auto_restart,
}
for mid, p in self._processes.items()
]
)
def _refresh_statusbar(self) -> None:
running = sum(1 for p in self._processes.values() if p.status == Status.RUNNING)
total = len(self._processes)
crashed = sum(1 for p in self._processes.values() if p.status == Status.CRASHED)
parts = [f"{running} running", f"{total - running} stopped"]
if crashed:
parts.append(f"{crashed} crashed")
self._statusbar.showMessage(" " + " · ".join(parts))
def closeEvent(self, event) -> None:
for proc in self._processes.values():
proc.auto_restart = False
proc.stop()
event.accept()