Initial port to using QWebView to display comments in Book details pane

This commit is contained in:
Kovid Goyal 2010-11-03 14:37:50 -06:00
parent 6e8296de1f
commit 00e6016242

View File

@ -7,9 +7,10 @@ __docformat__ = 'restructuredtext en'
import os, collections import os, collections
from PyQt4.Qt import QLabel, QPixmap, QSize, QWidget, Qt, pyqtSignal, \ from PyQt4.Qt import QPixmap, QSize, QWidget, Qt, pyqtSignal, \
QVBoxLayout, QScrollArea, QPropertyAnimation, QEasingCurve, \ QVBoxLayout, QScrollArea, QPropertyAnimation, QEasingCurve, \
QSizePolicy, QPainter, QRect, pyqtProperty QSizePolicy, QPainter, QRect, pyqtProperty
from PyQt4.QtWebKit import QWebView
from calibre import fit_image, prepare_string_for_xml from calibre import fit_image, prepare_string_for_xml
from calibre.gui2.widgets import IMAGE_EXTENSIONS from calibre.gui2.widgets import IMAGE_EXTENSIONS
@ -165,31 +166,26 @@ class CoverView(QWidget): # {{{
# }}} # }}}
# Book Info {{{ # Book Info {{{
class Label(QLabel): class InfoBook(QWebView):
mr = pyqtSignal(object)
link_clicked = pyqtSignal(object) link_clicked = pyqtSignal(object)
def __init__(self): def __init__(self, parent):
QLabel.__init__(self) QWebView.__init__(self, parent)
self.setTextFormat(Qt.RichText) self.page().setLinkDelegationPolicy(self.page().DelegateAllLinks)
self.setText('') self.linkClicked.connect(self.link_activated)
self.setWordWrap(True)
self.setAlignment(Qt.AlignTop)
self.linkActivated.connect(self.link_activated)
self._link_clicked = False self._link_clicked = False
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
#self.loadFinished.connect(self.turnoff_scrollbar)
def link_activated(self, link): def link_activated(self, link):
self._link_clicked = True self._link_clicked = True
link = unicode(link) link = unicode(link.toString())
self.link_clicked.emit(link) self.link_clicked.emit(link)
def mouseReleaseEvent(self, ev): def turnoff_scrollbar(self, *args):
QLabel.mouseReleaseEvent(self, ev) self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
if not self._link_clicked:
self.mr.emit(ev)
self._link_clicked = False
class BookInfo(QScrollArea): class BookInfo(QScrollArea):
@ -197,14 +193,13 @@ class BookInfo(QScrollArea):
QScrollArea.__init__(self, parent) QScrollArea.__init__(self, parent)
self.vertical = vertical self.vertical = vertical
self.setWidgetResizable(True) self.setWidgetResizable(True)
self.label = Label() self.label = InfoView(self)
self.setWidget(self.label) self.setWidget(self.label)
self.link_clicked = self.label.link_clicked self.link_clicked = self.label.link_clicked
self.mr = self.label.mr
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
def show_data(self, data): def show_data(self, data):
self.label.setText('') self.label.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])
@ -215,11 +210,11 @@ class BookInfo(QScrollArea):
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
self.label.setText(u'<table>%s</table>'%rows) self.label.setHtml(u'<table>%s</table>'%rows)
else: else:
left_pane = u'<table>%s</table>'%rows left_pane = u'<table>%s</table>'%rows
right_pane = u'<div>%s</div>'%comments right_pane = u'<div>%s</div>'%comments
self.label.setText(u'<table><tr><td valign="top" ' self.label.setHtml(u'<table><tr><td valign="top" '
'style="padding-right:2em">%s</td><td valign="top">%s</td></tr></table>' 'style="padding-right:2em">%s</td><td valign="top">%s</td></tr></table>'
% (left_pane, right_pane)) % (left_pane, right_pane))
@ -281,7 +276,6 @@ class BookDetails(QWidget): # {{{
self.book_info = BookInfo(vertical, self) self.book_info = BookInfo(vertical, self)
self._layout.addWidget(self.book_info) self._layout.addWidget(self.book_info)
self.book_info.link_clicked.connect(self._link_clicked) self.book_info.link_clicked.connect(self._link_clicked)
self.book_info.mr.connect(self.mouseReleaseEvent)
if vertical: if vertical:
self.setMinimumSize(QSize(190, 200)) self.setMinimumSize(QSize(190, 200))
else: else: