From ae7d4922e047b5190dd37247d7e619f52f7930f9 Mon Sep 17 00:00:00 2001 From: Clemens Brunner Date: Mon, 2 Dec 2024 16:08:21 +0100 Subject: [PATCH] Remove from paintEvent --- src/mnelab/dialogs/append.py | 43 +++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/mnelab/dialogs/append.py b/src/mnelab/dialogs/append.py index 1cfab19e..6e15b7ba 100644 --- a/src/mnelab/dialogs/append.py +++ b/src/mnelab/dialogs/append.py @@ -21,7 +21,7 @@ class DragDropTableWidget(QTableWidget): - def __init__(self, parent=None): + def __init__(self, parent=None, items=None): super().__init__(parent) self.setDragEnabled(True) self.setAcceptDrops(True) @@ -32,6 +32,30 @@ def __init__(self, parent=None): self.setDragDropOverwriteMode(False) self.drop_row = -1 + self.setColumnCount(2) + self.horizontalHeader().hide() + self.verticalHeader().hide() + self.setShowGrid(False) + self.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents) + self.horizontalHeader().setStretchLastSection(True) + self.setEditTriggers(QAbstractItemView.NoEditTriggers) + if items is not None: + self.setRowCount(len(items)) + for i, (idx, name) in enumerate(items): + self.setItem(i, 0, QTableWidgetItem(str(idx))) + self.setItem(i, 1, QTableWidgetItem(name)) + self.style_rows() + + def style_rows(self): + for i in range(self.rowCount()): + self.resizeRowToContents(i) + self.setRowHeight(i, ROW_HEIGHT) + self.item(i, 0).setTextAlignment(Qt.AlignRight | Qt.AlignVCenter) + self.item(i, 0).setForeground(QColor("gray")) + self.item(i, 0).setFlags(self.item(i, 0).flags() & ~Qt.ItemIsEditable) + self.item(i, 1).setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter) + self.item(i, 1).setFlags(self.item(i, 1).flags() & ~Qt.ItemIsEditable) + def dragMoveEvent(self, event): drop_row = self.indexAt(event.pos()).row() if drop_row == -1: @@ -42,18 +66,11 @@ def dragMoveEvent(self, event): def dragLeaveEvent(self, event): self.drop_row = -1 + self.style_rows() event.accept() def paintEvent(self, event): super().paintEvent(event) - for i in range(self.rowCount()): - self.resizeRowToContents(i) - self.setRowHeight(i, ROW_HEIGHT) - self.item(i, 0).setTextAlignment(Qt.AlignRight | Qt.AlignVCenter) - self.item(i, 0).setForeground(QColor("gray")) - self.item(i, 0).setFlags(self.item(i, 0).flags() & ~Qt.ItemIsEditable) - self.item(i, 1).setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter) - self.item(i, 1).setFlags(self.item(i, 1).flags() & ~Qt.ItemIsEditable) if self.drop_row >= 0: painter = QPainter(self.viewport()) pen = QPen(QColor("black")) @@ -99,7 +116,7 @@ def dropEvent(self, event): self.insertRow(drop_row + i) for col, value in enumerate(data): self.setItem(drop_row + i, col, QTableWidgetItem(value)) - + self.style_rows() event.accept() @@ -114,15 +131,15 @@ def __init__(self, parent, compatibles, title="Append data"): grid.addWidget(QLabel("Source"), 0, 0, Qt.AlignCenter) grid.addWidget(QLabel("Destination"), 0, 2, Qt.AlignCenter) - self.source = DragDropTableWidget(self) - self.setup_table(self.source, compatibles) + self.source = DragDropTableWidget(self, items=compatibles) + # self.setup_table(self.source, compatibles) self.move_button = QPushButton("→") self.move_button.setEnabled(False) grid.addWidget(self.move_button, 1, 1, Qt.AlignHCenter) self.destination = DragDropTableWidget(self) - self.setup_table(self.destination, []) + # self.setup_table(self.destination, []) grid.addWidget(self.source, 1, 0) grid.addWidget(self.destination, 1, 2)