/************************************************************************ ** ** @file vistoolcubicbezierpath.cpp ** @author Roman Telezhynskyi ** @date 18 3, 2016 ** ** @brief ** @copyright ** This source code is part of the Valentine project, a pattern making ** program, whose allow create and modeling patterns of clothing. ** Copyright (C) 2016 Valentina project ** All Rights Reserved. ** ** Valentina is free software: you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation, either version 3 of the License, or ** (at your option) any later version. ** ** Valentina is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with Valentina. If not, see . ** *************************************************************************/ #include "vistoolcubicbezierpath.h" #include "../vgeometry/vspline.h" //--------------------------------------------------------------------------------------------------------------------- VisToolCubicBezierPath::VisToolCubicBezierPath(const VContainer *data, QGraphicsItem *parent) : VisPath(data, parent), mainPoints(), ctrlPoints(), lines(), newCurveSegment(nullptr), path(), helpLine1(nullptr), helpLine2(nullptr) { helpLine1 = InitItem(mainColor, this); helpLine2 = InitItem(mainColor, this); newCurveSegment = InitItem(mainColor, this); } //--------------------------------------------------------------------------------------------------------------------- VisToolCubicBezierPath::~VisToolCubicBezierPath() { qDeleteAll(mainPoints); qDeleteAll(lines); } //--------------------------------------------------------------------------------------------------------------------- void VisToolCubicBezierPath::RefreshGeometry() { if (path.CountPoints() > 0) { const QVector pathPoints = path.GetSplinePath(); const int size = pathPoints.size(); const int countSubSpl = path.CountSubSpl(); for (int i = 0; i < size; ++i) { QGraphicsEllipseItem *point = this->getPoint(mainPoints, static_cast(i), 1/*zValue*/); DrawPoint(point, pathPoints.at(i).toQPointF(), supportColor); } if (mode == Mode::Creation) { for (qint32 i = 1; i<=countSubSpl; ++i) { const int preLastPoint = (countSubSpl - 1) * 2; const int lastPoint = preLastPoint + 1; const VSpline spl = path.GetSpline(i); QGraphicsLineItem *ctrlLine1 = this->getLine(static_cast(preLastPoint)); DrawLine(ctrlLine1, QLineF(spl.GetP1().toQPointF(), spl.GetP2()), mainColor, Qt::DashLine); QGraphicsEllipseItem *p2 = this->getPoint(ctrlPoints, static_cast(preLastPoint)); DrawPoint(p2, spl.GetP2(), Qt::green); QGraphicsLineItem *ctrlLine2 = this->getLine(static_cast(lastPoint)); DrawLine(ctrlLine2, QLineF(spl.GetP4().toQPointF(), spl.GetP3()), mainColor, Qt::DashLine); QGraphicsEllipseItem *p3 = this->getPoint(ctrlPoints, static_cast(lastPoint)); DrawPoint(p3, spl.GetP3(), Qt::green); } const qint32 last = (countSubSpl - 1) * 3 + 3; Creating(pathPoints, size-1-last); } if (countSubSpl >= 1) { DrawPath(this, path.GetPath(PathDirection::Show), mainColor, Qt::SolidLine, Qt::RoundCap); } if (countSubSpl < 7) { Visualization::toolTip = tr("Curved path: select seven or more points"); } else if (countSubSpl >= 7 && size - ((countSubSpl - 1) * 3 + 4) == 0) { Visualization::toolTip = tr("Curved path: select seven or more points, " "Enter - finish creation"); } else { Visualization::toolTip = tr("Curved path: select more points for complete segment"); } } } //--------------------------------------------------------------------------------------------------------------------- void VisToolCubicBezierPath::setPath(const VCubicBezierPath &value) { path = value; } //--------------------------------------------------------------------------------------------------------------------- // cppcheck-suppress unusedFunction VCubicBezierPath VisToolCubicBezierPath::getPath() { return path; } //--------------------------------------------------------------------------------------------------------------------- QGraphicsEllipseItem *VisToolCubicBezierPath::getPoint(QVector &points, quint32 i, qreal z) { if (not points.isEmpty() && static_cast(points.size() - 1) >= i) { return points.at(static_cast(i)); } else { auto point = InitPoint(supportColor, this, z); points.append(point); return point; } return nullptr; } //--------------------------------------------------------------------------------------------------------------------- QGraphicsLineItem *VisToolCubicBezierPath::getLine(quint32 i) { if (static_cast(lines.size() - 1) >= i && lines.isEmpty() == false) { return lines.at(static_cast(i)); } else { auto line = InitItem(mainColor, this); lines.append(line); return line; } return nullptr; } //--------------------------------------------------------------------------------------------------------------------- void VisToolCubicBezierPath::Creating(const QVector &pathPoints, int pointsLeft) { if (pathPoints.isEmpty() || pathPoints.size()+1 < pointsLeft) { return; } switch(pointsLeft) { case 0: { const QPointF p1 = pathPoints.last().toQPointF(); DrawLine(helpLine1, QLineF(p1, Visualization::scenePos), mainColor, Qt::DashLine); break; } case 1: { const VPointF p1 = pathPoints.at(pointsLeft-1); const QPointF p2 = pathPoints.at(pointsLeft).toQPointF(); DrawLine(helpLine1, QLineF(p1.toQPointF(), p2), mainColor, Qt::DashLine); VSpline spline(p1, p2, Visualization::scenePos, VPointF(Visualization::scenePos)); DrawPath(newCurveSegment, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap); break; } case 2: { const VPointF p1 = pathPoints.at(pointsLeft-2); const QPointF p2 = pathPoints.at(pointsLeft-1).toQPointF(); const QPointF p3 = pathPoints.at(pointsLeft).toQPointF(); DrawLine(helpLine1, QLineF(p1.toQPointF(), p2), mainColor, Qt::DashLine); DrawLine(helpLine2, QLineF(p3, Visualization::scenePos), mainColor, Qt::DashLine); VSpline spline(p1, p2, p3, VPointF(Visualization::scenePos)); DrawPath(newCurveSegment, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap); break; } default: break; } }