This repository has been archived on 2020-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
pyaudviz/src/components/text.py

173 lines
6.5 KiB
Python
Raw Normal View History

from PIL import Image, ImageDraw
from PyQt5.QtGui import QColor, QFont
2017-06-24 23:12:41 -04:00
from PyQt5 import QtGui, QtCore, QtWidgets
import os
2017-07-02 20:46:48 -04:00
from component import Component
from frame import FramePainter
2017-07-02 20:46:48 -04:00
class Component(Component):
2017-05-29 20:39:11 -04:00
'''Title Text'''
modified = QtCore.pyqtSignal(int, dict)
def __init__(self, *args):
super().__init__(*args)
2017-06-03 10:01:47 -04:00
self.titleFont = QFont()
def widget(self, parent):
height = int(parent.settings.value('outputHeight'))
width = int(parent.settings.value('outputWidth'))
2017-06-22 19:59:31 -04:00
self.parent = parent
self.textColor = (255, 255, 255)
self.title = 'Text'
self.alignment = 1
2017-06-03 15:38:21 -04:00
self.fontSize = height / 13.5
2017-06-22 19:59:31 -04:00
fm = QtGui.QFontMetrics(self.titleFont)
self.xPosition = width / 2 - fm.width(self.title)/2
2017-06-03 15:38:21 -04:00
self.yPosition = height / 2 * 1.036
2017-06-24 23:12:41 -04:00
page = self.loadUi('text.ui')
page.comboBox_textAlign.addItem("Left")
page.comboBox_textAlign.addItem("Middle")
page.comboBox_textAlign.addItem("Right")
page.lineEdit_textColor.setText('%s,%s,%s' % self.textColor)
page.pushButton_textColor.clicked.connect(self.pickColor)
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.textColor).name()
page.pushButton_textColor.setStyleSheet(btnStyle)
page.lineEdit_title.setText(self.title)
page.comboBox_textAlign.setCurrentIndex(int(self.alignment))
page.spinBox_fontSize.setValue(int(self.fontSize))
page.spinBox_xTextAlign.setValue(int(self.xPosition))
page.spinBox_yTextAlign.setValue(int(self.yPosition))
page.fontComboBox_titleFont.currentFontChanged.connect(self.update)
page.lineEdit_title.textChanged.connect(self.update)
page.comboBox_textAlign.currentIndexChanged.connect(self.update)
page.spinBox_xTextAlign.valueChanged.connect(self.update)
page.spinBox_yTextAlign.valueChanged.connect(self.update)
page.spinBox_fontSize.valueChanged.connect(self.update)
page.lineEdit_textColor.textChanged.connect(self.update)
self.page = page
return page
def update(self):
self.title = self.page.lineEdit_title.text()
self.alignment = self.page.comboBox_textAlign.currentIndex()
self.titleFont = self.page.fontComboBox_titleFont.currentFont()
self.fontSize = self.page.spinBox_fontSize.value()
self.xPosition = self.page.spinBox_xTextAlign.value()
self.yPosition = self.page.spinBox_yTextAlign.value()
2017-06-06 11:14:39 -04:00
self.textColor = self.RGBFromString(
self.page.lineEdit_textColor.text())
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.textColor).name()
self.page.pushButton_textColor.setStyleSheet(btnStyle)
self.parent.drawPreview()
super().update()
def getXY(self):
'''Returns true x, y after considering alignment settings'''
2017-05-28 14:49:35 -04:00
fm = QtGui.QFontMetrics(self.titleFont)
if self.alignment == 0: # Left
x = self.xPosition
if self.alignment == 1: # Middle
offset = fm.width(self.title)/2
x = self.xPosition - offset
if self.alignment == 2: # Right
offset = fm.width(self.title)
x = self.xPosition - offset
return x, self.yPosition
def loadPreset(self, pr, presetName=None):
super().loadPreset(pr, presetName)
self.page.lineEdit_title.setText(pr['title'])
font = QFont()
font.fromString(pr['titleFont'])
self.page.fontComboBox_titleFont.setCurrentFont(font)
self.page.spinBox_fontSize.setValue(pr['fontSize'])
self.page.comboBox_textAlign.setCurrentIndex(pr['alignment'])
self.page.spinBox_xTextAlign.setValue(pr['xPosition'])
self.page.spinBox_yTextAlign.setValue(pr['yPosition'])
self.page.lineEdit_textColor.setText('%s,%s,%s' % pr['textColor'])
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*pr['textColor']).name()
self.page.pushButton_textColor.setStyleSheet(btnStyle)
2017-05-28 21:24:51 -04:00
def savePreset(self):
return {
'preset': self.currentPreset,
2017-06-06 11:14:39 -04:00
'title': self.title,
'titleFont': self.titleFont.toString(),
'alignment': self.alignment,
'fontSize': self.fontSize,
'xPosition': self.xPosition,
'yPosition': self.yPosition,
'textColor': self.textColor
}
2017-05-28 21:24:51 -04:00
def previewRender(self, previewWorker):
width = int(previewWorker.core.settings.value('outputWidth'))
height = int(previewWorker.core.settings.value('outputHeight'))
return self.addText(width, height)
def properties(self):
return ['static']
def frameRender(self, layerNo, frameNo):
width = int(self.worker.core.settings.value('outputWidth'))
height = int(self.worker.core.settings.value('outputHeight'))
return self.addText(width, height)
def addText(self, width, height):
x, y = self.getXY()
image = FramePainter(width, height)
self.titleFont.setPixelSize(self.fontSize)
image.setFont(self.titleFont)
image.setPen(self.textColor)
image.drawText(x, y, self.title)
return image.finalize()
def pickColor(self):
2017-05-29 20:39:11 -04:00
RGBstring, btnStyle = super().pickColor()
if not RGBstring:
return
2017-05-29 20:39:11 -04:00
self.page.lineEdit_textColor.setText(RGBstring)
self.page.pushButton_textColor.setStyleSheet(btnStyle)
def commandHelp(self):
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')
def command(self, arg):
if not arg.startswith('preset=') and '=' in arg:
key, arg = arg.split('=', 1)
if key == 'color':
self.page.lineEdit_textColor.setText(arg)
return
elif key == 'size':
self.page.spinBox_fontSize.setValue(int(arg))
return
elif key == 'x':
self.page.spinBox_xTextAlign.setValue(int(arg))
return
elif key == 'y':
self.page.spinBox_yTextAlign.setValue(int(arg))
return
elif key == 'title':
self.page.lineEdit_title.setText(arg)
return
super().command(arg)