fosKeyMan
Loading...
Searching...
No Matches
columnconfigurator.py
Go to the documentation of this file.
1from PySide6.QtCore import QSize
2from PySide6.QtGui import QIcon
3from PySide6.QtWidgets import QDialog, QTableWidgetItem, QHeaderView, QPushButton, QSizePolicy, QMessageBox
4
5from frontend.ui.uicolumn import Ui_Column
6from utils.utils import apply_icon_button_style
7
8
9class ColumnConfigurator(QDialog):
10 r"""
11 Class to configure (add, remove) the customized table columns.
12 """
13 def __init__(self, initial_columns, parent=None):
14 r"""
15 Initialize the column configurator dialog.
16
17 \param initial_columns (list[str]): The initial list of customized columns.
18 \param parent (QWidget, optional): Parent widget for this dialog.
19 """
20 super(ColumnConfigurator, self).__init__(parent)
21 self.setWindowTitle(self.tr("Configure Table Columns"))
22
23 self.ui = Ui_Column()
24 self.ui.setupUi(self)
25
26 self.initial_columns = initial_columns
27 self.selected_columns = initial_columns.copy()
28
29 self.setup_main_table()
30 self.setup_add_table()
31
32 self.ui.cancelButton.clicked.connect(self.reject)
33 self.ui.confirmButton.clicked.connect(self.confirm_and_close)
34
36 r"""
37 Set up the main table to display and manage the current customized columns.
38 """
39 self.ui.tableWidget.setColumnCount(2)
40 self.ui.tableWidget.setHorizontalHeaderLabels([self.tr("Column Name"), ""])
41 self.ui.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
42 self.ui.tableWidget.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed)
43 self.ui.tableWidget.setColumnWidth(1, 40)
44
45 for col_name in self.selected_columns:
46 self.insert_column_row(col_name)
47
48 def setup_add_table(self):
49 r"""
50 Set up a small input table for adding new column names.
51 """
52 self.ui.addTableWidget.setColumnCount(2)
53 self.ui.addTableWidget.setRowCount(1)
54 self.ui.addTableWidget.horizontalHeader().setVisible(False)
55 self.ui.addTableWidget.verticalHeader().setVisible(False)
56 self.ui.addTableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
57 self.ui.addTableWidget.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed)
58 self.ui.addTableWidget.setColumnWidth(1, 40)
59
60 row_height = self.ui.addTableWidget.verticalHeader().defaultSectionSize()
61 self.ui.addTableWidget.setFixedHeight(row_height + 2)
62
63 self.ui.addTableWidget.setItem(0, 0, QTableWidgetItem())
64
65 add_button = QPushButton()
66 add_button.setIcon(QIcon(":/icons/icons/editadd.svg"))
67 apply_icon_button_style(add_button)
68 add_button.setIconSize(QSize(20, 20))
69 add_button.setFixedSize(24, 24)
70 add_button.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
71 self.ui.addTableWidget.setCellWidget(0, 1, add_button)
72
73 add_button.clicked.connect(self.add_new_column)
74
75 def add_new_column(self):
76 r"""
77 Add a new column name from the input field to the main table.
78 """
79 item = self.ui.addTableWidget.item(0, 0)
80 col_name = item.text().strip() if item else ""
81 if col_name:
82 self.insert_column_row(col_name)
83 self.ui.addTableWidget.setItem(0, 0, QTableWidgetItem())
84 else:
85 QMessageBox.warning(self, self.tr("Input Error"), self.tr("Please enter a column name."))
86
87 def insert_column_row(self, column_name):
88 r"""
89 Insert a new column name as a row into the main table with a delete button.
90
91 \param column_name (str): The name of the column to insert.
92 """
93 row_pos = self.ui.tableWidget.rowCount()
94 self.ui.tableWidget.insertRow(row_pos)
95 self.ui.tableWidget.setItem(row_pos, 0, QTableWidgetItem(column_name))
96
97 delete_button = QPushButton()
98 delete_button.setIcon(QIcon(":/icons/icons/editbin.svg"))
99 apply_icon_button_style(delete_button)
100 delete_button.setIconSize(QSize(20, 20))
101 delete_button.setFixedSize(24, 24)
102 delete_button.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
103 delete_button.clicked.connect(self.delete_current_row)
104 self.ui.tableWidget.setCellWidget(row_pos, 1, delete_button)
105
107 r"""
108 Find the row where the clicked button is located and delete it
109 """
110 button = self.sender()
111 if not button:
112 return
113
114 for row in range(self.ui.tableWidget.rowCount()):
115 if self.ui.tableWidget.cellWidget(row, 1) == button:
116 self.ui.tableWidget.removeRow(row)
117 break
118
120 r"""
121 Confirm the selected columns to display and close the configurator dialog.
122 """
123 self.selected_columns = []
124 for row in range(self.ui.tableWidget.rowCount()):
125 item = self.ui.tableWidget.item(row, 0)
126 if item:
127 self.selected_columns.append(item.text().strip())
128 self.accept()
129
130
131
132
133
Class to configure (add, remove) the customized table columns.
insert_column_row(self, column_name)
Insert a new column name as a row into the main table with a delete button.
__init__(self, initial_columns, parent=None)
Initialize the column configurator dialog.
setup_add_table(self)
Set up a small input table for adding new column names.
setup_main_table(self)
Set up the main table to display and manage the current customized columns.