2016-11-08 11:57:01 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 6 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 "vtoolseamallowance.h"
|
|
|
|
#include "../dialogs/tools/dialogseamallowance.h"
|
|
|
|
#include "../vpatterndb/vpiecenode.h"
|
|
|
|
#include "nodeDetails/vnodearc.h"
|
|
|
|
#include "nodeDetails/vnodepoint.h"
|
|
|
|
#include "nodeDetails/vnodespline.h"
|
|
|
|
#include "nodeDetails/vnodesplinepath.h"
|
|
|
|
#include "../vgeometry/varc.h"
|
|
|
|
#include "../vgeometry/vcubicbezier.h"
|
|
|
|
#include "../vgeometry/vcubicbezierpath.h"
|
|
|
|
#include "../vgeometry/vpointf.h"
|
|
|
|
#include "../vgeometry/vspline.h"
|
|
|
|
#include "../vgeometry/vsplinepath.h"
|
|
|
|
#include "../ifc/xml/vpatternconverter.h"
|
|
|
|
#include "../undocommands/addpiece.h"
|
2016-11-09 10:57:39 +01:00
|
|
|
#include "../undocommands/deletepiece.h"
|
2016-11-09 12:06:05 +01:00
|
|
|
#include "../undocommands/movepiece.h"
|
2016-11-09 13:11:58 +01:00
|
|
|
#include "../undocommands/savepieceoptions.h"
|
2016-11-09 14:53:22 +01:00
|
|
|
#include "../undocommands/togglepieceinlayout.h"
|
2016-11-09 12:06:05 +01:00
|
|
|
#include "../vwidgets/vmaingraphicsview.h"
|
2016-11-08 11:57:01 +01:00
|
|
|
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2016-11-09 12:06:05 +01:00
|
|
|
#include <QGraphicsView>
|
2016-11-09 10:57:39 +01:00
|
|
|
#include <QKeyEvent>
|
2016-11-08 11:57:01 +01:00
|
|
|
#include <QMenu>
|
2016-11-09 10:57:39 +01:00
|
|
|
#include <QMessageBox>
|
2016-11-08 11:57:01 +01:00
|
|
|
|
|
|
|
// Current version of seam allowance tag nned for backward compatibility
|
|
|
|
const quint8 VToolSeamAllowance::pieceVersion = 2;
|
|
|
|
|
|
|
|
const QString VToolSeamAllowance::TagNodes = QStringLiteral("nodes");
|
|
|
|
const QString VToolSeamAllowance::TagNode = QStringLiteral("node");
|
|
|
|
|
2016-11-10 10:56:40 +01:00
|
|
|
const QString VToolSeamAllowance::AttrVersion = QStringLiteral("version");
|
|
|
|
const QString VToolSeamAllowance::AttrNodeReverse = QStringLiteral("reverse");
|
|
|
|
const QString VToolSeamAllowance::AttrForbidFlipping = QStringLiteral("forbidFlipping");
|
2016-11-10 11:53:02 +01:00
|
|
|
const QString VToolSeamAllowance::AttrSeamAllowance = QStringLiteral("seamAllowance");
|
|
|
|
const QString VToolSeamAllowance::AttrWidth = QStringLiteral("width");
|
2016-11-12 12:18:16 +01:00
|
|
|
const QString VToolSeamAllowance::AttrSABefore = QStringLiteral("before");
|
|
|
|
const QString VToolSeamAllowance::AttrSAAfter = QStringLiteral("after");
|
2016-11-08 11:57:01 +01:00
|
|
|
|
|
|
|
const QString VToolSeamAllowance::NodeArc = QStringLiteral("NodeArc");
|
|
|
|
const QString VToolSeamAllowance::NodePoint = QStringLiteral("NodePoint");
|
|
|
|
const QString VToolSeamAllowance::NodeSpline = QStringLiteral("NodeSpline");
|
|
|
|
const QString VToolSeamAllowance::NodeSplinePath = QStringLiteral("NodeSplinePath");
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VToolSeamAllowance::~VToolSeamAllowance()
|
|
|
|
{
|
|
|
|
delete m_dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VToolSeamAllowance *VToolSeamAllowance::Create(DialogTool *dialog, VMainGraphicsScene *scene, VAbstractPattern *doc,
|
|
|
|
VContainer *data)
|
|
|
|
{
|
|
|
|
SCASSERT(dialog != nullptr);
|
|
|
|
DialogSeamAllowance *dialogTool = qobject_cast<DialogSeamAllowance*>(dialog);
|
|
|
|
SCASSERT(dialogTool != nullptr);
|
|
|
|
VPiece detail = dialogTool->GetPiece();
|
|
|
|
QVector<VPieceNode> nodes;
|
|
|
|
qApp->getUndoStack()->beginMacro("add detail");
|
|
|
|
for (int i = 0; i< detail.CountNodes(); ++i)
|
|
|
|
{
|
|
|
|
quint32 id = 0;
|
|
|
|
VPieceNode nodeD = detail.at(i);
|
|
|
|
switch (nodeD.GetTypeTool())
|
|
|
|
{
|
|
|
|
case (Tool::NodePoint):
|
|
|
|
{
|
|
|
|
id = CreateNode<VPointF>(data, nodeD.GetId());
|
|
|
|
VNodePoint::Create(doc, data, scene, id, nodeD.GetId(), Document::FullParse, Source::FromGui);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (Tool::NodeArc):
|
|
|
|
{
|
|
|
|
id = CreateNode<VArc>(data, nodeD.GetId());
|
|
|
|
VNodeArc::Create(doc, data, id, nodeD.GetId(), Document::FullParse, Source::FromGui);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (Tool::NodeSpline):
|
|
|
|
{
|
|
|
|
const auto obj = data->GetGObject(nodeD.GetId());
|
|
|
|
if (obj->getType() == GOType::Spline)
|
|
|
|
{
|
|
|
|
id = CreateNode<VSpline>(data, nodeD.GetId());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id = CreateNode<VCubicBezier>(data, nodeD.GetId());
|
|
|
|
}
|
|
|
|
VNodeSpline::Create(doc, data, id, nodeD.GetId(), Document::FullParse, Source::FromGui);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case (Tool::NodeSplinePath):
|
|
|
|
{
|
|
|
|
const auto obj = data->GetGObject(nodeD.GetId());
|
|
|
|
if (obj->getType() == GOType::SplinePath)
|
|
|
|
{
|
|
|
|
id = CreateNode<VSplinePath>(data, nodeD.GetId());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id = CreateNode<VCubicBezierPath>(data, nodeD.GetId());
|
|
|
|
}
|
|
|
|
VNodeSplinePath::Create(doc, data, id, nodeD.GetId(), Document::FullParse, Source::FromGui);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qDebug()<<"May be wrong tool type!!! Ignoring."<<Q_FUNC_INFO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nodeD.SetId(id);
|
|
|
|
nodes.append(nodeD);
|
|
|
|
}
|
|
|
|
|
|
|
|
detail.SetNodes(nodes);
|
|
|
|
VToolSeamAllowance *piece = Create(0, detail, scene, doc, data, Document::FullParse, Source::FromGui);
|
|
|
|
|
|
|
|
if (piece != nullptr)
|
|
|
|
{
|
|
|
|
piece->m_dialog = dialogTool;
|
|
|
|
}
|
|
|
|
return piece;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VToolSeamAllowance *VToolSeamAllowance::Create(quint32 id, const VPiece &newPiece, VMainGraphicsScene *scene,
|
|
|
|
VAbstractPattern *doc, VContainer *data, const Document &parse,
|
|
|
|
const Source &typeCreation, const QString &drawName)
|
|
|
|
{
|
|
|
|
if (typeCreation == Source::FromGui || typeCreation == Source::FromTool)
|
|
|
|
{
|
|
|
|
id = data->AddPiece(newPiece);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->UpdatePiece(id, newPiece);
|
|
|
|
if (parse != Document::FullParse)
|
|
|
|
{
|
|
|
|
doc->UpdateToolData(id, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VAbstractTool::AddRecord(id, Tool::Piece, doc);
|
|
|
|
if (parse == Document::FullParse)
|
|
|
|
{
|
|
|
|
VToolSeamAllowance *piece = new VToolSeamAllowance(doc, data, id, typeCreation, scene, drawName);
|
|
|
|
scene->addItem(piece);
|
|
|
|
connect(piece, &VToolSeamAllowance::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem);
|
|
|
|
connect(scene, &VMainGraphicsScene::EnableDetailItemHover, piece, &VToolSeamAllowance::AllowHover);
|
|
|
|
connect(scene, &VMainGraphicsScene::EnableDetailItemSelection, piece, &VToolSeamAllowance::AllowSelecting);
|
|
|
|
connect(scene, &VMainGraphicsScene::HighlightDetail, piece, &VToolSeamAllowance::Highlight);
|
|
|
|
doc->AddTool(id, piece);
|
2016-11-08 16:52:56 +01:00
|
|
|
|
|
|
|
return piece;
|
2016-11-08 11:57:01 +01:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-09 10:57:39 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::Remove(bool ask)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DeleteTool(ask);
|
|
|
|
}
|
|
|
|
catch(const VExceptionToolWasDeleted &e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
return;//Leave this method immediately!!!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:57:01 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node)
|
|
|
|
{
|
|
|
|
QDomElement nod = doc->createElement(TagNode);
|
|
|
|
|
|
|
|
doc->SetAttribute(nod, AttrIdObject, node.GetId());
|
|
|
|
|
2016-11-12 12:18:16 +01:00
|
|
|
const Tool type = node.GetTypeTool();
|
|
|
|
if (type != Tool::NodePoint)
|
2016-11-08 11:57:01 +01:00
|
|
|
{
|
|
|
|
doc->SetAttribute(nod, AttrNodeReverse, static_cast<quint8>(node.GetReverse()));
|
|
|
|
}
|
2016-11-12 12:18:16 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const qreal w1 = node.GetSABefore();
|
|
|
|
w1 < 0 ? domElement.removeAttribute(AttrSABefore) : doc->SetAttribute(nod, AttrSABefore, w1);
|
|
|
|
|
|
|
|
const qreal w2 = node.GetSAAfter();
|
|
|
|
w2 < 0 ? domElement.removeAttribute(AttrSAAfter) : doc->SetAttribute(nod, AttrSAAfter, w2);
|
|
|
|
}
|
2016-11-08 11:57:01 +01:00
|
|
|
|
2016-11-12 12:18:16 +01:00
|
|
|
switch (type)
|
2016-11-08 11:57:01 +01:00
|
|
|
{
|
|
|
|
case (Tool::NodeArc):
|
|
|
|
doc->SetAttribute(nod, AttrType, NodeArc);
|
|
|
|
break;
|
|
|
|
case (Tool::NodePoint):
|
|
|
|
doc->SetAttribute(nod, AttrType, NodePoint);
|
|
|
|
break;
|
|
|
|
case (Tool::NodeSpline):
|
|
|
|
doc->SetAttribute(nod, AttrType, NodeSpline);
|
|
|
|
break;
|
|
|
|
case (Tool::NodeSplinePath):
|
|
|
|
doc->SetAttribute(nod, AttrType, NodeSplinePath);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qDebug()<<"May be wrong tool type!!! Ignoring."<<Q_FUNC_INFO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
domElement.appendChild(nod);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece)
|
|
|
|
{
|
|
|
|
if (piece.CountNodes() > 0)
|
|
|
|
{
|
|
|
|
QDomElement nodesElement = doc->createElement(TagNodes);
|
|
|
|
for (int i = 0; i < piece.CountNodes(); ++i)
|
|
|
|
{
|
|
|
|
AddNode(doc, nodesElement, piece.at(i));
|
|
|
|
}
|
|
|
|
domElement.appendChild(nodesElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:11:58 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AddAttributes(VAbstractPattern *doc, QDomElement &domElement, quint32 id, const VPiece &piece)
|
|
|
|
{
|
|
|
|
SCASSERT(doc != nullptr);
|
|
|
|
|
|
|
|
doc->SetAttribute(domElement, VDomDocument::AttrId, id);
|
2016-11-09 14:53:22 +01:00
|
|
|
doc->SetAttribute(domElement, AttrName, piece.GetName());
|
2016-11-09 13:11:58 +01:00
|
|
|
doc->SetAttribute(domElement, AttrVersion, QString().setNum(pieceVersion));
|
|
|
|
doc->SetAttribute(domElement, AttrMx, qApp->fromPixel(piece.GetMx()));
|
|
|
|
doc->SetAttribute(domElement, AttrMy, qApp->fromPixel(piece.GetMy()));
|
2016-11-09 14:53:22 +01:00
|
|
|
doc->SetAttribute(domElement, AttrInLayout, piece.IsInLayout());
|
2016-11-10 11:53:02 +01:00
|
|
|
doc->SetAttribute(domElement, AttrForbidFlipping, piece.IsForbidFlipping());
|
|
|
|
doc->SetAttribute(domElement, AttrSeamAllowance, piece.IsSeamAllowance());
|
|
|
|
doc->SetAttribute(domElement, AttrWidth, piece.GetSAWidth());
|
2016-11-09 13:11:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AddPatternPieceData(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AddPatternInfo(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AddGrainline(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:57:01 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QString VToolSeamAllowance::getTagName() const
|
|
|
|
{
|
|
|
|
return VAbstractPattern::TagDetail;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::ShowVisualization(bool show)
|
|
|
|
{
|
|
|
|
Q_UNUSED(show)
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::GroupVisibility(quint32 object, bool visible)
|
|
|
|
{
|
|
|
|
Q_UNUSED(object);
|
|
|
|
Q_UNUSED(visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::FullUpdateFromFile()
|
2016-11-09 12:06:05 +01:00
|
|
|
{
|
|
|
|
RefreshGeometry();
|
|
|
|
}
|
2016-11-08 11:57:01 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::FullUpdateFromGuiOk(int result)
|
2016-11-09 13:11:58 +01:00
|
|
|
{
|
|
|
|
if (result == QDialog::Accepted)
|
|
|
|
{
|
2016-11-09 15:27:55 +01:00
|
|
|
SaveDialogChange();
|
2016-11-09 13:11:58 +01:00
|
|
|
}
|
|
|
|
delete m_dialog;
|
|
|
|
}
|
2016-11-08 11:57:01 +01:00
|
|
|
|
2016-11-09 15:27:55 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::FullUpdateFromGuiApply()
|
|
|
|
{
|
|
|
|
SaveDialogChange();
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:57:01 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::EnableToolMove(bool move)
|
|
|
|
{
|
|
|
|
setFlag(QGraphicsItem::ItemIsMovable, move);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AllowHover(bool enabled)
|
|
|
|
{
|
|
|
|
setAcceptHoverEvents(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AllowSelecting(bool enabled)
|
|
|
|
{
|
|
|
|
setFlag(QGraphicsItem::ItemIsSelectable, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::Highlight(quint32 id)
|
|
|
|
{
|
|
|
|
setSelected(this->id == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::AddToFile()
|
|
|
|
{
|
|
|
|
const VPiece piece = VAbstractTool::data.GetPiece(id);
|
|
|
|
|
|
|
|
QDomElement domElement = doc->createElement(getTagName());
|
|
|
|
|
2016-11-09 13:11:58 +01:00
|
|
|
AddAttributes(doc, domElement, id, piece);
|
|
|
|
AddPatternPieceData(doc, domElement, piece);
|
|
|
|
AddPatternInfo(doc, domElement, piece);
|
|
|
|
AddGrainline(doc, domElement, piece);
|
2016-11-08 11:57:01 +01:00
|
|
|
|
|
|
|
// nodes
|
|
|
|
AddNodes(doc, domElement, piece);
|
|
|
|
|
|
|
|
AddPiece *addDet = new AddPiece(domElement, doc, piece, m_drawName);
|
|
|
|
connect(addDet, &AddPiece::NeedFullParsing, doc, &VAbstractPattern::NeedFullParsing);
|
|
|
|
qApp->getUndoStack()->push(addDet);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::RefreshDataInFile()
|
|
|
|
{
|
|
|
|
QDomElement domElement = doc->elementById(id);
|
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
|
|
|
// Refresh only parts that we possibly need to update
|
|
|
|
{
|
2016-11-08 15:34:44 +01:00
|
|
|
// TODO. Delete if minimal supported version is 0.4.0
|
|
|
|
Q_STATIC_ASSERT_X(VPatternConverter::PatternMinVer < CONVERTER_VERSION_CHECK(0, 4, 0),
|
2016-11-08 11:57:01 +01:00
|
|
|
"Time to refactor the code.");
|
|
|
|
doc->SetAttribute(domElement, AttrVersion, QString().setNum(pieceVersion));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVariant VToolSeamAllowance::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
2016-11-09 12:06:05 +01:00
|
|
|
if (change == ItemPositionChange && scene())
|
|
|
|
{
|
|
|
|
// Each time we move something we call recalculation scene rect. In some cases this can cause moving
|
|
|
|
// objects positions. And this cause infinite redrawing. That's why we wait the finish of saving the last move.
|
|
|
|
static bool changeFinished = true;
|
|
|
|
if (changeFinished)
|
|
|
|
{
|
|
|
|
changeFinished = false;
|
|
|
|
|
|
|
|
// value - this is new position.
|
|
|
|
const QPointF newPos = value.toPointF();
|
|
|
|
|
|
|
|
MovePiece *moveDet = new MovePiece(doc, newPos.x(), newPos.y(), id, scene());
|
|
|
|
connect(moveDet, &MovePiece::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree);
|
|
|
|
qApp->getUndoStack()->push(moveDet);
|
|
|
|
|
|
|
|
const QList<QGraphicsView *> viewList = scene()->views();
|
|
|
|
if (not viewList.isEmpty())
|
|
|
|
{
|
|
|
|
if (QGraphicsView *view = viewList.at(0))
|
|
|
|
{
|
|
|
|
const int xmargin = 50;
|
|
|
|
const int ymargin = 50;
|
|
|
|
|
|
|
|
const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view);
|
|
|
|
const QRectF itemRect = mapToScene(boundingRect()|childrenBoundingRect()).boundingRect();
|
|
|
|
|
|
|
|
// If item's rect is bigger than view's rect ensureVisible works very unstable.
|
|
|
|
if (itemRect.height() + 2*ymargin < viewRect.height() &&
|
|
|
|
itemRect.width() + 2*xmargin < viewRect.width())
|
|
|
|
{
|
|
|
|
view->ensureVisible(itemRect, xmargin, ymargin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure visible only small rect around a cursor
|
|
|
|
VMainGraphicsScene *currentScene = qobject_cast<VMainGraphicsScene *>(scene());
|
|
|
|
SCASSERT(currentScene);
|
|
|
|
const QPointF cursorPosition = currentScene->getScenePos();
|
|
|
|
view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Don't forget to update geometry, because first change never call full parse
|
2016-11-12 14:41:15 +01:00
|
|
|
VPiece detail = VAbstractTool::data.GetPiece(id);
|
|
|
|
detail.SetMx(newPos.x());
|
|
|
|
detail.SetMy(newPos.y());
|
|
|
|
VAbstractTool::data.UpdatePiece(id, detail);
|
|
|
|
|
2016-11-09 12:06:05 +01:00
|
|
|
RefreshGeometry();
|
|
|
|
changeFinished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change == QGraphicsItem::ItemSelectedChange)
|
|
|
|
{
|
|
|
|
if (value == true)
|
|
|
|
{
|
|
|
|
// do stuff if selected
|
|
|
|
this->setFocus();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do stuff if not selected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:57:01 +01:00
|
|
|
return VNoBrushScalePathItem::itemChange(change, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
// Special for not selectable item first need to call standard mousePressEvent then accept event
|
|
|
|
VNoBrushScalePathItem::mousePressEvent(event);
|
|
|
|
|
|
|
|
// Somehow clicking on notselectable object do not clean previous selections.
|
|
|
|
if (not (flags() & ItemIsSelectable) && scene())
|
|
|
|
{
|
|
|
|
scene()->clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
|
|
|
|
{
|
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
|
|
|
|
{
|
|
|
|
doc->SelectedDetail(id);
|
|
|
|
emit ChoosedTool(id, SceneObject::Detail);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();// Special for not selectable item first need to call standard mousePressEvent then accept event
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
//Disable cursor-arrow-closehand
|
|
|
|
RestoreOverrideCursor(cursorArrowCloseHand);
|
|
|
|
}
|
|
|
|
VNoBrushScalePathItem::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
|
|
|
SetOverrideCursor(cursorArrowOpenHand, 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
|
|
|
SetOverrideCursor(cursorArrowOpenHand, 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
//Disable cursor-arrow-openhand
|
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
|
|
|
RestoreOverrideCursor(cursorArrowOpenHand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
QMenu menu;
|
|
|
|
QAction *actionOption = menu.addAction(QIcon::fromTheme("preferences-other"), tr("Options"));
|
|
|
|
|
2016-11-09 14:53:22 +01:00
|
|
|
QAction *inLayoutOption = menu.addAction(tr("In layout"));
|
|
|
|
inLayoutOption->setCheckable(true);
|
|
|
|
const VPiece detail = VAbstractTool::data.GetPiece(id);
|
|
|
|
inLayoutOption->setChecked(detail.IsInLayout());
|
2016-11-08 11:57:01 +01:00
|
|
|
|
2016-11-09 10:57:39 +01:00
|
|
|
QAction *actionRemove = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"));
|
|
|
|
_referens > 1 ? actionRemove->setEnabled(false) : actionRemove->setEnabled(true);
|
2016-11-08 11:57:01 +01:00
|
|
|
|
|
|
|
QAction *selectedAction = menu.exec(event->screenPos());
|
|
|
|
if (selectedAction == actionOption)
|
|
|
|
{
|
2016-11-08 18:11:47 +01:00
|
|
|
auto *dialog = new DialogSeamAllowance(getData(), id, qApp->getMainWindow());
|
|
|
|
dialog->EnableApply(true);
|
|
|
|
m_dialog = dialog;
|
2016-11-08 11:57:01 +01:00
|
|
|
m_dialog->setModal(true);
|
2016-11-09 13:11:58 +01:00
|
|
|
connect(m_dialog.data(), &DialogTool::DialogClosed, this, &VToolSeamAllowance::FullUpdateFromGuiOk);
|
2016-11-09 15:27:55 +01:00
|
|
|
connect(m_dialog.data(), &DialogTool::DialogApplied, this, &VToolSeamAllowance::FullUpdateFromGuiApply);
|
2016-11-08 11:57:01 +01:00
|
|
|
SetDialog();
|
|
|
|
m_dialog->show();
|
|
|
|
}
|
2016-11-09 14:53:22 +01:00
|
|
|
else if (selectedAction == inLayoutOption)
|
|
|
|
{
|
|
|
|
TogglePieceInLayout *togglePrint = new TogglePieceInLayout(id, selectedAction->isChecked(),
|
|
|
|
&(VAbstractTool::data), doc);
|
|
|
|
connect(togglePrint, &TogglePieceInLayout::UpdateList, doc, &VAbstractPattern::CheckInLayoutList);
|
|
|
|
qApp->getUndoStack()->push(togglePrint);
|
|
|
|
}
|
2016-11-09 10:57:39 +01:00
|
|
|
else if (selectedAction == actionRemove)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DeleteTool();
|
|
|
|
}
|
|
|
|
catch(const VExceptionToolWasDeleted &e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
return;//Leave this method immediately!!!
|
|
|
|
}
|
|
|
|
return; //Leave this method immediately after call!!!
|
|
|
|
}
|
2016-11-08 11:57:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
2016-11-09 10:57:39 +01:00
|
|
|
switch (event->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Delete:
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DeleteTool();
|
|
|
|
}
|
|
|
|
catch(const VExceptionToolWasDeleted &e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
return;//Leave this method immediately!!!
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:57:01 +01:00
|
|
|
VNoBrushScalePathItem::keyReleaseEvent ( event );
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::SetDialog()
|
|
|
|
{
|
2016-11-09 13:11:58 +01:00
|
|
|
SCASSERT(not m_dialog.isNull());
|
2016-11-08 11:57:01 +01:00
|
|
|
DialogSeamAllowance *dialogTool = qobject_cast<DialogSeamAllowance*>(m_dialog);
|
|
|
|
SCASSERT(dialogTool != nullptr);
|
|
|
|
dialogTool->SetPiece(VAbstractTool::data.GetPiece(id));
|
|
|
|
dialogTool->EnableApply(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VToolSeamAllowance::VToolSeamAllowance(VAbstractPattern *doc, VContainer *data, const quint32 &id,
|
|
|
|
const Source &typeCreation, VMainGraphicsScene *scene,
|
|
|
|
const QString &drawName, QGraphicsItem *parent)
|
|
|
|
: VAbstractTool(doc, data, id),
|
|
|
|
VNoBrushScalePathItem(parent),
|
2016-11-09 13:11:58 +01:00
|
|
|
m_dialog(),
|
2016-11-08 11:57:01 +01:00
|
|
|
m_sceneDetails(scene),
|
2016-11-10 13:06:09 +01:00
|
|
|
m_drawName(drawName),
|
|
|
|
m_seamAllowance(new VNoBrushScalePathItem(this))
|
2016-11-08 11:57:01 +01:00
|
|
|
{
|
|
|
|
VPiece detail = data->GetPiece(id);
|
|
|
|
for (int i = 0; i< detail.CountNodes(); ++i)
|
|
|
|
{
|
|
|
|
switch (detail.at(i).GetTypeTool())
|
|
|
|
{
|
|
|
|
case (Tool::NodePoint):
|
|
|
|
{
|
|
|
|
VNodePoint *tool = InitTool<VNodePoint>(scene, detail.at(i));
|
|
|
|
connect(tool, &VNodePoint::ShowContextMenu, this, &VToolSeamAllowance::contextMenuEvent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (Tool::NodeArc):
|
|
|
|
doc->IncrementReferens(detail.at(i).GetId());
|
|
|
|
break;
|
|
|
|
case (Tool::NodeSpline):
|
|
|
|
doc->IncrementReferens(detail.at(i).GetId());
|
|
|
|
break;
|
|
|
|
case (Tool::NodeSplinePath):
|
|
|
|
doc->IncrementReferens(detail.at(i).GetId());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qDebug()<<"Get wrong tool type. Ignore.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|
|
|
RefreshGeometry();
|
|
|
|
|
|
|
|
this->setBrush(QBrush(Qt::Dense7Pattern));
|
2016-11-10 13:06:09 +01:00
|
|
|
m_seamAllowance->setBrush(QBrush(Qt::FDiagPattern));
|
2016-11-08 11:57:01 +01:00
|
|
|
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
|
|
|
|
|
|
|
|
connect(scene, &VMainGraphicsScene::EnableToolMove, this, &VToolSeamAllowance::EnableToolMove);
|
|
|
|
//connect(scene, &VMainGraphicsScene::ItemClicked, this, &VToolSeamAllowance::ResetChildren);
|
|
|
|
if (typeCreation == Source::FromGui || typeCreation == Source::FromTool)
|
|
|
|
{
|
|
|
|
AddToFile();
|
|
|
|
if (typeCreation != Source::FromTool)
|
|
|
|
{
|
|
|
|
qApp->getUndoStack()->endMacro();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RefreshDataInFile();
|
|
|
|
}
|
|
|
|
setAcceptHoverEvents(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolSeamAllowance::RefreshGeometry()
|
|
|
|
{
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
|
|
|
|
|
|
|
|
const VPiece detail = VAbstractTool::data.GetPiece(id);
|
|
|
|
QPainterPath mainPath = detail.MainPathPath(this->getData());
|
|
|
|
this->setPath(mainPath);
|
|
|
|
this->setPos(detail.GetMx(), detail.GetMy());
|
|
|
|
|
2016-11-10 13:06:09 +01:00
|
|
|
if (detail.IsSeamAllowance())
|
|
|
|
{
|
|
|
|
mainPath.addPath(detail.SeamAllowancePath(this->getData()));
|
|
|
|
mainPath.setFillRule(Qt::OddEvenFill);
|
|
|
|
m_seamAllowance->setPath(mainPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_seamAllowance->setPath(QPainterPath());
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:57:01 +01:00
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-11-09 15:27:55 +01:00
|
|
|
void VToolSeamAllowance::SaveDialogChange()
|
|
|
|
{
|
|
|
|
SCASSERT(not m_dialog.isNull());
|
|
|
|
DialogSeamAllowance *dialogTool = qobject_cast<DialogSeamAllowance*>(m_dialog.data());
|
|
|
|
SCASSERT(dialogTool != nullptr);
|
|
|
|
const VPiece newDet = dialogTool->GetPiece();
|
|
|
|
const VPiece oldDet = VAbstractTool::data.GetPiece(id);
|
|
|
|
|
|
|
|
SavePieceOptions *saveCommand = new SavePieceOptions(oldDet, newDet, doc, id, this->scene());
|
|
|
|
connect(saveCommand, &SavePieceOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree);
|
|
|
|
qApp->getUndoStack()->push(saveCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-11-08 11:57:01 +01:00
|
|
|
void VToolSeamAllowance::DeleteTool(bool ask)
|
|
|
|
{
|
2016-11-09 10:57:39 +01:00
|
|
|
DeletePiece *delDet = new DeletePiece(doc, id, VAbstractTool::data.GetPiece(id));
|
|
|
|
if (ask)
|
|
|
|
{
|
|
|
|
if (ConfirmDeletion() == QMessageBox::No)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* If UnionDetails tool delete detail no need emit FullParsing.*/
|
|
|
|
connect(delDet, &DeletePiece::NeedFullParsing, doc, &VAbstractPattern::NeedFullParsing);
|
|
|
|
}
|
|
|
|
qApp->getUndoStack()->push(delDet);
|
2016-11-08 11:57:01 +01:00
|
|
|
|
2016-11-09 10:57:39 +01:00
|
|
|
// Throw exception, this will help prevent case when we forget to immediately quit function.
|
|
|
|
VExceptionToolWasDeleted e("Tool was used after deleting.");
|
|
|
|
throw e;
|
2016-11-08 11:57:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
|
|
/**
|
|
|
|
* @brief CreateNode create new node for detail.
|
|
|
|
* @param data container.
|
|
|
|
* @param id id parent object.
|
|
|
|
* @return id for new object.
|
|
|
|
*/
|
|
|
|
quint32 VToolSeamAllowance::CreateNode(VContainer *data, const quint32 &id)
|
|
|
|
{
|
|
|
|
//We can't use exist object. Need create new.
|
|
|
|
T *node = new T(*data->GeometricObject<T>(id).data());
|
|
|
|
node->setMode(Draw::Modeling);
|
|
|
|
return data->AddGObject(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Tool>
|
|
|
|
/**
|
|
|
|
* @brief InitTool initial node item on scene
|
|
|
|
* @param scene pointer to scene.
|
|
|
|
* @param node node of detail.
|
|
|
|
*/
|
|
|
|
Tool *VToolSeamAllowance::InitTool(VMainGraphicsScene *scene, const VPieceNode &node)
|
|
|
|
{
|
|
|
|
QHash<quint32, VDataTool*>* tools = doc->getTools();
|
|
|
|
SCASSERT(tools != nullptr);
|
|
|
|
Tool *tool = qobject_cast<Tool*>(tools->value(node.GetId()));
|
|
|
|
SCASSERT(tool != nullptr);
|
|
|
|
connect(tool, &Tool::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem);
|
|
|
|
tool->setParentItem(this);
|
|
|
|
tool->SetParentType(ParentType::Item);
|
|
|
|
doc->IncrementReferens(node.GetId());
|
|
|
|
return tool;
|
|
|
|
}
|