Convert comments to HTML for book details panel in separate thread to make scrolling through the book list faster when large comments are present

This commit is contained in:
Kovid Goyal 2010-11-03 20:08:03 -06:00
parent ed1a4cc460
commit 4cac965ea7

View File

@ -5,10 +5,11 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os, collections import os, collections, sys
from Queue import Queue
from PyQt4.Qt import QPixmap, QSize, QWidget, Qt, pyqtSignal, \ from PyQt4.Qt import QPixmap, QSize, QWidget, Qt, pyqtSignal, \
QPropertyAnimation, QEasingCurve, \ QPropertyAnimation, QEasingCurve, QThread, \
QSizePolicy, QPainter, QRect, pyqtProperty, QLayout QSizePolicy, QPainter, QRect, pyqtProperty, QLayout
from PyQt4.QtWebKit import QWebView from PyQt4.QtWebKit import QWebView
@ -150,6 +151,33 @@ class CoverView(QWidget): # {{{
# }}} # }}}
# Book Info {{{ # Book Info {{{
class RenderComments(QThread):
rdone = pyqtSignal(object, object)
def __init__(self, parent):
QThread.__init__(self, parent)
self.queue = Queue()
self.start()
def run(self):
while True:
try:
rows, comments = self.queue.get()
except:
break
import time
time.sleep(0.001)
oint = sys.getcheckinterval()
sys.setcheckinterval(5)
try:
self.rdone.emit(rows, comments_to_html(comments))
except:
pass
sys.setcheckinterval(oint)
class BookInfo(QWebView): class BookInfo(QWebView):
link_clicked = pyqtSignal(object) link_clicked = pyqtSignal(object)
@ -157,6 +185,8 @@ class BookInfo(QWebView):
def __init__(self, vertical, parent=None): def __init__(self, vertical, parent=None):
QWebView.__init__(self, parent) QWebView.__init__(self, parent)
self.vertical = vertical self.vertical = vertical
self.renderer = RenderComments(self)
self.renderer.rdone.connect(self._show_data, type=Qt.QueuedConnection)
self.page().setLinkDelegationPolicy(self.page().DelegateAllLinks) self.page().setLinkDelegationPolicy(self.page().DelegateAllLinks)
self.linkClicked.connect(self.link_activated) self.linkClicked.connect(self.link_activated)
self._link_clicked = False self._link_clicked = False
@ -170,14 +200,16 @@ class BookInfo(QWebView):
self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
def show_data(self, data): def show_data(self, data):
self.setHtml('')
rows = render_rows(data) rows = render_rows(data)
rows = u'\n'.join([u'<tr><td valign="top"><b>%s:</b></td><td valign="top">%s</td></tr>'%(k,t) for rows = u'\n'.join([u'<tr><td valign="top"><b>%s:</b></td><td valign="top">%s</td></tr>'%(k,t) for
k, t in rows]) k, t in rows])
comments = '' comments = data.get(_('Comments'), '')
if data.get(_('Comments'), '') not in ('', u'None'): if comments and comments != u'None':
comments = data[_('Comments')] self.renderer.queue.put((rows, comments))
comments = comments_to_html(comments) self._show_data(rows, '')
def _show_data(self, rows, comments):
if self.vertical: if self.vertical:
if comments: if comments:
rows += u'<tr><td colspan="2">%s</td></tr>'%comments rows += u'<tr><td colspan="2">%s</td></tr>'%comments