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

138 lines
4.4 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 toolkit.frame import FramePainter
2017-07-02 20:46:48 -04:00
class Component(Component):
name = 'Title Text'
version = '1.0.1'
def __init__(self, *args):
super().__init__(*args)
2017-06-03 10:01:47 -04:00
self.titleFont = QFont()
def widget(self, *args):
super().widget(*args)
self.textColor = (255, 255, 255)
self.title = 'Text'
self.alignment = 1
self.fontSize = self.height / 13.5
self.page.comboBox_textAlign.addItem("Left")
self.page.comboBox_textAlign.addItem("Middle")
self.page.comboBox_textAlign.addItem("Right")
self.page.comboBox_textAlign.setCurrentIndex(int(self.alignment))
self.page.lineEdit_textColor.setText('%s,%s,%s' % self.textColor)
self.page.spinBox_fontSize.setValue(int(self.fontSize))
self.page.lineEdit_title.setText(self.title)
self.page.pushButton_center.clicked.connect(self.centerXY)
self.page.fontComboBox_titleFont.currentFontChanged.connect(
self.update
)
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,
}, colorWidgets={
'textColor': self.page.pushButton_textColor,
}, relativeWidgets=[
'xPosition', 'yPosition', 'fontSize',
])
self.centerXY()
def update(self):
self.titleFont = self.page.fontComboBox_titleFont.currentFont()
super().update()
def centerXY(self):
self.setRelativeWidget('xPosition', 0.5)
self.setRelativeWidget('yPosition', 0.5)
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
2017-07-11 06:06:22 -04:00
x = int(self.xPosition)
if self.alignment == 1: # Middle
2017-07-13 00:05:11 -04:00
offset = int(fm.width(self.title)/2)
x = self.xPosition - offset
if self.alignment == 2: # Right
offset = fm.width(self.title)
2017-07-13 00:05:11 -04:00
x = self.xPosition - offset
return x, self.yPosition
def loadPreset(self, pr, *args):
super().loadPreset(pr, *args)
font = QFont()
font.fromString(pr['titleFont'])
self.page.fontComboBox_titleFont.setCurrentFont(font)
2017-05-28 21:24:51 -04:00
def savePreset(self):
saveValueStore = super().savePreset()
saveValueStore['titleFont'] = self.titleFont.toString()
return saveValueStore
2017-05-28 21:24:51 -04:00
def previewRender(self):
return self.addText(self.width, self.height)
def properties(self):
props = ['static']
if not self.title:
props.append('error')
return props
def error(self):
return "No text provided."
def frameRender(self, frameNo):
return self.addText(self.width, self.height)
def addText(self, width, height):
2017-07-11 06:06:22 -04:00
image = FramePainter(width, height)
self.titleFont.setPixelSize(self.fontSize)
image.setFont(self.titleFont)
image.setPen(self.textColor)
2017-07-11 06:06:22 -04:00
x, y = self.getXY()
image.drawText(x, y, self.title)
return image.finalize()
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 '=' 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)