Paged display: Allow customization of the margins and number of pages per screen

This commit is contained in:
Kovid Goyal 2012-07-19 16:00:50 +05:30
parent 9da41e1756
commit b82edf32f7
5 changed files with 260 additions and 30 deletions

Binary file not shown.

View File

@ -27,22 +27,48 @@ class PagedDisplay
this.is_full_screen_layout = false
this.max_col_width = -1
this.document_margins = null
this.use_document_margins = false
read_document_margins: () ->
if false and this.document_margins is null
# Read page margins from the document. First checks for an @page rule.
# If that is not found, side margins are set to the side margins of the
# body element.
if this.document_margins is null
this.document_margins = {left:null, top:null, right:null, bottom:null}
tmp = document.createElement('div')
tmp.style.visibility = 'hidden'
tmp.style.position = 'absolute'
document.body.appendChild(tmp)
for sheet in document.styleSheets
for rule in sheet.rules
if rule.type == CSSRule.PAGE_RULE
for prop in ['margin-top', 'margin-bottom', 'margin-left', 'margin-right']
val = rule.style.getPropertyValue(prop)
for prop in ['left', 'top', 'bottom', 'right']
val = rule.style.getPropertyValue('margin-'+prop)
if val
log(val)
tmp.style.height = val
pxval = parseInt(window.getComputedStyle(tmp).height)
if not isNaN(pxval)
this.document_margins[prop] = pxval
document.body.removeChild(tmp)
if this.document_margins.left is null
val = parseInt(window.getComputedStyle(document.body).marginLeft)
if not isNaN(val)
this.document_margins.left = val
if this.document_margins.right is null
val = parseInt(window.getComputedStyle(document.body).marginRight)
if not isNaN(val)
this.document_margins.right = val
set_geometry: (cols_per_screen=1, margin_top=20, margin_side=40, margin_bottom=20) ->
this.margin_top = margin_top
this.margin_side = margin_side
this.margin_bottom = margin_bottom
this.cols_per_screen = cols_per_screen
if this.use_document_margins and this.document_margins != null
this.margin_top = this.document_margins.top or margin_top
this.margin_bottom = this.document_margins.bottom or margin_bottom
this.margin_side = this.document_margins.left or this.document_margins.right or margin_side
else
this.margin_top = margin_top
this.margin_side = margin_side
this.margin_bottom = margin_bottom
layout: () ->
# start_time = new Date().getTime()
@ -374,4 +400,3 @@ if window?
# TODO:
# Highlight on jump_to_anchor
# Handle document specified margins and allow them to be overridden

View File

