valentina/src/app/tools/drawTools/vtoolsplinepath.cpp

332 lines
14 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vtoolsplinepath.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date November 15, 2013
**
** @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) 2013 Valentina project
** <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/>.
**
*************************************************************************/
2013-08-09 08:49:34 +02:00
#include "vtoolsplinepath.h"
#include "../../dialogs/tools/dialogsplinepath.h"
2013-08-09 08:49:34 +02:00
const QString VToolSplinePath::ToolType = QStringLiteral("path");
//---------------------------------------------------------------------------------------------------------------------
2014-03-11 12:43:24 +01:00
VToolSplinePath::VToolSplinePath(VPattern *doc, VContainer *data, quint32 id, const Valentina::Sources &typeCreation,
2014-03-04 17:50:54 +01:00
QGraphicsItem *parent) :VAbstractSpline(doc, data, id, parent)
{
const VSplinePath *splPath = data->GeometricObject<const VSplinePath *>(id);
2013-08-09 08:49:34 +02:00
QPainterPath path;
path.addPath(splPath->GetPath());
2013-08-09 08:49:34 +02:00
path.setFillRule( Qt::WindingFill );
this->setPath(path);
this->setPen(QPen(Qt::black, qApp->toPixel(qApp->widthHairLine())/factor));
2013-08-09 08:49:34 +02:00
this->setFlag(QGraphicsItem::ItemIsSelectable, true);
this->setFlag(QGraphicsItem::ItemIsFocusable, true);
2013-08-09 08:49:34 +02:00
this->setAcceptHoverEvents(true);
for (qint32 i = 1; i<=splPath->Count(); ++i)
{
VSpline spl = splPath->GetSpline(i);
2013-08-09 08:49:34 +02:00
VControlPointSpline *controlPoint = new VControlPointSpline(i, SplinePoint::FirstPoint, spl.GetP2(),
spl.GetP1().toQPointF(), this);
2013-08-09 08:49:34 +02:00
connect(controlPoint, &VControlPointSpline::ControlPointChangePosition, this,
&VToolSplinePath::ControlPointChangePosition);
connect(this, &VToolSplinePath::RefreshLine, controlPoint, &VControlPointSpline::RefreshLine);
connect(this, &VToolSplinePath::setEnabledPoint, controlPoint, &VControlPointSpline::setEnabledPoint);
controlPoints.append(controlPoint);
controlPoint = new VControlPointSpline(i, SplinePoint::LastPoint, spl.GetP3(), spl.GetP4().toQPointF(), this);
2013-08-09 08:49:34 +02:00
connect(controlPoint, &VControlPointSpline::ControlPointChangePosition, this,
&VToolSplinePath::ControlPointChangePosition);
connect(this, &VToolSplinePath::RefreshLine, controlPoint, &VControlPointSpline::RefreshLine);
connect(this, &VToolSplinePath::setEnabledPoint, controlPoint, &VControlPointSpline::setEnabledPoint);
controlPoints.append(controlPoint);
}
2014-03-11 12:43:24 +01:00
if (typeCreation == Valentina::FromGui)
{
2013-08-09 08:49:34 +02:00
AddToFile();
}
else
{
RefreshDataInFile();
}
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::setDialog()
{
Q_CHECK_PTR(dialog);
DialogSplinePath *dialogTool = qobject_cast<DialogSplinePath*>(dialog);
Q_CHECK_PTR(dialogTool);
const VSplinePath *splPath = VAbstractTool::data.GeometricObject<const VSplinePath *>(id);
dialogTool->SetPath(*splPath);
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::Create(DialogTool *dialog, VMainGraphicsScene *scene, VPattern *doc, VContainer *data)
{
Q_CHECK_PTR(dialog);
DialogSplinePath *dialogTool = qobject_cast<DialogSplinePath*>(dialog);
Q_CHECK_PTR(dialogTool);
VSplinePath *path = new VSplinePath(dialogTool->GetPath());
for (qint32 i = 0; i < path->CountPoint(); ++i)
{
doc->IncrementReferens((*path)[i].P().id());
}
2014-03-11 12:43:24 +01:00
Create(0, path, scene, doc, data, Document::FullParse, Valentina::FromGui);
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::Create(const quint32 _id, VSplinePath *path, VMainGraphicsScene *scene,
VPattern *doc, VContainer *data, const Document::Documents &parse,
2014-03-11 12:43:24 +01:00
const Valentina::Sources &typeCreation)
{
quint32 id = _id;
2014-03-11 12:43:24 +01:00
if (typeCreation == Valentina::FromGui)
{
id = data->AddGObject(path);
data->AddLengthSpline(path->name(), qApp->fromPixel(path->GetLength()));
}
else
{
data->UpdateGObject(id, path);
data->AddLengthSpline(path->name(), qApp->fromPixel(path->GetLength()));
if (parse != Document::FullParse)
{
doc->UpdateToolData(id, data);
}
}
2014-03-11 12:43:24 +01:00
VDrawTool::AddRecord(id, Valentina::SplinePathTool, doc);
if (parse == Document::FullParse)
{
VToolSplinePath *spl = new VToolSplinePath(doc, data, id, typeCreation);
scene->addItem(spl);
connect(spl, &VToolSplinePath::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem);
connect(scene, &VMainGraphicsScene::NewFactor, spl, &VToolSplinePath::SetFactor);
doc->AddTool(id, spl);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::ControlPointChangePosition(const qint32 &indexSpline, const SplinePoint::Position &position,
const QPointF &pos)
{
VSplinePath splPath = *VAbstractTool::data.GeometricObject<const VSplinePath *>(id);
VSpline spl = splPath.GetSpline(indexSpline);
if (position == SplinePoint::FirstPoint)
{
spl = VSpline(spl.GetP1(), pos, spl.GetP3(), spl.GetP4(), spl.GetKcurve());
}
else
{
spl = VSpline(spl.GetP1(), spl.GetP2(), pos, spl.GetP4(), spl.GetKcurve());
2013-08-09 08:49:34 +02:00
}
2013-08-21 10:03:53 +02:00
CorectControlPoints(spl, splPath, indexSpline);
2013-08-09 08:49:34 +02:00
QDomElement domElement = doc->elementById(QString().setNum(id));
if (domElement.isElement())
{
doc->SetAttribute(domElement, AttrKCurve, QString().setNum(splPath.getKCurve()));
2013-08-09 08:49:34 +02:00
UpdatePathPoint(domElement, splPath);
emit FullUpdateTree();
emit toolhaveChange();
2013-08-09 08:49:34 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::CorectControlPoints(const VSpline &spl, VSplinePath &splPath, const qint32 &indexSpline)
{
2013-08-09 08:49:34 +02:00
VSplinePoint p = splPath.GetSplinePoint(indexSpline, SplinePoint::FirstPoint);
p.SetAngle2(spl.GetAngle1());
2013-08-09 08:49:34 +02:00
p.SetKAsm2(spl.GetKasm1());
splPath.UpdatePoint(indexSpline, SplinePoint::FirstPoint, p);
p = splPath.GetSplinePoint(indexSpline, SplinePoint::LastPoint);
p.SetAngle2(spl.GetAngle2()-180);
2013-08-09 08:49:34 +02:00
p.SetKAsm1(spl.GetKasm2());
splPath.UpdatePoint(indexSpline, SplinePoint::LastPoint, p);
}
//---------------------------------------------------------------------------------------------------------------------
2014-03-04 13:55:57 +01:00
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)
{
2013-08-09 08:49:34 +02:00
QDomNodeList nodeList = node.childNodes();
qint32 num = nodeList.size();
for (qint32 i = 0; i < num; ++i)
{
2013-08-09 08:49:34 +02:00
QDomElement domElement = nodeList.at(i).toElement();
if (domElement.isNull() == false)
{
2013-08-09 08:49:34 +02:00
VSplinePoint p = path[i];
doc->SetAttribute(domElement, AttrPSpline, p.P().id());
doc->SetAttribute(domElement, AttrKAsm1, p.KAsm1());
doc->SetAttribute(domElement, AttrKAsm2, p.KAsm2());
doc->SetAttribute(domElement, AttrAngle, p.Angle2());
2013-08-09 08:49:34 +02:00
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
ContextMenu<DialogSplinePath>(this, event);
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::AddToFile()
{
VSplinePath splPath = *VAbstractTool::data.GeometricObject<const VSplinePath *>(id);
QDomElement domElement = doc->createElement(TagName);
2013-08-09 08:49:34 +02:00
doc->SetAttribute(domElement, VDomDocument::AttrId, id);
doc->SetAttribute(domElement, AttrType, ToolType);
doc->SetAttribute(domElement, AttrKCurve, splPath.getKCurve());
2013-08-09 08:49:34 +02:00
for (qint32 i = 0; i < splPath.CountPoint(); ++i)
{
2013-08-09 08:49:34 +02:00
AddPathPoint(domElement, splPath[i]);
}
AddToCalculation(domElement);
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::RefreshDataInFile()
{
2014-03-04 13:55:57 +01:00
QDomElement domElement = doc->elementById(QString().setNum(id));
if (domElement.isElement() == false)
{
qDebug()<<"Can't find element with id="<<id<<"in pattern file";
2014-03-04 13:55:57 +01:00
return;
}
2014-03-04 13:55:57 +01:00
VSplinePath splPath = *VAbstractTool::data.GeometricObject<const VSplinePath *>(id);
RefreshSplinePath(splPath);
doc->SetAttribute(domElement, AttrKCurve, QString().setNum(splPath.getKCurve()));
UpdatePathPoint(domElement, splPath);
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::AddPathPoint(QDomElement &domElement, const VSplinePoint &splPoint)
{
QDomElement pathPoint = doc->createElement(AttrPathPoint);
2013-08-09 08:49:34 +02:00
doc->SetAttribute(pathPoint, AttrPSpline, splPoint.P().id());
doc->SetAttribute(pathPoint, AttrKAsm1, splPoint.KAsm1());
doc->SetAttribute(pathPoint, AttrKAsm2, splPoint.KAsm2());
doc->SetAttribute(pathPoint, AttrAngle, splPoint.Angle2());
2013-08-09 08:49:34 +02:00
domElement.appendChild(pathPoint);
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
2014-03-11 12:43:24 +01:00
emit ChoosedTool(id, Valentina::SplinePath);
2013-08-09 08:49:34 +02:00
}
QGraphicsItem::mouseReleaseEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::RemoveReferens()
{
VSplinePath splPath = *VAbstractTool::data.GeometricObject<const VSplinePath *>(id);
for (qint32 i = 0; i < splPath.Count(); ++i)
{
doc->DecrementReferens(splPath[i].P().id());
}
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::SaveDialog(QDomElement &domElement)
{
Q_CHECK_PTR(dialog);
DialogSplinePath *dialogTool = qobject_cast<DialogSplinePath*>(dialog);
Q_CHECK_PTR(dialogTool);
VSplinePath splPath = dialogTool->GetPath();
2014-03-04 13:55:57 +01:00
RefreshSplinePath(splPath);
doc->SetAttribute(domElement, AttrKCurve, QString().setNum(splPath.getKCurve()));
UpdatePathPoint(domElement, splPath);
}
//---------------------------------------------------------------------------------------------------------------------
void VToolSplinePath::RefreshGeometry()
{
this->setPen(QPen(currentColor, qApp->toPixel(qApp->widthHairLine())/factor));
const VSplinePath *splPath = VAbstractTool::data.GeometricObject<const VSplinePath *>(id);
2013-08-09 08:49:34 +02:00
QPainterPath path;
path.addPath(splPath->GetPath());
2013-08-09 08:49:34 +02:00
path.setFillRule( Qt::WindingFill );
this->setPath(path);
for (qint32 i = 1; i<=splPath->Count(); ++i)
{
VSpline spl = splPath->GetSpline(i);
QPointF splinePoint = spl.GetP1().toQPointF();
2013-08-09 08:49:34 +02:00
QPointF controlPoint = spl.GetP2();
emit RefreshLine(i, SplinePoint::FirstPoint, controlPoint, splinePoint);
splinePoint = spl.GetP4().toQPointF();
2013-08-09 08:49:34 +02:00
controlPoint = spl.GetP3();
emit RefreshLine(i, SplinePoint::LastPoint, controlPoint, splinePoint);
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);
2013-08-09 08:49:34 +02:00
}
}