component class now tracks colorwidgets

so adding new color-selection widgets is now simple
This commit is contained in:
tassaron 2017-08-01 17:57:39 -04:00
parent a472246dab
commit 3c1b52205f
8 changed files with 90 additions and 170 deletions

View File

@ -2,7 +2,7 @@ from setuptools import setup
import os import os
__version__ = '2.0.0.rc2' __version__ = '2.0.0.rc3'
def package_files(directory): def package_files(directory):

View File

@ -3,18 +3,20 @@
on making a valid component. on making a valid component.
''' '''
from PyQt5 import uic, QtCore, QtWidgets from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtGui import QColor
import os import os
import sys import sys
import time import time
from toolkit.frame import BlankFrame from toolkit.frame import BlankFrame
from toolkit import getWidgetValue, setWidgetValue, connectWidget from toolkit import (
getWidgetValue, setWidgetValue, connectWidget, rgbFromString
)
class ComponentMetaclass(type(QtCore.QObject)): class ComponentMetaclass(type(QtCore.QObject)):
''' '''
Checks the validity of each Component class imported, and Checks the validity of each Component class and mutates some attrs.
mutates some attributes for easier use by the core program.
E.g., takes only major version from version string & decorates methods E.g., takes only major version from version string & decorates methods
''' '''
@ -173,6 +175,8 @@ class Component(QtCore.QObject, metaclass=ComponentMetaclass):
self._trackedWidgets = {} self._trackedWidgets = {}
self._presetNames = {} self._presetNames = {}
self._commandArgs = {} self._commandArgs = {}
self._colorWidgets = {}
self._relativeWidgets = {}
self._lockedProperties = None self._lockedProperties = None
self._lockedError = None self._lockedError = None
@ -188,7 +192,7 @@ class Component(QtCore.QObject, metaclass=ComponentMetaclass):
) )
# =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~ # =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~
# Critical Methods # Render Methods
# =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~ # =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~==~=~=~=~=~=~=~=~=~=~=~=~=~=~
def previewRender(self): def previewRender(self):
@ -286,7 +290,17 @@ class Component(QtCore.QObject, metaclass=ComponentMetaclass):
Call super() at the END if you need to subclass this. Call super() at the END if you need to subclass this.
''' '''
for attr, widget in self._trackedWidgets.items(): for attr, widget in self._trackedWidgets.items():
if attr in self._colorWidgets:
rgbTuple = rgbFromString(widget.text())
setattr(self, attr, rgbTuple)
btnStyle = (
"QPushButton { background-color : %s; outline: none; }"
% QColor(*rgbTuple).name()
)
self._colorWidgets[attr].setStyleSheet(btnStyle)
else:
setattr(self, attr, getWidgetValue(widget)) setattr(self, attr, getWidgetValue(widget))
if not self.core.openingProject: if not self.core.openingProject:
self.parent.drawPreview() self.parent.drawPreview()
saveValueStore = self.savePreset() saveValueStore = self.savePreset()
@ -305,6 +319,15 @@ class Component(QtCore.QObject, metaclass=ComponentMetaclass):
key = attr if attr not in self._presetNames \ key = attr if attr not in self._presetNames \
else self._presetNames[attr] else self._presetNames[attr]
val = presetDict[key] val = presetDict[key]
if attr in self._colorWidgets:
widget.setText('%s,%s,%s' % val)
btnStyle = (
"QPushButton { background-color : %s; outline: none; }"
% QColor(*val).name()
)
self._colorWidgets[attr].setStyleSheet(btnStyle)
else:
setWidgetValue(widget, val) setWidgetValue(widget, val)
def savePreset(self): def savePreset(self):
@ -352,7 +375,12 @@ class Component(QtCore.QObject, metaclass=ComponentMetaclass):
self._trackedWidgets = trackDict self._trackedWidgets = trackDict
for kwarg in kwargs: for kwarg in kwargs:
try: try:
if kwarg in ('presetNames', 'commandArgs'): if kwarg in (
'presetNames',
'commandArgs',
'colorWidgets',
'relativeWidgets',
):
setattr(self, '_%s' % kwarg, kwargs[kwarg]) setattr(self, '_%s' % kwarg, kwargs[kwarg])
else: else:
raise ComponentError( raise ComponentError(
@ -360,6 +388,37 @@ class Component(QtCore.QObject, metaclass=ComponentMetaclass):
except ComponentError: except ComponentError:
continue continue
if kwarg == 'colorWidgets':
def makeColorFunc(attr):
def pickColor_():
self.pickColor(
self._trackedWidgets[attr],
self._colorWidgets[attr]
)
return pickColor_
self._colorFuncs = {
attr: makeColorFunc(attr) for attr in kwargs[kwarg]
}
for attr, func in self._colorFuncs.items():
self._colorWidgets[attr].clicked.connect(func)
self._colorWidgets[attr].setStyleSheet(
"QPushButton {"
"background-color : #FFFFFF; outline: none; }"
)
def pickColor(self, textWidget, button):
'''Use color picker to get color input from the user.'''
dialog = QtWidgets.QColorDialog()
dialog.setOption(QtWidgets.QColorDialog.ShowAlphaChannel, True)
color = dialog.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()
textWidget.setText(RGBstring)
button.setStyleSheet(btnStyle)
def lockProperties(self, propList): def lockProperties(self, propList):
self._lockedProperties = propList self._lockedProperties = propList

View File

@ -6,7 +6,6 @@ import os
from component import Component from component import Component
from toolkit.frame import BlankFrame, FloodFrame, FramePainter, PaintColor from toolkit.frame import BlankFrame, FloodFrame, FramePainter, PaintColor
from toolkit import rgbFromString, pickColor
class Component(Component): class Component(Component):
@ -14,25 +13,12 @@ class Component(Component):
version = '1.0.0' version = '1.0.0'
def widget(self, *args): def widget(self, *args):
self.color1 = (0, 0, 0)
self.color2 = (133, 133, 133)
self.x = 0 self.x = 0
self.y = 0 self.y = 0
super().widget(*args) super().widget(*args)
self.page.lineEdit_color1.setText('%s,%s,%s' % self.color1) self.page.lineEdit_color1.setText('0,0,0')
self.page.lineEdit_color2.setText('%s,%s,%s' % self.color2) self.page.lineEdit_color2.setText('133,133,133')
btnStyle1 = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.color1).name()
btnStyle2 = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.color2).name()
self.page.pushButton_color1.setStyleSheet(btnStyle1)
self.page.pushButton_color2.setStyleSheet(btnStyle2)
self.page.pushButton_color1.clicked.connect(lambda: self.pickColor(1))
self.page.pushButton_color2.clicked.connect(lambda: self.pickColor(2))
# disable color #2 until non-default 'fill' option gets changed # disable color #2 until non-default 'fill' option gets changed
self.page.lineEdit_color2.setDisabled(True) self.page.lineEdit_color2.setDisabled(True)
@ -66,16 +52,18 @@ class Component(Component):
'LG_end': self.page.spinBox_linearGradient_end, 'LG_end': self.page.spinBox_linearGradient_end,
'RG_centre': self.page.spinBox_radialGradient_spread, 'RG_centre': self.page.spinBox_radialGradient_spread,
'fillType': self.page.comboBox_fill, 'fillType': self.page.comboBox_fill,
'color1': self.page.lineEdit_color1,
'color2': self.page.lineEdit_color2,
}, presetNames={ }, presetNames={
'sizeWidth': 'width', 'sizeWidth': 'width',
'sizeHeight': 'height', 'sizeHeight': 'height',
} }, colorWidgets={
'color1': self.page.pushButton_color1,
'color2': self.page.pushButton_color2,
},
) )
def update(self): def update(self):
self.color1 = rgbFromString(self.page.lineEdit_color1.text())
self.color2 = rgbFromString(self.page.lineEdit_color2.text())
fillType = self.page.comboBox_fill.currentIndex() fillType = self.page.comboBox_fill.currentIndex()
if fillType == 0: if fillType == 0:
self.page.lineEdit_color2.setEnabled(False) self.page.lineEdit_color2.setEnabled(False)
@ -161,36 +149,6 @@ class Component(Component):
return image.finalize() return image.finalize()
def loadPreset(self, pr, *args):
super().loadPreset(pr, *args)
self.page.lineEdit_color1.setText('%s,%s,%s' % pr['color1'])
self.page.lineEdit_color2.setText('%s,%s,%s' % pr['color2'])
btnStyle1 = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*pr['color1']).name()
btnStyle2 = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*pr['color2']).name()
self.page.pushButton_color1.setStyleSheet(btnStyle1)
self.page.pushButton_color2.setStyleSheet(btnStyle2)
def savePreset(self):
saveValueStore = super().savePreset()
saveValueStore['color1'] = self.color1
saveValueStore['color2'] = self.color2
return saveValueStore
def pickColor(self, num):
RGBstring, btnStyle = pickColor()
if not RGBstring:
return
if num == 1:
self.page.lineEdit_color1.setText(RGBstring)
self.page.pushButton_color1.setStyleSheet(btnStyle)
else:
self.page.lineEdit_color2.setText(RGBstring)
self.page.pushButton_color2.setStyleSheet(btnStyle)
def commandHelp(self): def commandHelp(self):
print('Specify a color:\n color=255,255,255') print('Specify a color:\n color=255,255,255')

