diff options
| author | tassaron | 2026-01-11 14:29:58 -0500 |
|---|---|---|
| committer | tassaron | 2026-01-11 14:29:58 -0500 |
| commit | 669756b391d26661cf2e2a97a304e73343ef6655 (patch) | |
| tree | 9cf2d4858c209bdab9f44d5c7f95c2a30b37f7a6 /src/components/text.py | |
| parent | 9d45f7f1a986aaa5d3c084c7ae747442b94a61b1 (diff) | |
update to Qt 6 and Pillow 12
and yeah, I accidentally ran black on the codebase. I don't want to spend more free time fixing that. All of these changes are simple renames or removals, nothing too major.
Diffstat (limited to 'src/components/text.py')
| -rw-r--r-- | src/components/text.py | 148 |
1 files changed, 81 insertions, 67 deletions
diff --git a/src/components/text.py b/src/components/text.py index 3238d2a..40c981a 100644 --- a/src/components/text.py +++ b/src/components/text.py @@ -1,22 +1,22 @@ from PIL import ImageEnhance, ImageFilter, ImageChops -from PyQt5.QtGui import QColor, QFont -from PyQt5 import QtGui, QtCore, QtWidgets +from PyQt6.QtGui import QColor, QFont +from PyQt6 import QtGui, QtCore, QtWidgets import os import logging from ..component import Component from ..toolkit.frame import FramePainter, PaintColor -log = logging.getLogger('AVP.Components.Text') +log = logging.getLogger("AVP.Components.Text") class Component(Component): - name = 'Title Text' - version = '1.0.1' + name = "Title Text" + version = "1.0.1" def widget(self, *args): super().widget(*args) - self.title = 'Text' + self.title = "Text" self.alignment = 1 self.titleFont = QFont() self.fontSize = self.height / 13.5 @@ -29,33 +29,44 @@ class Component(Component): self.page.lineEdit_title.setText(self.title) self.page.pushButton_center.clicked.connect(self.centerXY) - self.page.fontComboBox_titleFont.currentFontChanged.connect(self._sendUpdateSignal) + self.page.fontComboBox_titleFont.currentFontChanged.connect( + self._sendUpdateSignal + ) # The QFontComboBox must be connected directly to the Qt Signal # which triggers the preview to update. # This unfortunately makes changing the font into a non-undoable action. # Must be something broken in the conversion to a ComponentAction - self.trackWidgets({ - 'textColor': self.page.lineEdit_textColor, - 'title': self.page.lineEdit_title, - 'alignment': self.page.comboBox_textAlign, - 'fontSize': self.page.spinBox_fontSize, - 'xPosition': self.page.spinBox_xTextAlign, - 'yPosition': self.page.spinBox_yTextAlign, - 'fontStyle': self.page.comboBox_fontStyle, - 'stroke': self.page.spinBox_stroke, - 'strokeColor': self.page.lineEdit_strokeColor, - 'shadow': self.page.checkBox_shadow, - 'shadX': self.page.spinBox_shadX, - 'shadY': self.page.spinBox_shadY, - 'shadBlur': self.page.spinBox_shadBlur, - }, colorWidgets={ - 'textColor': self.page.pushButton_textColor, - 'strokeColor': self.page.pushButton_strokeColor, - }, relativeWidgets=[ - 'xPosition', 'yPosition', 'fontSize', - 'stroke', 'shadX', 'shadY', 'shadBlur' - ]) + self.trackWidgets( + { + "textColor": self.page.lineEdit_textColor, + "title": self.page.lineEdit_title, + "alignment": self.page.comboBox_textAlign, + "fontSize": self.page.spinBox_fontSize, + "xPosition": self.page.spinBox_xTextAlign, + "yPosition": self.page.spinBox_yTextAlign, + "fontStyle": self.page.comboBox_fontStyle, + "stroke": self.page.spinBox_stroke, + "strokeColor": self.page.lineEdit_strokeColor, + "shadow": self.page.checkBox_shadow, + "shadX": self.page.spinBox_shadX, + "shadY": self.page.spinBox_shadY, + "shadBlur": self.page.spinBox_shadBlur, + }, + colorWidgets={ + "textColor": self.page.pushButton_textColor, + "strokeColor": self.page.pushButton_strokeColor, + }, + relativeWidgets=[ + "xPosition", + "yPosition", + "fontSize", + "stroke", + "shadX", + "shadY", + "shadBlur", + ], + ) self.centerXY() def update(self): @@ -74,20 +85,23 @@ class Component(Component): self.page.spinBox_shadBlur.setHidden(True) def centerXY(self): - self.setRelativeWidget('xPosition', 0.5) - self.setRelativeWidget('yPosition', 0.521) + self.setRelativeWidget("xPosition", 0.5) + self.setRelativeWidget("yPosition", 0.521) def getXY(self): - '''Returns true x, y after considering alignment settings''' + """Returns true x, y after considering alignment settings""" fm = QtGui.QFontMetrics(self.titleFont) - x = self.pixelValForAttr('xPosition') + text_width = fm.boundingRect(self.title).width() + x = self.pixelValForAttr("xPosition") - if self.alignment == 1: # Middle - offset = int(fm.width(self.title)/2) - x -= offset - if self.alignment == 2: # Right - offset = fm.width(self.title) - x -= offset + if self.alignment == 1: # Middle + offset = int(text_width / 2) + elif self.alignment == 2: # Right + offset = text_width + else: + raise ValueError(f"Alignment value {self.alignment} unknown") + + x -= offset return x, self.yPosition @@ -95,21 +109,21 @@ class Component(Component): super().loadPreset(pr, *args) font = QFont() - font.fromString(pr['titleFont']) + font.fromString(pr["titleFont"]) self.page.fontComboBox_titleFont.setCurrentFont(font) def savePreset(self): saveValueStore = super().savePreset() - saveValueStore['titleFont'] = self.titleFont.toString() + saveValueStore["titleFont"] = self.titleFont.toString() return saveValueStore def previewRender(self): return self.addText(self.width, self.height) def properties(self): - props = ['static'] + props = ["static"] if not self.title: - props.append('error') + props.append("error") return props def error(self): @@ -121,26 +135,26 @@ class Component(Component): def addText(self, width, height): font = self.titleFont font.setPixelSize(self.fontSize) - font.setStyle(QFont.StyleNormal) - font.setWeight(QFont.Normal) - font.setCapitalization(QFont.MixedCase) + font.setStyle(QFont.Style.StyleNormal) + font.setWeight(QFont.Weight.Normal) + font.setCapitalization(QFont.Capitalization.MixedCase) if self.fontStyle == 1: - font.setWeight(QFont.DemiBold) + font.setWeight(QFont.Weight.DemiBold) if self.fontStyle == 2: - font.setWeight(QFont.Bold) + font.setWeight(QFont.Weight.Bold) elif self.fontStyle == 3: - font.setStyle(QFont.StyleItalic) + font.setStyle(QFont.Style.StyleItalic) elif self.fontStyle == 4: - font.setWeight(QFont.Bold) - font.setStyle(QFont.StyleItalic) + font.setWeight(QFont.Weight.Bold) + font.setStyle(QFont.Style.StyleItalic) elif self.fontStyle == 5: - font.setStyle(QFont.StyleOblique) + font.setStyle(QFont.Style.StyleOblique) elif self.fontStyle == 6: - font.setCapitalization(QFont.SmallCaps) + font.setCapitalization(QFont.Capitalization.SmallCaps) image = FramePainter(width, height) x, y = self.getXY() - log.debug('Text position translates to %s, %s', x, y) + log.debug("Text position translates to %s, %s", x, y) if self.stroke > 0: outliner = QtGui.QPainterPathStroker() outliner.setWidth(self.stroke) @@ -149,16 +163,16 @@ class Component(Component): # PathStroker ignores smallcaps so we need this weird hack path.addText(x, y, font, self.title[0]) fm = QtGui.QFontMetrics(font) - newX = x + fm.width(self.title[0]) + newX = x + fm.boundingRect(self.title[0]).width() strokeFont = self.page.fontComboBox_titleFont.currentFont() - strokeFont.setCapitalization(QFont.SmallCaps) + strokeFont.setCapitalization(QFont.Capitalization.SmallCaps) strokeFont.setPixelSize(int((self.fontSize / 7) * 5)) - strokeFont.setLetterSpacing(QFont.PercentageSpacing, 139) + strokeFont.setLetterSpacing(QFont.SpacingType.PercentageSpacing, 139) path.addText(newX, y, strokeFont, self.title[1:]) else: path.addText(x, y, font, self.title) path = outliner.createStroke(path) - image.setPen(QtCore.Qt.NoPen) + image.setPen(QtCore.Qt.PenStyle.NoPen) image.setBrush(PaintColor(*self.strokeColor)) image.drawPath(path) @@ -178,27 +192,27 @@ class Component(Component): return frame def commandHelp(self): - print('Enter a string to use as centred white text:') + print("Enter a string to use as centred white text:") print(' "title=User Error"') - print('Specify a text color:\n color=255,255,255') - print('Set custom x, y position:\n x=500 y=500') + print("Specify a text color:\n color=255,255,255") + print("Set custom x, y position:\n x=500 y=500") def command(self, arg): - if '=' in arg: - key, arg = arg.split('=', 1) - if key == 'color': + if "=" in arg: + key, arg = arg.split("=", 1) + if key == "color": self.page.lineEdit_textColor.setText(arg) return - elif key == 'size': + elif key == "size": self.page.spinBox_fontSize.setValue(int(arg)) return - elif key == 'x': + elif key == "x": self.page.spinBox_xTextAlign.setValue(int(arg)) return - elif key == 'y': + elif key == "y": self.page.spinBox_yTextAlign.setValue(int(arg)) return - elif key == 'title': + elif key == "title": self.page.lineEdit_title.setText(arg) return super().command(arg) |
