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"
|
|
|
|
#include "../../dialogs/tools/dialogpiecepath.h"
|
|
|
|
#include "../vpatterndb/vpiecepath.h"
|
|
|
|
#include "../vpatterndb/vpiecenode.h"
|
|
|
|
#include "../../undocommands/savepieceoptions.h"
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VToolPiecePath *VToolPiecePath::Create(DialogTool *dialog, VMainGraphicsScene *scene, VAbstractPattern *doc,
|
|
|
|
VContainer *data)
|
|
|
|
{
|
|
|
|
SCASSERT(dialog != nullptr);
|
|
|
|
DialogPiecePath *dialogTool = qobject_cast<DialogPiecePath*>(dialog);
|
|
|
|
SCASSERT(dialogTool != nullptr);
|
|
|
|
const VPiecePath path = dialogTool->GetPiecePath();
|
|
|
|
const quint32 pieceId = dialogTool->GetPieceId();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VAbstractTool::AddRecord(id, Tool::PiecePath, doc);
|
|
|
|
if (parse == Document::FullParse)
|
|
|
|
{
|
|
|
|
//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);
|
|
|
|
|
|
|
|
doc->AddTool(id, pathTool);
|
|
|
|
if (idTool != NULL_ID)
|
|
|
|
{
|
|
|
|
//Some nodes we don't show on scene. Tool that create this nodes must free memory.
|
|
|
|
VDataTool *tool = doc->getTool(idTool);
|
|
|
|
SCASSERT(tool != nullptr);
|
|
|
|
pathTool->setParent(tool);// Adopted by a tool
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return pathTool;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VToolPiecePath::getTagName() const
|
|
|
|
{
|
|
|
|
return VAbstractPattern::TagPath;
|
|
|
|
}
|
|
|
|
|
2016-11-29 13:58:00 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::incrementReferens()
|
|
|
|
{
|
|
|
|
++_referens;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if (_referens > 0)
|
|
|
|
{
|
|
|
|
--_referens;
|
|
|
|
}
|
|
|
|
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-24 20:35:54 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node)
|
|
|
|
{
|
2016-11-28 10:58:44 +01:00
|
|
|
domElement.appendChild(AddSANode(doc, VAbstractPattern::TagNode, node));
|
2016-11-24 20:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiecePath &path)
|
|
|
|
{
|
|
|
|
if (path.CountNodes() > 0)
|
|
|
|
{
|
|
|
|
QDomElement nodesElement = doc->createElement(VAbstractPattern::TagNodes);
|
|
|
|
for (int i = 0; i < path.CountNodes(); ++i)
|
|
|
|
{
|
|
|
|
AddNode(doc, nodesElement, path.at(i));
|
|
|
|
}
|
|
|
|
domElement.appendChild(nodesElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
qApp->getUndoStack()->beginMacro(tr("new path"));
|
|
|
|
|
|
|
|
AddToModeling(domElement);
|
|
|
|
|
|
|
|
VPiece oldDet = VAbstractTool::data.GetPiece(m_pieceId);
|
|
|
|
VPiece newDet = oldDet;
|
|
|
|
|
|
|
|
if (path.GetType() == PiecePathType::InternalPath)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (path.GetType() == PiecePathType::CustomSeamAllowance)
|
|
|
|
{
|
2016-11-25 13:19:44 +01:00
|
|
|
CustomSARecord record;
|
2016-11-29 13:11:36 +01:00
|
|
|
record.path = id;
|
2016-11-24 20:35:54 +01:00
|
|
|
|
2016-11-25 13:19:44 +01:00
|
|
|
QVector<CustomSARecord> records = newDet.GetCustomSARecords();
|
|
|
|
records.append(record);
|
|
|
|
newDet.SetCustomSARecords(records);
|
2016-11-24 20:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SavePieceOptions *saveCommand = new SavePieceOptions(oldDet, newDet, doc, m_pieceId);
|
2016-11-29 13:11:36 +01:00
|
|
|
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
|
2016-11-24 20:35:54 +01:00
|
|
|
connect(saveCommand, &SavePieceOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree);
|
|
|
|
|
|
|
|
qApp->getUndoStack()->endMacro();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
this->setPath(p);
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 13:58:00 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::IncrementNodes(const VPiecePath &path) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < path.CountNodes(); ++i)
|
|
|
|
{
|
|
|
|
const QSharedPointer<VGObject> node = VAbstractTool::data.GetGObject(path.at(i).GetId());
|
|
|
|
doc->IncrementReferens(node->getIdTool());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolPiecePath::DecrementNodes(const VPiecePath &path) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < path.CountNodes(); ++i)
|
|
|
|
{
|
|
|
|
const QSharedPointer<VGObject> node = VAbstractTool::data.GetGObject(path.at(i).GetId());
|
|
|
|
doc->DecrementReferens(node->getIdTool());
|
|
|
|
}
|
|
|
|
}
|