0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

refactor: align_table_center as a static method

This commit is contained in:
wyzula-jan
2023-09-11 11:35:21 +02:00
parent 63e3896725
commit 702e758812
2 changed files with 50 additions and 3 deletions

View File

@ -578,6 +578,13 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="pushButton_duplicate">
<property name="text">
<string>Duplicate Last Entry</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>

View File

@ -429,6 +429,10 @@ class MotorApp(QWidget):
lambda: self.resizeTable(self.tableWidget_coordinates)
)
self.pushButton_duplicate.clicked.connect(
lambda: self.duplicate_last_row(self.tableWidget_coordinates)
)
self.pushButton_help.clicked.connect(self.show_help_dialog)
def init_ui(self) -> None:
@ -606,15 +610,51 @@ class MotorApp(QWidget):
self.saved_motor_map.setData(pos=self.saved_motor_positions, brush=brushes)
self.align_table_center(table)
if self.checkBox_resize_auto.isChecked():
table.resizeColumnsToContents()
def duplicate_last_row(self, table: QtWidgets.QTableWidget) -> None:
last_row = table.rowCount() - 1
if last_row == -1:
return
table.setRowCount(last_row + 2)
new_row = last_row + 1
for col in range(table.columnCount()):
cell_widget = table.cellWidget(last_row, col)
cell_item = table.item(last_row, col)
if isinstance(cell_widget, QtWidgets.QCheckBox):
new_checkbox = QtWidgets.QCheckBox()
new_checkbox.setChecked(cell_widget.isChecked())
table.setCellWidget(new_row, col, new_checkbox)
elif isinstance(cell_widget, QtWidgets.QPushButton):
new_button = QtWidgets.QPushButton(cell_widget.text())
new_button.clicked.connect(partial(self.move_to_row_coordinates, table, new_row))
table.setCellWidget(new_row, col, new_button)
elif cell_item:
new_item = QtWidgets.QTableWidgetItem(cell_item.text())
new_item.setFlags(cell_item.flags())
table.setItem(new_row, col, new_item)
self.align_table_center(table)
if self.checkBox_resize_auto.isChecked():
table.resizeColumnsToContents()
@staticmethod
def align_table_center(table: QtWidgets.QTableWidget) -> None:
for row in range(table.rowCount()):
for col in range(table.columnCount()):
item = table.item(row, col)
if item:
item.setTextAlignment(Qt.AlignCenter)
if self.checkBox_resize_auto.isChecked():
table.resizeColumnsToContents()
def move_to_row_coordinates(self, table, row):
x = float(table.item(row, 2).text())
y = float(table.item(row, 3).text())