|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
基础按钮委托类
|
|
|
|
|
提供通用的按钮索引获取和数据访问方法
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from PyQt5.QtWidgets import QItemDelegate, QMessageBox
|
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BaseButtonDelegate(QItemDelegate):
|
|
|
|
|
"""基础按钮委托类,提供通用的索引处理方法"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
|
super(BaseButtonDelegate, self).__init__(parent)
|
|
|
|
|
|
|
|
|
|
def _get_button_row_indices(self, button):
|
|
|
|
|
"""
|
|
|
|
|
获取按钮所在的视图行和源模型行索引
|
|
|
|
|
返回: (view_row, source_row) 或 (None, None) 如果找不到
|
|
|
|
|
"""
|
|
|
|
|
table_view = self.parent()
|
|
|
|
|
|
|
|
|
|
# 获取操作列索引(最后一列)
|
|
|
|
|
if hasattr(table_view, 'proxy') and table_view.proxy:
|
|
|
|
|
operation_col = table_view.proxy.columnCount() - 1
|
|
|
|
|
row_count = table_view.proxy.rowCount()
|
|
|
|
|
else:
|
|
|
|
|
operation_col = table_view.model.columnCount() - 1
|
|
|
|
|
row_count = table_view.model.rowCount()
|
|
|
|
|
|
|
|
|
|
# 查找按钮所在的行
|
|
|
|
|
for row in range(row_count):
|
|
|
|
|
if hasattr(table_view, 'proxy') and table_view.proxy:
|
|
|
|
|
index = table_view.proxy.index(row, operation_col)
|
|
|
|
|
else:
|
|
|
|
|
index = table_view.model.index(row, operation_col)
|
|
|
|
|
|
|
|
|
|
widget = table_view.indexWidget(index)
|
|
|
|
|
if widget and widget.layout():
|
|
|
|
|
for i in range(widget.layout().count()):
|
|
|
|
|
if widget.layout().itemAt(i).widget() == button:
|
|
|
|
|
view_row = row
|
|
|
|
|
|
|
|
|
|
# 转换为源模型索引
|
|
|
|
|
if hasattr(table_view, 'proxy') and table_view.proxy:
|
|
|
|
|
proxy_index = table_view.proxy.index(view_row, 0)
|
|
|
|
|
source_index = table_view.proxy.mapToSource(proxy_index)
|
|
|
|
|
source_row = source_index.row()
|
|
|
|
|
else:
|
|
|
|
|
source_row = view_row
|
|
|
|
|
|
|
|
|
|
return view_row, source_row
|
|
|
|
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
def _get_view_data(self, view_row, column):
|
|
|
|
|
"""
|
|
|
|
|
从视图中获取指定行列的数据
|
|
|
|
|
"""
|
|
|
|
|
table_view = self.parent()
|
|
|
|
|
if hasattr(table_view, 'proxy') and table_view.proxy:
|
|
|
|
|
index = table_view.proxy.index(view_row, column)
|
|
|
|
|
return table_view.proxy.data(index, Qt.DisplayRole)
|
|
|
|
|
else:
|
|
|
|
|
return table_view.model.datas[view_row][column]
|
|
|
|
|
|
|
|
|
|
def _validate_button_indices(self, button):
|
|
|
|
|
"""
|
|
|
|
|
验证并获取按钮索引,如果失败则显示错误消息
|
|
|
|
|
返回: (view_row, source_row) 或 (None, None)
|
|
|
|
|
"""
|
|
|
|
|
view_row, source_row = self._get_button_row_indices(button)
|
|
|
|
|
if view_row is None:
|
|
|
|
|
QMessageBox.warning(self.parent(), '错误', '无法确定按钮所在行', QMessageBox.Yes)
|
|
|
|
|
return None, None
|
|
|
|
|
return view_row, source_row
|