@ -53,6 +53,11 @@ def config(defaults=None):
'0 and 1.'))
c.add_opt('fullscreen_clock', default=False, action='store_true',
help=_('Show a clock in fullscreen mode.'))
c.add_opt('cols_per_screen', default=1)
c.add_opt('use_book_margins', default=False, action='store_true')
c.add_opt('top_margin', default=20)
c.add_opt('side_margin', default=40)
c.add_opt('bottom_margin', default=20)
fonts = c.add_group('FONTS', _('Font options'))
fonts('serif_family', default='Times New Roman' if iswindows else 'Liberation Serif',
@ -120,6 +125,11 @@ class ConfigDialog(QDialog, Ui_Dialog):
self.hyphenate_default_lang.setVisible(False)
self.hyphenate_label.setVisible(False)
self.opt_fullscreen_clock.setChecked(opts.fullscreen_clock)
self.opt_cols_per_screen.setValue(opts.cols_per_screen)
self.opt_override_book_margins.setChecked(not opts.use_book_margins)
for x in ('top', 'bottom', 'side'):
getattr(self, 'opt_%s_margin'%x).setValue(getattr(opts,
x+'_margin'))
def accept(self, *args):
if self.shortcut_config.is_editing:
@ -152,6 +162,11 @@ class ConfigDialog(QDialog, Ui_Dialog):
c.set('line_scrolling_stops_on_pagebreaks',
self.opt_line_scrolling_stops_on_pagebreaks.isChecked())
c.set('fullscreen_clock', self.opt_fullscreen_clock.isChecked())
c.set('cols_per_screen', int(self.opt_cols_per_screen.value()))
c.set('use_book_margins', not
self.opt_override_book_margins.isChecked())
for x in ('top', 'bottom', 'side'):
c.set(x+'_margin', int(getattr(self, 'opt_%s_margin'%x).value()))
return QDialog.accept(self, *args)

View File

@ -18,16 +18,6 @@
<normaloff>:/images/config.png</normaloff>:/images/config.png</iconset>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QTabWidget" name="tabs">
<property name="currentIndex">
@ -68,7 +58,7 @@ QToolBox::tab:hover {
<x>0</x>
<y>0</y>
<width>811</width>
<height>416</height>
<height>384</height>
</rect>
</property>
<attribute name="label">
@ -212,13 +202,143 @@ QToolBox::tab:hover {
</item>
</layout>
</widget>
<widget class="QWidget" name="page_5">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>811</width>
<height>384</height>
</rect>
</property>
<attribute name="label">
<string>Text &amp;layout in paged mode</string>
</attribute>
<layout class="QFormLayout" name="formLayout_5">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_8">
<property name="text">
<string>&lt;p&gt;These options only apply in &quot;paged&quot; mode, where the text is broken up into pages, as in a paper book. To get into this mode, use the button with the yellow scroll icon in the top right corner of the viewer window.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_13">
<property name="text">
<string>The number of &amp;pages of text to show on screen </string>
</property>
<property name="buddy">
<cstring>opt_cols_per_screen</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="opt_cols_per_screen">
<property name="suffix">
<string> page(s)</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>5</number>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="opt_override_book_margins">
<property name="text">
<string>&amp;Override the page margin settings specified in the book</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_18">
<property name="text">
<string>&amp;Top margin</string>
</property>
<property name="buddy">
<cstring>opt_top_margin</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="opt_top_margin">
<property name="suffix">
<string> px</string>
</property>
<property name="minimum">
<number>10</number>
</property>
<property name="maximum">
<number>1000</number>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_19">
<property name="text">
<string>&amp;Side margin</string>
</property>
<property name="buddy">
<cstring>opt_side_margin</cstring>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QSpinBox" name="opt_side_margin">
<property name="suffix">
<string> px</string>
</property>
<property name="minimum">
<number>10</number>
</property>
<property name="maximum">
<number>1000</number>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_17">
<property name="text">
<string>&amp;Bottom margin</string>
</property>
<property name="buddy">
<cstring>opt_bottom_margin</cstring>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QSpinBox" name="opt_bottom_margin">
<property name="suffix">
<string> px</string>
</property>
<property name="minimum">
<number>10</number>
</property>
<property name="maximum">
<number>1000</number>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>811</width>
<height>416</height>
<height>384</height>
</rect>
</property>
<attribute name="label">
@ -266,7 +386,7 @@ QToolBox::tab:hover {
<x>0</x>
<y>0</y>
<width>811</width>
<height>416</height>
<height>384</height>
</rect>
</property>
<attribute name="label">
@ -337,7 +457,7 @@ QToolBox::tab:hover {
<x>0</x>
<y>0</y>
<width>811</width>
<height>416</height>
<height>384</height>
</rect>
</property>
<attribute name="label">
@ -430,6 +550,16 @@ QToolBox::tab:hover {
</widget>
</widget>
</item>
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
@ -446,8 +576,8 @@ QToolBox::tab:hover {
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>252</x>
<y>569</y>
<x>258</x>
<y>623</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
@ -462,8 +592,8 @@ QToolBox::tab:hover {
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>320</x>
<y>569</y>
<x>326</x>
<y>623</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
@ -478,12 +608,60 @@ QToolBox::tab:hover {
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>83</x>
<y>279</y>
<x>89</x>
<y>226</y>
</hint>
<hint type="destinationlabel">
<x>349</x>
<y>312</y>
<x>332</x>
<y>259</y>
</hint>
</hints>
</connection>
<connection>
<sender>opt_override_book_margins</sender>
<signal>toggled(bool)</signal>
<receiver>opt_top_margin</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>137</x>
<y>189</y>
</hint>
<hint type="destinationlabel">
<x>367</x>
<y>218</y>
</hint>
</hints>
</connection>
<connection>
<sender>opt_override_book_margins</sender>
<signal>toggled(bool)</signal>
<receiver>opt_side_margin</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>71</x>
<y>193</y>
</hint>
<hint type="destinationlabel">
<x>347</x>
<y>253</y>
</hint>
</hints>
</connection>
<connection>
<sender>opt_override_book_margins</sender>
<signal>toggled(bool)</signal>
<receiver>opt_bottom_margin</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>513</x>
<y>196</y>
</hint>
<hint type="destinationlabel">
<x>371</x>
<y>281</y>
</hint>
</hints>
</connection>

View File

@ -134,6 +134,10 @@ class Document(QWebPage): # {{{
# Leave some space for the scrollbar and some border
self.max_fs_width = min(opts.max_fs_width, screen_width-50)
self.fullscreen_clock = opts.fullscreen_clock
self.use_book_margins = opts.use_book_margins
self.cols_per_screen = opts.cols_per_screen
self.side_margin = opts.side_margin
self.top_margin, self.bottom_margin = opts.top_margin, opts.bottom_margin
def fit_images(self):
if self.do_fit_images and not self.in_paged_mode:
@ -216,6 +220,14 @@ class Document(QWebPage): # {{{
def switch_to_paged_mode(self, onresize=False):
if onresize and not self.loaded_javascript:
return
self.javascript('''
window.paged_display.use_document_margins = %s;
window.paged_display.set_geometry(%d, %d, %d, %d);
'''%(
('true' if self.use_book_margins else 'false'),
self.cols_per_screen, self.top_margin, self.side_margin,
self.bottom_margin
))
side_margin = self.javascript('window.paged_display.layout()', typ=int)
# Setup the contents size to ensure that there is a right most margin.
# Without this webkit renders the final column with no margin, as the