forked from W5DMH/commstatone
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroups.py
More file actions
367 lines (305 loc) · 15.1 KB
/
Copy pathgroups.py
File metadata and controls
367 lines (305 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
# Copyright (c) 2025 Manuel Ochoa
# This file is part of CommStat.
# Licensed under the GNU General Public License v3.0.
"""
groups.py - Manage Groups Dialog
Displays all groups in a table with Add, Edit, and Delete actions.
Editing is done inline directly in the table row.
"""
from typing import Optional
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QDialog, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QTableWidget, QTableWidgetItem,
QHeaderView, QMessageBox, QAbstractItemView, QWidget,
)
from constants import DEFAULT_COLORS
from ui_helpers import make_button, make_input, confirm, apply_standard_dialog_chrome
# ── Constants ──────────────────────────────────────────────────────────────────
_PROG_BG = DEFAULT_COLORS.get("program_background", "#A52A2A")
_PROG_FG = DEFAULT_COLORS.get("program_foreground", "#FFFFFF")
_PANEL_BG = DEFAULT_COLORS.get("module_background", "#DDDDDD")
_PANEL_FG = DEFAULT_COLORS.get("module_foreground", "#000000")
_TITLE_BG = DEFAULT_COLORS.get("title_bar_background", "#F07800")
_TITLE_FG = DEFAULT_COLORS.get("title_bar_foreground", "#FFFFFF")
_DATA_BG = DEFAULT_COLORS.get("data_background", "#F8F6F4")
_DATA_FG = DEFAULT_COLORS.get("data_foreground", "#000000")
_COL_ADD = "#28a745"
_COL_EDIT = "#007bff"
_COL_DELETE = "#dc3545"
_COL_CLOSE = "#555555"
_COL_SAVE = "#28a745"
_COL_CANCEL = "#555555"
_WIN_W = 520
_WIN_H = 400
_TABLE_COLS = ["Group Name", "Comment"]
# ── Dialog ─────────────────────────────────────────────────────────────────────
class GroupsDialog(QDialog):
"""Manage Groups dialog — add, edit, and delete groups with inline row editing."""
def __init__(self, db_manager, parent=None):
super().__init__(parent)
self.db = db_manager
self._in_edit_mode: bool = False
self._edit_row: int = -1
self._adding: bool = False
self._edit_name: str = ""
self._iw_name: Optional[QLineEdit] = None
self._iw_comment: Optional[QLineEdit] = None
apply_standard_dialog_chrome(self, "Groups", _WIN_W, _WIN_H)
self._setup_ui()
self._load()
# ── UI construction ────────────────────────────────────────────────────────
def _setup_ui(self) -> None:
self.setStyleSheet(
f"QDialog {{ background-color:{_PANEL_BG}; color:{_PANEL_FG}; }}"
f"QLabel {{ font-size:13px; color:{_PANEL_FG}; }}"
)
body = QVBoxLayout(self)
body.setContentsMargins(15, 15, 15, 15)
body.setSpacing(10)
# ── Title ─────────────────────────────────────────────────────────────
title_lbl = QLabel("Groups")
title_lbl.setAlignment(Qt.AlignCenter)
title_lbl.setFont(QtGui.QFont("Roboto Slab", -1, QtGui.QFont.Black))
title_lbl.setFixedHeight(36)
title_lbl.setStyleSheet(
f"QLabel {{ background-color:{_PROG_BG}; color:{_PROG_FG};"
f" font-family:'Roboto Slab'; font-size:16px; font-weight:900;"
f" padding-top:9px; padding-bottom:9px; }}"
)
body.addWidget(title_lbl)
# ── Table ─────────────────────────────────────────────────────────────
self.table = QTableWidget(0, len(_TABLE_COLS))
self.table.setHorizontalHeaderLabels(_TABLE_COLS)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table.setSelectionMode(QAbstractItemView.SingleSelection)
self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.table.setTabKeyNavigation(False)
self.table.setShowGrid(True)
self.table.verticalHeader().setVisible(False)
self.table.setAlternatingRowColors(False)
hh = self.table.horizontalHeader()
hh.setSectionResizeMode(0, QHeaderView.ResizeToContents)
hh.setSectionResizeMode(1, QHeaderView.Stretch)
self.table.setStyleSheet(
f"QTableWidget {{ background-color:{_DATA_BG}; alternate-background-color:{_DATA_BG};"
f" gridline-color:#cccccc; color:{_DATA_FG};"
f" font-family:'Kode Mono'; font-size:13px; }}"
f"QTableWidget::item {{ padding:4px 6px; }}"
f"QHeaderView::section {{ background-color:{_TITLE_BG}; color:{_TITLE_FG};"
f" padding:5px 6px; border:none; font-family:Roboto; font-size:13px;"
f" font-weight:bold; }}"
f"QTableWidget::item:selected {{ background-color:#cce5ff; color:#000000; }}"
)
self.table.selectionModel().selectionChanged.connect(self._on_selection_changed)
self.table.doubleClicked.connect(self._on_edit)
body.addWidget(self.table)
# ── Note ──────────────────────────────────────────────────────────────
note = QLabel(
f"<b><span style='color:#AA0000'>Note:</span></b>"
f" <span style='color:{_PANEL_FG}'>The @ symbol is not required"
f" (e.g., enter MAGNET, not @MAGNET)</span>"
)
note.setStyleSheet("QLabel { font-family:Roboto; font-size:13px; }")
body.addWidget(note)
# ── Action buttons ────────────────────────────────────────────────────
btn_row = QHBoxLayout()
btn_row.setSpacing(8)
self.btn_add = make_button("Add", _COL_ADD, 80)
self.btn_edit = make_button("Edit", _COL_EDIT, 80)
self.btn_delete = make_button("Delete", _COL_DELETE, 80)
self.btn_save = make_button("Save", _COL_SAVE, 80)
self.btn_cancel = make_button("Cancel", _COL_CANCEL, 80)
self.btn_close = make_button("Close", _COL_CLOSE, 80)
self.btn_edit.setEnabled(False)
self.btn_delete.setEnabled(False)
self.btn_save.setVisible(False)
self.btn_cancel.setVisible(False)
self.btn_save.setEnabled(False)
self.btn_add.clicked.connect(self._on_add)
self.btn_edit.clicked.connect(self._on_edit)
self.btn_delete.clicked.connect(self._on_delete)
self.btn_save.clicked.connect(lambda: self._exit_edit_mode(save=True))
self.btn_cancel.clicked.connect(lambda: self._exit_edit_mode(save=False))
self.btn_close.clicked.connect(self.accept)
btn_row.addWidget(self.btn_add)
btn_row.addWidget(self.btn_edit)
btn_row.addWidget(self.btn_delete)
btn_row.addWidget(self.btn_save)
btn_row.addWidget(self.btn_cancel)
btn_row.addStretch()
btn_row.addWidget(self.btn_close)
body.addLayout(btn_row)
# ── Data loading ───────────────────────────────────────────────────────────
def _load(self) -> None:
self._in_edit_mode = False
self._edit_row = -1
self._edit_name = ""
groups = self.db.get_all_groups_details()
self.table.setRowCount(0)
mono = QtGui.QFont("Kode Mono")
for g in groups:
row = self.table.rowCount()
self.table.insertRow(row)
for col, val in enumerate([g["name"], g["comment"]]):
item = QTableWidgetItem(val)
item.setFont(mono)
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.table.setItem(row, col, item)
self._on_selection_changed()
# ── Selection ──────────────────────────────────────────────────────────────
def _on_selection_changed(self) -> None:
if self._in_edit_mode:
return
has_sel = bool(self.table.selectedItems())
self.btn_add.setEnabled(True)
self.btn_edit.setEnabled(has_sel)
self.btn_delete.setEnabled(has_sel)
# ── Inline edit ────────────────────────────────────────────────────────────
def _enter_edit_mode(self, row: int, adding: bool) -> None:
self._in_edit_mode = True
self._edit_row = row
self._adding = adding
if adding:
name_val = ""
comment_val = ""
else:
name_item = self.table.item(row, 0)
comment_item = self.table.item(row, 1)
name_val = name_item.text() if name_item else ""
comment_val = comment_item.text() if comment_item else ""
self._edit_name = name_val
self._iw_name = make_input(
placeholder="Group name (max 15 chars)",
default=name_val,
max_len=15,
)
self._iw_name.textChanged.connect(
lambda t: self._iw_name.setText(t.upper()) if t != t.upper() else None
)
self._iw_name.textChanged.connect(lambda _: self._on_inline_changed())
self._iw_comment = make_input(
placeholder="Optional description",
default=comment_val,
max_len=80,
)
# Qt force-stretches a QLineEdit installed via setCellWidget. Wrap the
# fixed-width name input in a QWidget + HBoxLayout so the container
# fills the cell while the input keeps its size. Comment stays
# unwrapped — the Stretch column gives it the full remaining width.
def _wrap_fixed(input_widget: QLineEdit, width_px: int) -> QWidget:
input_widget.setFixedWidth(width_px)
container = QWidget()
layout = QHBoxLayout(container)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.addWidget(input_widget)
layout.addStretch()
return container
_name_w = 136
# Col 0 is ResizeToContents and would clamp back to data width, clipping
# the input. Switch to Interactive and force a width that holds it;
# _exit_edit_mode restores ResizeToContents.
_hh = self.table.horizontalHeader()
_hh.setSectionResizeMode(0, QHeaderView.Interactive)
self.table.setColumnWidth(0, _name_w + 16)
self.table.setCellWidget(row, 0, _wrap_fixed(self._iw_name, _name_w))
self.table.setCellWidget(row, 1, self._iw_comment)
self.table.setRowHeight(row, 42)
self.table.setSelectionMode(QAbstractItemView.NoSelection)
self.btn_add.setVisible(False)
self.btn_edit.setVisible(False)
self.btn_delete.setVisible(False)
self.btn_save.setVisible(True)
self.btn_cancel.setVisible(True)
self.btn_close.setEnabled(False)
QWidget.setTabOrder(self._iw_name, self._iw_comment)
QWidget.setTabOrder(self._iw_comment, self.btn_save)
self._on_inline_changed()
self._iw_name.setFocus()
def _on_inline_changed(self) -> None:
if not self._in_edit_mode:
return
has_name = bool(self._iw_name and self._iw_name.text().strip())
self.btn_save.setEnabled(has_name)
def _exit_edit_mode(self, save: bool) -> None:
row = self._edit_row
if save:
if self._adding:
raw = self._iw_name.text().strip()
name = raw.lstrip("@").strip().upper()
if not name:
QMessageBox.warning(self, "Groups", "Group name is required.")
return
comment = self._iw_comment.text().strip()
ok = self.db.add_group(name, comment, "", "")
if not ok:
QMessageBox.warning(
self, "Groups",
"Could not add group. The name may already exist."
)
return
else:
raw = self._iw_name.text().strip()
new_name = raw.lstrip("@").strip().upper()
if not new_name:
QMessageBox.warning(self, "Groups", "Group name is required.")
return
comment = self._iw_comment.text().strip()
ok = self.db.update_group_full(self._edit_name, new_name, comment)
if not ok:
QMessageBox.critical(self, "Error", "Could not update group.")
return
for col in range(2):
self.table.removeCellWidget(row, col)
self._iw_name = self._iw_comment = None
self._in_edit_mode = False
self._edit_name = ""
self.table.setRowHeight(row, self.table.verticalHeader().defaultSectionSize())
self.table.setSelectionMode(QAbstractItemView.SingleSelection)
# Restore col 0 to auto-fit after edit-mode widening.
self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
self.btn_add.setVisible(True)
self.btn_edit.setVisible(True)
self.btn_delete.setVisible(True)
self.btn_save.setVisible(False)
self.btn_cancel.setVisible(False)
self.btn_close.setEnabled(True)
self._load()
# ── Actions ────────────────────────────────────────────────────────────────
def _on_add(self) -> None:
if self._in_edit_mode:
return
new_row = self.table.rowCount()
self.table.insertRow(new_row)
self._enter_edit_mode(row=new_row, adding=True)
def _on_edit(self) -> None:
if self._in_edit_mode:
return
row = self.table.currentRow()
if row < 0:
return
self._enter_edit_mode(row=row, adding=False)
def _on_delete(self) -> None:
if self._in_edit_mode:
return
row = self.table.currentRow()
if row < 0:
return
name_item = self.table.item(row, 0)
name = name_item.text() if name_item else ""
if not name:
return
if not confirm(self, "Delete Group", f"Delete group '{name}'?",
no_label="Cancel"):
return
if not self.db.remove_group(name):
QMessageBox.critical(self, "Error", "Could not delete group.")
return
self._load()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
print("This dialog requires a DatabaseManager instance.")
sys.exit(1)