2015-01-10 14:47:46 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vlayoutpaper.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 7 1, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2015-01-10 14:47:46 +01:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2015-01-10 14:47:46 +01: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 "vlayoutpaper.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QBrush>
|
2015-01-16 13:54:37 +01:00
|
|
|
#include <QCoreApplication>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QGraphicsRectItem>
|
2015-05-02 18:21:47 +02:00
|
|
|
#include <QGraphicsScene>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QList>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QPointF>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QRectF>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QVector>
|
|
|
|
#include <Qt>
|
|
|
|
#include <QtAlgorithms>
|
|
|
|
|
|
|
|
#include "vbestsquare.h"
|
|
|
|
#include "vcontour.h"
|
2017-01-22 10:02:02 +01:00
|
|
|
#include "vlayoutpiece.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "vlayoutpaper_p.h"
|
|
|
|
#include "vposition.h"
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::VLayoutPaper()
|
|
|
|
:d(new VLayoutPaperData)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::VLayoutPaper(int height, int width)
|
|
|
|
:d(new VLayoutPaperData(height, width))
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::VLayoutPaper(const VLayoutPaper &paper)
|
|
|
|
:d (paper.d)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper &VLayoutPaper::operator=(const VLayoutPaper &paper)
|
|
|
|
{
|
|
|
|
if ( &paper == this )
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
d = paper.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VLayoutPaper::~VLayoutPaper()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutPaper::GetHeight() const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
return d->globalContour.GetHeight();
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetHeight(int height)
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetHeight(height);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutPaper::GetWidth() const
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
return d->globalContour.GetWidth();
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetWidth(int width)
|
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetWidth(width);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 15:00:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VLayoutPaper::GetLayoutWidth() const
|
|
|
|
{
|
|
|
|
return d->layoutWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetLayoutWidth(qreal width)
|
|
|
|
{
|
|
|
|
if (width >= 0)
|
|
|
|
{
|
|
|
|
d->layoutWidth = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:23:25 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-10-26 09:36:07 +01:00
|
|
|
quint32 VLayoutPaper::GetShift() const
|
2015-01-12 16:23:25 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
return d->globalContour.GetShift();
|
2015-01-12 16:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-10-26 09:36:07 +01:00
|
|
|
void VLayoutPaper::SetShift(quint32 shift)
|
2015-01-12 16:23:25 +01:00
|
|
|
{
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetShift(shift);
|
2015-01-12 16:23:25 +01:00
|
|
|
}
|
|
|
|
|
2015-01-22 15:11:50 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VLayoutPaper::GetRotate() const
|
|
|
|
{
|
2016-09-06 15:00:25 +02:00
|
|
|
return d->globalRotate;
|
2015-01-22 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetRotate(bool value)
|
|
|
|
{
|
2016-09-06 15:00:25 +02:00
|
|
|
d->globalRotate = value;
|
|
|
|
d->localRotate = d->globalRotate;
|
2015-01-22 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutPaper::GetRotationIncrease() const
|
|
|
|
{
|
2016-09-06 15:00:25 +02:00
|
|
|
return d->globalRotationIncrease;
|
2015-01-22 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetRotationIncrease(int value)
|
|
|
|
{
|
2016-09-06 15:00:25 +02:00
|
|
|
d->globalRotationIncrease = value;
|
2015-01-22 15:11:50 +01:00
|
|
|
|
2016-09-06 15:00:25 +02:00
|
|
|
if ((d->globalRotationIncrease >= 1
|
|
|
|
&& d->globalRotationIncrease <= 180
|
|
|
|
&& 360 % d->globalRotationIncrease == 0) == false)
|
2015-01-22 15:11:50 +01:00
|
|
|
{
|
2016-09-06 15:00:25 +02:00
|
|
|
d->globalRotationIncrease = 180;
|
2015-01-22 15:11:50 +01:00
|
|
|
}
|
2016-09-06 15:00:25 +02:00
|
|
|
|
|
|
|
d->localRotationIncrease = d->globalRotationIncrease;
|
2015-01-22 15:11:50 +01:00
|
|
|
}
|
|
|
|
|
2015-05-08 12:10:56 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VLayoutPaper::IsSaveLength() const
|
|
|
|
{
|
|
|
|
return d->saveLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetSaveLength(bool value)
|
|
|
|
{
|
|
|
|
d->saveLength = value;
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VLayoutPaper::SetPaperIndex(quint32 index)
|
|
|
|
{
|
|
|
|
d->paperIndex = index;
|
|
|
|
}
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-02-22 13:04:47 +01:00
|
|
|
bool VLayoutPaper::ArrangeDetail(const VLayoutPiece &detail, std::atomic_bool &stop)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
// First need set size of paper
|
2015-01-21 19:56:59 +01:00
|
|
|
if (d->globalContour.GetHeight() <= 0 || d->globalContour.GetWidth() <= 0)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-10 17:38:34 +01:00
|
|
|
if (detail.LayoutEdgesCount() < 3 || detail.DetailEdgesCount() < 3)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
|
|
|
return false;//Not enough edges
|
|
|
|
}
|
|
|
|
|
2017-12-17 14:06:46 +01:00
|
|
|
if ((detail.IsForceFlipping() || detail.IsForbidFlipping()) && not d->globalRotate)
|
2016-09-06 15:00:25 +02:00
|
|
|
{ // Compensate forbidden flipping by rotating. 180 degree will be enough.
|
|
|
|
d->localRotate = true;
|
|
|
|
d->localRotationIncrease = 180;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Return to global values if was changed
|
|
|
|
d->localRotate = d->globalRotate;
|
|
|
|
d->localRotationIncrease = d->globalRotationIncrease;
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:35:32 +01:00
|
|
|
d->frame = 0;
|
|
|
|
|
2015-01-21 15:33:06 +01:00
|
|
|
return AddToSheet(detail, stop);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VLayoutPaper::Count() const
|
|
|
|
{
|
|
|
|
return d->details.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-02-22 13:04:47 +01:00
|
|
|
bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-05-08 12:10:56 +02:00
|
|
|
VBestSquare bestResult(d->globalContour.GetSize(), d->saveLength);
|
2015-01-21 22:49:38 +01:00
|
|
|
QThreadPool *thread_pool = QThreadPool::globalInstance();
|
2015-05-02 19:18:31 +02:00
|
|
|
thread_pool->setExpiryTimeout(1000);
|
2015-01-21 22:49:38 +01:00
|
|
|
QVector<VPosition *> threads;
|
2015-01-10 14:47:46 +01:00
|
|
|
|
2016-12-10 17:38:34 +01:00
|
|
|
int detailEdgesCount = 0;
|
|
|
|
|
|
|
|
if (d->globalContour.GetContour().isEmpty())
|
|
|
|
{
|
|
|
|
detailEdgesCount = detail.DetailEdgesCount();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
detailEdgesCount = detail.LayoutEdgesCount();
|
|
|
|
}
|
|
|
|
|
2016-03-27 21:53:41 +02:00
|
|
|
for (int j=1; j <= d->globalContour.GlobalEdgesCount(); ++j)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2016-12-10 17:38:34 +01:00
|
|
|
for (int i=1; i<= detailEdgesCount; ++i)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2016-09-06 15:00:25 +02:00
|
|
|
VPosition *thread = new VPosition(d->globalContour, j, detail, i, &stop, d->localRotate,
|
|
|
|
d->localRotationIncrease,
|
2015-05-08 12:10:56 +02:00
|
|
|
d->saveLength);
|
2015-01-21 22:49:38 +01:00
|
|
|
//Info for debug
|
2015-01-22 12:29:40 +01:00
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
thread->setPaperIndex(d->paperIndex);
|
|
|
|
thread->setFrame(d->frame);
|
|
|
|
thread->setDetailsCount(d->details.count());
|
|
|
|
thread->setDetails(d->details);
|
|
|
|
#endif
|
2015-01-21 22:49:38 +01:00
|
|
|
|
|
|
|
thread->setAutoDelete(false);
|
|
|
|
threads.append(thread);
|
|
|
|
thread_pool->start(thread);
|
|
|
|
|
2016-09-06 15:00:25 +02:00
|
|
|
d->frame = d->frame + 3 + static_cast<quint32>(360/d->localRotationIncrease*2);
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 16:09:58 +02:00
|
|
|
// Wait for done
|
|
|
|
do
|
|
|
|
{
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QThread::msleep(250);
|
|
|
|
}
|
2017-02-22 13:04:47 +01:00
|
|
|
while(thread_pool->activeThreadCount() > 0 && not stop.load());
|
2015-01-10 14:47:46 +01:00
|
|
|
|
2017-02-22 13:04:47 +01:00
|
|
|
if (stop.load())
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2015-05-02 19:18:31 +02:00
|
|
|
qDeleteAll(threads.begin(), threads.end());
|
|
|
|
threads.clear();
|
2015-01-21 22:49:38 +01:00
|
|
|
return false;
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
|
|
|
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto thread : threads)
|
2015-01-10 14:47:46 +01:00
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
bestResult.NewResult(thread->getBestResult());
|
2015-01-10 14:47:46 +01:00
|
|
|
}
|
2015-01-18 19:56:01 +01:00
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
qDeleteAll(threads.begin(), threads.end());
|
|
|
|
threads.clear();
|
2015-01-18 19:56:01 +01:00
|
|
|
|
2015-01-21 22:49:38 +01:00
|
|
|
return SaveResult(bestResult, detail);
|
2015-01-12 16:59:15 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 14:47:46 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail)
|
2015-01-12 16:23:25 +01:00
|
|
|
{
|
2015-05-06 16:11:12 +02:00
|
|
|
if (bestResult.ValidResult())
|
2015-01-12 16:23:25 +01:00
|
|
|
{
|
2017-01-22 10:02:02 +01:00
|
|
|
VLayoutPiece workDetail = detail;
|
2015-01-12 16:23:25 +01:00
|
|
|
workDetail.SetMatrix(bestResult.Matrix());// Don't forget set matrix
|
2015-01-18 19:56:01 +01:00
|
|
|
workDetail.SetMirror(bestResult.Mirror());
|
2015-01-21 19:56:59 +01:00
|
|
|
const QVector<QPointF> newGContour = d->globalContour.UniteWithContour(workDetail, bestResult.GContourEdge(),
|
|
|
|
bestResult.DetailEdge(),
|
|
|
|
bestResult.Type());
|
2015-01-12 16:23:25 +01:00
|
|
|
if (newGContour.isEmpty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
d->details.append(workDetail);
|
2015-01-21 19:56:59 +01:00
|
|
|
d->globalContour.SetContour(newGContour);
|
2015-01-18 19:56:01 +01:00
|
|
|
|
|
|
|
#ifdef LAYOUT_DEBUG
|
|
|
|
# ifdef SHOW_BEST
|
2015-01-21 22:49:38 +01:00
|
|
|
VPosition::DrawDebug(d->globalContour, workDetail, UINT_MAX, d->paperIndex, d->details.count(), d->details);
|
2015-01-18 19:56:01 +01:00
|
|
|
# endif
|
|
|
|
#endif
|
2015-01-12 16:23:25 +01:00
|
|
|
}
|
|
|
|
|
2015-05-06 16:11:12 +02:00
|
|
|
return bestResult.ValidResult(); // Do we have the best result?
|
2015-01-12 16:23:25 +01:00
|
|
|
}
|
2015-01-12 21:35:32 +01:00
|
|
|
|
2015-01-13 11:38:51 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-07-01 19:32:54 +02:00
|
|
|
QGraphicsRectItem *VLayoutPaper::GetPaperItem(bool autoCrop, bool textAsPaths) const
|
2015-01-13 11:38:51 +01:00
|
|
|
{
|
2015-05-02 18:21:47 +02:00
|
|
|
QGraphicsRectItem *paper;
|
|
|
|
if (autoCrop)
|
|
|
|
{
|
2016-12-10 17:38:34 +01:00
|
|
|
QScopedPointer<QGraphicsScene> scene(new QGraphicsScene());
|
2017-07-01 19:32:54 +02:00
|
|
|
QList<QGraphicsItem *> list = GetItemDetails(textAsPaths);
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto item : list)
|
2015-05-02 18:21:47 +02:00
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
scene->addItem(item);
|
2015-05-02 18:21:47 +02:00
|
|
|
}
|
2016-12-10 17:38:34 +01:00
|
|
|
const int height = scene->itemsBoundingRect().toRect().height();
|
2015-10-28 15:22:36 +01:00
|
|
|
if (d->globalContour.GetHeight() > height) //-V807
|
2015-05-02 18:21:47 +02:00
|
|
|
{
|
|
|
|
paper = new QGraphicsRectItem(QRectF(0, 0, d->globalContour.GetWidth(), height));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
paper = new QGraphicsRectItem(QRectF(0, 0, d->globalContour.GetWidth(), d->globalContour.GetHeight()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
paper = new QGraphicsRectItem(QRectF(0, 0, d->globalContour.GetWidth(), d->globalContour.GetHeight()));
|
|
|
|
}
|
2015-01-13 11:38:51 +01:00
|
|
|
paper->setPen(QPen(Qt::black, 1));
|
|
|
|
paper->setBrush(QBrush(Qt::white));
|
2015-01-23 11:07:58 +01:00
|
|
|
return paper;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-07-01 19:32:54 +02:00
|
|
|
QList<QGraphicsItem *> VLayoutPaper::GetItemDetails(bool textAsPaths) const
|
2015-01-23 11:07:58 +01:00
|
|
|
{
|
|
|
|
QList<QGraphicsItem *> list;
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto &detail : d->details)
|
2015-01-13 11:38:51 +01:00
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
list.append(detail.GetItem(textAsPaths));
|
2015-01-13 11:38:51 +01:00
|
|
|
}
|
2015-01-23 11:07:58 +01:00
|
|
|
return list;
|
2015-01-13 11:38:51 +01:00
|
|
|
}
|
2016-02-23 13:13:10 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
QVector<VLayoutPiece> VLayoutPaper::GetDetails() const
|
2016-02-23 13:13:10 +01:00
|
|
|
{
|
|
|
|
return d->details;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-01-22 10:02:02 +01:00
|
|
|
void VLayoutPaper::SetDetails(const QList<VLayoutPiece> &details)
|
2016-02-23 13:13:10 +01:00
|
|
|
{
|
|
|
|
d->details = details.toVector();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-12-10 17:38:34 +01:00
|
|
|
QRectF VLayoutPaper::DetailsBoundingRect() const
|
2016-02-23 13:13:10 +01:00
|
|
|
{
|
|
|
|
QRectF rec;
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto &detail : d->details)
|
2016-02-23 13:13:10 +01:00
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
rec = rec.united(detail.DetailBoundingRect());
|
2016-02-23 13:13:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rec;
|
|
|
|
}
|