|
1 | 1 | import sys |
2 | 2 | import math |
3 | 3 |
|
4 | | -from PyQt5.QtCore import Qt, QRectF, QEvent, QCoreApplication, QObject, QPointF |
| 4 | +from PyQt5.QtCore import ( |
| 5 | + Qt, QRectF, QEvent, QCoreApplication, QObject, QPointF, QRect |
| 6 | +) |
5 | 7 | from PyQt5.QtGui import QBrush, QPalette, QTransform, QPolygonF |
6 | 8 | from PyQt5.QtWidgets import ( |
7 | 9 | QGraphicsView, QGraphicsScene, QWidget, QVBoxLayout, QSizePolicy, |
@@ -219,10 +221,10 @@ def __updateView(self, view: QGraphicsView, rect: QRectF) -> None: |
219 | 221 | return |
220 | 222 | # map the rect to (main) viewport coordinates |
221 | 223 | viewrect = qgraphicsview_map_rect_from_scene(self, rect).boundingRect() |
222 | | - viewrect = viewrect.toAlignedRect() |
| 224 | + viewrect = qrectf_to_inscribed_rect(viewrect) |
223 | 225 | viewportrect = self.viewport().rect() |
224 | | - visible = not (viewrect.top() >= viewportrect.top() |
225 | | - and viewrect.bottom() <= viewportrect.y() + viewportrect.height()) |
| 226 | + visible = (viewrect.top() < viewportrect.top() or |
| 227 | + viewrect.y() + viewrect.height() > viewportrect.y() + viewportrect.height()) |
226 | 228 | container.setVisible(visible) |
227 | 229 | # force immediate layout of the container overlay |
228 | 230 | QCoreApplication.sendEvent(container, QEvent(QEvent.LayoutRequest)) |
@@ -263,6 +265,18 @@ def qgraphicsview_map_rect_from_scene( |
263 | 265 | return QPolygonF([p1, p2, p3, p4]) |
264 | 266 |
|
265 | 267 |
|
| 268 | +def qrectf_to_inscribed_rect(rect: QRectF) -> QRect: |
| 269 | + """ |
| 270 | + Return the largest integer QRect such that it is completely contained in |
| 271 | + `rect`. |
| 272 | + """ |
| 273 | + xmin = int(math.ceil(rect.x())) |
| 274 | + xmax = int(math.floor(rect.right())) |
| 275 | + ymin = int(math.ceil(rect.top())) |
| 276 | + ymax = int(math.floor(rect.bottom())) |
| 277 | + return QRect(xmin, ymin, max(xmax - xmin, 0), max(ymax - ymin, 0)) |
| 278 | + |
| 279 | + |
266 | 280 | def main(args): # pragma: no cover |
267 | 281 | # pylint: disable=import-outside-toplevel,protected-access |
268 | 282 | from PyQt5.QtWidgets import QApplication, QAction |
|
0 commit comments