Extend paintEvent instead of using an eventFilter

This commit is contained in:
Florian Bruhin 2014-02-18 17:26:26 +01:00
parent bc589c3417
commit 0425ce7b48

View File

@ -519,7 +519,6 @@ class BrowserTab(QWebView):
self.signal_cache = SignalCache(uncached=['linkHovered']) self.signal_cache = SignalCache(uncached=['linkHovered'])
self.page_.setLinkDelegationPolicy(QWebPage.DelegateAllLinks) self.page_.setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.page_.linkHovered.connect(self.linkHovered) self.page_.linkHovered.connect(self.linkHovered)
self.installEventFilter(self)
self.linkClicked.connect(self.on_link_clicked) self.linkClicked.connect(self.on_link_clicked)
# FIXME find some way to hide scrollbars without setScrollBarPolicy # FIXME find some way to hide scrollbars without setScrollBarPolicy
@ -605,33 +604,30 @@ class BrowserTab(QWebView):
logging.debug("Everything destroyed, calling callback") logging.debug("Everything destroyed, calling callback")
self._shutdown_callback() self._shutdown_callback()
def eventFilter(self, watched, e): def paintEvent(self, e):
"""Dirty hack to emit a signal if the scroll position changed. """Extend paintEvent to emit a signal if the scroll position changed.
We listen to repaint requests here, in the hope a repaint will always This is a bit of a hack: We listen to repaint requests here, in the
be requested when scrolling, and if the scroll position actually hope a repaint will always be requested when scrolling, and if the
changed, we emit a signal. scroll position actually changed, we emit a signal.
watched -- The watched Qt object. e -- The QPaintEvent.
e -- The new event.
""" """
if watched == self and e.type() == QEvent.Paint: frame = self.page_.mainFrame()
new_pos = (frame.scrollBarValue(Qt.Horizontal),
frame.scrollBarValue(Qt.Vertical))
if self._scroll_pos != new_pos:
self._scroll_pos = new_pos
logging.debug("Updating scroll position")
frame = self.page_.mainFrame() frame = self.page_.mainFrame()
new_pos = (frame.scrollBarValue(Qt.Horizontal), m = (frame.scrollBarMaximum(Qt.Horizontal),
frame.scrollBarValue(Qt.Vertical)) frame.scrollBarMaximum(Qt.Vertical))
if self._scroll_pos != new_pos: perc = (round(100 * new_pos[0] / m[0]) if m[0] != 0 else 0,
self._scroll_pos = new_pos round(100 * new_pos[1] / m[1]) if m[1] != 0 else 0)
logging.debug("Updating scroll position") self.scroll_pos_changed.emit(*perc)
frame = self.page_.mainFrame() # Let superclass handle the event
m = (frame.scrollBarMaximum(Qt.Horizontal), return super().paintEvent(e)
frame.scrollBarMaximum(Qt.Vertical))
perc = (round(100 * new_pos[0] / m[0]) if m[0] != 0 else 0,
round(100 * new_pos[1] / m[1]) if m[1] != 0 else 0)
self.scroll_pos_changed.emit(*perc)
# we're not actually filtering something, let superclass handle the
# event
return super().eventFilter(watched, e)
def event(self, e): def event(self, e):
"""Check if a link was clicked with the middle button or Ctrl. """Check if a link was clicked with the middle button or Ctrl.