2014-09-07 18:51:22 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vistoolsplinepath.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 7 9, 2014
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2014-09-07 18:51:22 +02:00
|
|
|
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vistoolsplinepath.h"
|
2016-02-13 17:24:32 +01:00
|
|
|
#include "../vwidgets/vcontrolpointspline.h"
|
2014-09-07 18:51:22 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VisToolSplinePath::VisToolSplinePath(const VContainer *data, QGraphicsItem *parent)
|
2016-02-13 17:24:32 +01:00
|
|
|
: VisPath(data, parent),
|
|
|
|
points(QVector<QGraphicsEllipseItem *>()),
|
|
|
|
ctrlPoints(QVector<VControlPointSpline *>()),
|
|
|
|
newCurveSegment(nullptr),
|
|
|
|
path(VSplinePath()),
|
|
|
|
isLeftMousePressed(false),
|
|
|
|
pointSelected(false),
|
|
|
|
ctrlPoint()
|
2014-09-07 18:51:22 +02:00
|
|
|
{
|
2016-02-13 17:24:32 +01:00
|
|
|
newCurveSegment = InitItem<QGraphicsPathItem>(mainColor, this);
|
2014-09-07 18:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VisToolSplinePath::~VisToolSplinePath()
|
|
|
|
{
|
2016-02-13 17:24:32 +01:00
|
|
|
qDeleteAll(ctrlPoints);
|
|
|
|
qDeleteAll(points);
|
2016-07-13 13:42:50 +02:00
|
|
|
emit ToolTip("");
|
2014-09-07 18:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VisToolSplinePath::RefreshGeometry()
|
|
|
|
{
|
2016-03-16 19:07:50 +01:00
|
|
|
if (path.CountPoints() > 0)
|
2014-09-07 18:51:22 +02:00
|
|
|
{
|
2016-02-13 17:24:32 +01:00
|
|
|
const QVector<VSplinePoint> pathPoints = path.GetSplinePath();
|
|
|
|
const int size = pathPoints.size();
|
2014-09-07 18:51:22 +02:00
|
|
|
|
2016-02-13 17:24:32 +01:00
|
|
|
for (int i = 0; i < size; ++i)
|
2014-09-07 18:51:22 +02:00
|
|
|
{
|
2016-02-13 17:24:32 +01:00
|
|
|
QGraphicsEllipseItem *point = this->getPoint(static_cast<unsigned>(i));
|
2016-05-14 21:28:09 +02:00
|
|
|
DrawPoint(point, pathPoints.at(i).P(), supportColor);
|
2016-02-13 17:24:32 +01:00
|
|
|
}
|
2014-09-07 18:51:22 +02:00
|
|
|
|
2016-02-13 17:24:32 +01:00
|
|
|
if (mode == Mode::Creation)
|
|
|
|
{
|
2016-02-13 22:39:47 +01:00
|
|
|
if (size > 1)
|
|
|
|
{
|
2016-03-16 19:07:50 +01:00
|
|
|
for (qint32 i = 1; i<=path.CountSubSpl(); ++i)
|
2016-02-13 22:39:47 +01:00
|
|
|
{
|
2016-03-16 19:07:50 +01:00
|
|
|
const int preLastPoint = (path.CountSubSpl() - 1) * 2;
|
2016-02-13 22:39:47 +01:00
|
|
|
const int lastPoint = preLastPoint + 1;
|
|
|
|
|
|
|
|
VSpline spl = path.GetSpline(i);
|
|
|
|
|
|
|
|
ctrlPoints[preLastPoint]->RefreshCtrlPoint(i, SplinePointPosition::FirstPoint, spl.GetP2(),
|
2016-05-14 21:28:09 +02:00
|
|
|
spl.GetP1());
|
2016-02-13 22:39:47 +01:00
|
|
|
ctrlPoints[lastPoint]->RefreshCtrlPoint(i, SplinePointPosition::LastPoint, spl.GetP3(),
|
2016-05-14 21:28:09 +02:00
|
|
|
spl.GetP4());
|
2016-02-13 22:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 21:28:09 +02:00
|
|
|
Creating(pathPoints.at(size-1).P(), size);
|
2016-02-13 17:24:32 +01:00
|
|
|
}
|
2014-09-07 18:51:22 +02:00
|
|
|
|
2016-02-13 17:24:32 +01:00
|
|
|
if (size > 1)
|
|
|
|
{
|
2014-09-07 18:51:22 +02:00
|
|
|
DrawPath(this, path.GetPath(PathDirection::Show), mainColor, Qt::SolidLine, Qt::RoundCap);
|
|
|
|
}
|
2016-02-13 17:24:32 +01:00
|
|
|
|
2016-03-16 19:07:50 +01:00
|
|
|
if (path.CountPoints() < 3)
|
2014-11-21 18:24:16 +01:00
|
|
|
{
|
2014-12-04 12:23:40 +01:00
|
|
|
Visualization::toolTip = tr("<b>Curved path</b>: select three or more points");
|
2014-11-21 18:24:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-04 12:23:40 +01:00
|
|
|
Visualization::toolTip = tr("<b>Curved path</b>: select three or more points, "
|
2014-11-22 11:57:05 +01:00
|
|
|
"<b>Enter</b> - finish creation");
|
2014-11-21 18:24:16 +01:00
|
|
|
}
|
2016-07-13 13:42:50 +02:00
|
|
|
if (mode == Mode::Show)
|
|
|
|
{
|
|
|
|
Visualization::toolTip = tr("Use <b>Shift</b> for sticking angle!");
|
|
|
|
emit ToolTip(Visualization::toolTip);
|
|
|
|
}
|
2014-09-07 18:51:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VisToolSplinePath::setPath(const VSplinePath &value)
|
|
|
|
{
|
|
|
|
path = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-09-07 18:51:22 +02:00
|
|
|
VSplinePath VisToolSplinePath::getPath()
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-02-13 17:24:32 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VisToolSplinePath::MouseLeftPressed()
|
|
|
|
{
|
|
|
|
if (mode == Mode::Creation)
|
|
|
|
{
|
|
|
|
isLeftMousePressed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VisToolSplinePath::MouseLeftReleased()
|
|
|
|
{
|
|
|
|
if (mode == Mode::Creation)
|
|
|
|
{
|
|
|
|
isLeftMousePressed = false;
|
|
|
|
RefreshGeometry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-07 18:51:22 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-10-26 09:36:07 +01:00
|
|
|
QGraphicsEllipseItem *VisToolSplinePath::getPoint(quint32 i)
|
2014-09-07 18:51:22 +02:00
|
|
|
{
|
2015-10-26 09:36:07 +01:00
|
|
|
if (static_cast<quint32>(points.size() - 1) >= i && points.isEmpty() == false)
|
2014-09-11 16:15:49 +02:00
|
|
|
{
|
2014-09-11 18:52:02 +02:00
|
|
|
return points.at(static_cast<int>(i));
|
2014-09-07 18:51:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-13 17:24:32 +01:00
|
|
|
pointSelected = false;
|
|
|
|
|
|
|
|
auto point = InitPoint(supportColor, this);
|
2014-09-07 18:51:22 +02:00
|
|
|
points.append(point);
|
2016-02-13 17:24:32 +01:00
|
|
|
|
|
|
|
if (points.size() == 1)
|
|
|
|
{
|
|
|
|
auto *controlPoint1 = new VControlPointSpline(points.size(), SplinePointPosition::FirstPoint,
|
|
|
|
*Visualization::data->GetPatternUnit(),
|
|
|
|
this);
|
|
|
|
controlPoint1->hide();
|
|
|
|
ctrlPoints.append(controlPoint1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto *controlPoint1 = new VControlPointSpline(points.size()-1, SplinePointPosition::LastPoint,
|
|
|
|
*Visualization::data->GetPatternUnit(),
|
|
|
|
this);
|
|
|
|
controlPoint1->hide();
|
|
|
|
ctrlPoints.append(controlPoint1);
|
|
|
|
|
|
|
|
auto *controlPoint2 = new VControlPointSpline(points.size(), SplinePointPosition::FirstPoint,
|
|
|
|
*Visualization::data->GetPatternUnit(),
|
|
|
|
this);
|
|
|
|
controlPoint2->hide();
|
|
|
|
ctrlPoints.append(controlPoint2);
|
|
|
|
}
|
|
|
|
|
2014-09-07 18:51:22 +02:00
|
|
|
return point;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-13 17:24:32 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VisToolSplinePath::Creating(const QPointF &pSpl, int size)
|
|
|
|
{
|
|
|
|
//Radius of point circle, but little bigger. Need handle with hover sizes.
|
|
|
|
const static qreal radius = ToPixel(DefPointRadius/*mm*/, Unit::Mm)*1.5;
|
|
|
|
|
|
|
|
int lastPoint = 0;
|
|
|
|
int preLastPoint = 0;
|
|
|
|
|
|
|
|
if (size > 1)
|
|
|
|
{
|
|
|
|
lastPoint = (size - 1) * 2;
|
|
|
|
preLastPoint = lastPoint - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLeftMousePressed && not pointSelected)
|
|
|
|
{
|
|
|
|
newCurveSegment->hide();
|
|
|
|
|
|
|
|
ctrlPoint = Visualization::scenePos;
|
|
|
|
|
|
|
|
if (not ctrlPoints[lastPoint]->isVisible())
|
|
|
|
{
|
|
|
|
if (QLineF(pSpl, ctrlPoint).length() > radius)
|
|
|
|
{
|
|
|
|
if (size == 1)
|
|
|
|
{
|
|
|
|
ctrlPoints[lastPoint]->show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctrlPoints[preLastPoint]->show();
|
|
|
|
ctrlPoints[lastPoint]->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctrlPoint = pSpl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 22:39:47 +01:00
|
|
|
QLineF ctrlLine (pSpl, Visualization::scenePos);
|
|
|
|
ctrlLine.setAngle(ctrlLine.angle()+180);
|
|
|
|
|
2016-02-13 17:24:32 +01:00
|
|
|
if (size == 1)
|
|
|
|
{
|
|
|
|
ctrlPoints[lastPoint]->RefreshCtrlPoint(size, SplinePointPosition::FirstPoint, ctrlPoint, pSpl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctrlPoints[preLastPoint]->RefreshCtrlPoint(size-1, SplinePointPosition::LastPoint, ctrlLine.p2(), pSpl);
|
|
|
|
ctrlPoints[lastPoint]->RefreshCtrlPoint(size, SplinePointPosition::FirstPoint, ctrlPoint, pSpl);
|
|
|
|
}
|
|
|
|
|
2016-02-27 15:49:10 +01:00
|
|
|
VSpline spline(VPointF(pSpl), ctrlPoint, Visualization::scenePos, VPointF(Visualization::scenePos));
|
2016-02-13 17:24:32 +01:00
|
|
|
|
|
|
|
if (size == 1)
|
|
|
|
{
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetAngle2(spline.GetStartAngle(), spline.GetStartAngleFormula());
|
2016-02-13 22:39:47 +01:00
|
|
|
if (ctrlPoint != pSpl)
|
|
|
|
{
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetLength2(spline.GetC1Length(), spline.GetC1LengthFormula());
|
2016-02-13 22:39:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetLength2(0, "0");
|
2016-02-13 22:39:47 +01:00
|
|
|
}
|
2016-02-13 17:24:32 +01:00
|
|
|
emit PathChanged(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-13 22:39:47 +01:00
|
|
|
const VSpline spl = path.GetSpline(size - 1);
|
2016-02-27 15:49:10 +01:00
|
|
|
VSpline preSpl(spl.GetP1(), spl.GetP2(), ctrlLine.p2(), VPointF(pSpl));
|
2016-02-13 22:39:47 +01:00
|
|
|
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetAngle2(spline.GetStartAngle(), spline.GetStartAngleFormula());
|
2016-02-13 22:39:47 +01:00
|
|
|
if (ctrlPoint != pSpl)
|
|
|
|
{
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetLength1(preSpl.GetC2Length(), preSpl.GetC2LengthFormula());
|
|
|
|
path[size-1].SetLength2(spline.GetC1Length(), spline.GetC1LengthFormula());
|
2016-02-13 22:39:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetLength1(0, "0");
|
|
|
|
path[size-1].SetLength2(0, "0");
|
2016-02-13 22:39:47 +01:00
|
|
|
}
|
2016-02-13 17:24:32 +01:00
|
|
|
emit PathChanged(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pointSelected = true;
|
|
|
|
|
2016-02-27 15:49:10 +01:00
|
|
|
VSpline spline(VPointF(pSpl), ctrlPoint, Visualization::scenePos, VPointF(Visualization::scenePos));
|
2016-02-13 17:24:32 +01:00
|
|
|
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetAngle2(spline.GetStartAngle(), spline.GetStartAngleFormula());
|
2016-02-13 22:39:47 +01:00
|
|
|
|
|
|
|
if (ctrlPoint != pSpl)
|
|
|
|
{
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetLength2(spline.GetC1Length(), spline.GetC1LengthFormula());
|
2016-02-13 22:39:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-04 17:08:29 +01:00
|
|
|
path[size-1].SetLength2(0, "0");
|
2016-02-13 22:39:47 +01:00
|
|
|
}
|
2016-02-13 17:24:32 +01:00
|
|
|
emit PathChanged(path);
|
|
|
|
|
|
|
|
DrawPath(newCurveSegment, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap);
|
|
|
|
}
|
|
|
|
}
|