From f98ca6eea1cb49ffb92156225db7fc5c99cc53ad Mon Sep 17 00:00:00 2001 From: dismine Date: Wed, 15 Jan 2014 11:20:46 +0200 Subject: [PATCH] Refactoring calculation scene rect. --HG-- branch : develop --- src/mainwindow.cpp | 21 ++---------------- src/tools/drawTools/vtoolsinglepoint.cpp | 28 ++---------------------- src/tools/vabstracttool.cpp | 28 ++++++++++++++++++++++++ src/tools/vabstracttool.h | 1 + src/tools/vtooldetail.cpp | 28 ++---------------------- src/widgets/vmaingraphicsview.cpp | 27 ++--------------------- 6 files changed, 37 insertions(+), 96 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 085c46332..642074f3e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1035,25 +1035,8 @@ void MainWindow::ActionOpen() { OpenPattern(fName); - QRectF rect = sceneDraw->itemsBoundingRect(); - //Correct BoundingRect - rect = QRectF(0, 0, rect.width() + rect.x(), rect.height() + rect.y()); - - QRect rec = view->contentsRect(); - //Correct contentsRect - rec = QRect(0, 0, rec.width() - rec.x(), rec.height() - rec.y()); - - if(rec.contains(rect.toRect())) - { - sceneDraw->setSceneRect(rec); - sceneDetails->setSceneRect(rec); - } - else - { - rect = rect.united(rec); - sceneDraw->setSceneRect(rect); - sceneDetails->setSceneRect(rect); - } + VAbstractTool::NewSceneRect(sceneDraw, view); + VAbstractTool::NewSceneRect(sceneDetails, view); } else { diff --git a/src/tools/drawTools/vtoolsinglepoint.cpp b/src/tools/drawTools/vtoolsinglepoint.cpp index 5154af819..8b6b6cc69 100644 --- a/src/tools/drawTools/vtoolsinglepoint.cpp +++ b/src/tools/drawTools/vtoolsinglepoint.cpp @@ -113,32 +113,8 @@ QVariant VToolSinglePoint::itemChange(QGraphicsItem::GraphicsItemChange change, domElement.setAttribute(AttrX, QString().setNum(toMM(newPos.x()))); domElement.setAttribute(AttrY, QString().setNum(toMM(newPos.y()))); - QGraphicsScene *sc = this->scene(); - QRectF rect = sc->itemsBoundingRect(); - - QList list = sc->views(); - QRect rec0 = list[0]->rect(); - rec0 = QRect(0, 0, rec0.width()-2, rec0.height()-2); - - QTransform t = list[0]->transform(); - - QRectF rec1; - if(t.m11() < 1) - { - rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22()); - - rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y()); - QPolygonF polygone = list[0]->mapToScene(rec1.toRect()); - rec1 = polygone.boundingRect(); - - } - else - { - rec1 = rec0; - } - - rec1 = rec1.united(rect.toRect()); - sc->setSceneRect(rec1); + QList list = this->scene()->views(); + VAbstractTool::NewSceneRect(this->scene(), list[0]); //I don't now why but signal does not work. doc->FullUpdateTree(); diff --git a/src/tools/vabstracttool.cpp b/src/tools/vabstracttool.cpp index eb762bd41..1c342b46c 100644 --- a/src/tools/vabstracttool.cpp +++ b/src/tools/vabstracttool.cpp @@ -74,6 +74,34 @@ VAbstractTool::VAbstractTool(VDomDocument *doc, VContainer *data, qint64 id, QOb connect(this, &VAbstractTool::FullUpdateTree, this->doc, &VDomDocument::FullUpdateTree); } +void VAbstractTool::NewSceneRect(QGraphicsScene *sc, QGraphicsView *view) +{ + QRectF rect = sc->itemsBoundingRect(); + + QRect rec0 = view->rect(); + rec0 = QRect(0, 0, rec0.width()-2, rec0.height()-2); + + QTransform t = view->transform(); + + QRectF rec1; + if(t.m11() < 1) + { + rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22()); + + rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y()); + QPolygonF polygone = view->mapToScene(rec1.toRect()); + rec1 = polygone.boundingRect(); + + } + else + { + rec1 = rec0; + } + + rec1 = rec1.united(rect.toRect()); + sc->setSceneRect(rec1); +} + QPointF VAbstractTool::LineIntersectRect(QRectF rec, QLineF line) { qreal x1, y1, x2, y2; diff --git a/src/tools/vabstracttool.h b/src/tools/vabstracttool.h index 638c092ee..ce6faaced 100644 --- a/src/tools/vabstracttool.h +++ b/src/tools/vabstracttool.h @@ -48,6 +48,7 @@ public: */ VAbstractTool(VDomDocument *doc, VContainer *data, qint64 id, QObject *parent = 0); virtual ~VAbstractTool() {} + static void NewSceneRect(QGraphicsScene *sc, QGraphicsView *view); /** * @brief LineIntersectRect * @param rec diff --git a/src/tools/vtooldetail.cpp b/src/tools/vtooldetail.cpp index 50f7daf0e..42a23f9fd 100644 --- a/src/tools/vtooldetail.cpp +++ b/src/tools/vtooldetail.cpp @@ -259,32 +259,8 @@ QVariant VToolDetail::itemChange(QGraphicsItem::GraphicsItemChange change, const domElement.setAttribute(AttrMx, QString().setNum(toMM(newPos.x()))); domElement.setAttribute(AttrMy, QString().setNum(toMM(newPos.y()))); - QGraphicsScene *sc = this->scene(); - QRectF rect = sc->itemsBoundingRect(); - - QList list = sc->views(); - QRect rec0 = list[0]->rect(); - rec0 = QRect(0, 0, rec0.width()-2, rec0.height()-2); - - QTransform t = list[0]->transform(); - - QRectF rec1; - if(t.m11() < 1) - { - rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22()); - - rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y()); - QPolygonF polygone = list[0]->mapToScene(rec1.toRect()); - rec1 = polygone.boundingRect(); - - } - else - { - rec1 = rec0; - } - - rec1 = rec1.united(rect.toRect()); - sc->setSceneRect(rec1); + QList list = this->scene()->views(); + VAbstractTool::NewSceneRect(this->scene(), list[0]); doc->haveLiteChange(); } diff --git a/src/widgets/vmaingraphicsview.cpp b/src/widgets/vmaingraphicsview.cpp index 7b9b4f71a..d17023090 100644 --- a/src/widgets/vmaingraphicsview.cpp +++ b/src/widgets/vmaingraphicsview.cpp @@ -32,6 +32,7 @@ #include #include #include +#include "../tools/vabstracttool.h" VMainGraphicsView::VMainGraphicsView(QWidget *parent) :QGraphicsView(parent), _numScheduledScalings(0) @@ -80,31 +81,7 @@ void VMainGraphicsView::scalingTime(qreal x) horizontalScrollBar()->setValue(move.x() + horizontalScrollBar()->value()); verticalScrollBar()->setValue(move.y() + verticalScrollBar()->value()); - QGraphicsScene *sc = this->scene(); - QRectF rect = sc->itemsBoundingRect(); - - QRect rec0 = this->rect(); - rec0 = QRect(0, 0, rec0.width()-2, rec0.height()-2); - - QTransform t = this->transform(); - - QRectF rec1; - if(t.m11() < 1) - { - rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22()); - - rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y()); - QPolygonF polygone = this->mapToScene(rec1.toRect()); - rec1 = polygone.boundingRect(); - - } - else - { - rec1 = rec0; - } - - rec1 = rec1.united(rect.toRect()); - sc->setSceneRect(rec1); + VAbstractTool::NewSceneRect(this->scene(), this); emit NewFactor(factor); }