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
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)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"
|
2014-06-08 20:10:57 +02:00
|
|
|
#include "../../geometry/vpointf.h"
|
2013-10-18 20:20:54 +02:00
|
|
|
|
2014-08-14 13:16:02 +02:00
|
|
|
#include <QtMath>
|
|
|
|
|
2013-10-29 14:45:07 +01:00
|
|
|
const QString VToolTriangle::ToolType = QStringLiteral("triangle");
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief VToolTriangle constructor.
|
|
|
|
* @param doc dom document container.
|
|
|
|
* @param data container with variables.
|
|
|
|
* @param id object id in container.
|
|
|
|
* @param axisP1Id id first axis point.
|
|
|
|
* @param axisP2Id id second axis point.
|
|
|
|
* @param firstPointId id first triangle point, what lies on the hypotenuse.
|
|
|
|
* @param secondPointId id second triangle point, what lies on the hypotenuse.
|
|
|
|
* @param typeCreation way we create this tool.
|
|
|
|
* @param parent parent object.
|
|
|
|
*/
|
2014-05-01 13:33:40 +02:00
|
|
|
VToolTriangle::VToolTriangle(VPattern *doc, VContainer *data, const quint32 &id, const quint32 &axisP1Id,
|
|
|
|
const quint32 &axisP2Id, const quint32 &firstPointId, const quint32 &secondPointId,
|
2014-06-12 09:22:29 +02:00
|
|
|
const Source &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;
|
2014-06-12 09:22:29 +02:00
|
|
|
if (typeCreation == Source::FromGui)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief setDialog set dialog when user want change tool option.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
void VToolTriangle::setDialog()
|
|
|
|
{
|
2014-05-30 21:56:05 +02:00
|
|
|
SCASSERT(dialog != nullptr);
|
2014-02-06 14:57:23 +01:00
|
|
|
DialogTriangle *dialogTool = qobject_cast<DialogTriangle*>(dialog);
|
2014-05-30 21:56:05 +02:00
|
|
|
SCASSERT(dialogTool != nullptr);
|
2014-08-21 14:44:40 +02:00
|
|
|
const QSharedPointer<VPointF> p = VAbstractTool::data.GeometricObject<VPointF>(id);
|
2014-07-29 15:08:38 +02:00
|
|
|
dialogTool->setAxisP1Id(axisP1Id);
|
|
|
|
dialogTool->setAxisP2Id(axisP2Id);
|
|
|
|
dialogTool->setFirstPointId(firstPointId);
|
|
|
|
dialogTool->setSecondPointId(secondPointId);
|
2014-02-06 14:57:23 +01:00
|
|
|
dialogTool->setPointName(p->name());
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Create help create tool from GUI.
|
|
|
|
* @param dialog dialog.
|
|
|
|
* @param scene pointer to scene.
|
|
|
|
* @param doc dom document container.
|
|
|
|
* @param data container with variables.
|
2014-08-14 13:16:02 +02:00
|
|
|
* @return the created tool
|
2014-06-13 19:02:41 +02:00
|
|
|
*/
|
2014-08-14 13:16:02 +02:00
|
|
|
VToolTriangle* VToolTriangle::Create(DialogTool *dialog, VMainGraphicsScene *scene, VPattern *doc, VContainer *data)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-05-30 21:56:05 +02:00
|
|
|
SCASSERT(dialog != nullptr);
|
2014-02-06 14:57:23 +01:00
|
|
|
DialogTriangle *dialogTool = qobject_cast<DialogTriangle*>(dialog);
|
2014-05-30 21:56:05 +02:00
|
|
|
SCASSERT(dialogTool != nullptr);
|
2014-05-30 13:50:41 +02:00
|
|
|
const quint32 axisP1Id = dialogTool->getAxisP1Id();
|
|
|
|
const quint32 axisP2Id = dialogTool->getAxisP2Id();
|
|
|
|
const quint32 firstPointId = dialogTool->getFirstPointId();
|
|
|
|
const quint32 secondPointId = dialogTool->getSecondPointId();
|
|
|
|
const QString pointName = dialogTool->getPointName();
|
2014-08-14 13:16:02 +02:00
|
|
|
VToolTriangle* point = nullptr;
|
|
|
|
point = Create(0, pointName, axisP1Id, axisP2Id, firstPointId, secondPointId, 5, 10, scene, doc, data,
|
|
|
|
Document::FullParse, Source::FromGui);
|
|
|
|
if (point != nullptr)
|
|
|
|
{
|
|
|
|
point->dialog=dialogTool;
|
|
|
|
}
|
|
|
|
return point;
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief Create help create tool.
|
|
|
|
* @param _id tool id, 0 if tool doesn't exist yet.
|
|
|
|
* @param pointName point name.
|
|
|
|
* @param axisP1Id id first axis point.
|
|
|
|
* @param axisP2Id id second axis point.
|
|
|
|
* @param firstPointId id first triangle point, what lies on the hypotenuse.
|
|
|
|
* @param secondPointId id second triangle point, what lies on the hypotenuse.
|
|
|
|
* @param mx label bias x axis.
|
|
|
|
* @param my label bias y axis.
|
|
|
|
* @param scene pointer to scene.
|
|
|
|
* @param doc dom document container.
|
|
|
|
* @param data container with variables.
|
|
|
|
* @param parse parser file mode.
|
|
|
|
* @param typeCreation way we create this tool.
|
2014-08-14 13:16:02 +02:00
|
|
|
* @return the created tool
|
2014-06-13 19:02:41 +02:00
|
|
|
*/
|
2014-08-14 13:16:02 +02:00
|
|
|
VToolTriangle* VToolTriangle::Create(const quint32 _id, const QString &pointName, const quint32 &axisP1Id,
|
|
|
|
const quint32 &axisP2Id, const quint32 &firstPointId, const quint32 &secondPointId,
|
|
|
|
const qreal &mx, const qreal &my, VMainGraphicsScene *scene, VPattern *doc,
|
|
|
|
VContainer *data, const Document &parse, const Source &typeCreation)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-08-21 14:44:40 +02:00
|
|
|
const QSharedPointer<VPointF> axisP1 = data->GeometricObject<VPointF>(axisP1Id);
|
|
|
|
const QSharedPointer<VPointF> axisP2 = data->GeometricObject<VPointF>(axisP2Id);
|
|
|
|
const QSharedPointer<VPointF> firstPoint = data->GeometricObject<VPointF>(firstPointId);
|
|
|
|
const QSharedPointer<VPointF> secondPoint = data->GeometricObject<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;
|
2014-06-12 09:22:29 +02:00
|
|
|
if (typeCreation == Source::FromGui)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-07-25 13:53:39 +02:00
|
|
|
id = data->AddGObject(new VPointF(point, pointName, mx, my));
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-25 13:53:39 +02:00
|
|
|
data->UpdateGObject(id, new VPointF(point, 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);
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 09:22:29 +02:00
|
|
|
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);
|
2014-06-24 10:23:39 +02:00
|
|
|
connect(scene, &VMainGraphicsScene::DisableItem, point, &VToolPoint::Disable);
|
2013-10-18 20:20:54 +02:00
|
|
|
doc->AddTool(id, point);
|
|
|
|
doc->IncrementReferens(axisP1Id);
|
|
|
|
doc->IncrementReferens(axisP2Id);
|
|
|
|
doc->IncrementReferens(firstPointId);
|
|
|
|
doc->IncrementReferens(secondPointId);
|
2014-08-14 13:16:02 +02:00
|
|
|
return point;
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
2014-08-14 13:16:02 +02:00
|
|
|
return nullptr;
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief FindPoint find point intersection two foots right triangle.
|
|
|
|
* @param axisP1 first axis point.
|
|
|
|
* @param axisP2 second axis point.
|
|
|
|
* @param firstPoint first triangle point, what lies on the hypotenuse.
|
|
|
|
* @param secondPoint second triangle point, what lies on the hypotenuse.
|
|
|
|
* @return point intersection two foots right triangle.
|
|
|
|
*/
|
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
|
|
|
{
|
2014-08-14 13:16:02 +02:00
|
|
|
QLineF axis(axisP1, axisP2);
|
|
|
|
QLineF hypotenuse(firstPoint, secondPoint);
|
|
|
|
|
|
|
|
QPointF startPoint;
|
|
|
|
QLineF::IntersectType intersect = axis.intersect(hypotenuse, &startPoint);
|
|
|
|
if (intersect != QLineF::UnboundedIntersection && intersect != QLineF::BoundedIntersection)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-08-14 13:16:02 +02:00
|
|
|
return QPointF();
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-08-14 13:16:02 +02:00
|
|
|
qreal step = 1;
|
|
|
|
|
|
|
|
QLineF line;
|
|
|
|
line.setP1(startPoint);
|
|
|
|
line.setAngle(axis.angle());
|
|
|
|
line.setLength(step);
|
|
|
|
|
|
|
|
int c = qFloor(hypotenuse.length());
|
2013-11-04 21:35:15 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2013-10-18 20:20:54 +02:00
|
|
|
line.setLength(line.length()+step);
|
2014-08-14 13:16:02 +02:00
|
|
|
int a = qFloor(QLineF(line.p2(), firstPoint).length());
|
|
|
|
int b = qFloor(QLineF(line.p2(), secondPoint).length());
|
|
|
|
if (c*c <= (a*a + b*b))
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-28 16:45:27 +01:00
|
|
|
return line.p2();
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief FullUpdateFromFile update tool data form file.
|
|
|
|
*/
|
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
|
|
|
}
|
2014-08-21 14:44:40 +02:00
|
|
|
VToolPoint::RefreshPointGeometry(*VDrawTool::data.GeometricObject<VPointF>(id));
|
2013-10-18 20:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief ShowContextMenu show context menu.
|
|
|
|
* @param event context menu event.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief RemoveReferens decrement value of reference.
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief contextMenuEvent handle context menu events.
|
|
|
|
* @param event context menu event.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief SaveDialog save options into file after change in dialog.
|
|
|
|
*/
|
2014-02-06 14:57:23 +01:00
|
|
|
void VToolTriangle::SaveDialog(QDomElement &domElement)
|
|
|
|
{
|
2014-05-30 21:56:05 +02:00
|
|
|
SCASSERT(dialog != nullptr);
|
2014-02-06 14:57:23 +01:00
|
|
|
DialogTriangle *dialogTool = qobject_cast<DialogTriangle*>(dialog);
|
2014-05-30 21:56:05 +02:00
|
|
|
SCASSERT(dialogTool != nullptr);
|
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
|
|
|
}
|
2014-08-30 21:58:31 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VToolTriangle::SaveOptions(QDomElement &tag, QSharedPointer<VGObject> &obj)
|
|
|
|
{
|
|
|
|
QSharedPointer<VPointF> point = qSharedPointerDynamicCast<VPointF>(obj);
|
|
|
|
SCASSERT(point.isNull() == false);
|
|
|
|
|
|
|
|
doc->SetAttribute(tag, VDomDocument::AttrId, id);
|
|
|
|
doc->SetAttribute(tag, AttrType, ToolType);
|
|
|
|
doc->SetAttribute(tag, AttrName, point->name());
|
|
|
|
doc->SetAttribute(tag, AttrMx, qApp->fromPixel(point->mx()));
|
|
|
|
doc->SetAttribute(tag, AttrMy, qApp->fromPixel(point->my()));
|
|
|
|
|
|
|
|
doc->SetAttribute(tag, AttrAxisP1, axisP1Id);
|
|
|
|
doc->SetAttribute(tag, AttrAxisP2, axisP2Id);
|
|
|
|
doc->SetAttribute(tag, AttrFirstPoint, firstPointId);
|
|
|
|
doc->SetAttribute(tag, AttrSecondPoint, secondPointId);
|
|
|
|
}
|