From 6f5db879c1e0462a6e594fa8b003a0945b028e13 Mon Sep 17 00:00:00 2001 From: ispyisail Date: Sun, 4 Oct 2026 11:23:26 +1300 Subject: [PATCH] Zoom to content: count only visible items itemsBoundingRect() also counts hidden items. The text of a single-line wire, and the wire texts hidden by "one text per potential", are never positioned again and can sit far outside the drawing, so Zoom to content zoomed out to include them. On photovoltaique.qet 14 such texts reach x = -729 and the zoom covered 2421x924 instead of 1135x648. Add Diagram::visibleItemsBoundingRect() and use it for Zoom to content, the scene rect and the borderless export. Zoom to content now goes through zoomToRect(), which re-centres after the scroll bars appear; before, the view could stay scrolled to the top-left with part of the content cut off. Fixes #1281 Co-Authored-By: Claude Opus 5.5 --- sources/diagram.cpp | 25 ++++++++++++++++++++++--- sources/diagram.h | 1 + sources/diagramview.cpp | 5 +++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/sources/diagram.cpp b/sources/diagram.cpp index f7c336ed6..1d07e7d82 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -929,7 +929,7 @@ bool Diagram::toPaintDevice(QPaintDevice &pix, // determine la zone source = contenu du schema + marges QRectF source_area; if (!use_border_) { - source_area = itemsBoundingRect(); + source_area = visibleItemsBoundingRect(); source_area.translate(-margin, -margin); source_area.setWidth (source_area.width () + 2.0 * margin); source_area.setHeight(source_area.height() + 2.0 * margin); @@ -997,7 +997,7 @@ QSize Diagram::imageSize() const // determine la zone source = contenu du schema + marges qreal image_width, image_height; if (!use_border_) { - QRectF items_rect = itemsBoundingRect(); + QRectF items_rect = visibleItemsBoundingRect(); image_width = items_rect.width(); image_height = items_rect.height(); } else { @@ -2838,10 +2838,29 @@ void Diagram::adjustSceneRect() { QRectF old_rect = sceneRect(); setSceneRect(border_and_titleblock.borderAndTitleBlockRect().united( - itemsBoundingRect())); + visibleItemsBoundingRect())); update(old_rect.united(sceneRect())); } +/** + @brief Diagram::visibleItemsBoundingRect + Same as QGraphicsScene::itemsBoundingRect(), but only counts items that + are shown. A hidden item keeps whatever position it last had: the text of + a single-line wire, and the wire texts hidden by "one text per potential", + are never positioned again and can sit far outside the drawing (#1281). + @return the bounding rect of the visible items, in scene coordinates +*/ +QRectF Diagram::visibleItemsBoundingRect() const +{ + QRectF rect; + const auto scene_items = items(); + for (QGraphicsItem *item : scene_items) { + if (item->isVisible()) + rect |= item->sceneBoundingRect(); + } + return rect; +} + /** @brief Diagram::applyProperties This method allows you to apply new rendering options while diff --git a/sources/diagram.h b/sources/diagram.h index ff7ff1f9b..7a11f6d07 100644 --- a/sources/diagram.h +++ b/sources/diagram.h @@ -258,6 +258,7 @@ class Diagram : public QGraphicsScene bool toPaintDevice(QPaintDevice &, int = -1, int = -1, Qt::AspectRatioMode = Qt::KeepAspectRatio); QSize imageSize() const; + QRectF visibleItemsBoundingRect() const; bool isEmpty() const; diff --git a/sources/diagramview.cpp b/sources/diagramview.cpp index f0cd7ca5f..cbf8eff0d 100644 --- a/sources/diagramview.cpp +++ b/sources/diagramview.cpp @@ -479,8 +479,9 @@ void DiagramView::zoomFit() */ void DiagramView::zoomContent() { - fitInView(m_diagram -> itemsBoundingRect(), Qt::KeepAspectRatio); - adjustGridToZoom(); + const QRectF content = m_diagram->visibleItemsBoundingRect(); + if (!content.isNull()) + zoomToRect(content); } /**