Add color and bold font if difference in model

* Could be better optimized if there is a way to
set those variables earlier in the model or somewhere
in the viewer when it requests the data.
* Right now it compares strings(?) many times for every role
we handle, which is not ideal.
This commit is contained in:
glubsy 2020-06-30 04:20:27 +02:00
parent eb6946343b
commit c6f5031dd8
1 changed files with 19 additions and 4 deletions

View File

@ -8,6 +8,7 @@
from PyQt5.QtCore import Qt, QAbstractTableModel
from PyQt5.QtWidgets import QHeaderView, QTableView
from PyQt5.QtGui import QFont, QBrush, QColor
from hscommon.trans import trget
@ -27,12 +28,25 @@ class DetailsModel(QAbstractTableModel):
def data(self, index, role):
if not index.isValid():
return None
if role != Qt.DisplayRole:
return None
# Skip first value "Attribute"
column = index.column() + 1
row = index.row()
return self.model.row(row)[column]
if self.model.row(row)[0] == "Dupe Count":
if role != Qt.DisplayRole:
return None
return self.model.row(row)[column]
if role == Qt.DisplayRole:
return self.model.row(row)[column]
if role == Qt.ForegroundRole and self.model.row(row)[1] != self.model.row(row)[2]:
return QBrush(QColor(250, 20, 20)) # red
if role == Qt.FontRole and self.model.row(row)[1] != self.model.row(row)[2]:
font = QFont(self.model.view.font()) # or simply QFont()
font.setBold(True)
return font
return None # QVariant()
def headerData(self, section, orientation, role):
if (
@ -46,6 +60,7 @@ class DetailsModel(QAbstractTableModel):
and role == Qt.DisplayRole
and section < self.model.row_count()
):
# Read "Attribute" cell for horizontal header
return self.model.row(section)[0]
return None
@ -58,7 +73,7 @@ class DetailsTable(QTableView):
QTableView.__init__(self, *args)
self.setAlternatingRowColors(True)
self.setSelectionBehavior(QTableView.SelectRows)
self.setSelectionMode(QTableView.SingleSelection)
self.setSelectionMode(QTableView.NoSelection)
self.setShowGrid(False)
self.setWordWrap(False)
self.setCornerButtonEnabled(False)