Code cleanups

This commit is contained in:
Tim Bentley 2010-07-30 17:36:11 +01:00
parent b191f1391c
commit 0e7712aecc
2 changed files with 271 additions and 277 deletions

View File

@ -80,7 +80,7 @@ class Renderer(object):
self.bg_image = None self.bg_image = None
self._bg_image_filename = None self._bg_image_filename = None
self.theme_name = theme.theme_name self.theme_name = theme.theme_name
self._set_theme_font() #self._set_theme_font()
if theme.background_type == u'image': if theme.background_type == u'image':
if theme.background_filename: if theme.background_filename:
self.set_bg_image(theme.background_filename) self.set_bg_image(theme.background_filename)
@ -132,24 +132,19 @@ class Renderer(object):
frame_height) frame_height)
self.frame = QtGui.QImage(frame_width, frame_height, self.frame = QtGui.QImage(frame_width, frame_height,
QtGui.QImage.Format_ARGB32_Premultiplied) QtGui.QImage.Format_ARGB32_Premultiplied)
# self.frame_opaque = QtGui.QImage(frame_width, frame_height,
# QtGui.QImage.Format_ARGB32_Premultiplied)
if self._bg_image_filename and not self.bg_image: if self._bg_image_filename and not self.bg_image:
self.bg_image = resize_image(self._bg_image_filename, self.bg_image = resize_image(self._bg_image_filename,
self.frame.width(), self.frame.height()) self.frame.width(), self.frame.height())
if self.bg_frame is None: if self.bg_frame is None:
self._generate_background_frame() self._generate_background_frame()
def format_slide(self, words, footer): def format_slide(self, words):
""" """
Figure out how much text can appear on a slide, using the current Figure out how much text can appear on a slide, using the current
theme settings. theme settings.
``words`` ``words``
The words to be fitted on the slide. The words to be fitted on the slide.
``footer``
The footer of the slide.
""" """
log.debug(u'format_slide - Start') log.debug(u'format_slide - Start')
words = words.replace(u'\r\n', u'\n') words = words.replace(u'\r\n', u'\n')
@ -169,7 +164,6 @@ class Renderer(object):
main_weight = 75 main_weight = 75
df.setWeight(main_weight) df.setWeight(main_weight)
doc.setDefaultFont(df) doc.setDefaultFont(df)
myCursor = QtGui.QTextCursor(doc)
layout = doc.documentLayout() layout = doc.documentLayout()
formatted = [] formatted = []
shell = u'<p>%s</p>' shell = u'<p>%s</p>'
@ -352,271 +346,271 @@ class Renderer(object):
painter.end() painter.end()
log.debug(u'render background End') log.debug(u'render background End')
def _correct_alignment(self, rect, bbox): # def _correct_alignment(self, rect, bbox):
""" # """
Corrects the vertical alignment of text. # Corrects the vertical alignment of text.
#
``rect`` # ``rect``
The block dimentions. # The block dimentions.
#
``bbox`` # ``bbox``
Footer dimensions? # Footer dimensions?
""" # """
x = rect.left() # x = rect.left()
if self._theme.display_verticalAlign == 0: # if self._theme.display_verticalAlign == 0:
# top align # # top align
y = rect.top() # y = rect.top()
elif self._theme.display_verticalAlign == 2: # elif self._theme.display_verticalAlign == 2:
# bottom align # # bottom align
y = rect.bottom() - bbox.height() # y = rect.bottom() - bbox.height()
elif self._theme.display_verticalAlign == 1: # elif self._theme.display_verticalAlign == 1:
# centre align # # centre align
y = rect.top() + (rect.height() - bbox.height()) / 2 # y = rect.top() + (rect.height() - bbox.height()) / 2
else: # else:
log.error(u'Invalid value for theme.VerticalAlign:%s', # log.error(u'Invalid value for theme.VerticalAlign:%s',
self._theme.display_verticalAlign) # self._theme.display_verticalAlign)
return x, y # return x, y
#
def _render_lines_unaligned(self, lines, footer, tlcorner=(0, 0), # def _render_lines_unaligned(self, lines, footer, tlcorner=(0, 0),
live=False): # live=False):
""" # """
Given a list of lines to render, render each one in turn (using the # Given a list of lines to render, render each one in turn (using the
``_render_single_line`` fn - which may result in going off the bottom). # ``_render_single_line`` fn - which may result in going off the bottom).
They are expected to be pre-arranged to less than a screenful (eg. by # They are expected to be pre-arranged to less than a screenful (eg. by
using split_set_of_lines). # using split_set_of_lines).
#
Returns the bounding box of the text as QRect. # Returns the bounding box of the text as QRect.
#
``lines`` # ``lines``
The lines of text to render. # The lines of text to render.
#
``footer`` # ``footer``
The slide footer. # The slide footer.
#
``tlcorner`` # ``tlcorner``
Defaults to *``(0, 0)``*. Co-ordinates of the top left corner. # Defaults to *``(0, 0)``*. Co-ordinates of the top left corner.
#
``live`` # ``live``
Defaults to *False*. Whether or not this is a live screen. # Defaults to *False*. Whether or not this is a live screen.
""" # """
x, y = tlcorner # x, y = tlcorner
brx = x # brx = x
bry = y # bry = y
for line in lines: # for line in lines:
# render after current bottom, but at original left edge # # render after current bottom, but at original left edge
# keep track of right edge to see which is biggest # # keep track of right edge to see which is biggest
(thisx, bry) = self._render_and_wrap_single_line(line, footer, # (thisx, bry) = self._render_and_wrap_single_line(line, footer,
(x, bry), live) # (x, bry), live)
if (thisx > brx): # if (thisx > brx):
brx = thisx # brx = thisx
retval = QtCore.QRect(x, y, brx - x, bry - y) # retval = QtCore.QRect(x, y, brx - x, bry - y)
if self._debug: # if self._debug:
painter = QtGui.QPainter() # painter = QtGui.QPainter()
painter.begin(self.frame) # painter.begin(self.frame)
painter.setPen(QtGui.QPen(QtGui.QColor(0, 0, 255))) # painter.setPen(QtGui.QPen(QtGui.QColor(0, 0, 255)))
painter.drawRect(retval) # painter.drawRect(retval)
painter.end() # painter.end()
return retval # return retval
#
def _render_and_wrap_single_line(self, line, footer, tlcorner=(0, 0), # def _render_and_wrap_single_line(self, line, footer, tlcorner=(0, 0),
live=False): # live=False):
""" # """
Render a single line of words onto the DC, top left corner specified. # Render a single line of words onto the DC, top left corner specified.
If the line is too wide for the context, it wraps, but right-aligns # If the line is too wide for the context, it wraps, but right-aligns
the surplus words in the manner of song lyrics. # the surplus words in the manner of song lyrics.
#
Returns the bottom-right corner (of what was rendered) as a tuple(x, y). # Returns the bottom-right corner (of what was rendered) as a tuple(x, y).
#
``line`` # ``line``
Line of text to be rendered. # Line of text to be rendered.
#
``footer`` # ``footer``
The footer of the slide. # The footer of the slide.
#
``tlcorner`` # ``tlcorner``
Defaults to *``(0, 0)``*. The top left corner. # Defaults to *``(0, 0)``*. The top left corner.
#
``live`` # ``live``
Defaults to *False*. Whether or not this is a live screen. # Defaults to *False*. Whether or not this is a live screen.
""" # """
x, y = tlcorner # x, y = tlcorner
maxx = self._rect.width() # maxx = self._rect.width()
maxy = self._rect.height() # maxy = self._rect.height()
lines = [] # lines = []
lines.append(line) # lines.append(line)
startx = x # startx = x
starty = y # starty = y
rightextent = None # rightextent = None
self.painter = QtGui.QPainter() # self.painter = QtGui.QPainter()
self.painter.begin(self.frame) # self.painter.begin(self.frame)
self.painter.setRenderHint(QtGui.QPainter.Antialiasing) # self.painter.setRenderHint(QtGui.QPainter.Antialiasing)
if self._theme.display_slideTransition: # if self._theme.display_slideTransition:
self.painter2 = QtGui.QPainter() # self.painter2 = QtGui.QPainter()
self.painter2.begin(self.frame_opaque) # self.painter2.begin(self.frame_opaque)
self.painter2.setRenderHint(QtGui.QPainter.Antialiasing) # self.painter2.setRenderHint(QtGui.QPainter.Antialiasing)
self.painter2.setOpacity(0.7) # self.painter2.setOpacity(0.7)
# dont allow alignment messing with footers # # dont allow alignment messing with footers
if footer: # if footer:
align = 0 # align = 0
display_shadow_size = self._display_shadow_size_footer # display_shadow_size = self._display_shadow_size_footer
display_outline_size = self._display_outline_size_footer # display_outline_size = self._display_outline_size_footer
else: # else:
align = self._theme.display_horizontalAlign # align = self._theme.display_horizontalAlign
display_shadow_size = int(self._theme.display_shadow_size) # display_shadow_size = int(self._theme.display_shadow_size)
display_outline_size = int(self._theme.display_outline_size) # display_outline_size = int(self._theme.display_outline_size)
for linenum in range(len(lines)): # for linenum in range(len(lines)):
line = lines[linenum] # line = lines[linenum]
#find out how wide line is # #find out how wide line is
w, h = self._get_extent_and_render(line, footer, tlcorner=(x, y), # w, h = self._get_extent_and_render(line, footer, tlcorner=(x, y),
draw=False) # draw=False)
if self._theme.display_shadow: # if self._theme.display_shadow:
w += display_shadow_size # w += display_shadow_size
h += display_shadow_size # h += display_shadow_size
if self._theme.display_outline: # if self._theme.display_outline:
# pixels either side # # pixels either side
w += 2 * display_outline_size # w += 2 * display_outline_size
# pixels top/bottom # # pixels top/bottom
h += 2 * display_outline_size # h += 2 * display_outline_size
if align == 0: # left align # if align == 0: # left align
rightextent = x + w # rightextent = x + w
# shift right from last line's rh edge # # shift right from last line's rh edge
if self._theme.display_wrapStyle == 1 and linenum != 0: # if self._theme.display_wrapStyle == 1 and linenum != 0:
rightextent = self._first_line_right_extent # rightextent = self._first_line_right_extent
if rightextent > maxx: # if rightextent > maxx:
rightextent = maxx # rightextent = maxx
x = rightextent - w # x = rightextent - w
# right align # # right align
elif align == 1: # elif align == 1:
rightextent = maxx # rightextent = maxx
x = maxx - w # x = maxx - w
# centre # # centre
elif align == 2: # elif align == 2:
x = (maxx - w) / 2 # x = (maxx - w) / 2
rightextent = x + w # rightextent = x + w
if live: # if live:
# now draw the text, and any outlines/shadows # # now draw the text, and any outlines/shadows
if self._theme.display_shadow: # if self._theme.display_shadow:
self._get_extent_and_render(line, footer, # self._get_extent_and_render(line, footer,
tlcorner=(x + display_shadow_size, # tlcorner=(x + display_shadow_size,
y + display_shadow_size), # y + display_shadow_size),
draw=True, color=self._theme.display_shadow_color) # draw=True, color=self._theme.display_shadow_color)
self._get_extent_and_render(line, footer, tlcorner=(x, y), # self._get_extent_and_render(line, footer, tlcorner=(x, y),
draw=True, outline_size=display_outline_size) # draw=True, outline_size=display_outline_size)
y += h # y += h
if linenum == 0: # if linenum == 0:
self._first_line_right_extent = rightextent # self._first_line_right_extent = rightextent
# draw a box around the text - debug only # # draw a box around the text - debug only
#
if self._debug: # if self._debug:
self.painter.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0))) # self.painter.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0)))
self.painter.drawRect(startx, starty, rightextent-startx, y-starty) # self.painter.drawRect(startx, starty, rightextent-startx, y-starty)
brcorner = (rightextent, y) # brcorner = (rightextent, y)
self.painter.end() # self.painter.end()
if self._theme.display_slideTransition: # if self._theme.display_slideTransition:
self.painter2.end() # self.painter2.end()
return brcorner # return brcorner
#
def _set_theme_font(self): # def _set_theme_font(self):
""" # """
Set the fonts from the current theme settings. # Set the fonts from the current theme settings.
""" # """
footer_weight = 50 # footer_weight = 50
if self._theme.font_footer_weight == u'Bold': # if self._theme.font_footer_weight == u'Bold':
footer_weight = 75 # footer_weight = 75
#TODO Add myfont.setPixelSize((screen_height / 100) * font_size) # #TODO Add myfont.setPixelSize((screen_height / 100) * font_size)
self.footer_font = QtGui.QFont(self._theme.font_footer_name, # self.footer_font = QtGui.QFont(self._theme.font_footer_name,
self._theme.font_footer_proportion, # size # self._theme.font_footer_proportion, # size
footer_weight, # weight # footer_weight, # weight
self._theme.font_footer_italics) # italic # self._theme.font_footer_italics) # italic
self.footer_font.setPixelSize(self._theme.font_footer_proportion) # self.footer_font.setPixelSize(self._theme.font_footer_proportion)
main_weight = 50 # main_weight = 50
if self._theme.font_main_weight == u'Bold': # if self._theme.font_main_weight == u'Bold':
main_weight = 75 # main_weight = 75
self.main_font = QtGui.QFont(self._theme.font_main_name, # self.main_font = QtGui.QFont(self._theme.font_main_name,
self._theme.font_main_proportion, # size # self._theme.font_main_proportion, # size
main_weight, # weight # main_weight, # weight
self._theme.font_main_italics)# italic # self._theme.font_main_italics)# italic
self.main_font.setPixelSize(self._theme.font_main_proportion) # self.main_font.setPixelSize(self._theme.font_main_proportion)
#
def _get_extent_and_render(self, line, footer, tlcorner=(0, 0), draw=False, # def _get_extent_and_render(self, line, footer, tlcorner=(0, 0), draw=False,
color=None, outline_size=0): # color=None, outline_size=0):
""" # """
Find bounding box of text - as render_single_line. If draw is set, # Find bounding box of text - as render_single_line. If draw is set,
actually draw the text to the current DC as well return width and # actually draw the text to the current DC as well return width and
height of text as a tuple (w, h). # height of text as a tuple (w, h).
#
``line`` # ``line``
The line of text to render. # The line of text to render.
#
``footer`` # ``footer``
The footer text. # The footer text.
#
``tlcorner`` # ``tlcorner``
Defaults to *``(0, 0)``*. The top left corner co-ordinates. # Defaults to *``(0, 0)``*. The top left corner co-ordinates.
#
``draw`` # ``draw``
Defaults to *False*. Draw the text to the current surface. # Defaults to *False*. Draw the text to the current surface.
#
``color`` # ``color``
Defaults to *None*. The colour to draw with. # Defaults to *None*. The colour to draw with.
""" # """
# setup defaults # # setup defaults
if footer: # if footer:
font = self.footer_font # font = self.footer_font
else: # else:
font = self.main_font # font = self.main_font
metrics = QtGui.QFontMetrics(font) # metrics = QtGui.QFontMetrics(font)
w = metrics.width(line) # w = metrics.width(line)
if footer: # if footer:
h = metrics.height() # h = metrics.height()
else: # else:
h = metrics.height() + int(self._theme.font_main_line_adjustment) # h = metrics.height() + int(self._theme.font_main_line_adjustment)
if draw: # if draw:
self.painter.setFont(font) # self.painter.setFont(font)
if color is None: # if color is None:
if footer: # if footer:
pen = QtGui.QColor(self._theme.font_footer_color) # pen = QtGui.QColor(self._theme.font_footer_color)
else: # else:
pen = QtGui.QColor(self._theme.font_main_color) # pen = QtGui.QColor(self._theme.font_main_color)
else: # else:
pen = QtGui.QColor(color) # pen = QtGui.QColor(color)
x, y = tlcorner # x, y = tlcorner
rowpos = y + metrics.ascent() # rowpos = y + metrics.ascent()
if self._theme.display_outline and outline_size != 0 and not footer: # if self._theme.display_outline and outline_size != 0 and not footer:
path = QtGui.QPainterPath() # path = QtGui.QPainterPath()
path.addText(QtCore.QPointF(x, rowpos), font, line) # path.addText(QtCore.QPointF(x, rowpos), font, line)
self.painter.setBrush(self.painter.pen().brush()) # self.painter.setBrush(self.painter.pen().brush())
self.painter.setPen(QtGui.QPen(QtGui.QColor( # self.painter.setPen(QtGui.QPen(QtGui.QColor(
self._theme.display_outline_color), outline_size)) # self._theme.display_outline_color), outline_size))
self.painter.drawPath(path) # self.painter.drawPath(path)
self.painter.setPen(pen) # self.painter.setPen(pen)
self.painter.drawText(x, rowpos, line) # self.painter.drawText(x, rowpos, line)
if self._theme.display_slideTransition: # if self._theme.display_slideTransition:
# Print 2nd image with 70% weight # # Print 2nd image with 70% weight
if self._theme.display_outline and outline_size != 0 and \ # if self._theme.display_outline and outline_size != 0 and \
not footer: # not footer:
path = QtGui.QPainterPath() # path = QtGui.QPainterPath()
path.addText(QtCore.QPointF(x, rowpos), font, line) # path.addText(QtCore.QPointF(x, rowpos), font, line)
self.painter2.setBrush(self.painter2.pen().brush()) # self.painter2.setBrush(self.painter2.pen().brush())
self.painter2.setPen(QtGui.QPen( # self.painter2.setPen(QtGui.QPen(
QtGui.QColor(self._theme.display_outline_color), # QtGui.QColor(self._theme.display_outline_color),
outline_size)) # outline_size))
self.painter2.drawPath(path) # self.painter2.drawPath(path)
self.painter2.setFont(font) # self.painter2.setFont(font)
self.painter2.setPen(pen) # self.painter2.setPen(pen)
self.painter2.drawText(x, rowpos, line) # self.painter2.drawText(x, rowpos, line)
return (w, h) # return (w, h)
#
def snoop_image(self, image, image2=None): # def snoop_image(self, image, image2=None):
""" # """
Debugging method to allow images to be viewed. # Debugging method to allow images to be viewed.
#
``image`` # ``image``
An image to save to disk. # An image to save to disk.
#
``image2`` # ``image2``
Defaults to *None*. Another image to save to disk. # Defaults to *None*. Another image to save to disk.
""" # """
image.save(u'renderer.png', u'png') # image.save(u'renderer.png', u'png')
if image2: # if image2:
image2.save(u'renderer2.png', u'png') # image2.save(u'renderer2.png', u'png')

View File

@ -219,7 +219,7 @@ class RenderManager(object):
""" """
log.debug(u'format slide') log.debug(u'format slide')
self.build_text_rectangle(self.themedata) self.build_text_rectangle(self.themedata)
return self.renderer.format_slide(words, False) return self.renderer.format_slide(words)
# def generate_slide(self, main_text): # def generate_slide(self, main_text):
# """ # """