Renderer cleanups

Remove code duplication and simplifications
This commit is contained in:
Tim Bentley 2009-04-19 20:12:18 +01:00
parent 6a309c43ff
commit 570a35ae65
3 changed files with 52 additions and 89 deletions

View File

@ -247,7 +247,7 @@ class Renderer:
assert(0, u'Invalid value for theme.VerticalAlign:%s' % self._theme.display_verticalAlign) assert(0, u'Invalid value for theme.VerticalAlign:%s' % self._theme.display_verticalAlign)
return x, y return x, y
def _render_lines(self, lines, lines1=None): def render_lines(self, lines, lines1=None):
"""render a set of lines according to the theme, return bounding box""" """render a set of lines according to the theme, return bounding box"""
#log.debug(u'_render_lines %s', lines) #log.debug(u'_render_lines %s', lines)

View File

@ -33,8 +33,9 @@ log = logging.getLogger(u'AmendThemeForm')
class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog): class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
def __init__(self, parent=None): def __init__(self, thememanager, parent=None):
QtGui.QDialog.__init__(self, parent) QtGui.QDialog.__init__(self, parent)
self.thememanager = thememanager
self.setupUi(self) self.setupUi(self)
#define signals #define signals
@ -71,28 +72,28 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
xml = fileToXML(xml_file) xml = fileToXML(xml_file)
self.theme.parse(xml) self.theme.parse(xml)
self.paintUi(self.theme) self.paintUi(self.theme)
self.generateImage(self.theme) self.previewTheme(self.theme)
def onGradientComboBoxSelected(self): def onGradientComboBoxSelected(self, currentIndex):
if self.GradientComboBox.currentIndex() == 0: # Horizontal if currentIndex == 0: # Horizontal
self.theme.background_direction = u'horizontal' self.theme.background_direction = u'horizontal'
elif self.GradientComboBox.currentIndex() == 1: # vertical elif currentIndex == 1: # vertical
self.theme.background_direction = u'vertical' self.theme.background_direction = u'vertical'
else: else:
self.theme.background_direction = u'circular' self.theme.background_direction = u'circular'
self.stateChanging(self.theme) self.stateChanging(self.theme)
self.generateImage(self.theme) self.previewTheme(self.theme)
def onBackgroundComboBoxSelected(self): def onBackgroundComboBoxSelected(self, currentIndex):
if self.BackgroundComboBox.currentIndex() == 0: # Opaque if currentIndex == 0: # Opaque
self.theme.background_mode = u'opaque' self.theme.background_mode = u'opaque'
else: else:
self.theme.background_mode = u'transparent' self.theme.background_mode = u'transparent'
self.stateChanging(self.theme) self.stateChanging(self.theme)
self.generateImage(self.theme) self.previewTheme(self.theme)
def onBackgroundTypeComboBoxSelected(self): def onBackgroundTypeComboBoxSelected(self, currentIndex):
if self.BackgroundTypeComboBox.currentIndex() == 0: # Solid if currentIndex == 0: # Solid
self.theme.background_type = u'solid' self.theme.background_type = u'solid'
if self.theme.background_direction == None: # never defined if self.theme.background_direction == None: # never defined
self.theme.background_direction = u'horizontal' self.theme.background_direction = u'horizontal'
@ -100,7 +101,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.theme.background_startColor = u'#000000' self.theme.background_startColor = u'#000000'
if self.theme.background_endColor is None : if self.theme.background_endColor is None :
self.theme.background_endColor = u'#000000' self.theme.background_endColor = u'#000000'
elif self.BackgroundTypeComboBox.currentIndex() == 1: # Gradient elif currentIndex == 1: # Gradient
self.theme.background_type = u'gradient' self.theme.background_type = u'gradient'
if self.theme.background_direction == None: # never defined if self.theme.background_direction == None: # never defined
self.theme.background_direction = u'horizontal' self.theme.background_direction = u'horizontal'
@ -111,7 +112,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
else: else:
self.theme.background_type = u'image' self.theme.background_type = u'image'
self.stateChanging(self.theme) self.stateChanging(self.theme)
self.generateImage(self.theme) self.previewTheme(self.theme)
def onColor1PushButtonClicked(self): def onColor1PushButtonClicked(self):
if self.theme.background_type == u'solid': if self.theme.background_type == u'solid':
@ -125,7 +126,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.Color1PushButton.setStyleSheet( self.Color1PushButton.setStyleSheet(
'background-color: %s' % str(self.theme.background_startColor)) 'background-color: %s' % str(self.theme.background_startColor))
self.generateImage(self.theme) self.previewTheme(self.theme)
def onColor2PushButtonClicked(self): def onColor2PushButtonClicked(self):
self.theme.background_endColor = QtGui.QColorDialog.getColor( self.theme.background_endColor = QtGui.QColorDialog.getColor(
@ -133,7 +134,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.Color2PushButton.setStyleSheet( self.Color2PushButton.setStyleSheet(
'background-color: %s' % str(self.theme.background_endColor)) 'background-color: %s' % str(self.theme.background_endColor))
self.generateImage(self.theme) self.previewTheme(self.theme)
def onMainFontColorPushButtonClicked(self): def onMainFontColorPushButtonClicked(self):
self.theme.font_main_color = QtGui.QColorDialog.getColor( self.theme.font_main_color = QtGui.QColorDialog.getColor(
@ -141,7 +142,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.MainFontColorPushButton.setStyleSheet( self.MainFontColorPushButton.setStyleSheet(
'background-color: %s' % str(self.theme.font_main_color)) 'background-color: %s' % str(self.theme.font_main_color))
self.generateImage(self.theme) self.previewTheme(self.theme)
def onFontFooterColorPushButtonClicked(self): def onFontFooterColorPushButtonClicked(self):
self.theme.font_footer_color = QtGui.QColorDialog.getColor( self.theme.font_footer_color = QtGui.QColorDialog.getColor(
@ -149,7 +150,7 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.FontFooterColorPushButton.setStyleSheet( self.FontFooterColorPushButton.setStyleSheet(
'background-color: %s' % str(self.theme.font_footer_color)) 'background-color: %s' % str(self.theme.font_footer_color))
self.generateImage(self.theme) self.previewTheme(self.theme)
def baseTheme(self): def baseTheme(self):
log.debug(u'base Theme') log.debug(u'base Theme')
@ -200,42 +201,6 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
self.Color2Label.setVisible(False) self.Color2Label.setVisible(False)
self.Color2PushButton.setVisible(False) self.Color2PushButton.setVisible(False)
def generateImage(self, theme): def previewTheme(self, theme):
log.debug(u'generateImage %s ', theme) frame = self.thememanager.generateImage(theme)
#theme = ThemeXML() self.ThemePreview.setPixmap(frame)
#theme.parse(theme_xml)
#print theme
size=QtCore.QSize(800,600)
frame=TstFrame(size)
frame=frame
paintdest=frame.GetPixmap()
r=Renderer(self.path)
r.set_paint_dest(paintdest)
r.set_theme(theme) # set default theme
r._render_background()
r.set_text_rectangle(QtCore.QRect(0,0, size.width()-1, size.height()-1), QtCore.QRect(10,560, size.width()-1, size.height()-1))
lines=[]
lines.append(u'Amazing Grace!')
lines.append(u'How sweet the sound')
lines.append(u'To save a wretch like me;')
lines.append(u'I once was lost but now am found,')
lines.append(u'Was blind, but now I see.')
lines1=[]
lines1.append(u'Amazing Grace (John Newton)' )
lines1.append(u'CCLI xxx (c)Openlp.org')
answer=r._render_lines(lines, lines1)
self.ThemePreview.setPixmap(frame.GetPixmap())
class TstFrame:
def __init__(self, size):
"""Create the DemoPanel."""
self.width=size.width();
self.height=size.height();
# create something to be painted into
self._Buffer = QtGui.QPixmap(self.width, self.height)
def GetPixmap(self):
return self._Buffer

View File

@ -154,7 +154,7 @@ class ThemeManager(QWidget):
self.Layout = QtGui.QVBoxLayout(self) self.Layout = QtGui.QVBoxLayout(self)
self.Layout.setSpacing(0) self.Layout.setSpacing(0)
self.Layout.setMargin(0) self.Layout.setMargin(0)
self.amendThemeForm = AmendThemeForm() self.amendThemeForm = AmendThemeForm(self)
self.Toolbar = OpenLPToolbar(self) self.Toolbar = OpenLPToolbar(self)
self.Toolbar.addToolbarButton(translate('ThemeManager',u'New Theme'), ":/themes/theme_new.png", self.Toolbar.addToolbarButton(translate('ThemeManager',u'New Theme'), ":/themes/theme_new.png",
translate('ThemeManager',u'Allows a Theme to be created'), self.onAddTheme) translate('ThemeManager',u'Allows a Theme to be created'), self.onAddTheme)
@ -247,6 +247,11 @@ class ThemeManager(QWidget):
os.mkdir(dir) os.mkdir(dir)
def unzipTheme(self, filename, dir): def unzipTheme(self, filename, dir):
"""
Unzip the theme , remove the preview file if stored
Generate a new preview fileCheck the XML theme version and upgrade if
necessary.
"""
log.debug(u'Unzipping theme %s', filename) log.debug(u'Unzipping theme %s', filename)
zip = zipfile.ZipFile(str(filename)) zip = zipfile.ZipFile(str(filename))
filexml = None filexml = None
@ -265,7 +270,7 @@ class ThemeManager(QWidget):
xml_data = zip.read(file) xml_data = zip.read(file)
if os.path.splitext (file) [1].lower () in [u'.xml']: if os.path.splitext (file) [1].lower () in [u'.xml']:
if self.checkVersion1(xml_data): if self.checkVersion1(xml_data):
filexml = self.migrateVersion122(filename, fullpath, xml_data) filexml = self.migrateVersion122(filename, fullpath, xml_data) # upgrade theme xml
else: else:
file_xml = xml_data file_xml = xml_data
outfile = open(fullpath, 'w') outfile = open(fullpath, 'w')
@ -275,7 +280,7 @@ class ThemeManager(QWidget):
outfile = open(fullpath, 'w') outfile = open(fullpath, 'w')
outfile.write(zip.read(file)) outfile.write(zip.read(file))
outfile.close() outfile.close()
self.generateImage(dir,themename, filexml) self.generateAndSaveImage(dir,themename, filexml)
def checkVersion1(self, xmlfile): def checkVersion1(self, xmlfile):
log.debug(u'checkVersion1 ') log.debug(u'checkVersion1 ')
@ -314,20 +319,30 @@ class ThemeManager(QWidget):
str(t.HorizontalAlign), str(t.VerticalAlign), str(t.WrapStyle)) str(t.HorizontalAlign), str(t.VerticalAlign), str(t.WrapStyle))
return newtheme.extract_xml() return newtheme.extract_xml()
def generateImage(self, dir, name, theme_xml): def generateAndSaveImage(self, dir, name, theme_xml):
log.debug(u'generateImage %s %s %s', dir, name, theme_xml) log.debug(u'generateImage %s %s %s', dir, name, theme_xml)
theme = ThemeXML() theme = ThemeXML()
theme.parse(theme_xml) theme.parse(theme_xml)
#print theme
size=QtCore.QSize(800,600)
frame=TstFrame(size)
frame=frame
paintdest=frame.GetPixmap()
r=Renderer(dir)
r.set_paint_dest(paintdest)
r.set_theme(theme) # set default theme frame = self.generateImage(theme)
r.set_text_rectangle(QtCore.QRect(0,0, size.width()-1, size.height()-1), QtCore.QRect(10,560, size.width()-1, size.height()-1))
im=frame.toImage()
samplepathname=os.path.join(dir, name+u'.png')
if os.path.exists(samplepathname):
os.unlink(samplepathname)
im.save(samplepathname, u'png')
log.debug(u'Theme image written to %s',samplepathname)
def generateImage(self, theme):
log.debug(u'generateImage %s ', theme)
size=QtCore.QSize(800,600)
frame = QtGui.QPixmap(size.width(), size.height())
renderer=Renderer(self.path)
renderer.set_paint_dest(frame)
renderer.set_theme(theme) # set default theme
#renderer._render_background()
renderer.set_text_rectangle(QtCore.QRect(0,0, size.width()-1, size.height()-1), QtCore.QRect(10,560, size.width()-1, size.height()-1))
lines=[] lines=[]
lines.append(u'Amazing Grace!') lines.append(u'Amazing Grace!')
@ -338,23 +353,6 @@ class ThemeManager(QWidget):
lines1=[] lines1=[]
lines1.append(u'Amazing Grace (John Newton)' ) lines1.append(u'Amazing Grace (John Newton)' )
lines1.append(u'CCLI xxx (c)Openlp.org') lines1.append(u'CCLI xxx (c)Openlp.org')
answer=renderer.render_lines(lines, lines1)
return frame
answer=r._render_lines(lines, lines1)
im=frame.GetPixmap().toImage()
samplepathname=os.path.join(dir, name+u'.png')
if os.path.exists(samplepathname):
os.unlink(samplepathname)
im.save(samplepathname, u'png')
log.debug(u'Theme image written to %s',samplepathname)
class TstFrame:
def __init__(self, size):
"""Create the DemoPanel."""
self.width=size.width();
self.height=size.height();
# create something to be painted into
self._Buffer = QtGui.QPixmap(self.width, self.height)
def GetPixmap(self):
return self._Buffer