Class that drive all process, also speak with outside world.

--HG--
branch : feature
This commit is contained in:
dismine 2015-01-12 14:12:15 +02:00
parent b53d32225a
commit 98d5dde2e1
5 changed files with 240 additions and 6 deletions

View File

@ -31,12 +31,13 @@
#include <QPointF>
#include <climits>
#include <QRectF>
//---------------------------------------------------------------------------------------------------------------------
VBank::VBank()
:details(QVector<VLayoutDetail>()), unsorted(QHash<int, qint64>()), big(QHash<int, qint64>()),
middle(QHash<int, qint64>()), small(QHash<int, qint64>()), layoutWidth(0), caseType(Cases::CaseDesc),
prepare(false)
prepare(false), boundingRect(QRectF())
{}
//---------------------------------------------------------------------------------------------------------------------
@ -180,6 +181,7 @@ bool VBank::Prepare()
unsorted.insert(i, square);
}
BiggestBoundingRect();
PrepareGroup();
prepare = true;
@ -194,6 +196,7 @@ void VBank::Reset()
big.clear();
middle.clear();
small.clear();
boundingRect = QRectF();
}
//---------------------------------------------------------------------------------------------------------------------
@ -214,6 +217,40 @@ int VBank::LeftArrange() const
return big.count() + middle.count() + small.count();
}
//---------------------------------------------------------------------------------------------------------------------
void VBank::BiggestBoundingRect()
{
int index = -1;
qint64 sMax = LLONG_MIN;
QHash<int, qint64>::const_iterator i = unsorted.constBegin();
while (i != unsorted.constEnd())
{
if (i.value() > sMax)
{
sMax = i.value();
index = i.key();
}
++i;
}
if (index >= 0 && index < details.size())
{
boundingRect = QPolygonF(details.at(index).GetLayoutAllowencePoints()).boundingRect();
}
else
{
boundingRect = QRectF();
}
}
//---------------------------------------------------------------------------------------------------------------------
QRectF VBank::GetBiggestBoundingRect() const
{
return boundingRect;
}
//---------------------------------------------------------------------------------------------------------------------
void VBank::PrepareGroup()
{
@ -251,7 +288,7 @@ void VBank::PrepareThreeGroups()
{
big.insert(i.key(), i.value());
}
else if (s1 > i.value() && i.value() > s2)
else if (s1 >= i.value() && i.value() > s2)
{
middle.insert(i.key(), i.value());
}

View File

@ -34,6 +34,7 @@
class QPointF;
class VLayoutDetail;
class QRectF;
enum class Cases : char { CaseThreeGroup, CaseTwoGroup, CaseDesc};
@ -59,6 +60,8 @@ public:
int AllDetailsCount() const;
int LeftArrange() const;
QRectF GetBiggestBoundingRect() const;
private:
Q_DISABLE_COPY(VBank)
QVector<VLayoutDetail> details;
@ -72,6 +75,7 @@ private:
Cases caseType;
bool prepare;
QRectF boundingRect;
void PrepareGroup();
@ -84,6 +88,7 @@ private:
int GetNextDescGroup() const;
void SqMaxMin(qint64 &sMax, qint64 &sMin) const;
void BiggestBoundingRect();
};
#endif // VBANK_H

View File

@ -31,4 +31,13 @@
enum class EquidistantType : char { OpenEquidistant, CloseEquidistant };
enum class LayoutErrors : char
{
NoError,
PrepareLayoutError,
PaperSizeError,
ProcessStoped,
EmptyPaperError
};
#endif // VLAYOUTDEF_H

View File

