tao/PandasModel.py

71 lines
2.4 KiB
Python

from PyQt5.QtCore import QAbstractTableModel
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
class PandasModel(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[1]
def data(self, index, role):
if not index.isValid():
return None
# 배경색 변경
if role == Qt.BackgroundColorRole:
matching_url = self._data.iloc[index.row()]['MatchingUrl']
if matching_url and matching_url.startswith('http'):
return QColor('lightgray')
if self.selected_cell == (index.row(), index.column()): # 선택된 셀
return QColor('yellow')
# 폰트 변경
if role == Qt.FontRole:
current_row = index.row()
current_keyword_id = self._data.iloc[current_row, 1]
# None 값 처리
if current_keyword_id is None:
current_keyword_id = ""
previous_keyword_id = ""
if current_row > 0:
previous_keyword_id = self._data.iloc[current_row - 1, 1]
if previous_keyword_id is None:
previous_keyword_id = ""
# 첫 번째 행이거나 이전 행보다 keyword_id가 큰 경우 폰트 스타일 변경
if current_row == 0 or current_keyword_id > previous_keyword_id:
font = QtGui.QFont()
font.setBold(True)
font.setUnderline(True)
return font
# 기타 데이터 처리
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return self._data.columns[section]
else:
return str(self._data.index[section])
return None
def selected_cell(self, cell):
self.selected_cell = cell
self.layoutChanged.emit() # 뷰 업데이트
def update_selected_cell(self, cell):
self.selected_cell = cell
self.layoutChanged.emit() # 뷰 업데이트