2016-11-24 20:35:54 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 24 11, 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
|
|
|
|
** <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 "vtoolpiecepath.h"
|
2017-05-30 10:56:16 +02:00
|
|
|
#include "../../dialogs/tools/piece/dialogpiecepath.h"
|
2016-11-24 20:35:54 +01:00
|
|
|
#include "../vpatterndb/vpiecepath.h"
|
|
|
|
#include "../vpatterndb/vpiecenode.h"
|
|
|
|
#include "../../undocommands/savepieceoptions.h"
|
2017-01-13 18:07:14 +01:00
|
|
|
#include "../vtoolseamallowance.h"
|
2016-11-24 20:35:54 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-04-15 16:39:59 +02:00
|
|
|
VToolPiecePath *VToolPiecePath::Create(QSharedPointer<DialogTool> dialog, VMainGraphicsScene *scene,
|
|
|
|
VAbstractPattern *doc, VContainer *data)
|
2016-11-24 20:35:54 +01:00
|
|
|
{
|
2017-04-15 16:39:59 +02:00
|
|
|
SCASSERT(not dialog.isNull());
|
|
|
|
QSharedPointer<DialogPiecePath> dialogTool = dialog.objectCast<DialogPiecePath>();
|
|
|
|
SCASSERT(not dialogTool.isNull())
|
2017-01-18 09:17:18 +01:00
|
|
|
VPiecePath path = dialogTool->GetPiecePath();
|
2016-11-24 20:35:54 +01:00
|
|
|
const quint32 pieceId = dialogTool->GetPieceId();
|
2017-01-18 09:17:18 +01:00
|
|
|
qApp->getUndoStack()->beginMacro("add path");
|
|
|
|
path.SetNodes(PrepareNodes(path, scene, doc, data));
|
2016-11-24 20:35:54 +01:00
|
|
|
|
|
|
|
VToolPiecePath *pathTool = Create(0, path, pieceId, scene, doc, data, Document::FullParse, Source::FromGui);
|
|
|
|
return pathTool;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VToolPiecePath *VToolPiecePath::Create(quint32 _id, const VPiecePath &path, quint32 pieceId, VMainGraphicsScene *scene,
|
|
|
|
VAbstractPattern *doc, VContainer *data, const Document &parse,
|
|
|
|
const Source &typeCreation, const QString &drawName, const quint32 &idTool)
|
|
|
|
{
|
|
|
|
quint32 id = _id;
|
|
|
|
if (typeCreation == Source::FromGui)
|
|
|
|
{
|
|
|
|
id = data->AddPiecePath(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->UpdatePiecePath(id, path);
|
|
|
|
if (parse != Document::FullParse)
|
|
|
|
{
|
|
|
|
doc->UpdateToolData(id, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse == Document::FullParse)
|
|
|
|
{
|
2017-02-13 07:55:42 +01:00
|
|
|
VAbstractTool::AddRecord(id, Tool::PiecePath, doc);
|
2016-11-24 20:35:54 +01:00
|
|
|
//TODO Need create garbage collector and remove all nodes, that we don't use.
|
|
|
|
//Better check garbage before each saving file. Check only modeling tags.
|
|
|
|
VToolPiecePath *pathTool = new VToolPiecePath(doc, data, id, pieceId, typeCreation, drawName, idTool, doc);
|
|
|
|
|
2017-03-23 09:25:46 +01:00
|
|
|
VAbstractPattern::AddTool(id, pathTool);
|
2016-11-24 20:35:54 +01:00
|
|
|
if (idTool != NULL_ID)
|
|
|
|
{
|
|
|
|
//Some nodes we don't show on scene. Tool that create this nodes must free memory.
|
2017-03-23 09:25:46 +01:00
|
|
|
VDataTool *tool = VAbstractPattern::getTool(idTool);
|
2016-11-24 20:35:54 +01:00
|
|
|
SCASSERT(tool != nullptr);
|
|
|
|
pathTool->setParent(tool);// Adopted by a tool
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-13 18:07:14 +01:00
|
|
|
if (typeCreation == Source::FromGui && path.GetType() == PiecePathType::InternalPath)
|
|
|
|
{ // Seam allowance tool already initializated and can't init the path
|
|
|
|
SCASSERT(pieceId > NULL_ID);
|
2017-03-23 09:25:46 +01:00
|
|
|
VToolSeamAllowance *saTool = qobject_cast<VToolSeamAllowance*>(VAbstractPattern::getTool(pieceId));
|
2017-01-13 18:07:14 +01:00
|
|
|
SCASSERT(saTool != nullptr);
|
|
|
|
pathTool->setParentItem(saTool);
|
|
|
|
pathTool->SetParentType(ParentType::Item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Try to prevent memory leak
|
|
|
|
scene->addItem(pathTool);// First adopted by scene
|
|
|
|
pathTool->hide();// If no one will use node, it will stay hidden
|
|
|
|
pathTool->SetParentType(ParentType::Scene);
|
|
|
|
}
|
2016-11-24 20:35:54 +01:00
|
|
|
}
|
|
|
|
return pathTool;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VToolPiecePath::getTagName() const
|
|
|
|
{
|
|
|
|
return VAbstractPattern::TagPath;
|
|
|
|
}
|
|
|
|
|
2016-11-29 13:58:00 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::incrementReferens()
|
|
|
|
{
|
2017-01-16 11:06:50 +01:00
|
|
|
VAbstractTool::incrementReferens();
|
2016-11-29 13:58:00 +01:00
|
|
|
if (_referens == 1)
|
|
|
|
{
|
|
|
|
if (idTool != NULL_ID)
|
|
|
|
{
|
|
|
|
doc->IncrementReferens(idTool);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IncrementNodes(VAbstractTool::data.GetPiecePath(id));
|
|
|
|
}
|
|
|
|
ShowNode();
|
|
|
|
QDomElement domElement = doc->elementById(id);
|
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
|
|
|
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::InUse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::decrementReferens()
|
|
|
|
{
|
2017-01-16 11:06:50 +01:00
|
|
|
VAbstractTool::decrementReferens();
|
2016-11-29 13:58:00 +01:00
|
|
|
if (_referens == 0)
|
|
|
|
{
|
|
|
|
if (idTool != NULL_ID)
|
|
|
|
{
|
|
|
|
doc->DecrementReferens(idTool);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DecrementNodes(VAbstractTool::data.GetPiecePath(id));
|
|
|
|
}
|
|
|
|
HideNode();
|
|
|
|
QDomElement domElement = doc->elementById(id);
|
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
|
|
|
doc->SetParametrUsage(domElement, AttrInUse, NodeUsage::NotInUse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 09:47:36 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::AddAttributes(VAbstractPattern *doc, QDomElement &domElement, quint32 id, const VPiecePath &path)
|
|
|
|
{
|
|
|
|
doc->SetAttribute(domElement, VDomDocument::AttrId, id);
|
|
|
|
doc->SetAttribute(domElement, AttrName, path.GetName());
|
|
|
|
doc->SetAttribute(domElement, AttrType, static_cast<int>(path.GetType()));
|
2016-12-03 17:01:39 +01:00
|
|
|
doc->SetAttribute(domElement, AttrTypeLine, PenStyleToLineStyle(path.GetPenType()));
|
2016-11-28 09:47:36 +01:00
|
|
|
}
|
|
|
|
|
2016-11-24 20:35:54 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::FullUpdateFromFile()
|
|
|
|
{
|
|
|
|
RefreshGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::AllowHover(bool enabled)
|
|
|
|
{
|
|
|
|
Q_UNUSED(enabled)
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::AllowSelecting(bool enabled)
|
|
|
|
{
|
|
|
|
Q_UNUSED(enabled)
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::AddToFile()
|
|
|
|
{
|
|
|
|
QDomElement domElement = doc->createElement(getTagName());
|
|
|
|
const VPiecePath path = VAbstractTool::data.GetPiecePath(id);
|
2016-11-28 09:47:36 +01:00
|
|
|
|
|
|
|
AddAttributes(doc, domElement, id, path);
|
2016-11-24 20:35:54 +01:00
|
|
|
|
|
|
|
if (idTool != NULL_ID)
|
|
|
|
{
|
|
|
|
doc->SetAttribute(domElement, AttrIdTool, idTool);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddNodes(doc, domElement, path);
|
|
|
|
|
|
|
|
AddToModeling(domElement);
|
|
|
|
|
2017-01-16 16:37:15 +01:00
|
|
|
if (m_pieceId > NULL_ID)
|
2016-11-24 20:35:54 +01:00
|
|
|
{
|
2017-01-16 16:37:15 +01:00
|
|
|
const VPiece oldDet = VAbstractTool::data.GetPiece(m_pieceId);
|
|
|
|
VPiece newDet = oldDet;
|
2016-11-24 20:35:54 +01:00
|
|
|
|
2017-01-16 16:37:15 +01:00
|
|
|
if (path.GetType() == PiecePathType::InternalPath)
|
|
|
|
{
|
2017-01-31 15:26:28 +01:00
|
|
|
newDet.GetInternalPaths().append(id);
|
2017-01-16 16:37:15 +01:00
|
|
|
}
|
|
|
|
else if (path.GetType() == PiecePathType::CustomSeamAllowance)
|
|
|
|
{
|
|
|
|
CustomSARecord record;
|
|
|
|
record.path = id;
|
2016-11-24 20:35:54 +01:00
|
|
|
|
2017-01-31 15:26:28 +01:00
|
|
|
newDet.GetCustomSARecords().append(record);
|
2017-01-16 16:37:15 +01:00
|
|
|
}
|
2016-11-24 20:35:54 +01:00
|
|
|
|
2017-01-16 16:37:15 +01:00
|
|
|
SavePieceOptions *saveCommand = new SavePieceOptions(oldDet, newDet, doc, m_pieceId);
|
|
|
|
qApp->getUndoStack()->push(saveCommand);// First push then make a connect
|
|
|
|
VAbstractTool::data.UpdatePiece(m_pieceId, newDet);// Update piece because first save will not call lite update
|
|
|
|
connect(saveCommand, &SavePieceOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree);
|
|
|
|
}
|
2016-11-24 20:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::RefreshDataInFile()
|
|
|
|
{
|
|
|
|
QDomElement domElement = doc->elementById(id);
|
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
|
|
|
if (idTool != NULL_ID)
|
|
|
|
{
|
|
|
|
doc->SetAttribute(domElement, AttrIdTool, idTool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::ShowNode()
|
|
|
|
{
|
|
|
|
if (parentType != ParentType::Scene)
|
|
|
|
{
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::HideNode()
|
|
|
|
{
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
2017-01-18 09:17:18 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::ToolCreation(const Source &typeCreation)
|
|
|
|
{
|
|
|
|
if (typeCreation == Source::FromGui || typeCreation == Source::FromTool)
|
|
|
|
{
|
|
|
|
AddToFile();
|
|
|
|
if (typeCreation != Source::FromTool)
|
|
|
|
{
|
|
|
|
qApp->getUndoStack()->endMacro();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RefreshDataInFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 20:35:54 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VToolPiecePath::VToolPiecePath(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 pieceId,
|
|
|
|
const Source &typeCreation, const QString &drawName, const quint32 &idTool,
|
|
|
|
QObject *qoParent, QGraphicsItem *parent)
|
|
|
|
:VAbstractNode(doc, data, id, 0, drawName, idTool, qoParent),
|
|
|
|
QGraphicsPathItem(parent),
|
|
|
|
m_pieceId(pieceId)
|
|
|
|
{
|
2017-01-18 09:17:18 +01:00
|
|
|
IncrementNodes(VAbstractTool::data.GetPiecePath(id));
|
2016-11-24 20:35:54 +01:00
|
|
|
RefreshGeometry();
|
|
|
|
ToolCreation(typeCreation);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::RefreshGeometry()
|
|
|
|
{
|
|
|
|
const VPiecePath path = VAbstractTool::data.GetPiecePath(id);
|
|
|
|
if (path.GetType() == PiecePathType::InternalPath)
|
|
|
|
{
|
|
|
|
QPainterPath p = path.PainterPath(this->getData());
|
|
|
|
p.setFillRule(Qt::OddEvenFill);
|
2016-12-03 17:01:39 +01:00
|
|
|
|
2016-11-24 20:35:54 +01:00
|
|
|
this->setPath(p);
|
2016-12-03 17:01:39 +01:00
|
|
|
QPen pen = this->pen();
|
|
|
|
pen.setStyle(path.GetPenType());
|
|
|
|
this->setPen(pen);
|
2016-11-24 20:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 13:58:00 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::IncrementNodes(const VPiecePath &path) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < path.CountNodes(); ++i)
|
|
|
|
{
|
2017-01-18 09:17:18 +01:00
|
|
|
doc->IncrementReferens(VAbstractTool::data.GetGObject(path.at(i).GetId())->getIdTool());
|
2016-11-29 13:58:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::DecrementNodes(const VPiecePath &path) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < path.CountNodes(); ++i)
|
|
|
|
{
|
2017-01-18 09:17:18 +01:00
|
|
|
doc->DecrementReferens(VAbstractTool::data.GetGObject(path.at(i).GetId())->getIdTool());
|
2016-11-29 13:58:00 +01:00
|
|
|
}
|
|
|
|
}
|