Calculate items bounding rect without hidden items.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-01-13 15:47:38 +02:00
parent e9e8a41c38
commit 8745a78c67
3 changed files with 26 additions and 5 deletions

View File

@ -88,6 +88,21 @@ QPointF VMainGraphicsScene::getScenePos() const
return scenePos; 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. * @brief transform return view transformation.

View File

@ -49,6 +49,8 @@ public:
void setTransform(const QTransform &transform); void setTransform(const QTransform &transform);
void SetDisableTools(bool enabled, const QString &namePP); void SetDisableTools(bool enabled, const QString &namePP);
QPointF getScenePos() const; QPointF getScenePos() const;
QRectF VisibleItemsBoundingRect() const;
public slots: public slots:
void ChoosedItem(quint32 id, const SceneObject &type); void ChoosedItem(quint32 id, const SceneObject &type);
void SetFactor(qreal factor); void SetFactor(qreal factor);

View File

@ -33,6 +33,7 @@
#include <QApplication> #include <QApplication>
#include <QScrollBar> #include <QScrollBar>
#include "vsimplepoint.h" #include "vsimplepoint.h"
#include "vmaingraphicsscene.h"
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QMouseEvent> #include <QMouseEvent>
@ -263,15 +264,16 @@ void VMainGraphicsView::ZoomOriginal()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VMainGraphicsView::ZoomFitBest() void VMainGraphicsView::ZoomFitBest()
{ {
QRectF rect = this->scene()->itemsBoundingRect(); VMainGraphicsScene *currentScene = qobject_cast<VMainGraphicsScene *>(scene());
SCASSERT(currentScene);
const QRectF rect = currentScene->VisibleItemsBoundingRect();
if (rect.isEmpty()) if (rect.isEmpty())
{ {
return; return;
} }
this->fitInView(rect, Qt::KeepAspectRatio); this->fitInView(rect, Qt::KeepAspectRatio);
VMainGraphicsView::NewSceneRect(this->scene(), this); VMainGraphicsView::NewSceneRect(scene(), this);
emit NewFactor(this->transform().m11()); emit NewFactor(this->transform().m11());
} }
@ -357,12 +359,14 @@ void VMainGraphicsView::NewSceneRect(QGraphicsScene *sc, QGraphicsView *view)
//Calculate view rect //Calculate view rect
//to receive the currently visible area, map the widgets bounds to the scene //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 QPointF b = view->mapToScene(view->width(), view->height());
const QRectF viewRect = QRectF( a, b ); const QRectF viewRect = QRectF( a, b );
//Calculate scene rect //Calculate scene rect
const QRectF itemsRect = sc->itemsBoundingRect(); VMainGraphicsScene *currentScene = qobject_cast<VMainGraphicsScene *>(sc);
SCASSERT(currentScene);
const QRectF itemsRect = currentScene->VisibleItemsBoundingRect();
//Unite two rects //Unite two rects
const QRectF newRect = itemsRect.united(viewRect); const QRectF newRect = itemsRect.united(viewRect);