/************************************************************************ ** ** @file vtoolcubicbezierpath.cpp ** @author Roman Telezhynskyi ** @date 18 3, 2016 ** ** @brief ** @copyright ** This source code is part of the Valentina project, a pattern making ** program, whose allow create and modeling patterns of clothing. ** Copyright (C) 2016 Valentina project ** 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 . ** *************************************************************************/ #include "vtoolcubicbezierpath.h" #include #include #include #include #include #include #include #include #include "../../../dialogs/tools/dialogtool.h" #include "../../../dialogs/tools/dialogcubicbezierpath.h" #include "../../../visualization/visualization.h" #include "../../../visualization/path/vistoolcubicbezierpath.h" #include "../ifc/exception/vexception.h" #include "../ifc/xml/vdomdocument.h" #include "../ifc/ifcdef.h" #include "../vgeometry/vabstractcubicbezierpath.h" #include "../vgeometry/vabstractcurve.h" #include "../vgeometry/vcubicbezierpath.h" #include "../vgeometry/vgobject.h" #include "../vgeometry/vpointf.h" #include "../vmisc/vabstractapplication.h" #include "../vpatterndb/vcontainer.h" #include "../vwidgets/vmaingraphicsscene.h" #include "../../vabstracttool.h" #include "../vdrawtool.h" #include "vabstractspline.h" const QString VToolCubicBezierPath::ToolType = QStringLiteral("cubicBezierPath"); //--------------------------------------------------------------------------------------------------------------------- VToolCubicBezierPath::VToolCubicBezierPath(const VToolCubicBezierPathInitData &initData, QGraphicsItem *parent) : VAbstractSpline(initData.doc, initData.data, initData.id, parent) { sceneType = SceneObject::SplinePath; this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus ToolCreation(initData.typeCreation); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::setDialog() { SCASSERT(not m_dialog.isNull()) auto dialogTool = qobject_cast(m_dialog); SCASSERT(dialogTool != nullptr) const QSharedPointer splPath = VAbstractTool::data.GeometricObject(m_id); dialogTool->SetPath(*splPath); } //--------------------------------------------------------------------------------------------------------------------- VToolCubicBezierPath *VToolCubicBezierPath::Create(const QPointer &dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data) { SCASSERT(not dialog.isNull()) const QPointer dialogTool = qobject_cast(dialog); SCASSERT(not dialogTool.isNull()) VToolCubicBezierPathInitData initData; initData.scene = scene; initData.doc = doc; initData.data = data; initData.parse = Document::FullParse; initData.typeCreation = Source::FromGui; initData.path = new VCubicBezierPath(dialogTool->GetPath()); for (qint32 i = 0; i < initData.path->CountPoints(); ++i) { doc->IncrementReferens((*initData.path)[i].getIdTool()); } VToolCubicBezierPath* spl = Create(initData); if (spl != nullptr) { spl->m_dialog = dialog; } return spl; } //--------------------------------------------------------------------------------------------------------------------- VToolCubicBezierPath *VToolCubicBezierPath::Create(VToolCubicBezierPathInitData initData) { if (initData.typeCreation == Source::FromGui) { initData.id = initData.data->AddGObject(initData.path); initData.data->AddCurveWithSegments(initData.data->GeometricObject(initData.id), initData.id); } else { initData.data->UpdateGObject(initData.id, initData.path); initData.data->AddCurveWithSegments(initData.data->GeometricObject(initData.id), initData.id); if (initData.parse != Document::FullParse) { initData.doc->UpdateToolData(initData.id, initData.data); } } if (initData.parse == Document::FullParse) { VAbstractTool::AddRecord(initData.id, Tool::CubicBezierPath, initData.doc); VToolCubicBezierPath *spl = new VToolCubicBezierPath(initData); initData.scene->addItem(spl); InitSplinePathToolConnections(initData.scene, spl); VAbstractPattern::AddTool(initData.id, spl); return spl; } return nullptr; } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::UpdatePathPoints(VAbstractPattern *doc, QDomElement &element, const VCubicBezierPath &path) { VDomDocument::RemoveAllChildren(element); for (qint32 i = 0; i < path.CountPoints(); ++i) { AddPathPoint(doc, element, path.at(i)); } } //--------------------------------------------------------------------------------------------------------------------- VCubicBezierPath VToolCubicBezierPath::getSplinePath() const { QSharedPointer splPath = VAbstractTool::data.GeometricObject(m_id); return *splPath.data(); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::setSplinePath(const VCubicBezierPath &splPath) { QSharedPointer obj = VAbstractTool::data.GetGObject(m_id); QSharedPointer splinePath = qSharedPointerDynamicCast(obj); *splinePath.data() = splPath; SaveOption(obj); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::ShowVisualization(bool show) { ShowToolVisualization(show); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::ShowContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id) { Q_UNUSED(id) try { ContextMenu(event); } catch(const VExceptionToolWasDeleted &e) { Q_UNUSED(e) return;//Leave this method immediately!!! } } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::RemoveReferens() { const QSharedPointer splPath = VAbstractTool::data.GeometricObject(m_id); for (qint32 i = 0; i < splPath->CountPoints(); ++i) { doc->DecrementReferens(splPath->at(i).getIdTool()); } } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::SaveDialog(QDomElement &domElement, QList &oldDependencies, QList &newDependencies) { SCASSERT(not m_dialog.isNull()) const auto dialogTool = qobject_cast(m_dialog); SCASSERT(dialogTool != nullptr) const auto oldSplPath = VAbstractTool::data.GeometricObject(m_id); for (qint32 i = 0; i < oldSplPath->CountPoints(); ++i) { AddDependence(oldDependencies, oldSplPath->at(i).id()); } const VCubicBezierPath splPath = dialogTool->GetPath(); for (qint32 i = 0; i < splPath.CountPoints(); ++i) { AddDependence(newDependencies, splPath.at(i).id()); } SetSplinePathAttributes(domElement, splPath); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::SaveOptions(QDomElement &tag, QSharedPointer &obj) { VAbstractSpline::SaveOptions(tag, obj); QSharedPointer splPath = qSharedPointerDynamicCast(obj); SCASSERT(splPath.isNull() == false) SetSplinePathAttributes(tag, *splPath); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::SetVisualization() { if (not vis.isNull()) { auto visual = qobject_cast(vis); SCASSERT(visual != nullptr) QSharedPointer splPath = VAbstractTool::data.GeometricObject(m_id); visual->setPath(*splPath.data()); visual->setLineStyle(LineStyleToPenStyle(splPath->GetPenStyle())); visual->SetMode(Mode::Show); visual->RefreshGeometry(); } } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::RefreshGeometry() { QSharedPointer splPath = VAbstractTool::data.GeometricObject(m_id); setPath(splPath->GetPath()); SetVisualization(); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::AddPathPoint(VAbstractPattern *doc, QDomElement &domElement, const VPointF &splPoint) { SCASSERT(doc != nullptr) QDomElement pathPoint = doc->createElement(AttrPathPoint); doc->SetAttribute(pathPoint, AttrPSpline, splPoint.id()); domElement.appendChild(pathPoint); } //--------------------------------------------------------------------------------------------------------------------- void VToolCubicBezierPath::SetSplinePathAttributes(QDomElement &domElement, const VCubicBezierPath &path) { doc->SetAttribute(domElement, AttrType, ToolType); doc->SetAttributeOrRemoveIf(domElement, AttrDuplicate, path.GetDuplicate(), path.GetDuplicate() <= 0); doc->SetAttribute(domElement, AttrColor, path.GetColor()); doc->SetAttribute(domElement, AttrPenStyle, path.GetPenStyle()); doc->SetAttribute(domElement, AttrAScale, path.GetApproximationScale()); UpdatePathPoints(doc, domElement, path); }