View File

@ -8,7 +8,6 @@ from copy import copy
from component import Component from component import Component
from toolkit.frame import BlankFrame from toolkit.frame import BlankFrame
from toolkit import rgbFromString, pickColor
class Component(Component): class Component(Component):
@ -22,7 +21,6 @@ class Component(Component):
return ['pcm'] return ['pcm']
def widget(self, *args): def widget(self, *args):
self.visColor = (255, 255, 255)
self.scale = 20 self.scale = 20
self.y = 0 self.y = 0
super().widget(*args) super().widget(*args)
@ -33,35 +31,17 @@ class Component(Component):
self.page.comboBox_visLayout.addItem("Top") self.page.comboBox_visLayout.addItem("Top")
self.page.comboBox_visLayout.setCurrentIndex(0) self.page.comboBox_visLayout.setCurrentIndex(0)
self.page.lineEdit_visColor.setText('%s,%s,%s' % self.visColor) self.page.lineEdit_visColor.setText('255,255,255')
self.page.pushButton_visColor.clicked.connect(lambda: self.pickColor())
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.visColor).name()
self.page.pushButton_visColor.setStyleSheet(btnStyle)
self.trackWidgets({ self.trackWidgets({
'visColor': self.page.lineEdit_visColor,
'layout': self.page.comboBox_visLayout, 'layout': self.page.comboBox_visLayout,
'scale': self.page.spinBox_scale, 'scale': self.page.spinBox_scale,
'y': self.page.spinBox_y, 'y': self.page.spinBox_y,
}, colorWidgets={
'visColor': self.page.pushButton_visColor,
}) })
def update(self):
self.visColor = rgbFromString(self.page.lineEdit_visColor.text())
super().update()
def loadPreset(self, pr, *args):
super().loadPreset(pr, *args)
self.page.lineEdit_visColor.setText('%s,%s,%s' % pr['visColor'])
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*pr['visColor']).name()
self.page.pushButton_visColor.setStyleSheet(btnStyle)
def savePreset(self):
saveValueStore = super().savePreset()
saveValueStore['visColor'] = self.visColor
return saveValueStore
def previewRender(self): def previewRender(self):
spectrum = numpy.fromfunction( spectrum = numpy.fromfunction(
lambda x: float(self.scale)/2500*(x-128)**2, (255,), dtype="int16") lambda x: float(self.scale)/2500*(x-128)**2, (255,), dtype="int16")
@ -99,13 +79,6 @@ class Component(Component):
self.spectrumArray[arrayNo], self.spectrumArray[arrayNo],
self.visColor, self.layout) self.visColor, self.layout)
def pickColor(self):
RGBstring, btnStyle = pickColor()
if not RGBstring:
return
self.page.lineEdit_visColor.setText(RGBstring)
self.page.pushButton_visColor.setStyleSheet(btnStyle)
def transformData( def transformData(
self, i, completeAudioArray, sampleSize, self, i, completeAudioArray, sampleSize,
smoothConstantDown, smoothConstantUp, lastSpectrum): smoothConstantDown, smoothConstantUp, lastSpectrum):