@ -27,8 +27,148 @@
*************************************************************************/
#include "vlayoutgenerator.h"
#include "vlayoutpaper.h"
#include "vlayoutdetail.h"
#include <QRectF>
VLayoutGenerator::VLayoutGenerator()
//---------------------------------------------------------------------------------------------------------------------
VLayoutGenerator::VLayoutGenerator(QObject *parent)
:QObject(parent), papers(QVector<VLayoutPaper>()), bank(new VBank()), paperHeight(0), paperWidth(0),
stopGeneration(false), state(LayoutErrors::NoError)
{}
//---------------------------------------------------------------------------------------------------------------------
VLayoutGenerator::~VLayoutGenerator()
{
delete bank;
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::SetDetails(const QVector<VLayoutDetail> &details)
{
bank->SetDetails(details);
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::SetLayoutWidth(qreal width)
{
bank->SetLayoutWidth(width);
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::SetCaseType(Cases caseType)
{
bank->SetCaseType(caseType);
}
//---------------------------------------------------------------------------------------------------------------------
int VLayoutGenerator::DetailsCount()
{
return bank->AllDetailsCount();
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::Generate()
{
stopGeneration = false;
papers.clear();
state = LayoutErrors::NoError;
emit Start();
if (bank->Prepare())
{
CheckDetailsSize();
do
{
if (stopGeneration)
{
state = LayoutErrors::ProcessStoped;
break;
}
VLayoutPaper paper(paperHeight, paperWidth);
if (bank->LeftArrange() > 0)
{
const int index = bank->GetTiket();
if (paper.ArrangeDetail(bank->GetDetail(index)))
{
bank->Arranged(index);
}
else
{
bank->NotArranged(index);
}
}
else
{
if (paper.Count() > 0)
{
papers.append(paper);
}
else
{
state = LayoutErrors::EmptyPaperError;
emit Error(state);
return;
}
}
} while (bank->AllDetailsCount() > 0);
}
else
{
state = LayoutErrors::PrepareLayoutError;
emit Error(state);
return;
}
}
//---------------------------------------------------------------------------------------------------------------------
LayoutErrors VLayoutGenerator::State() const
{
return state;
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::Abort()
{
stopGeneration = true;
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::CheckDetailsSize()
{
const QRectF rec = bank->GetBiggestBoundingRect();
if (rec.width() > paperWidth || rec.height() > paperHeight)
{
state = LayoutErrors::PaperSizeError;
emit Error(state);
stopGeneration = true;
}
}
//---------------------------------------------------------------------------------------------------------------------
int VLayoutGenerator::GetPaperWidth() const
{
return paperWidth;
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::SetPaperWidth(int value)
{
paperWidth = value;
}
//---------------------------------------------------------------------------------------------------------------------
int VLayoutGenerator::GetPaperHeight() const
{
return paperHeight;
}
//---------------------------------------------------------------------------------------------------------------------
void VLayoutGenerator::SetPaperHeight(int value)
{
paperHeight = value;
}

View File

@ -29,12 +29,55 @@
#ifndef VLAYOUTGENERATOR_H
#define VLAYOUTGENERATOR_H
#include <QObject>
class VLayoutGenerator
#include "vlayoutdef.h"
#include "vbank.h"
class VLayoutPaper;
class VLayoutDetail;
class VLayoutGenerator :public QObject
{
Q_OBJECT
public:
VLayoutGenerator();
VLayoutGenerator(QObject *parent = 0);
virtual ~VLayoutGenerator();
void SetDetails(const QVector<VLayoutDetail> &details);
void SetLayoutWidth(qreal width);
void SetCaseType(Cases caseType);
int DetailsCount();
int GetPaperHeight() const;
void SetPaperHeight(int value);
int GetPaperWidth() const;
void SetPaperWidth(int value);
void Generate();
LayoutErrors State() const;
signals:
void Start();
void Arranged(int count);
void Error(const LayoutErrors &state);
void Finished();
public slots:
void Abort();
private:
Q_DISABLE_COPY(VLayoutGenerator)
QVector<VLayoutPaper> papers;
VBank *bank;
int paperHeight;
int paperWidth;
bool stopGeneration;
LayoutErrors state;
void CheckDetailsSize();
};
#endif // VLAYOUTGENERATOR_H