webelem: Avoid unnecessary ::geometry calls
This commit is contained in:
parent
658053842e
commit
84cdb30bcb
@ -336,7 +336,7 @@ def focus_elem(frame):
|
|||||||
return WebElementWrapper(elem)
|
return WebElementWrapper(elem)
|
||||||
|
|
||||||
|
|
||||||
def rect_on_view(elem):
|
def rect_on_view(elem, elem_geometry=None):
|
||||||
"""Get the geometry of the element relative to the webview.
|
"""Get the geometry of the element relative to the webview.
|
||||||
|
|
||||||
We need this as a standalone function (as opposed to a WebElementWrapper
|
We need this as a standalone function (as opposed to a WebElementWrapper
|
||||||
@ -345,11 +345,16 @@ def rect_on_view(elem):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
elem: The QWebElement to get the rect for.
|
elem: The QWebElement to get the rect for.
|
||||||
|
elem_geometry: The geometry of the element, or None.
|
||||||
|
Calling QWebElement::geometry is rather expensive so we
|
||||||
|
want to avoid doing it twice.
|
||||||
"""
|
"""
|
||||||
if elem.isNull():
|
if elem.isNull():
|
||||||
raise IsNullError("Got called on a null element!")
|
raise IsNullError("Got called on a null element!")
|
||||||
|
if elem_geometry is None:
|
||||||
|
elem_geometry = elem.geometry()
|
||||||
frame = elem.webFrame()
|
frame = elem.webFrame()
|
||||||
rect = QRect(elem.geometry())
|
rect = QRect(elem_geometry)
|
||||||
while frame is not None:
|
while frame is not None:
|
||||||
rect.translate(frame.geometry().topLeft())
|
rect.translate(frame.geometry().topLeft())
|
||||||
rect.translate(frame.scrollPosition() * -1)
|
rect.translate(frame.scrollPosition() * -1)
|
||||||
@ -378,35 +383,35 @@ def is_visible(elem, mainframe):
|
|||||||
for k, v in hidden_attributes.items():
|
for k, v in hidden_attributes.items():
|
||||||
if elem.styleProperty(k, QWebElement.ComputedStyle) == v:
|
if elem.styleProperty(k, QWebElement.ComputedStyle) == v:
|
||||||
return False
|
return False
|
||||||
geometry = elem.geometry()
|
elem_geometry = elem.geometry()
|
||||||
if not geometry.isValid() and geometry.x() == 0:
|
if not elem_geometry.isValid() and elem_geometry.x() == 0:
|
||||||
# Most likely an invisible link
|
# Most likely an invisible link
|
||||||
return False
|
return False
|
||||||
# First check if the element is visible on screen
|
# First check if the element is visible on screen
|
||||||
elem_rect = rect_on_view(elem)
|
elem_rect = rect_on_view(elem, elem_geometry=elem_geometry)
|
||||||
|
mainframe_geometry = mainframe.geometry()
|
||||||
if elem_rect.isValid():
|
if elem_rect.isValid():
|
||||||
visible_on_screen = mainframe.geometry().intersects(elem_rect)
|
visible_on_screen = mainframe_geometry.intersects(elem_rect)
|
||||||
else:
|
else:
|
||||||
# We got an invalid rectangle (width/height 0/0 probably), but this
|
# We got an invalid rectangle (width/height 0/0 probably), but this
|
||||||
# can still be a valid link.
|
# can still be a valid link.
|
||||||
visible_on_screen = mainframe.geometry().contains(
|
visible_on_screen = mainframe_geometry.contains(
|
||||||
elem_rect.topLeft())
|
elem_rect.topLeft())
|
||||||
# Then check if it's visible in its frame if it's not in the main
|
# Then check if it's visible in its frame if it's not in the main
|
||||||
# frame.
|
# frame.
|
||||||
elem_frame = elem.webFrame()
|
elem_frame = elem.webFrame()
|
||||||
elem_rect = elem.geometry()
|
|
||||||
framegeom = QRect(elem_frame.geometry())
|
framegeom = QRect(elem_frame.geometry())
|
||||||
if not framegeom.isValid():
|
if not framegeom.isValid():
|
||||||
visible_in_frame = False
|
visible_in_frame = False
|
||||||
elif elem_frame.parentFrame() is not None:
|
elif elem_frame.parentFrame() is not None:
|
||||||
framegeom.moveTo(0, 0)
|
framegeom.moveTo(0, 0)
|
||||||
framegeom.translate(elem_frame.scrollPosition())
|
framegeom.translate(elem_frame.scrollPosition())
|
||||||
if elem_rect.isValid():
|
if elem_geometry.isValid():
|
||||||
visible_in_frame = framegeom.intersects(elem_rect)
|
visible_in_frame = framegeom.intersects(elem_geometry)
|
||||||
else:
|
else:
|
||||||
# We got an invalid rectangle (width/height 0/0 probably), but
|
# We got an invalid rectangle (width/height 0/0 probably), but
|
||||||
# this can still be a valid link.
|
# this can still be a valid link.
|
||||||
visible_in_frame = framegeom.contains(elem_rect.topLeft())
|
visible_in_frame = framegeom.contains(elem_geometry.topLeft())
|
||||||
else:
|
else:
|
||||||
visible_in_frame = visible_on_screen
|
visible_in_frame = visible_on_screen
|
||||||
return all([visible_on_screen, visible_in_frame])
|
return all([visible_on_screen, visible_in_frame])
|
||||||
|
Loading…
Reference in New Issue
Block a user