From e8deb4b29c08dbeb48be558b9321fbf0b620fc35 Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 01:52:18 +0200 Subject: [PATCH 1/6] The user must select points in a clockwise direction --HG-- branch : feature --- src/libs/vlayout/vabstractdetail.cpp | 30 +++++++++++++++ src/libs/vlayout/vabstractdetail.h | 2 + src/libs/vlayout/vlayoutdetail.cpp | 25 +----------- .../vtools/dialogs/tools/dialogdetail.cpp | 38 +++++++++++++++++++ src/libs/vtools/dialogs/tools/dialogdetail.h | 1 + 5 files changed, 73 insertions(+), 23 deletions(-) diff --git a/src/libs/vlayout/vabstractdetail.cpp b/src/libs/vlayout/vabstractdetail.cpp index a3aad5ab1..a8bf32f66 100644 --- a/src/libs/vlayout/vabstractdetail.cpp +++ b/src/libs/vlayout/vabstractdetail.cpp @@ -506,3 +506,33 @@ QPointF VAbstractDetail::SingleParallelPoint(const QLineF &line, const qreal &an pLine.setLength( width ); return pLine.p2(); } + +//--------------------------------------------------------------------------------------------------------------------- +qreal VAbstractDetail::SumTrapezoids(int n, QVector x, QVector y) const +{ + // Calculation a polygon area through the sum of the areas of trapezoids + qreal s, res = 0; + + 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; + } + } + } + return res; +} diff --git a/src/libs/vlayout/vabstractdetail.h b/src/libs/vlayout/vabstractdetail.h index 94168f7c2..10ce5594e 100644 --- a/src/libs/vlayout/vabstractdetail.h +++ b/src/libs/vlayout/vabstractdetail.h @@ -65,6 +65,8 @@ public: void setWidth(const qreal &value); static QVector Equidistant(const QVector &points, const EquidistantType &eqv, qreal width); + qreal SumTrapezoids(int n, QVector x, QVector y) const; + protected: static QVector RemoveDublicates(const QVector &points); diff --git a/src/libs/vlayout/vlayoutdetail.cpp b/src/libs/vlayout/vlayoutdetail.cpp index 21fef345c..2b55a3c42 100644 --- a/src/libs/vlayout/vlayoutdetail.cpp +++ b/src/libs/vlayout/vlayoutdetail.cpp @@ -289,7 +289,6 @@ qint64 VLayoutDetail::Square() const } const int n = d->layoutAllowence.count(); - qreal s, res = 0; qint64 sq = 0; QVector x; @@ -301,28 +300,8 @@ qint64 VLayoutDetail::Square() const 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; - } - } - } + qreal res = this->SumTrapezoids(n, x, y); + sq = qFloor(qAbs(res/2.0)); return sq; } diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.cpp b/src/libs/vtools/dialogs/tools/dialogdetail.cpp index f0617c79b..6e485fd44 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.cpp +++ b/src/libs/vtools/dialogs/tools/dialogdetail.cpp @@ -473,6 +473,12 @@ bool DialogDetail::DetailIsValid() const } else { + if(not DetailIsClockwise()) + { + url += QString(" ") +tr("You have to choose points in a clockwise direction!"); + ui.helpLabel->setText(url); + return false; + } if (FirstPointEqualLast()) { url += QString(" ") +tr("First point can not equal the last point!"); @@ -518,3 +524,35 @@ bool DialogDetail::FirstPointEqualLast() const } return false; } + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogDetail::DetailIsClockwise() const +{ + VDetail detail; + if(ui.listWidget->count() < 3) + { + return true; + } + for (qint32 i = 0; i < ui.listWidget->count(); ++i) + { + QListWidgetItem *item = ui.listWidget->item(i); + detail.append( qvariant_cast(item->data(Qt::UserRole))); + } + QVector points = detail.ContourPoints(data); + + QVector x; + QVector y; + const int n = points.size(); + for (int i=0; i < n; ++i) + { + x.append(points.at(i).x()); + y.append(points.at(i).y()); + } + + qreal res = detail.SumTrapezoids(n, x, y); + if (res < 0) + { + return true; + } + return false; +} diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.h b/src/libs/vtools/dialogs/tools/dialogdetail.h index 9a8ec4314..6dcd5c43e 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.h +++ b/src/libs/vtools/dialogs/tools/dialogdetail.h @@ -78,6 +78,7 @@ private: bool flagWidth; bool DetailIsValid() const; bool FirstPointEqualLast() const; + bool DetailIsClockwise() const; void NewItem(quint32 id, const Tool &typeTool, const NodeDetail &typeNode, qreal mx = 0, qreal my = 0, bool reverse = false); From 40ef4732ca682ab4f5aeeaf19ffab120b9e581dd Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 03:01:56 +0200 Subject: [PATCH 2/6] Changed SumTrapezoids method to static --HG-- branch : feature --- src/libs/vlayout/vabstractdetail.cpp | 2 +- src/libs/vlayout/vabstractdetail.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/vlayout/vabstractdetail.cpp b/src/libs/vlayout/vabstractdetail.cpp index a8bf32f66..0483d6a6b 100644 --- a/src/libs/vlayout/vabstractdetail.cpp +++ b/src/libs/vlayout/vabstractdetail.cpp @@ -508,7 +508,7 @@ QPointF VAbstractDetail::SingleParallelPoint(const QLineF &line, const qreal &an } //--------------------------------------------------------------------------------------------------------------------- -qreal VAbstractDetail::SumTrapezoids(int n, QVector x, QVector y) const +qreal VAbstractDetail::SumTrapezoids(int n, QVector x, QVector y) { // Calculation a polygon area through the sum of the areas of trapezoids qreal s, res = 0; diff --git a/src/libs/vlayout/vabstractdetail.h b/src/libs/vlayout/vabstractdetail.h index 10ce5594e..67c147dc5 100644 --- a/src/libs/vlayout/vabstractdetail.h +++ b/src/libs/vlayout/vabstractdetail.h @@ -65,7 +65,7 @@ public: void setWidth(const qreal &value); static QVector Equidistant(const QVector &points, const EquidistantType &eqv, qreal width); - qreal SumTrapezoids(int n, QVector x, QVector y) const; + static qreal SumTrapezoids(int n, QVector x, QVector y); protected: From d679187f1594b546bf34321bc29e9801e4ea35c5 Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 15:58:31 +0200 Subject: [PATCH 3/6] Fixed SumTrapezoids method --HG-- branch : feature --- src/libs/vlayout/vabstractdetail.cpp | 28 +++++++++++-------- src/libs/vlayout/vabstractdetail.h | 2 +- src/libs/vlayout/vlayoutdetail.cpp | 17 ++--------- .../vtools/dialogs/tools/dialogdetail.cpp | 25 ++++++----------- 4 files changed, 28 insertions(+), 44 deletions(-) diff --git a/src/libs/vlayout/vabstractdetail.cpp b/src/libs/vlayout/vabstractdetail.cpp index 0483d6a6b..dc13d85ee 100644 --- a/src/libs/vlayout/vabstractdetail.cpp +++ b/src/libs/vlayout/vabstractdetail.cpp @@ -508,29 +508,33 @@ QPointF VAbstractDetail::SingleParallelPoint(const QLineF &line, const qreal &an } //--------------------------------------------------------------------------------------------------------------------- -qreal VAbstractDetail::SumTrapezoids(int n, QVector x, QVector y) +qreal VAbstractDetail::SumTrapezoids(const QVector &points) { // Calculation a polygon area through the sum of the areas of trapezoids qreal s, res = 0; + const int n = points.size(); - for (int i = 0; i < n; ++i) + if(n > 2) { - if (i == 0) + for (int i = 0; i < n; ++i) { - 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) + if (i == 0) { - s = x.at(i)*(y.at(i-1) - y.at(0)); // if i == n-1, then y[i+1] replace on y[0] + s = points.at(i).x()*(points.at(n-1).y() - points.at(i+1).y()); //if i == 0, then y[i-1] replace on y[n-1] res += s; } else { - s = x.at(i)*(y.at(i-1) - y.at(i+1)); - res += s; + if (i == n-1) + { + s = points.at(i).x()*(points.at(i-1).y() - points.at(0).y()); // if i == n-1, then y[i+1] replace on y[0] + res += s; + } + else + { + s = points.at(i).x()*(points.at(i-1).y() - points.at(i+1).y()); + res += s; + } } } } diff --git a/src/libs/vlayout/vabstractdetail.h b/src/libs/vlayout/vabstractdetail.h index 67c147dc5..f8aad520a 100644 --- a/src/libs/vlayout/vabstractdetail.h +++ b/src/libs/vlayout/vabstractdetail.h @@ -65,7 +65,7 @@ public: void setWidth(const qreal &value); static QVector Equidistant(const QVector &points, const EquidistantType &eqv, qreal width); - static qreal SumTrapezoids(int n, QVector x, QVector y); + static qreal SumTrapezoids(const QVector &points); protected: diff --git a/src/libs/vlayout/vlayoutdetail.cpp b/src/libs/vlayout/vlayoutdetail.cpp index 2b55a3c42..e04ca02c9 100644 --- a/src/libs/vlayout/vlayoutdetail.cpp +++ b/src/libs/vlayout/vlayoutdetail.cpp @@ -288,21 +288,10 @@ qint64 VLayoutDetail::Square() const return 0; } - const int n = d->layoutAllowence.count(); - qint64 sq = 0; + const QVector points = d->layoutAllowence; + const qreal res = SumTrapezoids(points); - QVector x; - QVector y; - - for (int i=0; i < n; ++i) - { - x.append(d->layoutAllowence.at(i).x()); - y.append(d->layoutAllowence.at(i).y()); - } - - qreal res = this->SumTrapezoids(n, x, y); - - sq = qFloor(qAbs(res/2.0)); + const qint64 sq = qFloor(qAbs(res/2.0)); return sq; } diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.cpp b/src/libs/vtools/dialogs/tools/dialogdetail.cpp index 6e485fd44..939da6c5e 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.cpp +++ b/src/libs/vtools/dialogs/tools/dialogdetail.cpp @@ -463,11 +463,11 @@ bool DialogDetail::DetailIsValid() const QByteArray byteArray; QBuffer buffer(&byteArray); pixmap.save(&buffer, "PNG"); - QString url = QString(""; + QString url = QString("" + QString(" "); if (ui.listWidget->count() < 3) { - url += QString(" ") + tr("You need more points!"); + url += tr("You need more points!"); ui.helpLabel->setText(url); return false; } @@ -475,13 +475,13 @@ bool DialogDetail::DetailIsValid() const { if(not DetailIsClockwise()) { - url += QString(" ") +tr("You have to choose points in a clockwise direction!"); + url += tr("You have to choose points in a clockwise direction!"); ui.helpLabel->setText(url); return false; } if (FirstPointEqualLast()) { - url += QString(" ") +tr("First point can not equal the last point!"); + url += tr("First point can not equal the last point!"); ui.helpLabel->setText(url); return false; } @@ -494,7 +494,7 @@ bool DialogDetail::DetailIsValid() const if (QString::compare(previousRow, nextRow) == 0) { - url += QString(" ") +tr("You have double points!"); + url += tr("You have double points!"); ui.helpLabel->setText(url); return false; } @@ -528,28 +528,19 @@ bool DialogDetail::FirstPointEqualLast() const //--------------------------------------------------------------------------------------------------------------------- bool DialogDetail::DetailIsClockwise() const { - VDetail detail; if(ui.listWidget->count() < 3) { return true; } + VDetail detail; for (qint32 i = 0; i < ui.listWidget->count(); ++i) { QListWidgetItem *item = ui.listWidget->item(i); detail.append( qvariant_cast(item->data(Qt::UserRole))); } - QVector points = detail.ContourPoints(data); + const QVector points = detail.ContourPoints(data); - QVector x; - QVector y; - const int n = points.size(); - for (int i=0; i < n; ++i) - { - x.append(points.at(i).x()); - y.append(points.at(i).y()); - } - - qreal res = detail.SumTrapezoids(n, x, y); + const qreal res = VDetail::SumTrapezoids(points); if (res < 0) { return true; From 6c653163f9124386b078237171f318a5ff703859 Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 16:36:55 +0200 Subject: [PATCH 4/6] Erased QString(" ") --HG-- branch : feature --- src/libs/vtools/dialogs/tools/dialogdetail.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.cpp b/src/libs/vtools/dialogs/tools/dialogdetail.cpp index 939da6c5e..9b1347a9b 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.cpp +++ b/src/libs/vtools/dialogs/tools/dialogdetail.cpp @@ -463,7 +463,7 @@ bool DialogDetail::DetailIsValid() const QByteArray byteArray; QBuffer buffer(&byteArray); pixmap.save(&buffer, "PNG"); - QString url = QString("" + QString(" "); + QString url = QString(" "; if (ui.listWidget->count() < 3) { From 3e9793dbbbec738e837ec1709f07a458ac12ab7c Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 16:47:50 +0200 Subject: [PATCH 5/6] We don't need variable points in method Square --HG-- branch : feature --- src/libs/vlayout/vlayoutdetail.cpp | 3 +-- src/libs/vtools/dialogs/tools/dialogdetail.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/vlayout/vlayoutdetail.cpp b/src/libs/vlayout/vlayoutdetail.cpp index e04ca02c9..1c887948b 100644 --- a/src/libs/vlayout/vlayoutdetail.cpp +++ b/src/libs/vlayout/vlayoutdetail.cpp @@ -288,8 +288,7 @@ qint64 VLayoutDetail::Square() const return 0; } - const QVector points = d->layoutAllowence; - const qreal res = SumTrapezoids(points); + const qreal res = SumTrapezoids(d->layoutAllowence); const qint64 sq = qFloor(qAbs(res/2.0)); return sq; diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.cpp b/src/libs/vtools/dialogs/tools/dialogdetail.cpp index 9b1347a9b..e99ae3384 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.cpp +++ b/src/libs/vtools/dialogs/tools/dialogdetail.cpp @@ -463,7 +463,7 @@ bool DialogDetail::DetailIsValid() const QByteArray byteArray; QBuffer buffer(&byteArray); pixmap.save(&buffer, "PNG"); - QString url = QString(" "; + QString url = QString(" "); if (ui.listWidget->count() < 3) { From d575ad5ad3c8a4cbe1ed7da8f74bb000e50029b4 Mon Sep 17 00:00:00 2001 From: Valentina Zhuravska Date: Sun, 22 Nov 2015 16:58:58 +0200 Subject: [PATCH 6/6] Return false if we have less than 3 points --HG-- branch : feature --- src/libs/vtools/dialogs/tools/dialogdetail.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/vtools/dialogs/tools/dialogdetail.cpp b/src/libs/vtools/dialogs/tools/dialogdetail.cpp index e99ae3384..2d75a9443 100644 --- a/src/libs/vtools/dialogs/tools/dialogdetail.cpp +++ b/src/libs/vtools/dialogs/tools/dialogdetail.cpp @@ -530,7 +530,7 @@ bool DialogDetail::DetailIsClockwise() const { if(ui.listWidget->count() < 3) { - return true; + return false; } VDetail detail; for (qint32 i = 0; i < ui.listWidget->count(); ++i)