From 8518d8f7dc6d6bd1393c1592d15a6b83bdb884fb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Apr 2014 13:23:16 +0530 Subject: [PATCH] More QVariant porting --- setup/qt5-migrate.py | 1 + src/calibre/ebooks/pdf/render/from_html.py | 17 ++++++++------- src/calibre/ebooks/pdf/writer.py | 24 +++++++++++++--------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/setup/qt5-migrate.py b/setup/qt5-migrate.py index 7efd3530ab..97462d8958 100644 --- a/setup/qt5-migrate.py +++ b/setup/qt5-migrate.py @@ -53,6 +53,7 @@ def detect_qvariant(): 'src/calibre/gui2/viewer/gestures.py': {'toPoint'}, 'src/calibre/utils/serve_coffee.py': {'toString()'}, 'src/calibre/gui2/job_indicator.py': {'toPoint'}, + 'src/calibre/ebooks/pdf/render/engine.py': {'toRect'}, } for path in all_py_files(): if os.path.basename(path) in { diff --git a/src/calibre/ebooks/pdf/render/from_html.py b/src/calibre/ebooks/pdf/render/from_html.py index cc6affa8dd..a571a05656 100644 --- a/src/calibre/ebooks/pdf/render/from_html.py +++ b/src/calibre/ebooks/pdf/render/from_html.py @@ -302,12 +302,12 @@ class PDFWriter(QObject): mjpath = P(u'viewer/mathjax').replace(os.sep, '/') if iswindows: mjpath = u'/' + mjpath - if evaljs(''' + if bool(evaljs(''' window.mathjax.base = %s; mathjax.check_for_math(); mathjax.math_present - '''%(json.dumps(mjpath, ensure_ascii=False))).toBool(): + '''%(json.dumps(mjpath, ensure_ascii=False)))): self.log.debug('Math present, loading MathJax') - while not evaljs('mathjax.math_loaded').toBool(): + while not bool(evaljs('mathjax.math_loaded')): self.loop.processEvents(self.loop.ExcludeUserInputEvents) evaljs('document.getElementById("MathJax_Message").style.display="none";') @@ -392,11 +392,14 @@ class PDFWriter(QObject): self.painter.save() mf.render(self.painter) self.painter.restore() - nsl = evaljs('paged_display.next_screen_location()').toInt() - self.doc.end_page() - if not nsl[1] or nsl[0] <= 0: + try: + nsl = int(evaljs('paged_display.next_screen_location()')) + except (TypeError, ValueError): break - evaljs('window.scrollTo(%d, 0); paged_display.position_header_footer();'%nsl[0]) + self.doc.end_page() + if nsl <= 0: + break + evaljs('window.scrollTo(%d, 0); paged_display.position_header_footer();'%nsl) if self.doc.errors_occurred: break col += 1 diff --git a/src/calibre/ebooks/pdf/writer.py b/src/calibre/ebooks/pdf/writer.py index 636dfd5aaf..07039b5779 100644 --- a/src/calibre/ebooks/pdf/writer.py +++ b/src/calibre/ebooks/pdf/writer.py @@ -24,7 +24,7 @@ from calibre.ebooks.oeb.display.webview import load_html def get_custom_size(opts): custom_size = None - if opts.custom_size != None: + if opts.custom_size is not None: width, sep, height = opts.custom_size.partition('x') if height: try: @@ -35,7 +35,7 @@ def get_custom_size(opts): custom_size = None return custom_size -def get_pdf_printer(opts, for_comic=False, output_file_name=None): # {{{ +def get_pdf_printer(opts, for_comic=False, output_file_name=None): # {{{ from calibre.gui2 import is_ok_to_use_qt if not is_ok_to_use_qt(): raise Exception('Not OK to use Qt') @@ -89,7 +89,7 @@ def draw_image_page(printer, painter, p, preserve_aspect_ratio=True): nw, nh = page_rect.width(), page_rect.height() if aspect_ratio > 1: nh = int(page_rect.width()/aspect_ratio) - else: # Width is smaller than height + else: # Width is smaller than height nw = page_rect.height()*aspect_ratio __, nnw, nnh = fit_image(nw, nh, page_rect.width(), page_rect.height()) @@ -101,7 +101,7 @@ def draw_image_page(printer, painter, p, preserve_aspect_ratio=True): painter.drawPixmap(page_rect, p, p.rect()) -class Page(QWebPage): # {{{ +class Page(QWebPage): # {{{ def __init__(self, opts, log): self.log = log @@ -134,7 +134,7 @@ class Page(QWebPage): # {{{ self.log(unicode(msg)) # }}} -class PDFWriter(QObject): # {{{ +class PDFWriter(QObject): # {{{ def __init__(self, opts, log, cover_data=None, toc=None): from calibre.gui2 import is_ok_to_use_qt @@ -274,15 +274,19 @@ class PDFWriter(QObject): # {{{ self.current_page_num += 1 self.first_page = False mf.render(self.painter) - nsl = evaljs('paged_display.next_screen_location()').toInt() - if not nsl[1] or nsl[0] <= 0: break - evaljs('window.scrollTo(%d, 0)'%nsl[0]) + try: + nsl = int(evaljs('paged_display.next_screen_location()')) + except (TypeError, ValueError): + break + if nsl <= 0: + break + evaljs('window.scrollTo(%d, 0)'%nsl) self.bridge_value = tuple(self.outline.anchor_map[self.current_item]) evaljs('py_bridge.value = book_indexing.anchor_positions(py_bridge.value)') amap = self.bridge_value if not isinstance(amap, dict): - amap = {} # Some javascript error occurred + amap = {} # Some javascript error occurred self.outline.set_pos(self.current_item, None, start_page, 0) for anchor, x in amap.iteritems(): pagenum, ypos = x @@ -339,7 +343,7 @@ class PDFWriter(QObject): # {{{ # }}} -class ImagePDFWriter(object): # {{{ +class ImagePDFWriter(object): # {{{ def __init__(self, opts, log, cover_data=None, toc=None): self.opts = opts