View File

@ -5,7 +5,6 @@ import os
from component import Component from component import Component
from toolkit.frame import FramePainter from toolkit.frame import FramePainter
from toolkit import rgbFromString, pickColor
class Component(Component): class Component(Component):
@ -33,11 +32,6 @@ class Component(Component):
self.page.comboBox_textAlign.addItem("Right") self.page.comboBox_textAlign.addItem("Right")
self.page.lineEdit_textColor.setText('%s,%s,%s' % self.textColor) self.page.lineEdit_textColor.setText('%s,%s,%s' % self.textColor)
self.page.pushButton_textColor.clicked.connect(self.pickColor)
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.textColor).name()
self.page.pushButton_textColor.setStyleSheet(btnStyle)
self.page.lineEdit_title.setText(self.title) self.page.lineEdit_title.setText(self.title)
self.page.comboBox_textAlign.setCurrentIndex(int(self.alignment)) self.page.comboBox_textAlign.setCurrentIndex(int(self.alignment))
self.page.spinBox_fontSize.setValue(int(self.fontSize)) self.page.spinBox_fontSize.setValue(int(self.fontSize))
@ -48,21 +42,18 @@ class Component(Component):
self.update self.update
) )
self.trackWidgets({ self.trackWidgets({
'textColor': self.page.lineEdit_textColor,
'title': self.page.lineEdit_title, 'title': self.page.lineEdit_title,
'alignment': self.page.comboBox_textAlign, 'alignment': self.page.comboBox_textAlign,
'fontSize': self.page.spinBox_fontSize, 'fontSize': self.page.spinBox_fontSize,
'xPosition': self.page.spinBox_xTextAlign, 'xPosition': self.page.spinBox_xTextAlign,
'yPosition': self.page.spinBox_yTextAlign, 'yPosition': self.page.spinBox_yTextAlign,
}, colorWidgets={
'textColor': self.page.pushButton_textColor,
}) })
def update(self): def update(self):
self.titleFont = self.page.fontComboBox_titleFont.currentFont() self.titleFont = self.page.fontComboBox_titleFont.currentFont()
self.textColor = rgbFromString(
self.page.lineEdit_textColor.text())
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.textColor).name()
self.page.pushButton_textColor.setStyleSheet(btnStyle)
super().update() super().update()
def getXY(self): def getXY(self):
@ -86,15 +77,10 @@ class Component(Component):
font = QFont() font = QFont()
font.fromString(pr['titleFont']) font.fromString(pr['titleFont'])
self.page.fontComboBox_titleFont.setCurrentFont(font) self.page.fontComboBox_titleFont.setCurrentFont(font)
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)
def savePreset(self): def savePreset(self):
saveValueStore = super().savePreset() saveValueStore = super().savePreset()
saveValueStore['titleFont'] = self.titleFont.toString() saveValueStore['titleFont'] = self.titleFont.toString()
saveValueStore['textColor'] = self.textColor
return saveValueStore return saveValueStore
def previewRender(self): def previewRender(self):
@ -122,13 +108,6 @@ class Component(Component):
return image.finalize() return image.finalize()
def pickColor(self):
RGBstring, btnStyle = pickColor()
if not RGBstring:
return
self.page.lineEdit_textColor.setText(RGBstring)
self.page.pushButton_textColor.setStyleSheet(btnStyle)
def commandHelp(self): 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(' "title=User Error"')

