From 98d5dde2e1e4c527a6f8b439f94fe29430cb21c8 Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 12 Jan 2015 14:12:15 +0200 Subject: [PATCH] Class that drive all process, also speak with outside world. --HG-- branch : feature --- src/libs/vlayout/vbank.cpp | 41 +++++++- src/libs/vlayout/vbank.h | 5 + src/libs/vlayout/vlayoutdef.h | 9 ++ src/libs/vlayout/vlayoutgenerator.cpp | 142 +++++++++++++++++++++++++- src/libs/vlayout/vlayoutgenerator.h | 49 ++++++++- 5 files changed, 240 insertions(+), 6 deletions(-) diff --git a/src/libs/vlayout/vbank.cpp b/src/libs/vlayout/vbank.cpp index 15fb5ce79..08323bf9a 100644 --- a/src/libs/vlayout/vbank.cpp +++ b/src/libs/vlayout/vbank.cpp @@ -31,12 +31,13 @@ #include #include +#include //--------------------------------------------------------------------------------------------------------------------- VBank::VBank() :details(QVector()), unsorted(QHash()), big(QHash()), middle(QHash()), small(QHash()), 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::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()); } diff --git a/src/libs/vlayout/vbank.h b/src/libs/vlayout/vbank.h index da8bb5edb..316826585 100644 --- a/src/libs/vlayout/vbank.h +++ b/src/libs/vlayout/vbank.h @@ -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 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 diff --git a/src/libs/vlayout/vlayoutdef.h b/src/libs/vlayout/vlayoutdef.h index 25d09a59d..ecadc2977 100644 --- a/src/libs/vlayout/vlayoutdef.h +++ b/src/libs/vlayout/vlayoutdef.h @@ -31,4 +31,13 @@ enum class EquidistantType : char { OpenEquidistant, CloseEquidistant }; +enum class LayoutErrors : char +{ + NoError, + PrepareLayoutError, + PaperSizeError, + ProcessStoped, + EmptyPaperError +}; + #endif // VLAYOUTDEF_H diff --git a/src/libs/vlayout/vlayoutgenerator.cpp b/src/libs/vlayout/vlayoutgenerator.cpp index 2f719e99b..b4e6782df 100644 --- a/src/libs/vlayout/vlayoutgenerator.cpp +++ b/src/libs/vlayout/vlayoutgenerator.cpp @@ -27,8 +27,148 @@ *************************************************************************/ #include "vlayoutgenerator.h" +#include "vlayoutpaper.h" +#include "vlayoutdetail.h" +#include -VLayoutGenerator::VLayoutGenerator() +//--------------------------------------------------------------------------------------------------------------------- +VLayoutGenerator::VLayoutGenerator(QObject *parent) + :QObject(parent), papers(QVector()), bank(new VBank()), paperHeight(0), paperWidth(0), + stopGeneration(false), state(LayoutErrors::NoError) +{} + +//--------------------------------------------------------------------------------------------------------------------- +VLayoutGenerator::~VLayoutGenerator() { + delete bank; } + +//--------------------------------------------------------------------------------------------------------------------- +void VLayoutGenerator::SetDetails(const QVector &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; +} + diff --git a/src/libs/vlayout/vlayoutgenerator.h b/src/libs/vlayout/vlayoutgenerator.h index 191446010..b70fab62e 100644 --- a/src/libs/vlayout/vlayoutgenerator.h +++ b/src/libs/vlayout/vlayoutgenerator.h @@ -29,12 +29,55 @@ #ifndef VLAYOUTGENERATOR_H #define VLAYOUTGENERATOR_H +#include -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 &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 papers; + VBank *bank; + int paperHeight; + int paperWidth; + bool stopGeneration; + LayoutErrors state; + + void CheckDetailsSize(); }; #endif // VLAYOUTGENERATOR_H