From 8745a78c67bb06f69c840dd2aeb873fa6a989780 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 13 Jan 2016 15:47:38 +0200 Subject: [PATCH] Calculate items bounding rect without hidden items. --HG-- branch : develop --- src/libs/vwidgets/vmaingraphicsscene.cpp | 15 +++++++++++++++ src/libs/vwidgets/vmaingraphicsscene.h | 2 ++ src/libs/vwidgets/vmaingraphicsview.cpp | 14 +++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/libs/vwidgets/vmaingraphicsscene.cpp b/src/libs/vwidgets/vmaingraphicsscene.cpp index 8a3e3f194..1c4d91c04 100644 --- a/src/libs/vwidgets/vmaingraphicsscene.cpp +++ b/src/libs/vwidgets/vmaingraphicsscene.cpp @@ -88,6 +88,21 @@ QPointF VMainGraphicsScene::getScenePos() const return scenePos; } +//--------------------------------------------------------------------------------------------------------------------- +QRectF VMainGraphicsScene::VisibleItemsBoundingRect() const +{ + QRectF rect; + foreach(QGraphicsItem *item, items()) + { + if(not item->isVisible()) + { + continue; + } + rect = rect.united(item->sceneBoundingRect()); + } + return rect; +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief transform return view transformation. diff --git a/src/libs/vwidgets/vmaingraphicsscene.h b/src/libs/vwidgets/vmaingraphicsscene.h index 903465414..3142f227c 100644 --- a/src/libs/vwidgets/vmaingraphicsscene.h +++ b/src/libs/vwidgets/vmaingraphicsscene.h @@ -49,6 +49,8 @@ public: void setTransform(const QTransform &transform); void SetDisableTools(bool enabled, const QString &namePP); QPointF getScenePos() const; + + QRectF VisibleItemsBoundingRect() const; public slots: void ChoosedItem(quint32 id, const SceneObject &type); void SetFactor(qreal factor); diff --git a/src/libs/vwidgets/vmaingraphicsview.cpp b/src/libs/vwidgets/vmaingraphicsview.cpp index 40479d640..6fb6e3e52 100644 --- a/src/libs/vwidgets/vmaingraphicsview.cpp +++ b/src/libs/vwidgets/vmaingraphicsview.cpp @@ -33,6 +33,7 @@ #include #include #include "vsimplepoint.h" +#include "vmaingraphicsscene.h" #include #include @@ -263,15 +264,16 @@ void VMainGraphicsView::ZoomOriginal() //--------------------------------------------------------------------------------------------------------------------- void VMainGraphicsView::ZoomFitBest() { - QRectF rect = this->scene()->itemsBoundingRect(); - + VMainGraphicsScene *currentScene = qobject_cast(scene()); + SCASSERT(currentScene); + const QRectF rect = currentScene->VisibleItemsBoundingRect(); if (rect.isEmpty()) { return; } this->fitInView(rect, Qt::KeepAspectRatio); - VMainGraphicsView::NewSceneRect(this->scene(), this); + VMainGraphicsView::NewSceneRect(scene(), this); emit NewFactor(this->transform().m11()); } @@ -357,12 +359,14 @@ void VMainGraphicsView::NewSceneRect(QGraphicsScene *sc, QGraphicsView *view) //Calculate view rect //to receive the currently visible area, map the widgets bounds to the scene - const QPointF a = view->mapToScene(0, 0 ); + const QPointF a = view->mapToScene(0, 0); const QPointF b = view->mapToScene(view->width(), view->height()); const QRectF viewRect = QRectF( a, b ); //Calculate scene rect - const QRectF itemsRect = sc->itemsBoundingRect(); + VMainGraphicsScene *currentScene = qobject_cast(sc); + SCASSERT(currentScene); + const QRectF itemsRect = currentScene->VisibleItemsBoundingRect(); //Unite two rects const QRectF newRect = itemsRect.united(viewRect);