From 96c19190a0dfeb3b553d4d67ba9abfa5333e3f00 Mon Sep 17 00:00:00 2001 From: dismine Date: Tue, 4 Mar 2014 14:55:57 +0200 Subject: [PATCH] Refactoring. --HG-- branch : feature --- src/tools/drawTools/vtoolsplinepath.cpp | 81 +++++-------- src/tools/drawTools/vtoolsplinepath.h | 1 + src/tools/vtooluniondetails.cpp | 154 ++++++++++-------------- src/tools/vtooluniondetails.h | 1 + 4 files changed, 99 insertions(+), 138 deletions(-) diff --git a/src/tools/drawTools/vtoolsplinepath.cpp b/src/tools/drawTools/vtoolsplinepath.cpp index 5dbef60a4..6f879da06 100644 --- a/src/tools/drawTools/vtoolsplinepath.cpp +++ b/src/tools/drawTools/vtoolsplinepath.cpp @@ -171,6 +171,30 @@ void VToolSplinePath::CorectControlPoints(const VSpline &spl, VSplinePath &splPa splPath.UpdatePoint(indexSpline, SplinePoint::LastPoint, p); } +void VToolSplinePath::RefreshSplinePath(VSplinePath &splPath) +{ + for (qint32 i = 1; i<=splPath.Count(); ++i) + { + VSpline spl = splPath.GetSpline(i); + qint32 j = i*2; + disconnect(controlPoints[j-2], &VControlPointSpline::ControlPointChangePosition, this, + &VToolSplinePath::ControlPointChangePosition); + disconnect(controlPoints[j-1], &VControlPointSpline::ControlPointChangePosition, this, + &VToolSplinePath::ControlPointChangePosition); + controlPoints[j-2]->setPos(spl.GetP2()); + controlPoints[j-1]->setPos(spl.GetP3()); + connect(controlPoints[j-2], &VControlPointSpline::ControlPointChangePosition, this, + &VToolSplinePath::ControlPointChangePosition); + connect(controlPoints[j-1], &VControlPointSpline::ControlPointChangePosition, this, + &VToolSplinePath::ControlPointChangePosition); + + spl = VSpline (spl.GetP1(), controlPoints[j-2]->pos(), controlPoints[j-1]->pos(), spl.GetP4(), + splPath.getKCurve()); + CorectControlPoints(spl, splPath, i); + CorectControlPoints(spl, splPath, i); + } +} + void VToolSplinePath::UpdatePathPoint(QDomNode& node, VSplinePath &path) { QDomNodeList nodeList = node.childNodes(); @@ -244,35 +268,16 @@ void VToolSplinePath::AddToFile() void VToolSplinePath::RefreshDataInFile() { - VSplinePath splPath = *VAbstractTool::data.GeometricObject(id); - for (qint32 i = 1; i<=splPath.Count(); ++i) + QDomElement domElement = doc->elementById(QString().setNum(id)); + if (domElement.isElement() == false) { - VSpline spl = splPath.GetSpline(i); - qint32 j = i*2; - disconnect(controlPoints[j-2], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - disconnect(controlPoints[j-1], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - controlPoints[j-2]->setPos(spl.GetP2()); - controlPoints[j-1]->setPos(spl.GetP3()); - connect(controlPoints[j-2], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - connect(controlPoints[j-1], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - - spl = VSpline (spl.GetP1(), controlPoints[j-2]->pos(), controlPoints[j-1]->pos(), spl.GetP4(), - splPath.getKCurve()); - CorectControlPoints(spl, splPath, i); - CorectControlPoints(spl, splPath, i); - - QDomElement domElement = doc->elementById(QString().setNum(id)); - if (domElement.isElement()) - { - doc->SetAttribute(domElement, AttrKCurve, QString().setNum(splPath.getKCurve())); - UpdatePathPoint(domElement, splPath); - } - + qWarning()<<"Can't find element with id="<(id); + RefreshSplinePath(splPath); + doc->SetAttribute(domElement, AttrKCurve, QString().setNum(splPath.getKCurve())); + UpdatePathPoint(domElement, splPath); } void VToolSplinePath::AddPathPoint(QDomElement &domElement, const VSplinePoint &splPoint) @@ -355,27 +360,7 @@ void VToolSplinePath::SaveDialog(QDomElement &domElement) Q_CHECK_PTR(dialogTool); VSplinePath splPath = dialogTool->GetPath(); - for (qint32 i = 1; i<=splPath.Count(); ++i) - { - VSpline spl = splPath.GetSpline(i); - qint32 j = i*2; - disconnect(controlPoints[j-2], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - disconnect(controlPoints[j-1], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - controlPoints[j-2]->setPos(spl.GetP2()); - controlPoints[j-1]->setPos(spl.GetP3()); - connect(controlPoints[j-2], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - connect(controlPoints[j-1], &VControlPointSpline::ControlPointChangePosition, this, - &VToolSplinePath::ControlPointChangePosition); - - spl = VSpline (spl.GetP1(), controlPoints[j-2]->pos(), controlPoints[j-1]->pos(), spl.GetP4(), - splPath.getKCurve()); - CorectControlPoints(spl, splPath, i); - CorectControlPoints(spl, splPath, i); - } - + RefreshSplinePath(splPath); doc->SetAttribute(domElement, AttrKCurve, QString().setNum(splPath.getKCurve())); UpdatePathPoint(domElement, splPath); } diff --git a/src/tools/drawTools/vtoolsplinepath.h b/src/tools/drawTools/vtoolsplinepath.h index 286585b00..a573a76e2 100644 --- a/src/tools/drawTools/vtoolsplinepath.h +++ b/src/tools/drawTools/vtoolsplinepath.h @@ -199,6 +199,7 @@ private: * @param indexSpline index spline in spline path. */ void CorectControlPoints(const VSpline &spl, VSplinePath &splPath, const qint32 &indexSpline); + void RefreshSplinePath(VSplinePath &splPath); }; #endif // VTOOLSPLINEPATH_H diff --git a/src/tools/vtooluniondetails.cpp b/src/tools/vtooluniondetails.cpp index 092e08f85..5fbb34d7d 100644 --- a/src/tools/vtooluniondetails.cpp +++ b/src/tools/vtooluniondetails.cpp @@ -450,49 +450,41 @@ void VToolUnionDetails::Create(const quint32 _id, const VDetail &d1, const VDeta } } + VNodeDetail det1p1; + VNodeDetail det1p2; + d1.NodeOnEdge(indexD1, det1p1, det1p2); + Q_UNUSED(det1p2); + + VPointF point1; + VPointF point2; + PointsOnEdge(d1, indexD1, point1, point2, data); + + VPointF point3; + VPointF point4; + PointsOnEdge(d2, indexD2, point3, point4, data); + + const qreal dx = point1.x() - point4.x(); + const qreal dy = point1.y() - point4.y(); + + point3.setX(point3.x()+dx); + point3.setY(point3.y()+dy); + + point4.setX(point4.x()+dx); + point4.setY(point4.y()+dy); + + const qreal angle = QLineF(point4.toQPointF(), point3.toQPointF()).angleTo(QLineF(point1.toQPointF(), + point2.toQPointF())); + qint32 pointsD2 = 0; //Keeps count points second detail, what we already add. if (typeCreation == Tool::FromGui) - { - VDetail uD1 = d1.RemoveEdge(indexD1); - VDetail uD2 = d2.RemoveEdge(indexD2); + { qint32 j = 0, i = 0; - qint32 nD1 = uD1.CountNode(); - qint32 nD2 = uD2.CountNode(); - qint32 pointsD2 = 0; //Keeps count points second detail, what we already add. VDetail newDetail; - - VNodeDetail det1p1; - VNodeDetail det1p2; - d1.NodeOnEdge(indexD1, det1p1, det1p2); - const VPointF *point1 = data->GeometricObject(det1p1.getId()); - const VPointF *point2 = data->GeometricObject(det1p2.getId()); - - VNodeDetail det2p1; - VNodeDetail det2p2; - d2.NodeOnEdge(indexD2, det2p1, det2p2); - VPointF point3 = VPointF(*data->GeometricObject(det2p1.getId())); - VPointF point4 = VPointF(*data->GeometricObject(det2p2.getId())); - - qreal dx = point1->x() - point4.x(); - qreal dy = point1->y() - point4.y(); - - point3.setX(point3.x()+dx); - point3.setY(point3.y()+dy); - - point4.setX(point4.x()+dx); - point4.setY(point4.y()+dy); - - QLineF l1(point1->toQPointF(), point2->toQPointF()); - QLineF l2(point4.toQPointF(), point3.toQPointF()); - qreal angle = l2.angleTo(l1); - - ptrdiff_t iD1 = d1.indexOfNode(det1p1.getId()); - do { - AddToNewDetail(unionDetails, doc, data, newDetail, uD1, i, id); + AddToNewDetail(unionDetails, doc, data, newDetail, d1.RemoveEdge(indexD1), i, id); ++i; - if (i > iD1 && pointsD2 < nD2-2) + if (i > d1.indexOfNode(det1p1.getId()) && pointsD2 < d2.RemoveEdge(indexD2).CountNode()-2) { do { @@ -501,83 +493,56 @@ void VToolUnionDetails::Create(const quint32 _id, const VDetail &d1, const VDeta VNodeDetail node1; VNodeDetail node2; d2.NodeOnEdge(indexD2, node1, node2); - ptrdiff_t k = uD2.indexOfNode(node2.getId()); - if (k == uD2.CountNode()-1) + ptrdiff_t k = d2.RemoveEdge(indexD2).indexOfNode(node2.getId()); + if (k == d2.RemoveEdge(indexD2).CountNode()-1) { j = 0; } else { - j = uD2.indexOfNode(node2.getId())+1; + j = d2.RemoveEdge(indexD2).indexOfNode(node2.getId())+1; } } - if (pointsD2 == nD2 -2) + if (pointsD2 == d2.RemoveEdge(indexD2).CountNode() -2) { break; } - if (j >= nD2) + if (j >= d2.RemoveEdge(indexD2).CountNode()) { j=0; } - AddToNewDetail(unionDetails, doc, data, newDetail, uD2, j, id, dx, dy, det1p1.getId(), angle); + AddToNewDetail(unionDetails, doc, data, newDetail, d2.RemoveEdge(indexD2), j, id, dx, dy, + det1p1.getId(), angle); ++pointsD2; ++j; - } while (pointsD2 < nD2); + } while (pointsD2 < d2.RemoveEdge(indexD2).CountNode()); } - } while(i* tools = doc->getTools(); + Q_CHECK_PTR(tools); - VToolDetail *toolDet = qobject_cast(tools->value(d1id)); - toolDet->Remove(); + { + VToolDetail *toolDet = qobject_cast(tools->value(d1id)); + Q_CHECK_PTR(toolDet); + toolDet->Remove(); + } - toolDet = qobject_cast(tools->value(d2id)); + VToolDetail *toolDet = qobject_cast(tools->value(d2id)); + Q_CHECK_PTR(toolDet); toolDet->Remove(); } else { - VDetail uD1 = d1.RemoveEdge(indexD1); - VDetail uD2 = d2.RemoveEdge(indexD2); quint32 idCount = 0; qint32 j = 0, i = 0; - qint32 nD1 = uD1.CountNode(); - qint32 nD2 = uD2.CountNode(); - qint32 pointsD2 = 0; //Keeps count points second detail, what we already add. - - VNodeDetail det1p1; - VNodeDetail det1p2; - d1.NodeOnEdge(indexD1, det1p1, det1p2); - const VPointF *point1 = data->GeometricObject(det1p1.getId()); - const VPointF *point2 = data->GeometricObject(det1p2.getId()); - - VNodeDetail det2p1; - VNodeDetail det2p2; - d2.NodeOnEdge(indexD2, det2p1, det2p2); - VPointF point3 = VPointF(*data->GeometricObject(det2p1.getId())); - VPointF point4 = VPointF(*data->GeometricObject(det2p2.getId())); - - qreal dx = point1->x() - point4.x(); - qreal dy = point1->y() - point4.y(); - - point3.setX(point3.x()+dx); - point3.setY(point3.y()+dy); - - point4.setX(point4.x()+dx); - point4.setY(point4.y()+dy); - - QLineF l1(point1->toQPointF(), point2->toQPointF()); - QLineF l2(point4.toQPointF(), point3.toQPointF()); - qreal angle = l2.angleTo(l1); - - ptrdiff_t iD1 = d1.indexOfNode(det1p1.getId()); - do { - UpdatePoints(id, data, uD1, i, idCount); + UpdatePoints(id, data, d1.RemoveEdge(indexD1), i, idCount); ++i; - if (i > iD1 && pointsD2 < nD2-2) + if (i > d1.indexOfNode(det1p1.getId()) && pointsD2 < d2.RemoveEdge(indexD2).CountNode()-2) { do { @@ -586,33 +551,42 @@ void VToolUnionDetails::Create(const quint32 _id, const VDetail &d1, const VDeta VNodeDetail node1; VNodeDetail node2; d2.NodeOnEdge(indexD2, node1, node2); - ptrdiff_t k = uD2.indexOfNode(node2.getId()); - if (k == uD2.CountNode()-1) + ptrdiff_t k = d2.RemoveEdge(indexD2).indexOfNode(node2.getId()); + if (k == d2.RemoveEdge(indexD2).CountNode()-1) { j = 0; } else { - j = uD2.indexOfNode(node2.getId())+1; + j = d2.RemoveEdge(indexD2).indexOfNode(node2.getId())+1; } } - if (pointsD2 == nD2-2) + if (pointsD2 == d2.RemoveEdge(indexD2).CountNode()-2) { break; } - if (j >= nD2) + if (j >= d2.RemoveEdge(indexD2).CountNode()) { j=0; } - UpdatePoints(id, data, uD2, j, idCount, dx, dy, det1p1.getId(), angle); + UpdatePoints(id, data, d2.RemoveEdge(indexD2), j, idCount, dx, dy, det1p1.getId(), angle); ++pointsD2; ++j; - } while (pointsD2 < nD2); + } while (pointsD2 < d2.RemoveEdge(indexD2).CountNode()); } - } while (iGeometricObject(det2p1.getId())); + p2 = VPointF(*data->GeometricObject(det2p2.getId())); +} + QVector VToolUnionDetails::GetDetailFromFile(VPattern *doc, const QDomElement &domElement) { QVector vector; diff --git a/src/tools/vtooluniondetails.h b/src/tools/vtooluniondetails.h index 625901f4e..ee7a2b845 100644 --- a/src/tools/vtooluniondetails.h +++ b/src/tools/vtooluniondetails.h @@ -84,6 +84,7 @@ public: const quint32 &d2id, const quint32 &indexD1, const quint32 &indexD2, VMainGraphicsScene *scene, VPattern *doc, VContainer *data, const Document::Documents &parse, const Tool::Sources &typeCreation); + static void PointsOnEdge(const VDetail &d, const qint32 &index, VPointF &p1, VPointF &p2, VContainer *data); /** * @brief GetDetailFromFile parse detail from file. * @param doc dom document container.