View File

@ -7,7 +7,7 @@ import subprocess
from component import Component from component import Component
from toolkit.frame import BlankFrame, scale from toolkit.frame import BlankFrame, scale
from toolkit import checkOutput, rgbFromString, pickColor from toolkit import checkOutput
from toolkit.ffmpeg import ( from toolkit.ffmpeg import (
openPipe, closePipe, getAudioDuration, FfmpegVideo, exampleSound openPipe, closePipe, getAudioDuration, FfmpegVideo, exampleSound
) )
@ -18,15 +18,9 @@ class Component(Component):
version = '1.0.0' version = '1.0.0'
def widget(self, *args): def widget(self, *args):
self.color = (255, 255, 255)
super().widget(*args) super().widget(*args)
self.page.lineEdit_color.setText('%s,%s,%s' % self.color) self.page.lineEdit_color.setText('255,255,255')
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.color).name()
self.page.pushButton_color.setStyleSheet(btnStyle)
self.page.pushButton_color.clicked.connect(lambda: self.pickColor())
self.page.spinBox_scale.valueChanged.connect(self.updateChunksize)
if hasattr(self.parent, 'window'): if hasattr(self.parent, 'window'):
self.parent.window.lineEdit_audioFile.textChanged.connect( self.parent.window.lineEdit_audioFile.textChanged.connect(
@ -35,6 +29,7 @@ class Component(Component):
self.trackWidgets( self.trackWidgets(
{ {
'color': self.page.lineEdit_color,
'mode': self.page.comboBox_mode, 'mode': self.page.comboBox_mode,
'amplitude': self.page.comboBox_amplitude, 'amplitude': self.page.comboBox_amplitude,
'x': self.page.spinBox_x, 'x': self.page.spinBox_x,
@ -44,36 +39,11 @@ class Component(Component):
'opacity': self.page.spinBox_opacity, 'opacity': self.page.spinBox_opacity,
'compress': self.page.checkBox_compress, 'compress': self.page.checkBox_compress,
'mono': self.page.checkBox_mono, 'mono': self.page.checkBox_mono,
}, colorWidgets={
'color': self.page.pushButton_color,
} }
) )
def update(self):
self.color = rgbFromString(self.page.lineEdit_color.text())
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*self.color).name()
self.page.pushButton_color.setStyleSheet(btnStyle)
super().update()
def loadPreset(self, pr, *args):
super().loadPreset(pr, *args)
self.page.lineEdit_color.setText('%s,%s,%s' % pr['color'])
btnStyle = "QPushButton { background-color : %s; outline: none; }" \
% QColor(*pr['color']).name()
self.page.pushButton_color.setStyleSheet(btnStyle)
def savePreset(self):
saveValueStore = super().savePreset()
saveValueStore['color'] = self.color
return saveValueStore
def pickColor(self):
RGBstring, btnStyle = pickColor()
if not RGBstring:
return
self.page.lineEdit_color.setText(RGBstring)
self.page.pushButton_color.setStyleSheet(btnStyle)
def previewRender(self): def previewRender(self):
self.updateChunksize() self.updateChunksize()
frame = self.getPreviewFrame(self.width, self.height) frame = self.getPreviewFrame(self.width, self.height)

View File

@ -74,25 +74,6 @@ def disableWhenOpeningProject(func):
return decorator return decorator
def pickColor():
'''
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
'''
dialog = QtWidgets.QColorDialog()
dialog.setOption(QtWidgets.QColorDialog.ShowAlphaChannel, True)
color = dialog.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
else:
return None, None
def rgbFromString(string): def rgbFromString(string):
'''Turns an RGB string like "255, 255, 255" into a tuple''' '''Turns an RGB string like "255, 255, 255" into a tuple'''
try: try:

View File

@ -42,9 +42,9 @@ class PaintColor(QtGui.QColor):
super().__init__(b, g, r, a) super().__init__(b, g, r, a)
def scale(scale, width, height, returntype=None): def scale(scalePercent, width, height, returntype=None):
width = (float(width) / 100.0) * float(scale) width = (float(width) / 100.0) * float(scalePercent)
height = (float(height) / 100.0) * float(scale) height = (float(height) / 100.0) * float(scalePercent)
if returntype == str: if returntype == str:
return (str(math.ceil(width)), str(math.ceil(height))) return (str(math.ceil(width)), str(math.ceil(height)))
elif returntype == int: elif returntype == int: