2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-11-04 21:38:54 +01:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vtooltriangle.cpp
|
2013-11-15 13:41:26 +01:00
|
|
|
** @author Roman Telezhinsky <dismine@gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-11-04 21:38:54 +01:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @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.
|
2013-11-04 21:38:54 +01:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-11-04 21:38:54 +01:00
|
|
|
** 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-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-11-04 21:38:54 +01:00
|
|
|
|
2013-10-18 20:20:54 +02:00
|
|
|
#include "vtooltriangle.h"
|
2014-02-21 14:04:39 +01:00
|
|
|
#include "../../dialogs/tools/dialogtriangle.h"
|
2013-10-18 20:20:54 +02:00
|
|
|
|
2013-10-29 14:45:07 +01:00
|
|
|
const QString VToolTriangle::ToolType = QStringLiteral("triangle");
|
|
|
|
|
2014-02-25 15:40:24 +01:00
|
|
|
VToolTriangle::VToolTriangle(VPattern *doc, VContainer *data, const quint32 &id,
|
|
|
|
const quint32 &axisP1Id, const quint32 &axisP2Id, const quint32 &firstPointId,
|
|
|
|
const quint32 &secondPointId, const Tool::Sources &typeCreation, QGraphicsItem *parent)
|
2013-10-18 20:20:54 +02:00
|
|
|
:VToolPoint(doc, data, id, parent), axisP1Id(axisP1Id), axisP2Id(axisP2Id), firstPointId(firstPointId),
|
2014-02-06 14:57:23 +01:00
|
|
|
secondPointId(secondPointId)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-21 16:09:05 +02:00
|
|
|
ignoreFullUpdate = true;
|
2013-11-04 21:35:15 +01:00
|
|
|
if (typeCreation == Tool::FromGui)
|
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
AddToFile();
|
|
|
|
}
|
2013-12-24 12:24:29 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
RefreshDataInFile();
|
|
|
|
}
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 21:35:15 +01:00
|
|
|
void VToolTriangle::setDialog()
|
|
|
|
{
|
2014-02-06 14:57:23 +01:00
|
|
|
Q_CHECK_PTR(dialog);
|
|
|
|
DialogTriangle *dialogTool = qobject_cast<DialogTriangle*>(dialog);
|
|
|
|
Q_CHECK_PTR(dialogTool);
|
2013-12-29 17:48:57 +01:00
|
|
|
const VPointF *p = VAbstractTool::data.GeometricObject<const VPointF *>(id);
|
2014-02-06 14:57:23 +01:00
|
|
|
dialogTool->setAxisP1Id(axisP1Id, id);
|
|
|
|
dialogTool->setAxisP2Id(axisP2Id, id);
|
|
|
|
dialogTool->setFirstPointId(firstPointId, id);
|
|
|
|
dialogTool->setSecondPointId(secondPointId, id);
|
|
|
|
dialogTool->setPointName(p->name());
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:57:23 +01:00
|
|
|
void VToolTriangle::Create(DialogTool *dialog, VMainGraphicsScene *scene,
|
2014-02-25 15:02:09 +01:00
|
|
|
VPattern *doc, VContainer *data)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-02-06 14:57:23 +01:00
|
|
|
Q_CHECK_PTR(dialog);
|
|
|
|
DialogTriangle *dialogTool = qobject_cast<DialogTriangle*>(dialog);
|
|
|
|
Q_CHECK_PTR(dialogTool);
|
2014-02-25 15:40:24 +01:00
|
|
|
quint32 axisP1Id = dialogTool->getAxisP1Id();
|
|
|
|
quint32 axisP2Id = dialogTool->getAxisP2Id();
|
|
|
|
quint32 firstPointId = dialogTool->getFirstPointId();
|
|
|
|
quint32 secondPointId = dialogTool->getSecondPointId();
|
2014-02-06 14:57:23 +01:00
|
|
|
QString pointName = dialogTool->getPointName();
|
2013-10-18 20:20:54 +02:00
|
|
|
Create(0, pointName, axisP1Id, axisP2Id, firstPointId, secondPointId, 5, 10, scene, doc, data,
|
|
|
|
Document::FullParse, Tool::FromGui);
|
|
|
|
}
|
|
|
|
|
2014-02-25 15:40:24 +01:00
|
|
|
void VToolTriangle::Create(const quint32 _id, const QString &pointName, const quint32 &axisP1Id,
|
|
|
|
const quint32 &axisP2Id, const quint32 &firstPointId, const quint32 &secondPointId,
|
2014-02-25 15:02:09 +01:00
|
|
|
const qreal &mx, const qreal &my, VMainGraphicsScene *scene, VPattern *doc,
|
2013-11-06 22:11:12 +01:00
|
|
|
VContainer *data, const Document::Documents &parse, const Tool::Sources &typeCreation)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-12-29 17:48:57 +01:00
|
|
|
const VPointF *axisP1 = data->GeometricObject<const VPointF *>(axisP1Id);
|
|
|
|
const VPointF *axisP2 = data->GeometricObject<const VPointF *>(axisP2Id);
|
|
|
|
const VPointF *firstPoint = data->GeometricObject<const VPointF *>(firstPointId);
|
|
|
|
const VPointF *secondPoint = data->GeometricObject<const VPointF *>(secondPointId);
|
2013-10-18 20:20:54 +02:00
|
|
|
|
2013-12-29 17:48:57 +01:00
|
|
|
QPointF point = FindPoint(axisP1->toQPointF(), axisP2->toQPointF(), firstPoint->toQPointF(),
|
|
|
|
secondPoint->toQPointF());
|
2014-02-25 15:40:24 +01:00
|
|
|
quint32 id = _id;
|
2013-11-04 21:35:15 +01:00
|
|
|
if (typeCreation == Tool::FromGui)
|
|
|
|
{
|
2013-12-29 17:48:57 +01:00
|
|
|
id = data->AddGObject(new VPointF(point.x(), point.y(), pointName, mx, my));
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-29 17:48:57 +01:00
|
|
|
data->UpdateGObject(id, new VPointF(point.x(), point.y(), pointName, mx, my));
|
2013-11-04 21:35:15 +01:00
|
|
|
if (parse != Document::FullParse)
|
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
doc->UpdateToolData(id, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VDrawTool::AddRecord(id, Tool::Triangle, doc);
|
2013-11-04 21:35:15 +01:00
|
|
|
if (parse == Document::FullParse)
|
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
VToolTriangle *point = new VToolTriangle(doc, data, id, axisP1Id, axisP2Id, firstPointId,
|
|
|
|
secondPointId, typeCreation);
|
|
|
|
scene->addItem(point);
|
|
|
|
connect(point, &VToolTriangle::ChoosedTool, scene, &VMainGraphicsScene::ChoosedItem);
|
|
|
|
connect(scene, &VMainGraphicsScene::NewFactor, point, &VToolTriangle::SetFactor);
|
|
|
|
doc->AddTool(id, point);
|
|
|
|
doc->IncrementReferens(axisP1Id);
|
|
|
|
doc->IncrementReferens(axisP2Id);
|
|
|
|
doc->IncrementReferens(firstPointId);
|
|
|
|
doc->IncrementReferens(secondPointId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-07 15:40:39 +01:00
|
|
|
QPointF VToolTriangle::FindPoint(const QPointF &axisP1, const QPointF &axisP2, const QPointF &firstPoint,
|
|
|
|
const QPointF &secondPoint)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
qreal c = QLineF(firstPoint, secondPoint).length();
|
|
|
|
qreal a = QLineF(axisP2, firstPoint).length();
|
|
|
|
qreal b = QLineF(axisP2, secondPoint).length();
|
2013-11-12 19:29:03 +01:00
|
|
|
if (fabs(c*c - (a*a + b*b)) < 0.0001)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-21 09:45:49 +02:00
|
|
|
QLineF l1(axisP2, firstPoint);
|
|
|
|
QLineF l2(axisP2, secondPoint);
|
2013-11-12 19:29:03 +01:00
|
|
|
if (fabs(l1.angleTo(l2) - 90) < 0.1 || fabs(l2.angleTo(l1) - 90) < 0.1)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-21 09:45:49 +02:00
|
|
|
return axisP2;
|
|
|
|
}
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QLineF line = QLineF(axisP1, axisP2);
|
|
|
|
qreal step = 0.01;
|
2013-11-04 21:35:15 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
line.setLength(line.length()+step);
|
|
|
|
a = QLineF(line.p2(), firstPoint).length();
|
|
|
|
b = QLineF(line.p2(), secondPoint).length();
|
2013-12-21 12:36:51 +01:00
|
|
|
if (qFuzzyCompare(c*c, a*a + b*b))
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-21 09:45:49 +02:00
|
|
|
QLineF l1(axisP2, firstPoint);
|
|
|
|
QLineF l2(axisP2, secondPoint);
|
2013-11-12 19:29:03 +01:00
|
|
|
if (fabs(l1.angleTo(l2) - 90) < 0.1 || fabs(l2.angleTo(l1) - 90) < 0.1)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-21 09:45:49 +02:00
|
|
|
return line.p2();
|
|
|
|
}
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
2013-11-04 21:35:15 +01:00
|
|
|
if (c*c < a*a + b*b)
|
|
|
|
{
|
2013-12-21 12:36:51 +01:00
|
|
|
//Still don't know why this code handled. Need to think about that.
|
2013-10-18 20:20:54 +02:00
|
|
|
qWarning()<<tr("Can't find point.")<<Q_FUNC_INFO;
|
2013-10-28 16:45:27 +01:00
|
|
|
return line.p2();
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 21:35:15 +01:00
|
|
|
void VToolTriangle::FullUpdateFromFile()
|
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
QDomElement domElement = doc->elementById(QString().setNum(id));
|
2013-11-04 21:35:15 +01:00
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
2014-02-25 15:55:02 +01:00
|
|
|
axisP1Id = domElement.attribute(AttrAxisP1, "").toUInt();
|
|
|
|
axisP2Id = domElement.attribute(AttrAxisP2, "").toUInt();
|
|
|
|
firstPointId = domElement.attribute(AttrFirstPoint, "").toUInt();
|
|
|
|
secondPointId = domElement.attribute(AttrSecondPoint, "").toUInt();
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
2013-12-29 17:48:57 +01:00
|
|
|
VToolPoint::RefreshPointGeometry(*VDrawTool::data.GeometricObject<const VPointF *>(id));
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:51:09 +01:00
|
|
|
void VToolTriangle::ShowContextMenu(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
2014-02-06 14:57:23 +01:00
|
|
|
ContextMenu<DialogTriangle>(this, event);
|
2014-01-09 13:51:09 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 21:35:15 +01:00
|
|
|
void VToolTriangle::RemoveReferens()
|
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
doc->DecrementReferens(axisP1Id);
|
|
|
|
doc->DecrementReferens(axisP2Id);
|
|
|
|
doc->DecrementReferens(firstPointId);
|
|
|
|
doc->DecrementReferens(secondPointId);
|
|
|
|
}
|
|
|
|
|
2013-11-04 21:35:15 +01:00
|
|
|
void VToolTriangle::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
2014-02-06 14:57:23 +01:00
|
|
|
ContextMenu<DialogTriangle>(this, event);
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 21:35:15 +01:00
|
|
|
void VToolTriangle::AddToFile()
|
|
|
|
{
|
2013-12-29 17:48:57 +01:00
|
|
|
const VPointF *point = VAbstractTool::data.GeometricObject<const VPointF *>(id);
|
2013-10-29 14:45:07 +01:00
|
|
|
QDomElement domElement = doc->createElement(TagName);
|
|
|
|
|
2014-02-25 15:02:09 +01:00
|
|
|
doc->SetAttribute(domElement, AttrId, id);
|
|
|
|
doc->SetAttribute(domElement, AttrType, ToolType);
|
|
|
|
doc->SetAttribute(domElement, AttrName, point->name());
|
|
|
|
doc->SetAttribute(domElement, AttrMx, toMM(point->mx()));
|
|
|
|
doc->SetAttribute(domElement, AttrMy, toMM(point->my()));
|
2013-10-29 14:45:07 +01:00
|
|
|
|
2014-02-25 15:02:09 +01:00
|
|
|
doc->SetAttribute(domElement, AttrAxisP1, axisP1Id);
|
|
|
|
doc->SetAttribute(domElement, AttrAxisP2, axisP2Id);
|
|
|
|
doc->SetAttribute(domElement, AttrFirstPoint, firstPointId);
|
|
|
|
doc->SetAttribute(domElement, AttrSecondPoint, secondPointId);
|
2013-10-18 20:20:54 +02:00
|
|
|
|
|
|
|
AddToCalculation(domElement);
|
|
|
|
}
|
2013-12-24 12:24:29 +01:00
|
|
|
|
|
|
|
void VToolTriangle::RefreshDataInFile()
|
|
|
|
{
|
2013-12-29 17:48:57 +01:00
|
|
|
const VPointF *point = VAbstractTool::data.GeometricObject<const VPointF *>(id);
|
2013-12-24 12:24:29 +01:00
|
|
|
QDomElement domElement = doc->elementById(QString().setNum(id));
|
|
|
|
if (domElement.isElement())
|
|
|
|
{
|
2014-02-25 15:02:09 +01:00
|
|
|
doc->SetAttribute(domElement, AttrName, point->name());
|
|
|
|
doc->SetAttribute(domElement, AttrMx, toMM(point->mx()));
|
|
|
|
doc->SetAttribute(domElement, AttrMy, toMM(point->my()));
|
|
|
|
doc->SetAttribute(domElement, AttrAxisP1, axisP1Id);
|
|
|
|
doc->SetAttribute(domElement, AttrAxisP2, axisP2Id);
|
|
|
|
doc->SetAttribute(domElement, AttrFirstPoint, firstPointId);
|
|
|
|
doc->SetAttribute(domElement, AttrSecondPoint, secondPointId);
|
2013-12-24 12:24:29 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-06 14:57:23 +01:00
|
|
|
|
|
|
|
void VToolTriangle::SaveDialog(QDomElement &domElement)
|
|
|
|
{
|
|
|
|
Q_CHECK_PTR(dialog);
|
|
|
|
DialogTriangle *dialogTool = qobject_cast<DialogTriangle*>(dialog);
|
|
|
|
Q_CHECK_PTR(dialogTool);
|
2014-02-25 15:02:09 +01:00
|
|
|
doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
|
|
|
|
doc->SetAttribute(domElement, AttrAxisP1, QString().setNum(dialogTool->getAxisP1Id()));
|
|
|
|
doc->SetAttribute(domElement, AttrAxisP2, QString().setNum(dialogTool->getAxisP2Id()));
|
|
|
|
doc->SetAttribute(domElement, AttrFirstPoint, QString().setNum(dialogTool->getFirstPointId()));
|
|
|
|
doc->SetAttribute(domElement, AttrSecondPoint, QString().setNum(dialogTool->getSecondPointId()));
|
2014-02-06 14:57:23 +01:00
|
|
|
}
|