added component base class

This commit is contained in:
tassaron 2017-05-29 20:39:11 -04:00
parent 6433f6d580
commit 8dd7b7d59a
4 changed files with 74 additions and 58 deletions

57
components/__base__.py Normal file
View File

@ -0,0 +1,57 @@
from PyQt4 import QtGui
class Component:
def __str__(self):
return self.__doc__
def preFrameRender(self, **kwargs):
for kwarg, value in kwargs.items():
exec('self.%s = value' % kwarg)
def pickColor(self):
color = QtGui.QColorDialog.getColor()
if color.isValid():
RGBstring = '%s,%s,%s' % (str(color.red()), str(color.green()), str(color.blue()))
btnStyle = "QPushButton { background-color : %s; outline: none; }" % color.name()
return RGBstring, btnStyle
def RGBFromString(self, string):
''' turns an RGB string like "255, 255, 255" into a tuple '''
try:
tup = tuple([int(i) for i in string.split(',')])
if len(tup) != 3:
raise ValueError
for i in tup:
if i > 255 or i < 0:
raise ValueError
return tup
except:
return (255, 255, 255)
'''
### Reference methods for creating a new component
### (Inherit from this class and define these)
def widget(self, parent):
self.parent = parent
page = uic.loadUi(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'example.ui'))
# connect widgets signals
self.page = page
return page
def update(self):
# read widget values
self.parent.drawPreview()
def previewRender(self, previewWorker):
width = int(previewWorker.core.settings.value('outputWidth'))
height = int(previewWorker.core.settings.value('outputHeight'))
image = Image.new("RGBA", (width, height), (0,0,0,0))
return image
def frameRender(self, moduleNo, frameNo):
width = int(self.worker.core.settings.value('outputWidth'))
height = int(self.worker.core.settings.value('outputHeight'))
image = Image.new("RGBA", (width, height), (0,0,0,0))
return image
'''

View File

@ -1,15 +1,13 @@
''' Original Audio Visualization '''
import numpy
from PIL import Image, ImageDraw
from PyQt4 import uic, QtGui
from PyQt4.QtGui import QColor
import os, random
from . import __base__
class Component:
def __str__(self):
return __doc__
class Component(__base__.Component):
'''Original Audio Visualization'''
def widget(self, parent):
self.parent = parent
self.visColor = (255,255,255)
@ -31,7 +29,7 @@ class Component:
def update(self):
self.layout = self.page.comboBox_visLayout.currentIndex()
self.visColor = RGBFromString(self.page.lineEdit_visColor.text())
self.visColor = self.RGBFromString(self.page.lineEdit_visColor.text())
self.parent.drawPreview()
def previewRender(self, previewWorker):
@ -41,8 +39,7 @@ class Component:
return drawBars(width, height, spectrum, self.visColor, self.layout)
def preFrameRender(self, **kwargs):
for kwarg, value in kwargs.items():
exec('self.%s = value' % kwarg)
super().preFrameRender(**kwargs)
self.smoothConstantDown = 0.08
self.smoothConstantUp = 0.8
self.lastSpectrum = None
@ -55,12 +52,9 @@ class Component:
return drawBars(width, height, self.lastSpectrum, self.visColor, self.layout)
def pickColor(self):
color = QtGui.QColorDialog.getColor()
if color.isValid():
RGBstring = '%s,%s,%s' % (str(color.red()), str(color.green()), str(color.blue()))
btnStyle = "QPushButton { background-color : %s; outline: none; }" % color.name()
self.page.lineEdit_visColor.setText(RGBstring)
self.page.pushButton_visColor.setStyleSheet(btnStyle)
RGBstring, btnStyle = super().pickColor()
self.page.lineEdit_visColor.setText(RGBstring)
self.page.pushButton_visColor.setStyleSheet(btnStyle)
def transformData(i, completeAudioArray, sampleSize, smoothConstantDown, smoothConstantUp, lastSpectrum):
if len(completeAudioArray) < (i + sampleSize):
@ -130,16 +124,3 @@ def drawBars(width, height, spectrum, color, layout):
im.paste(imTop, (0, y), mask=imTop)
return im
def RGBFromString(string):
''' turns an RGB string like "255, 255, 255" into a tuple '''
try:
tup = tuple([int(i) for i in string.split(',')])
if len(tup) != 3:
raise ValueError
for i in tup:
if i > 255 or i < 0:
raise ValueError
return tup
except:
return (255, 255, 255)

