wip: refactor

This commit is contained in:
2023-08-15 14:23:06 +08:00
parent 7e0aaeef35
commit 0126ce6b9a
13 changed files with 1010 additions and 734 deletions

View File

@ -0,0 +1,70 @@
from PySide6.QtCore import QFileInfo, QModelIndex, QRect, QSize, Qt
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QLabel, QStyledItemDelegate, QWidget
class ImageDelegate(QStyledItemDelegate):
def getPixmap(self, index: QModelIndex):
raise NotImplementedError("getPixmap not implemented.")
def getImagePath(self, index: QModelIndex):
raise NotImplementedError("getImagePath not implemented.")
def scalePixmap(self, pixmap: QPixmap):
return pixmap.scaled(
100,
100,
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
def paint(self, painter, option, index):
pixmap = self.getPixmap(index)
if not isinstance(pixmap, QPixmap):
imagePath = self.getImagePath(index)
option.text = imagePath
super().paint(painter, option, index)
else:
pixmap = self.scalePixmap(pixmap)
# https://stackoverflow.com/a/32047499/16484891
# CC BY-SA 3.0
x = option.rect.center().x() - pixmap.rect().width() / 2
y = option.rect.center().y() - pixmap.rect().height() / 2
painter.drawPixmap(
QRect(x, y, pixmap.rect().width(), pixmap.rect().height()), pixmap
)
def sizeHint(self, option, index) -> QSize:
pixmap = self.getPixmap(index)
if isinstance(pixmap, QPixmap):
pixmap = self.scalePixmap(pixmap)
return pixmap.size()
else:
return QSize(100, 75)
def createEditor(self, parent, option, index) -> QWidget:
pixmap = self.getPixmap(index)
if isinstance(pixmap, QPixmap):
label = QLabel(parent)
label.setWindowFlags(Qt.WindowType.Window)
label.setWindowFlag(Qt.WindowType.WindowMinimizeButtonHint, False)
label.setWindowFlag(Qt.WindowType.WindowMaximizeButtonHint, False)
label.setWindowFlag(Qt.WindowType.WindowCloseButtonHint, True)
label.setWindowTitle(QFileInfo(self.getImagePath(index)).fileName())
pixmap = pixmap.scaled(
800,
800,
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
label.setMinimumSize(pixmap.size())
label.setPixmap(pixmap)
label.move(parent.mapToGlobal(parent.pos()))
return label
def setModelData(self, *args):
...
def updateEditorGeometry(self, *args):
...

View File

@ -126,6 +126,8 @@ class ScoreDelegate(TextSegmentDelegate):
]
]
score_str = str(score.score).rjust(8, "0")
score_str = f"{score_str[:-6]}'{score_str[-6:-3]}'{score_str[-3:]}"
score_font = QFont(option.font)
score_font.setPointSize(12)
score_grade_font = QFont(score_font)
@ -140,7 +142,7 @@ class ScoreDelegate(TextSegmentDelegate):
self.FontRole: score_grade_font,
},
{self.TextRole: " | "},
{self.TextRole: str(score.score), self.FontRole: score_font},
{self.TextRole: score_str, self.FontRole: score_font},
],
[
{