2015-01-02 15:14:28 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vlayoutdetail.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 2 1, 2015
|
|
|
|
**
|
|
|
|
** @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) 2015 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 "vlayoutdetail.h"
|
2015-01-07 17:54:43 +01:00
|
|
|
#include "vlayoutdetail_p.h"
|
2015-01-02 15:14:28 +01:00
|
|
|
|
2015-01-13 11:38:51 +01:00
|
|
|
#include <QGraphicsItem>
|
2015-01-12 21:35:32 +01:00
|
|
|
#include <QPainterPath>
|
2015-01-11 14:11:56 +01:00
|
|
|
#include <QtMath>
|
|
|
|
|
2015-01-07 17:54:43 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-02 15:14:28 +01:00
|
|
|
VLayoutDetail::VLayoutDetail()
|
2015-01-07 17:54:43 +01:00
|
|
|
:VAbstractDetail(), d(new VLayoutDetailData)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutDetail::VLayoutDetail(const VLayoutDetail &detail)
|
|
|
|
:VAbstractDetail(detail), d (detail.d)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutDetail &VLayoutDetail::operator=(const VLayoutDetail &detail)
|
|
|
|
{
|
|
|
|
if ( &detail == this )
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
VAbstractDetail::operator=(detail);
|
|
|
|
d = detail.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutDetail::~VLayoutDetail()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-11 14:11:56 +01:00
|
|
|
QVector<QPointF> VLayoutDetail::GetContourPoints() const
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
|
|
|
return d->contour;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-11 14:11:56 +01:00
|
|
|
void VLayoutDetail::SetCountourPoints(const QVector<QPointF> &points)
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
|
|
|
d->contour = points;
|
2015-01-10 14:47:46 +01:00
|
|
|
// Contour can't be closed
|
|
|
|
if (d->contour.first() == d->contour.last())
|
|
|
|
{
|
|
|
|
d->contour.removeLast();
|
|
|
|
}
|
2015-01-16 13:54:37 +01:00
|
|
|
|
2015-01-20 13:50:24 +01:00
|
|
|
d->contour = RemoveDublicates(RoundPoints(d->contour));
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QPointF> VLayoutDetail::GetSeamAllowencePoints() const
|
|
|
|
{
|
|
|
|
return d->seamAllowence;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutDetail::SetSeamAllowencePoints(const QVector<QPointF> &points)
|
|
|
|
{
|
|
|
|
d->seamAllowence = points;
|
2015-01-10 14:47:46 +01:00
|
|
|
// Seam allowence can't be closed
|
|
|
|
if (d->seamAllowence.first() == d->seamAllowence.last())
|
|
|
|
{
|
|
|
|
d->seamAllowence.removeLast();
|
|
|
|
}
|
2015-01-16 13:54:37 +01:00
|
|
|
|
2015-01-20 13:50:24 +01:00
|
|
|
d->seamAllowence = RemoveDublicates(RoundPoints(d->seamAllowence));
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-11 14:11:56 +01:00
|
|
|
QVector<QPointF> VLayoutDetail::GetLayoutAllowencePoints() const
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
|
|
|
return Map(d->layoutAllowence);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-18 19:56:01 +01:00
|
|
|
QTransform VLayoutDetail::GetMatrix() const
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
|
|
|
return d->matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-18 19:56:01 +01:00
|
|
|
void VLayoutDetail::SetMatrix(const QTransform &matrix)
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
|
|
|
d->matrix = matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VLayoutDetail::GetLayoutWidth() const
|
|
|
|
{
|
|
|
|
return d->layoutWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutDetail::SetLayoutWidth(const qreal &value)
|
|
|
|
{
|
|
|
|
d->layoutWidth = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-10 14:47:46 +01:00
|
|
|
void VLayoutDetail::Translate(qreal dx, qreal dy)
|
|
|
|
{
|
2015-01-18 19:56:01 +01:00
|
|
|
QTransform m;
|
2015-01-16 13:54:37 +01:00
|
|
|
m.translate(dx, dy);
|
|
|
|
d->matrix *= m;
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutDetail::Rotate(const QPointF &originPoint, qreal degrees)
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
2015-01-18 19:56:01 +01:00
|
|
|
QTransform m;
|
2015-01-16 13:54:37 +01:00
|
|
|
m.translate(originPoint.x(), originPoint.y());
|
|
|
|
m.rotate(-degrees);
|
|
|
|
m.translate(-originPoint.x(), -originPoint.y());
|
|
|
|
d->matrix *= m;
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-10 14:47:46 +01:00
|
|
|
void VLayoutDetail::Mirror(const QLineF &edge)
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
2015-01-10 14:47:46 +01:00
|
|
|
if (edge.isNull())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-17 18:23:54 +01:00
|
|
|
const QLineF axis = QLineF(edge.x2(), edge.y2(), edge.x2() + 100, edge.y2()); // Ox axis
|
2015-01-10 14:47:46 +01:00
|
|
|
|
2015-01-16 14:20:15 +01:00
|
|
|
const qreal angle = edge.angleTo(axis);
|
2015-01-18 19:56:01 +01:00
|
|
|
QTransform m;
|
2015-01-16 14:20:15 +01:00
|
|
|
m.translate(edge.p2().x(), edge.p2().y());
|
|
|
|
m.rotate(-angle);
|
2015-01-17 18:23:54 +01:00
|
|
|
m.translate(-edge.p2().x(), -edge.p2().y());
|
|
|
|
d->matrix *= m;
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
m.translate(edge.p2().x(), edge.p2().y());
|
2015-01-16 14:20:15 +01:00
|
|
|
m.scale(m.m11(), m.m22()*-1);
|
2015-01-17 18:23:54 +01:00
|
|
|
m.translate(-edge.p2().x(), -edge.p2().y());
|
|
|
|
d->matrix *= m;
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
m.translate(edge.p2().x(), edge.p2().y());
|
|
|
|
m.rotate(-(360-angle));
|
2015-01-16 14:20:15 +01:00
|
|
|
m.translate(-edge.p2().x(), -edge.p2().y());
|
|
|
|
d->matrix *= m;
|
2015-01-18 19:56:01 +01:00
|
|
|
|
|
|
|
d->mirror = !d->mirror;
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutDetail::EdgesCount() const
|
|
|
|
{
|
2015-01-10 14:47:46 +01:00
|
|
|
return d->layoutAllowence.count();
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QLineF VLayoutDetail::Edge(int i) const
|
|
|
|
{
|
|
|
|
if (i < 1 || i > EdgesCount())
|
|
|
|
{ // Doesn't exist such edge
|
|
|
|
return QLineF();
|
|
|
|
}
|
2015-01-19 15:13:26 +01:00
|
|
|
|
|
|
|
int i1, i2;
|
2015-01-10 14:47:46 +01:00
|
|
|
if (i < EdgesCount())
|
|
|
|
{
|
2015-01-19 15:13:26 +01:00
|
|
|
i1 = i-1;
|
|
|
|
i2 = i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i1 = EdgesCount()-1;
|
|
|
|
i2 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->mirror)
|
|
|
|
{
|
|
|
|
const int oldI1 = i1;
|
|
|
|
i1 = (d->layoutAllowence.size()-1) - i2;
|
|
|
|
i2 = (d->layoutAllowence.size()-1) - oldI1;
|
|
|
|
return QLineF(d->matrix.map(d->layoutAllowence.at(i2)), d->matrix.map(d->layoutAllowence.at(i1)));
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-19 15:13:26 +01:00
|
|
|
return QLineF(d->matrix.map(d->layoutAllowence.at(i1)), d->matrix.map(d->layoutAllowence.at(i2)));
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutDetail::EdgeByPoint(const QPointF &p1) const
|
|
|
|
{
|
|
|
|
if (p1.isNull())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EdgesCount() < 3)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-11 14:11:56 +01:00
|
|
|
const QVector<QPointF> points = GetLayoutAllowencePoints();
|
2015-01-10 14:47:46 +01:00
|
|
|
for (int i=0; i< points.size(); i++)
|
|
|
|
{
|
|
|
|
if (points.at(i) == p1)
|
|
|
|
{
|
|
|
|
return i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // Did not find edge
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QRectF VLayoutDetail::BoundingRect() const
|
|
|
|
{
|
2015-01-11 14:11:56 +01:00
|
|
|
QVector<QPointF> points = GetLayoutAllowencePoints();
|
2015-01-10 14:47:46 +01:00
|
|
|
points.append(points.first());
|
2015-01-20 13:50:24 +01:00
|
|
|
QRectF rec = QPolygonF(points).boundingRect();
|
|
|
|
return rec;
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-01-11 14:11:56 +01:00
|
|
|
bool VLayoutDetail::isNull() const
|
|
|
|
{
|
|
|
|
if (d->contour.isEmpty() == false && d->layoutWidth > 0)
|
|
|
|
{
|
|
|
|
if (getSeamAllowance() && d->seamAllowence.isEmpty() == false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qint64 VLayoutDetail::Square() const
|
|
|
|
{
|
|
|
|
if (d->layoutAllowence.isEmpty())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int n = d->layoutAllowence.count();
|
|
|
|
qreal s, res = 0;
|
|
|
|
qint64 sq = 0;
|
|
|
|
|
|
|
|
QVector<qreal> x;
|
|
|
|
QVector<qreal> y;
|
|
|
|
|
|
|
|
for(int i=0; i < n; ++i)
|
|
|
|
{
|
|
|
|
x.append(d->layoutAllowence.at(i).x());
|
|
|
|
y.append(d->layoutAllowence.at(i).y());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculation a polygon area through the sum of the areas of trapezoids
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
s = x.at(i)*(y.at(n-1) - y.at(i+1)); //if i == 0, then y[i-1] replace on y[n-1]
|
|
|
|
res += s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i == n-1)
|
|
|
|
{
|
|
|
|
s = x.at(i)*(y.at(i-1) - y.at(0)); // if i == n-1, then y[i+1] replace on y[0]
|
|
|
|
res += s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s = x.at(i)*(y.at(i-1) - y.at(i+1));
|
|
|
|
res += s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sq = qFloor(qAbs(res/2.0));
|
|
|
|
return sq;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutDetail::SetLayoutAllowencePoints()
|
2015-01-07 17:54:43 +01:00
|
|
|
{
|
|
|
|
if (d->layoutWidth > 0)
|
|
|
|
{
|
|
|
|
if (getSeamAllowance())
|
|
|
|
{
|
|
|
|
d->layoutAllowence = Equidistant(d->seamAllowence, EquidistantType::CloseEquidistant, d->layoutWidth);
|
2015-01-16 13:54:37 +01:00
|
|
|
if (d->layoutAllowence.isEmpty() == false)
|
|
|
|
{
|
|
|
|
d->layoutAllowence.removeLast();
|
|
|
|
}
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->layoutAllowence = Equidistant(d->contour, EquidistantType::CloseEquidistant, d->layoutWidth);
|
2015-01-16 13:54:37 +01:00
|
|
|
if (d->layoutAllowence.isEmpty() == false)
|
|
|
|
{
|
|
|
|
d->layoutAllowence.removeLast();
|
|
|
|
}
|
2015-01-07 17:54:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->layoutAllowence.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QPointF> VLayoutDetail::Map(const QVector<QPointF> &points) const
|
2015-01-02 15:14:28 +01:00
|
|
|
{
|
2015-01-07 17:54:43 +01:00
|
|
|
QVector<QPointF> p;
|
|
|
|
for (int i = 0; i < points.size(); ++i)
|
|
|
|
{
|
|
|
|
p.append(d->matrix.map(points.at(i)));
|
|
|
|
}
|
2015-01-10 14:47:46 +01:00
|
|
|
|
2015-01-18 19:56:01 +01:00
|
|
|
if (d->mirror)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
QList<QPointF> list = p.toList();
|
|
|
|
for(int k=0, s=list.size(), max=(s/2); k<max; k++)
|
|
|
|
{
|
|
|
|
list.swap(k, s-(1+k));
|
|
|
|
}
|
|
|
|
p = list.toVector();
|
|
|
|
}
|
2015-01-07 17:54:43 +01:00
|
|
|
return p;
|
2015-01-02 15:14:28 +01:00
|
|
|
}
|
2015-01-12 21:35:32 +01:00
|
|
|
|
2015-01-16 13:54:37 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QPointF> VLayoutDetail::RoundPoints(const QVector<QPointF> &points) const
|
|
|
|
{
|
|
|
|
QVector<QPointF> p;
|
|
|
|
for (int i=0; i < points.size(); ++i)
|
|
|
|
{
|
|
|
|
p.append(QPointF(qRound(points.at(i).x()), qRound(points.at(i).y())));
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPainterPath VLayoutDetail::ContourPath() const
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
|
|
|
|
// contour
|
2015-01-13 11:38:51 +01:00
|
|
|
QVector<QPointF> points = Map(d->contour);
|
|
|
|
path.moveTo(points.at(0));
|
|
|
|
for (qint32 i = 1; i < points.count(); ++i)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
2015-01-13 11:38:51 +01:00
|
|
|
path.lineTo(points.at(i));
|
2015-01-12 21:35:32 +01:00
|
|
|
}
|
2015-01-13 11:38:51 +01:00
|
|
|
path.lineTo(points.at(0));
|
2015-01-12 21:35:32 +01:00
|
|
|
|
|
|
|
// seam allowence
|
|
|
|
if (getSeamAllowance() == true)
|
|
|
|
{
|
2015-01-13 11:38:51 +01:00
|
|
|
points = Map(d->seamAllowence);
|
2015-01-16 20:35:50 +01:00
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
if (getClosed() == true)
|
|
|
|
{
|
2015-01-16 20:35:50 +01:00
|
|
|
points.append(points.at(0));
|
2015-01-12 21:35:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 20:35:50 +01:00
|
|
|
QPainterPath ekv;
|
|
|
|
ekv.moveTo(points.at(0));
|
|
|
|
for (qint32 i = 1; i < points.count(); ++i)
|
2015-01-12 21:35:32 +01:00
|
|
|
{
|
2015-01-16 20:35:50 +01:00
|
|
|
ekv.lineTo(points.at(i));
|
2015-01-12 21:35:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
path.addPath(ekv);
|
|
|
|
path.setFillRule(Qt::WindingFill);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2015-01-13 11:38:51 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QGraphicsItem *VLayoutDetail::GetItem() const
|
|
|
|
{
|
|
|
|
QGraphicsPathItem *item = new QGraphicsPathItem();
|
|
|
|
item->setPath(ContourPath());
|
|
|
|
return item;
|
|
|
|
}
|
2015-01-18 19:56:01 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VLayoutDetail::IsMirror() const
|
|
|
|
{
|
|
|
|
return d->mirror;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutDetail::SetMirror(bool value)
|
|
|
|
{
|
|
|
|
d->mirror = value;
|
|
|
|
}
|