View File

@ -1,15 +1,13 @@
''' Title Text '''
from PIL import Image, ImageDraw
from PyQt4.QtGui import QPainter, QColor, QFont
from PyQt4 import uic, QtGui, QtCore
from PIL.ImageQt import ImageQt
import os, io
from . import __base__
class Component:
def __str__(self):
return __doc__
class Component(__base__.Component):
'''Title Text'''
def widget(self, parent):
height = int(parent.settings.value('outputHeight'))
width = int(parent.settings.value('outputWidth'))
@ -62,7 +60,7 @@ class Component:
self.fontSize = self.page.spinBox_fontSize.value()
self.xPosition = self.page.spinBox_xTextAlign.value()
self.yPosition = self.page.spinBox_yTextAlign.value()
self.textColor = RGBFromString(self.page.lineEdit_textColor.text())
self.textColor = self.RGBFromString(self.page.lineEdit_textColor.text())
fm = QtGui.QFontMetrics(self.titleFont)
if self.alignment == 0: #Left
self.xPosition = self.xPosition
@ -77,10 +75,6 @@ class Component:
width = int(previewWorker.core.settings.value('outputWidth'))
height = int(previewWorker.core.settings.value('outputHeight'))
return self.addText(width, height)
def preFrameRender(self, **kwargs):
for kwarg, value in kwargs.items():
exec('self.%s = value' % kwarg)
def frameRender(self, moduleNo, frameNo):
width = int(self.worker.core.settings.value('outputWidth'))
@ -112,22 +106,6 @@ class Component:
return Image.open(strio)
def pickColor(self):
color = QtGui.QColorDialog.getColor()
if color.isValid():
RGBstring = '%s,%s,%s' % (str(color.red()), str(color.green()), str(color.blue()))
btnStyle = "QPushButton { background-color : %s; outline: none; }" % color.name()
self.page.lineEdit_textColor.setText(RGBstring)
self.page.pushButton_textColor.setStyleSheet(btnStyle)
def RGBFromString(string):
''' turns an RGB string like "255, 255, 255" into a tuple '''
try:
tup = tuple([int(i) for i in string.split(',')])
if len(tup) != 3:
raise ValueError
for i in tup:
if i > 255 or i < 0:
raise ValueError
return tup
except:
return (255, 255, 255)
RGBstring, btnStyle = super().pickColor()
self.page.lineEdit_textColor.setText(RGBstring)
self.page.pushButton_textColor.setStyleSheet(btnStyle)

View File

@ -149,7 +149,7 @@ class Main(QtCore.QObject):
self.modules = self.findComponents()
for component in self.modules:
window.comboBox_componentSelection.addItem(component.__doc__)
window.comboBox_componentSelection.addItem(component.Component.__doc__)
window.listWidget_componentList.clicked.connect(lambda _: self.changeComponentWidget())
self.selectedComponents = []
@ -290,8 +290,8 @@ class Main(QtCore.QObject):
def addComponent(self, moduleIndex):
index = len(self.pages)
self.window.listWidget_componentList.addItem(self.modules[moduleIndex].__doc__)
self.selectedComponents.append(self.modules[moduleIndex].Component())
self.window.listWidget_componentList.addItem(self.selectedComponents[-1].__doc__)
self.pages.append(self.selectedComponents[-1].widget(self))
self.window.listWidget_componentList.setCurrentRow(index)
self.window.stackedWidget.addWidget(self.pages[-1])