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);
|
2014-09-07 18:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VisToolSplinePath::RefreshGeometry()
|
|
|
|
{
|
|
|
|
if (path.CountPoint() > 0)
|
|
|
|
{
|
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));
|
|
|
|
DrawPoint(point, pathPoints.at(i).P().toQPointF(), supportColor);
|
|
|
|
}
|
2014-09-07 18:51:22 +02:00
|
|
|
|
2016-02-13 17:24:32 +01:00
|
|
|
if (mode == Mode::Creation)
|
|
|
|
{
|
|
|
|
Creating(pathPoints.at(size-1).P().toQPointF(), size);
|
|
|
|
}
|
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
|
|
|
|
2014-11-21 18:24:16 +01:00
|
|
|
if (path.CountPoint() < 3)
|
|
|
|
{
|
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
|
|
|
}
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QSharedPointer<VSpline> VisToolSplinePath::NewCurveSegmen(const QPointF &pSpl, int size)
|
|
|
|
{
|
|
|
|
auto spline = QSharedPointer<VSpline>(new VSpline(VPointF(pSpl), ctrlPoint, Visualization::scenePos,
|
|
|
|
VPointF(Visualization::scenePos), path.GetKCurve()));
|
|
|
|
|
|
|
|
if (size == 1)
|
|
|
|
{
|
|
|
|
path[size-1].SetAngle2(spline->GetStartAngle());
|
|
|
|
path[size-1].SetKAsm2(spline->GetKasm1());
|
|
|
|
emit PathChanged(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path[size-1].SetAngle2(spline->GetStartAngle());
|
|
|
|
path[size-1].SetKAsm1(spline->GetKasm1());
|
|
|
|
path[size-1].SetKAsm2(spline->GetKasm1());
|
|
|
|
emit PathChanged(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return spline;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 1)
|
|
|
|
{
|
|
|
|
ctrlPoints[lastPoint]->RefreshCtrlPoint(size, SplinePointPosition::FirstPoint, ctrlPoint, pSpl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QLineF ctrlLine (pSpl, Visualization::scenePos);
|
|
|
|
ctrlLine.setAngle(ctrlLine.angle()+180);
|
|
|
|
|
|
|
|
ctrlPoints[preLastPoint]->RefreshCtrlPoint(size-1, SplinePointPosition::LastPoint, ctrlLine.p2(), pSpl);
|
|
|
|
ctrlPoints[lastPoint]->RefreshCtrlPoint(size, SplinePointPosition::FirstPoint, ctrlPoint, pSpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
VSpline spline(VPointF(pSpl), ctrlPoint, Visualization::scenePos,
|
|
|
|
VPointF(Visualization::scenePos), path.GetKCurve());
|
|
|
|
|
|
|
|
if (size == 1)
|
|
|
|
{
|
|
|
|
path[size-1].SetAngle2(spline.GetStartAngle());
|
|
|
|
path[size-1].SetKAsm2(spline.GetKasm1());
|
|
|
|
emit PathChanged(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path[size-1].SetAngle2(spline.GetStartAngle());
|
|
|
|
path[size-1].SetKAsm1(spline.GetKasm1());
|
|
|
|
path[size-1].SetKAsm2(spline.GetKasm1());
|
|
|
|
emit PathChanged(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pointSelected = true;
|
|
|
|
|
|
|
|
VSpline spline(VPointF(pSpl), ctrlPoint, Visualization::scenePos,
|
|
|
|
VPointF(Visualization::scenePos), path.GetKCurve());
|
|
|
|
|
|
|
|
path[size-1].SetAngle2(spline.GetStartAngle());
|
|
|
|
path[size-1].SetKAsm2(spline.GetKasm1());
|
|
|
|
emit PathChanged(path);
|
|
|
|
|
|
|
|
DrawPath(newCurveSegment, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap);
|
|
|
|
}
|
|
|
|
}
|