move previewWindow class into new file
and cache frequently-created blank frames
This commit is contained in:
parent
64da6f14ce
commit
282f1c4b12
|
@ -17,6 +17,7 @@ import logging
|
||||||
|
|
||||||
from core import Core
|
from core import Core
|
||||||
import preview_thread
|
import preview_thread
|
||||||
|
from preview_win import PreviewWindow
|
||||||
from presetmanager import PresetManager
|
from presetmanager import PresetManager
|
||||||
from toolkit import disableWhenEncoding, disableWhenOpeningProject, checkOutput
|
from toolkit import disableWhenEncoding, disableWhenOpeningProject, checkOutput
|
||||||
|
|
||||||
|
@ -24,66 +25,6 @@ from toolkit import disableWhenEncoding, disableWhenOpeningProject, checkOutput
|
||||||
log = logging.getLogger('AVP.MainWindow')
|
log = logging.getLogger('AVP.MainWindow')
|
||||||
|
|
||||||
|
|
||||||
class PreviewWindow(QtWidgets.QLabel):
|
|
||||||
'''
|
|
||||||
Paints the preview QLabel and maintains the aspect ratio when the
|
|
||||||
window is resized.
|
|
||||||
'''
|
|
||||||
log = logging.getLogger('AVP.MainWindow.Preview')
|
|
||||||
|
|
||||||
def __init__(self, parent, img):
|
|
||||||
super(PreviewWindow, self).__init__()
|
|
||||||
self.parent = parent
|
|
||||||
self.setFrameStyle(QtWidgets.QFrame.StyledPanel)
|
|
||||||
self.pixmap = QtGui.QPixmap(img)
|
|
||||||
|
|
||||||
def paintEvent(self, event):
|
|
||||||
size = self.size()
|
|
||||||
painter = QtGui.QPainter(self)
|
|
||||||
point = QtCore.QPoint(0, 0)
|
|
||||||
scaledPix = self.pixmap.scaled(
|
|
||||||
size,
|
|
||||||
QtCore.Qt.KeepAspectRatio,
|
|
||||||
transformMode=QtCore.Qt.SmoothTransformation)
|
|
||||||
|
|
||||||
# start painting the label from left upper corner
|
|
||||||
point.setX((size.width() - scaledPix.width())/2)
|
|
||||||
point.setY((size.height() - scaledPix.height())/2)
|
|
||||||
painter.drawPixmap(point, scaledPix)
|
|
||||||
|
|
||||||
def changePixmap(self, img):
|
|
||||||
self.pixmap = QtGui.QPixmap(img)
|
|
||||||
self.repaint()
|
|
||||||
|
|
||||||
def mousePressEvent(self, event):
|
|
||||||
if self.parent.encoding:
|
|
||||||
return
|
|
||||||
|
|
||||||
i = self.parent.window.listWidget_componentList.currentRow()
|
|
||||||
if i >= 0:
|
|
||||||
component = self.parent.core.selectedComponents[i]
|
|
||||||
if not hasattr(component, 'previewClickEvent'):
|
|
||||||
self.log.info('Ignored click event')
|
|
||||||
return
|
|
||||||
pos = (event.x(), event.y())
|
|
||||||
size = (self.width(), self.height())
|
|
||||||
butt = event.button()
|
|
||||||
self.log.info('Click event for #%s: %s button %s' % (
|
|
||||||
i, pos, butt))
|
|
||||||
component.previewClickEvent(
|
|
||||||
pos, size, butt
|
|
||||||
)
|
|
||||||
self.parent.core.updateComponent(i)
|
|
||||||
|
|
||||||
@QtCore.pyqtSlot(str)
|
|
||||||
def threadError(self, msg):
|
|
||||||
self.parent.showMessage(
|
|
||||||
msg=msg,
|
|
||||||
icon='Critical',
|
|
||||||
parent=self
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(QtWidgets.QMainWindow):
|
class MainWindow(QtWidgets.QMainWindow):
|
||||||
'''
|
'''
|
||||||
The MainWindow wraps many Core methods in order to update the GUI
|
The MainWindow wraps many Core methods in order to update the GUI
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
class PreviewWindow(QtWidgets.QLabel):
|
||||||
|
'''
|
||||||
|
Paints the preview QLabel in MainWindow and maintains the aspect ratio
|
||||||
|
when the window is resized.
|
||||||
|
'''
|
||||||
|
log = logging.getLogger('AVP.PreviewWindow')
|
||||||
|
|
||||||
|
def __init__(self, parent, img):
|
||||||
|
super(PreviewWindow, self).__init__()
|
||||||
|
self.parent = parent
|
||||||
|
self.setFrameStyle(QtWidgets.QFrame.StyledPanel)
|
||||||
|
self.pixmap = QtGui.QPixmap(img)
|
||||||
|
|
||||||
|
def paintEvent(self, event):
|
||||||
|
size = self.size()
|
||||||
|
painter = QtGui.QPainter(self)
|
||||||
|
point = QtCore.QPoint(0, 0)
|
||||||
|
scaledPix = self.pixmap.scaled(
|
||||||
|
size,
|
||||||
|
QtCore.Qt.KeepAspectRatio,
|
||||||
|
transformMode=QtCore.Qt.SmoothTransformation)
|
||||||
|
|
||||||
|
# start painting the label from left upper corner
|
||||||
|
point.setX((size.width() - scaledPix.width())/2)
|
||||||
|
point.setY((size.height() - scaledPix.height())/2)
|
||||||
|
painter.drawPixmap(point, scaledPix)
|
||||||
|
|
||||||
|
def changePixmap(self, img):
|
||||||
|
self.pixmap = QtGui.QPixmap(img)
|
||||||
|
self.repaint()
|
||||||
|
|
||||||
|
def mousePressEvent(self, event):
|
||||||
|
if self.parent.encoding:
|
||||||
|
return
|
||||||
|
|
||||||
|
i = self.parent.window.listWidget_componentList.currentRow()
|
||||||
|
if i >= 0:
|
||||||
|
component = self.parent.core.selectedComponents[i]
|
||||||
|
if not hasattr(component, 'previewClickEvent'):
|
||||||
|
self.log.info('Ignored click event')
|
||||||
|
return
|
||||||
|
pos = (event.x(), event.y())
|
||||||
|
size = (self.width(), self.height())
|
||||||
|
butt = event.button()
|
||||||
|
self.log.info('Click event for #%s: %s button %s' % (
|
||||||
|
i, pos, butt))
|
||||||
|
component.previewClickEvent(
|
||||||
|
pos, size, butt
|
||||||
|
)
|
||||||
|
self.parent.core.updateComponent(i)
|
||||||
|
|
||||||
|
@QtCore.pyqtSlot(str)
|
||||||
|
def threadError(self, msg):
|
||||||
|
self.parent.showMessage(
|
||||||
|
msg=msg,
|
||||||
|
icon='Critical',
|
||||||
|
parent=self
|
||||||
|
)
|
|
@ -77,27 +77,40 @@ def defaultSize(framefunc):
|
||||||
|
|
||||||
|
|
||||||
def FloodFrame(width, height, RgbaTuple):
|
def FloodFrame(width, height, RgbaTuple):
|
||||||
|
log.debug('Creating new %s*%s %s flood frame' % (
|
||||||
|
width, height,
|
||||||
|
'blank' if RgbaTuple[3] == 0 else RgbaTuple
|
||||||
|
)
|
||||||
|
)
|
||||||
return Image.new("RGBA", (width, height), RgbaTuple)
|
return Image.new("RGBA", (width, height), RgbaTuple)
|
||||||
|
|
||||||
|
|
||||||
@defaultSize
|
@defaultSize
|
||||||
def BlankFrame(width, height):
|
def BlankFrame(width, height, blankFrames={}):
|
||||||
'''The base frame used by each component to start drawing.'''
|
'''The base frame used by each component to start drawing.'''
|
||||||
log.debug('Creating new %s*%s blank frame' % (width, height))
|
try:
|
||||||
return FloodFrame(width, height, (0, 0, 0, 0))
|
return blankFrames[(width, height)]
|
||||||
|
except KeyError:
|
||||||
|
newFrame = FloodFrame(width, height, (0, 0, 0, 0))
|
||||||
|
blankFrames[(width, height)] = newFrame
|
||||||
|
return newFrame
|
||||||
|
|
||||||
|
|
||||||
@defaultSize
|
@defaultSize
|
||||||
def Checkerboard(width, height):
|
def Checkerboard(width, height, checkerboards={}):
|
||||||
'''
|
'''
|
||||||
A checkerboard to represent transparency to the user.
|
A checkerboard to represent transparency to the user.
|
||||||
TODO: Would be cool to generate this image with numpy instead.
|
TODO: Would be cool to generate this image with numpy instead.
|
||||||
'''
|
'''
|
||||||
log.debug('Creating new %s*%s checkerboard' % (width, height))
|
try:
|
||||||
image = FloodFrame(1920, 1080, (0, 0, 0, 0))
|
return checkerboards[(width, height)]
|
||||||
image.paste(Image.open(
|
except KeyError:
|
||||||
os.path.join(core.Core.wd, "background.png")),
|
log.debug('Creating new %s*%s checkerboard' % (width, height))
|
||||||
(0, 0)
|
image = FloodFrame(1920, 1080, (0, 0, 0, 0))
|
||||||
)
|
image.paste(Image.open(
|
||||||
image = image.resize((width, height))
|
os.path.join(core.Core.wd, "background.png")),
|
||||||
return image
|
(0, 0)
|
||||||
|
)
|
||||||
|
image = image.resize((width, height))
|
||||||
|
checkerboards[(width, height)] = image
|
||||||
|
return image
|
||||||
|
|
Reference in New Issue