2017-06-24 23:12:41 -04:00
|
|
|
from PyQt5 import uic, QtGui, QtCore, QtWidgets
|
2017-06-15 22:15:03 -04:00
|
|
|
from PIL import Image
|
2017-06-22 18:40:34 -04:00
|
|
|
import os
|
2017-05-29 20:39:11 -04:00
|
|
|
|
2017-06-13 22:47:18 -04:00
|
|
|
|
2017-06-12 22:34:37 -04:00
|
|
|
class Component(QtCore.QObject):
|
|
|
|
'''A base class for components to inherit from'''
|
2017-06-06 11:14:39 -04:00
|
|
|
|
2017-06-12 22:34:37 -04:00
|
|
|
# modified = QtCore.pyqtSignal(int, bool)
|
|
|
|
|
2017-06-22 18:40:34 -04:00
|
|
|
def __init__(self, moduleIndex, compPos, core):
|
2017-06-12 22:34:37 -04:00
|
|
|
super().__init__()
|
2017-06-07 23:22:55 -04:00
|
|
|
self.currentPreset = None
|
2017-06-26 19:07:49 -04:00
|
|
|
self.canceled = False
|
2017-06-12 22:34:37 -04:00
|
|
|
self.moduleIndex = moduleIndex
|
|
|
|
self.compPos = compPos
|
2017-06-22 18:40:34 -04:00
|
|
|
self.core = core
|
2017-06-07 23:22:55 -04:00
|
|
|
|
2017-05-29 20:39:11 -04:00
|
|
|
def __str__(self):
|
|
|
|
return self.__doc__
|
2017-05-30 19:31:10 -04:00
|
|
|
|
|
|
|
def version(self):
|
2017-05-30 22:05:56 -04:00
|
|
|
# change this number to identify new versions of a component
|
2017-05-30 19:31:10 -04:00
|
|
|
return 1
|
2017-06-04 13:00:36 -04:00
|
|
|
|
|
|
|
def cancel(self):
|
2017-06-12 22:34:37 -04:00
|
|
|
# please stop any lengthy process in response to this variable
|
2017-06-04 13:00:36 -04:00
|
|
|
self.canceled = True
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.canceled = False
|
2017-06-06 11:14:39 -04:00
|
|
|
|
2017-06-12 22:34:37 -04:00
|
|
|
def update(self):
|
2017-06-13 22:47:18 -04:00
|
|
|
self.modified.emit(self.compPos, self.savePreset())
|
|
|
|
# read your widget values, then call super().update()
|
2017-06-12 22:34:37 -04:00
|
|
|
|
|
|
|
def loadPreset(self, presetDict, presetName):
|
2017-06-22 18:40:34 -04:00
|
|
|
'''Subclasses take (presetDict, presetName=None) as args.
|
|
|
|
Must use super().loadPreset(presetDict, presetName) first,
|
|
|
|
then update self.page widgets using the preset dict.
|
|
|
|
'''
|
2017-06-12 22:34:37 -04:00
|
|
|
self.currentPreset = presetName \
|
2017-06-23 23:00:24 -04:00
|
|
|
if presetName is not None else presetDict['preset']
|
2017-06-22 18:40:34 -04:00
|
|
|
|
2017-05-29 20:39:11 -04:00
|
|
|
def preFrameRender(self, **kwargs):
|
2017-06-22 18:40:34 -04:00
|
|
|
'''Triggered only before a video is exported (video_thread.py)
|
|
|
|
self.worker = the video thread worker
|
|
|
|
self.completeAudioArray = a list of audio samples
|
|
|
|
self.sampleSize = number of audio samples per video frame
|
|
|
|
self.progressBarUpdate = signal to set progress bar number
|
|
|
|
self.progressBarSetText = signal to set progress bar text
|
|
|
|
Use the latter two signals to update the MainProgram if needed
|
|
|
|
for a long initialization procedure (i.e., for a visualizer)
|
|
|
|
'''
|
2017-05-31 18:00:10 -04:00
|
|
|
for var, value in kwargs.items():
|
|
|
|
exec('self.%s = value' % var)
|
2017-05-29 20:39:11 -04:00
|
|
|
|
2017-06-22 18:40:34 -04:00
|
|
|
def command(self, arg):
|
|
|
|
'''Configure a component using argument from the commandline.
|
|
|
|
Use super().command(arg) at the end of a subclass's method,
|
|
|
|
if no arguments are found in that method first
|
|
|
|
'''
|
|
|
|
if arg.startswith('preset='):
|
|
|
|
_, preset = arg.split('=', 1)
|
|
|
|
path = os.path.join(self.core.getPresetDir(self), preset)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
print('Couldn\'t locate preset "%s"' % preset)
|
|
|
|
quit(1)
|
|
|
|
else:
|
2017-06-23 23:00:24 -04:00
|
|
|
print('Opening "%s" preset on layer %s' % (
|
2017-06-23 17:14:39 -04:00
|
|
|
preset, self.compPos)
|
|
|
|
)
|
2017-06-22 18:40:34 -04:00
|
|
|
self.core.openPreset(path, self.compPos, preset)
|
|
|
|
else:
|
|
|
|
print(
|
2017-06-22 20:31:04 -04:00
|
|
|
self.__doc__, 'Usage:\n'
|
|
|
|
'Open a preset for this component:\n'
|
2017-06-22 22:23:04 -04:00
|
|
|
' "preset=Preset Name"')
|
2017-06-22 18:40:34 -04:00
|
|
|
self.commandHelp()
|
|
|
|
quit(0)
|
|
|
|
|
|
|
|
def commandHelp(self):
|
|
|
|
'''Print help text for this Component's commandline arguments'''
|
|
|
|
|
2017-06-15 22:15:03 -04:00
|
|
|
def blankFrame(self, width, height):
|
|
|
|
return Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
|
|
|
|
2017-05-29 20:39:11 -04:00
|
|
|
def pickColor(self):
|
2017-06-22 18:40:34 -04:00
|
|
|
'''Use color picker to get color input from the user,
|
|
|
|
and return this as an RGB string and QPushButton stylesheet.
|
|
|
|
In a subclass apply stylesheet to any color selection widgets
|
|
|
|
'''
|
2017-06-23 23:00:24 -04:00
|
|
|
dialog = QtWidgets.QColorDialog()
|
|
|
|
dialog.setOption(QtWidgets.QColorDialog.ShowAlphaChannel, True)
|
2017-06-11 12:52:29 -04:00
|
|
|
color = dialog.getColor()
|
2017-05-29 20:39:11 -04:00
|
|
|
if color.isValid():
|
2017-06-06 11:14:39 -04:00
|
|
|
RGBstring = '%s,%s,%s' % (
|
|
|
|
str(color.red()), str(color.green()), str(color.blue()))
|
|
|
|
btnStyle = "QPushButton{background-color: %s; outline: none;}" \
|
|
|
|
% color.name()
|
2017-05-30 22:05:56 -04:00
|
|
|
return RGBstring, btnStyle
|
|
|
|
else:
|
|
|
|
return None, None
|
2017-05-29 20:39:11 -04:00
|
|
|
|
|
|
|
def RGBFromString(self, string):
|
2017-06-22 18:40:34 -04:00
|
|
|
''' Turns an RGB string like "255, 255, 255" into a tuple '''
|
2017-06-06 11:14:39 -04:00
|
|
|
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)
|
2017-05-29 20:39:11 -04:00
|
|
|
|
2017-06-24 23:12:41 -04:00
|
|
|
def loadUi(self, filename):
|
|
|
|
return uic.loadUi(os.path.join(self.core.componentsPath, filename))
|
|
|
|
|
2017-05-29 20:39:11 -04:00
|
|
|
'''
|
|
|
|
### Reference methods for creating a new component
|
|
|
|
### (Inherit from this class and define these)
|
2017-06-06 11:14:39 -04:00
|
|
|
|
2017-05-29 20:39:11 -04:00
|
|
|
def widget(self, parent):
|
|
|
|
self.parent = parent
|
2017-06-06 11:14:39 -04:00
|
|
|
page = uic.loadUi(os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)), 'example.ui'))
|
2017-06-22 18:40:34 -04:00
|
|
|
# --- connect widget signals here ---
|
2017-05-29 20:39:11 -04:00
|
|
|
self.page = page
|
|
|
|
return page
|
|
|
|
|
2017-06-22 19:59:31 -04:00
|
|
|
def update(self):
|
|
|
|
super().update()
|
|
|
|
self.parent.drawPreview()
|
|
|
|
|
2017-05-29 20:39:11 -04:00
|
|
|
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
|
2017-06-06 11:14:39 -04:00
|
|
|
|
2017-05-29 20:39:11 -04:00
|
|
|
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
|
2017-06-25 18:12:16 -04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def names(cls):
|
|
|
|
# Alternative names for renaming a component between project files
|
|
|
|
return []
|
2017-05-29 20:39:11 -04:00
|
|
|
'''
|
2017-06-06 20:50:53 -04:00
|
|
|
|
2017-06-23 23:00:24 -04:00
|
|
|
|
2017-06-06 20:50:53 -04:00
|
|
|
class BadComponentInit(Exception):
|
|
|
|
def __init__(self, arg, name):
|
2017-06-23 23:00:24 -04:00
|
|
|
string = '''################################
|
2017-06-06 20:50:53 -04:00
|
|
|
Mandatory argument "%s" not specified
|
|
|
|
in %s instance initialization
|
|
|
|
###################################'''
|
|
|
|
print(string % (arg, name))
|
|
|
|
quit()
|