clean ups

This commit is contained in:
Andreas Preikschat 2011-06-13 10:16:50 +02:00
parent 062e52c6db
commit d7ea218342
2 changed files with 27 additions and 34 deletions

View File

@ -206,20 +206,24 @@ class Renderer(object):
self._calculate_default(self.screens.current[u'size']) self._calculate_default(self.screens.current[u'size'])
return preview return preview
def format_slide(self, text, line_break, item): def format_slide(self, text, item):
""" """
Calculate how much text can fit on a slide. Calculate how much text can fit on a slide.
``text`` ``text``
The words to go on the slides. The words to go on the slides.
``line_break`` ``item``
Add line endings after each line of text used for bibles. The service item object.
""" """
log.debug(u'format slide') log.debug(u'format slide')
# Add line endings after each line of text used for bibles.
line_end = u'<br>'
if item.is_capable(ItemCapabilities.NoLineBreaks):
line_end = u' '
# clean up line endings # clean up line endings
lines = self._lines_split(text) lines = self._lines_split(text)
pages = self._paginate_slide(lines, line_break, self.force_page) pages = self._paginate_slide(lines, line_end)
if len(pages) > 1: if len(pages) > 1:
# Songs and Custom # Songs and Custom
if item.is_capable(ItemCapabilities.AllowsVirtualSplit): if item.is_capable(ItemCapabilities.AllowsVirtualSplit):
@ -228,12 +232,11 @@ class Renderer(object):
pages = [] pages = []
for slide in slides: for slide in slides:
lines = slide.strip(u'\n').split(u'\n') lines = slide.strip(u'\n').split(u'\n')
new_pages = self._paginate_slide(lines, line_break, new_pages = self._paginate_slide(lines, line_end)
self.force_page)
pages.extend(new_pages) pages.extend(new_pages)
# Bibles # Bibles
elif item.is_capable(ItemCapabilities.AllowsWordSplit): elif item.is_capable(ItemCapabilities.AllowsWordSplit):
pages = self._paginate_slide_words(text, line_break) pages = self._paginate_slide_words(text, line_end)
return pages return pages
def _calculate_default(self, screen): def _calculate_default(self, screen):
@ -241,7 +244,7 @@ class Renderer(object):
Calculate the default dimentions of the screen. Calculate the default dimentions of the screen.
``screen`` ``screen``
The QSize of the screen. The screen to calculate the default of.
""" """
log.debug(u'_calculate default %s', screen) log.debug(u'_calculate default %s', screen)
self.width = screen.width() self.width = screen.width()
@ -308,25 +311,19 @@ class Renderer(object):
(build_lyrics_format_css(self.theme_data, self.page_width, (build_lyrics_format_css(self.theme_data, self.page_width,
self.page_height), build_lyrics_outline_css(self.theme_data)) self.page_height), build_lyrics_outline_css(self.theme_data))
def _paginate_slide(self, lines, line_break, force_page=False): def _paginate_slide(self, lines, line_end):
""" """
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.
``lines`` ``lines``
The words to be fitted on the slide split into lines. The text to be fitted on the slide split into lines.
``line_break`` ``line_end``
Add line endings after each line of text (used for bibles). Add line endings after each line of text (used for bibles).
``force_page``
Flag to tell message lines in page.
""" """
log.debug(u'_paginate_slide - Start') log.debug(u'_paginate_slide - Start')
line_end = u'' #print line_end
if line_break:
line_end = u'<br>'
formatted = [] formatted = []
previous_html = u'' previous_html = u''
previous_raw = u'' previous_raw = u''
@ -336,11 +333,8 @@ class Renderer(object):
self.web.setHtml(html) self.web.setHtml(html)
# Text too long so go to next page. # Text too long so go to next page.
if self.web_frame.contentsSize().height() > self.page_height: if self.web_frame.contentsSize().height() > self.page_height:
html_text, previous_raw, index = self._binary_chop( html_text, previous_raw = self._binary_chop(formatted,
formatted, previous_html, previous_raw, html_lines, lines, previous_html, previous_raw, html_lines, lines, line_end)
line_end)
if force_page:
Receiver.send_message(u'theme_line_count', index + 1)
else: else:
previous_raw = u''.join(lines) previous_raw = u''.join(lines)
while previous_raw.endswith(u'<br>'): while previous_raw.endswith(u'<br>'):
@ -350,7 +344,7 @@ class Renderer(object):
log.debug(u'_paginate_slide - End') log.debug(u'_paginate_slide - End')
return formatted return formatted
def _paginate_slide_words(self, text, line_break): def _paginate_slide_words(self, text, line_end):
""" """
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. This version is to handle text which needs to be split theme settings. This version is to handle text which needs to be split
@ -359,14 +353,11 @@ class Renderer(object):
``text`` ``text``
The words to be fitted on the slide split into lines. The words to be fitted on the slide split into lines.
``line_break`` ``line_end``
Add line endings after each line of text used for bibles. Add line endings after each line of text used for bibles.
""" """
log.debug(u'_paginate_slide_words - Start') log.debug(u'_paginate_slide_words - Start')
line_end = u' '
if line_break:
line_end = u'<br>'
formatted = [] formatted = []
previous_html = u'' previous_html = u''
previous_raw = u'' previous_raw = u''
@ -404,7 +395,7 @@ class Renderer(object):
# using the algorithm known as "binary chop". # using the algorithm known as "binary chop".
raw_words = self._words_split(line) raw_words = self._words_split(line)
html_words = map(expand_tags, raw_text) html_words = map(expand_tags, raw_text)
previous_html, previous_raw, index = self._binary_chop( previous_html, previous_raw = self._binary_chop(
formatted, previous_html, previous_raw, html_words, formatted, previous_html, previous_raw, html_words,
raw_words, line_end) raw_words, line_end)
else: else:
@ -466,6 +457,10 @@ class Renderer(object):
u''.join(raw_list[:index + 1])) u''.join(raw_list[:index + 1]))
previous_html = u'' previous_html = u''
previous_raw = u'' previous_raw = u''
# Stop here as the theme line count was requested.
if self.force_page:
Receiver.send_message(u'theme_line_count', index + 1)
break
else: else:
continue continue
# Check if the rest of the line fits on the slide. If it # Check if the rest of the line fits on the slide. If it
@ -487,7 +482,7 @@ class Renderer(object):
smallest_index = 0 smallest_index = 0
highest_index = len(html_list) - 1 highest_index = len(html_list) - 1
index = int(highest_index / 2) index = int(highest_index / 2)
return previous_html, previous_raw, index return previous_html, previous_raw
def _words_split(self, line): def _words_split(self, line):
""" """

View File

@ -165,7 +165,6 @@ class ServiceItem(object):
log.debug(u'Render called') log.debug(u'Render called')
self._display_frames = [] self._display_frames = []
self.bg_image_bytes = None self.bg_image_bytes = None
line_break = not self.is_capable(ItemCapabilities.NoLineBreaks)
theme = self.theme if self.theme else None theme = self.theme if self.theme else None
self.main, self.footer = \ self.main, self.footer = \
self.renderer.set_override_theme(theme, use_override) self.renderer.set_override_theme(theme, use_override)
@ -176,9 +175,8 @@ class ServiceItem(object):
import datetime import datetime
start = time.time() start = time.time()
for slide in self._raw_frames: for slide in self._raw_frames:
formatted = self.renderer \ pages = self.renderer.format_slide(slide[u'raw_slide'], self)
.format_slide(slide[u'raw_slide'], line_break, self) for page in pages:
for page in formatted:
page = page.replace(u'<br>', u'{br}') page = page.replace(u'<br>', u'{br}')
html = expand_tags(cgi.escape(page.rstrip())) html = expand_tags(cgi.escape(page.rstrip()))
self._display_frames.append({ self._display_frames.append({