2014-07-21 14:52:15 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file visline.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 21 7, 2014
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2014-07-21 14:52:15 +02:00
|
|
|
** <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 "visline.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2014-10-03 12:32:12 +02:00
|
|
|
#include <QtCore/qmath.h>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QRectF>
|
|
|
|
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../ifc/ifcdef.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "../vgeometry/vgobject.h"
|
|
|
|
#include "../vmisc/vabstractapplication.h"
|
|
|
|
#include "../vpatterndb/vcontainer.h"
|
|
|
|
|
|
|
|
class QPointF;
|
2014-07-21 14:52:15 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VisLine::VisLine(const VContainer *data, QGraphicsItem *parent)
|
2014-08-15 12:48:11 +02:00
|
|
|
:Visualization(data), QGraphicsLineItem(parent)
|
2014-07-24 12:19:41 +02:00
|
|
|
{
|
|
|
|
this->setZValue(1);// Show on top real tool
|
2014-08-15 12:48:11 +02:00
|
|
|
InitPen();
|
2014-07-24 12:19:41 +02:00
|
|
|
}
|
2014-07-21 14:52:15 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VisLine::~VisLine()
|
|
|
|
{}
|
|
|
|
|
2014-07-24 13:31:02 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-03 06:50:14 +02:00
|
|
|
qreal VisLine::CorrectAngle(const qreal &angle)
|
2014-07-24 15:26:59 +02:00
|
|
|
{
|
|
|
|
qreal ang = angle;
|
|
|
|
if (angle > 360)
|
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
ang = angle - 360.0 * qFloor(angle/360);
|
2014-07-24 15:26:59 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
switch (qFloor((qAbs(ang)+22.5)/45))
|
2014-07-24 15:26:59 +02:00
|
|
|
{
|
|
|
|
case 0: // <22.5
|
|
|
|
return 0;
|
|
|
|
case 1: // <67.5
|
|
|
|
return 45;
|
|
|
|
case 2: // <112.5
|
|
|
|
return 90;
|
|
|
|
case 3: // <157.5
|
|
|
|
return 135;
|
|
|
|
case 4: // <202.5
|
|
|
|
return 180;
|
|
|
|
case 5: // <247.5
|
|
|
|
return 225;
|
|
|
|
case 6: // < 292.5
|
|
|
|
return 270;
|
|
|
|
case 7: // <337.5
|
|
|
|
return 315;
|
|
|
|
default: // <360
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPointF VisLine::Ray(const QPointF &firstPoint, const qreal &angle) const
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (this->scene() == nullptr)
|
2014-08-12 19:02:14 +02:00
|
|
|
{
|
2014-08-15 12:48:11 +02:00
|
|
|
QLineF line = QLineF(firstPoint, Visualization::scenePos);
|
2014-08-13 13:12:00 +02:00
|
|
|
line.setAngle(angle);
|
|
|
|
return line.p2();// We can't find ray because item doesn't have scene. We will return cursor position on scene.
|
2014-08-12 19:02:14 +02:00
|
|
|
}
|
|
|
|
|
2014-07-24 15:26:59 +02:00
|
|
|
QRectF scRect = this->scene()->sceneRect();
|
2015-11-25 14:45:38 +01:00
|
|
|
|
|
|
|
//Limit size of the scene rect. Axis that has same size as scene rect cause scene size growth.
|
|
|
|
QLineF line1 = QLineF(scRect.topLeft(), scRect.bottomRight());
|
2015-12-04 21:21:07 +01:00
|
|
|
line1.setLength(2);
|
2015-11-25 14:45:38 +01:00
|
|
|
|
|
|
|
QLineF line2 = QLineF(scRect.bottomRight(), scRect.topLeft());
|
2015-12-04 21:21:07 +01:00
|
|
|
line2.setLength(2);
|
2015-11-25 14:45:38 +01:00
|
|
|
|
|
|
|
scRect = QRectF(line1.p2(), line2.p2());
|
|
|
|
|
2014-07-24 15:26:59 +02:00
|
|
|
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
|
|
|
|
{
|
2014-10-23 10:38:57 +02:00
|
|
|
return VGObject::BuildRay(firstPoint, CorrectAngle(angle), scRect);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return VGObject::BuildRay(firstPoint, angle, scRect);
|
2014-07-24 15:26:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPointF VisLine::Ray(const QPointF &firstPoint) const
|
|
|
|
{
|
2014-08-15 12:48:11 +02:00
|
|
|
QLineF line = QLineF(firstPoint, Visualization::scenePos);
|
2014-07-24 15:26:59 +02:00
|
|
|
return Ray(firstPoint, line.angle());
|
|
|
|
}
|
|
|
|
|
2014-07-23 10:56:32 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-15 12:48:11 +02:00
|
|
|
QLineF VisLine::Axis(const QPointF &p, const qreal &angle) const
|
2014-07-23 10:56:32 +02:00
|
|
|
{
|
2014-08-15 12:48:11 +02:00
|
|
|
QPointF endP1 = Ray(p, angle+180);
|
|
|
|
QPointF endP2 = Ray(p, angle);
|
|
|
|
return QLineF(endP1, endP2);
|
2014-07-23 10:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-15 12:48:11 +02:00
|
|
|
QLineF VisLine::Axis(const QPointF &p1, const QPointF &p2) const
|
2014-07-23 10:56:32 +02:00
|
|
|
{
|
2014-08-15 12:48:11 +02:00
|
|
|
QLineF line(p1, p2);
|
|
|
|
return Axis(p1, line.angle());
|
2014-07-21 14:52:15 +02:00
|
|
|
}
|
2014-08-14 13:16:02 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-15 12:48:11 +02:00
|
|
|
void VisLine::InitPen()
|
2014-08-14 13:16:02 +02:00
|
|
|
{
|
2015-06-15 11:32:27 +02:00
|
|
|
this->setPen(QPen(mainColor, qApp->toPixel(WidthHairLine(*Visualization::data->GetPatternUnit()))/factor,
|
|
|
|
lineStyle));
|
2014-08-14 13:16:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-15 12:48:11 +02:00
|
|
|
void VisLine::AddOnScene()
|
2014-08-14 13:16:02 +02:00
|
|
|
{
|
2014-08-15 12:48:11 +02:00
|
|
|
AddItem(this);
|
2014-08-14 13:16:02 +02:00
|
|
|
}
|
2015-06-05 15:43:41 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VisLine::DrawRay(QGraphicsLineItem *lineItem, const QPointF &p, const QPointF &pTangent, const QColor &color,
|
|
|
|
Qt::PenStyle style)
|
|
|
|
{
|
|
|
|
SCASSERT (lineItem != nullptr);
|
|
|
|
|
|
|
|
const qreal angle = QLineF(p, pTangent).angle();
|
|
|
|
const QPointF endRay = Ray(p, angle);
|
|
|
|
const QLineF tangent = VGObject::BuildLine(p, QLineF(p, endRay).length(), angle);
|
|
|
|
DrawLine(lineItem, tangent, color, style);
|
|
|
|
}
|