Several bug fixes.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2016-03-19 16:20:09 +02:00
parent 30ece7e856
commit 505cec891d
12 changed files with 345 additions and 54 deletions

View File

@ -214,15 +214,15 @@ void VAbstractCubicBezierPath::CreateName()
{
name = splPath;
name.append(QString("_%1").arg(FirstPoint().name()));
if (CountSubSpl() >= 1)
if (CountSubSpl() >= 2)
{
name.append(QString("_%1").arg(LastPoint().name()));
}
if (GetDuplicate() > 0)
{
name += QString("_%1").arg(GetDuplicate());
}
}
}
setName(name);
}

View File

@ -109,7 +109,7 @@ void VCubicBezierPath::append(const VPointF &point)
//---------------------------------------------------------------------------------------------------------------------
qint32 VCubicBezierPath::CountSubSpl() const
{
return qFloor(qAbs((d->path.size() - 4) / 3 + 1));
return CountSubSpl(d->path.size());
}
//---------------------------------------------------------------------------------------------------------------------
@ -138,7 +138,7 @@ VSpline VCubicBezierPath::GetSpline(qint32 index) const
throw VException(tr("This spline does not exist."));
}
const qint32 base = (index - 1) * 3;
const qint32 base = SubSplOffset(index);
// Correction the first control point of each next spline curve except for the first.
QPointF p2 = d->path.at(base + 1).toQPointF();
@ -189,6 +189,38 @@ QVector<VPointF> VCubicBezierPath::GetSplinePath() const
return d->path;
}
//---------------------------------------------------------------------------------------------------------------------
qint32 VCubicBezierPath::CountSubSpl(qint32 size)
{
if (size <= 0)
{
return 0;
}
return qFloor(qAbs((size - 4) / 3.0 + 1));
}
//---------------------------------------------------------------------------------------------------------------------
qint32 VCubicBezierPath::SubSplOffset(qint32 subSplIndex)
{
if (subSplIndex <= 0)
{
return -1;
}
return (subSplIndex - 1) * 3;
}
//---------------------------------------------------------------------------------------------------------------------
qint32 VCubicBezierPath::SubSplPointsCount(qint32 countSubSpl)
{
if (countSubSpl <= 0)
{
return 0;
}
return ((countSubSpl - 1) * 3 + 4);
}
//---------------------------------------------------------------------------------------------------------------------
VPointF VCubicBezierPath::FirstPoint() const
{
@ -208,8 +240,7 @@ VPointF VCubicBezierPath::LastPoint() const
const qint32 count = CountSubSpl();
if (count >= 1)
{
const qint32 base = (count - 1) * 3;
return d->path.at(base + 3);// Take last point of the last real spline
return d->path.at(SubSplOffset(count) + 3);// Take last point of the last real spline
}
else
{

View File

@ -61,6 +61,10 @@ public:
virtual qreal GetEndAngle () const Q_DECL_OVERRIDE;
QVector<VPointF> GetSplinePath() const;
static qint32 CountSubSpl(qint32 size);
static qint32 SubSplOffset(qint32 subSplIndex);
static qint32 SubSplPointsCount(qint32 countSubSpl);
protected:
virtual VPointF FirstPoint() const Q_DECL_OVERRIDE;
virtual VPointF LastPoint() const Q_DECL_OVERRIDE;

View File

@ -133,7 +133,7 @@ void VSplinePath::append(const VSplinePoint &point)
*/
qint32 VSplinePath::CountSubSpl() const
{
if (d->path.size() == 0)
if (d->path.isEmpty())
{
return 0;
}

View File

@ -107,7 +107,7 @@ void DialogCubicBezierPath::ChosenObject(quint32 id, const SceneObject &type)
{
if (type == SceneObject::Point)
{
if (AllIds().contains(id))
if (AllPathBackboneIds().contains(id))
{
return;
}
@ -138,9 +138,10 @@ void DialogCubicBezierPath::ShowDialog(bool click)
{
if (click == false)
{
if (path.CountPoints() >= 7)
const int size = path.CountPoints();
if (size >= 7)
{
if (path.CountPoints() - ((path.CountSubSpl() - 1) * 3 + 4) == 0)
if (size - VCubicBezierPath::SubSplPointsCount(path.CountSubSpl()) == 0)
{// Accept only if all subpaths are completed
emit ToolTip("");
@ -269,7 +270,7 @@ void DialogCubicBezierPath::SavePath()
}
//---------------------------------------------------------------------------------------------------------------------
QSet<quint32> DialogCubicBezierPath::AllIds() const
QSet<quint32> DialogCubicBezierPath::AllPathBackboneIds() const
{
QVector<quint32> points;
for (qint32 i = 0; i < ui->listWidget->count(); ++i)
@ -278,10 +279,10 @@ QSet<quint32> DialogCubicBezierPath::AllIds() const
}
QSet<quint32> ids;
const qint32 count = qFloor(qAbs((points.size() - 4) / 3 + 1));// Count subpaths
for (qint32 i = 0; i < count; ++i)
const qint32 count = VCubicBezierPath::CountSubSpl(points.size());// Count subpaths
for (qint32 i = 1; i <= count; ++i)
{
const qint32 base = (i - 1) * 3;
const qint32 base = VCubicBezierPath::SubSplOffset(i);
ids.insert(points.at(base));// The first subpath's point
ids.insert(points.at(base + 3));// The last subpath's point
}
@ -297,7 +298,7 @@ bool DialogCubicBezierPath::IsPathValid() const
return false;
}
return (AllIds().size() == path.CountSubSpl() + 1);
return (AllPathBackboneIds().size() == path.CountSubSpl() + 1);
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -72,7 +72,7 @@ private:
void NewItem(const VPointF &point);
void DataPoint(const VPointF &p);
void SavePath();
QSet<quint32> AllIds() const;
QSet<quint32> AllPathBackboneIds() const;
bool IsPathValid() const;
VCubicBezierPath ExtractPath() const;
};

View File

@ -56,11 +56,11 @@ VisToolCubicBezierPath::~VisToolCubicBezierPath()
//---------------------------------------------------------------------------------------------------------------------
void VisToolCubicBezierPath::RefreshGeometry()
{
if (path.CountPoints() > 0)
{
const QVector<VPointF> pathPoints = path.GetSplinePath();
const int size = pathPoints.size();
const int countSubSpl = path.CountSubSpl();
if (size > 0)
{
const int countSubSpl = VCubicBezierPath::CountSubSpl(size);
for (int i = 0; i < size; ++i)
{
@ -70,6 +70,21 @@ void VisToolCubicBezierPath::RefreshGeometry()
if (mode == Mode::Creation)
{
if (countSubSpl < 1)
{
Creating(pathPoints, size-1);
}
else
{
const qint32 last = VCubicBezierPath::SubSplOffset(countSubSpl) + 3;
Creating(pathPoints, size-1-last);
}
}
if (countSubSpl >= 1)
{
DrawPath(this, path.GetPath(PathDirection::Show), mainColor, Qt::SolidLine, Qt::RoundCap);
for (qint32 i = 1; i<=countSubSpl; ++i)
{
const int preLastPoint = (countSubSpl - 1) * 2;
@ -89,29 +104,9 @@ void VisToolCubicBezierPath::RefreshGeometry()
QGraphicsEllipseItem *p3 = this->getPoint(ctrlPoints, static_cast<unsigned>(lastPoint));
DrawPoint(p3, spl.GetP3(), Qt::green);
}
const qint32 last = (countSubSpl - 1) * 3 + 3;
Creating(pathPoints, size-1-last);
}
if (countSubSpl >= 1)
{
DrawPath(this, path.GetPath(PathDirection::Show), mainColor, Qt::SolidLine, Qt::RoundCap);
}
if (countSubSpl < 7)
{
Visualization::toolTip = tr("<b>Curved path</b>: select seven or more points");
}
else if (countSubSpl >= 7 && size - ((countSubSpl - 1) * 3 + 4) == 0)
{
Visualization::toolTip = tr("<b>Curved path</b>: select seven or more points, "
"<b>Enter</b> - finish creation");
}
else
{
Visualization::toolTip = tr("<b>Curved path</b>: select more points for complete segment");
}
RefreshToolTip();
}
}
@ -119,6 +114,8 @@ void VisToolCubicBezierPath::RefreshGeometry()
void VisToolCubicBezierPath::setPath(const VCubicBezierPath &value)
{
path = value;
RefreshToolTip();
}
//---------------------------------------------------------------------------------------------------------------------
@ -163,44 +160,121 @@ QGraphicsLineItem *VisToolCubicBezierPath::getLine(quint32 i)
//---------------------------------------------------------------------------------------------------------------------
void VisToolCubicBezierPath::Creating(const QVector<VPointF> &pathPoints, int pointsLeft)
{
if (pathPoints.isEmpty() || pathPoints.size()+1 < pointsLeft)
const int size = pathPoints.size();
if (pathPoints.isEmpty() || size+1 < pointsLeft)
{
return;
}
int subSplPoints = 0;
const int subSplCount = VCubicBezierPath::CountSubSpl(size);
if (subSplCount >= 1)
{
subSplPoints = VCubicBezierPath::SubSplPointsCount(subSplCount)-1;
}
switch(pointsLeft)
{
case 0:
{
const QPointF p1 = pathPoints.last().toQPointF();
DrawLine(helpLine1, QLineF(p1, Visualization::scenePos), mainColor, Qt::DashLine);
const VPointF p1 = pathPoints.last();
if (pathPoints.size() >= 4)
{
QLineF p1p2(p1.toQPointF(), Visualization::scenePos);
QLineF prP3p1(pathPoints.at(size-2).toQPointF(), p1.toQPointF());
p1p2.setAngle(prP3p1.angle());
const QPointF p2 = p1p2.p2();
VSpline spline(p1, p2, Visualization::scenePos, VPointF(Visualization::scenePos));
DrawPath(newCurveSegment, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap);
DrawLine(helpLine1, p1p2, mainColor, Qt::DashLine);
const int preLastPoint = subSplCount * 2;
QGraphicsEllipseItem *p2Ctrl = this->getPoint(ctrlPoints, static_cast<unsigned>(preLastPoint));
DrawPoint(p2Ctrl, p2, Qt::green);
}
else
{
DrawLine(helpLine1, QLineF(p1.toQPointF(), Visualization::scenePos), mainColor, Qt::DashLine);
}
break;
}
case 1:
{
const VPointF p1 = pathPoints.at(pointsLeft-1);
const QPointF p2 = pathPoints.at(pointsLeft).toQPointF();
const VPointF p1 = pathPoints.at(subSplPoints + pointsLeft-1);
QPointF p2 = pathPoints.at(subSplPoints + pointsLeft).toQPointF();
if (subSplCount >= 1)
{
QLineF p1p2(p1.toQPointF(), p2);
QLineF prP3p1(pathPoints.at(subSplPoints + pointsLeft-2).toQPointF(), p1.toQPointF());
p1p2.setAngle(prP3p1.angle());
p2 = p1p2.p2();
}
DrawLine(helpLine1, QLineF(p1.toQPointF(), p2), mainColor, Qt::DashLine);
VSpline spline(p1, p2, Visualization::scenePos, VPointF(Visualization::scenePos));
DrawPath(newCurveSegment, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap);
const int preLastPoint = subSplCount * 2;
QGraphicsEllipseItem *p2Ctrl = this->getPoint(ctrlPoints, static_cast<unsigned>(preLastPoint));
DrawPoint(p2Ctrl, p2, Qt::green);
break;
}
case 2:
{
const VPointF p1 = pathPoints.at(pointsLeft-2);
const QPointF p2 = pathPoints.at(pointsLeft-1).toQPointF();
const QPointF p3 = pathPoints.at(pointsLeft).toQPointF();
const VPointF p1 = pathPoints.at(subSplPoints + pointsLeft-2);
QPointF p2 = pathPoints.at(subSplPoints + pointsLeft-1).toQPointF();
const QPointF p3 = pathPoints.at(subSplPoints + pointsLeft).toQPointF();
if (subSplCount >= 1)
{
QLineF p1p2(p1.toQPointF(), p2);
QLineF prP3p1(pathPoints.at(subSplPoints + pointsLeft-3).toQPointF(), p1.toQPointF());
p1p2.setAngle(prP3p1.angle());
p2 = p1p2.p2();
}
DrawLine(helpLine1, QLineF(p1.toQPointF(), p2), mainColor, Qt::DashLine);
DrawLine(helpLine2, QLineF(p3, Visualization::scenePos), mainColor, Qt::DashLine);
VSpline spline(p1, p2, p3, VPointF(Visualization::scenePos));
DrawPath(newCurveSegment, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap);
const int preLastPoint = subSplCount * 2;
QGraphicsEllipseItem *p2Ctrl = this->getPoint(ctrlPoints, static_cast<unsigned>(preLastPoint));
DrawPoint(p2Ctrl, p2, Qt::green);
break;
}
default:
break;
}
}
//---------------------------------------------------------------------------------------------------------------------
void VisToolCubicBezierPath::RefreshToolTip()
{
const int size = path.CountPoints();
if (size > 0)
{
const int countSubSpl = VCubicBezierPath::CountSubSpl(size);
if (size < 7)
{
Visualization::toolTip = tr("<b>Curved path</b>: select seven or more points");
}
else if (size >= 7 && size - VCubicBezierPath::SubSplPointsCount(countSubSpl) == 0)
{
Visualization::toolTip = tr("<b>Curved path</b>: select seven or more points, "
"<b>Enter</b> - finish creation");
}
else
{
Visualization::toolTip = tr("<b>Curved path</b>: select more points for complete segment");
}
emit ToolTip(Visualization::toolTip);
}
}

View File

@ -57,9 +57,11 @@ protected:
QGraphicsLineItem *helpLine1;
QGraphicsLineItem *helpLine2;
private:
QGraphicsEllipseItem *getPoint(QVector<QGraphicsEllipseItem *> &points, quint32 i, qreal z = 0);
QGraphicsLineItem *getLine(quint32 i);
void Creating(const QVector<VPointF> &pathPoints , int pointsLeft);
void RefreshToolTip();
};
#endif // VISTOOLCUBICBEZIERPATH_H

View File

@ -56,7 +56,8 @@ SOURCES += \
tst_vdetail.cpp \
tst_vabstractcurve.cpp \
tst_findpoint.cpp \
tst_vellipticalarc.cpp
tst_vellipticalarc.cpp \
tst_vcubicbezierpath.cpp
HEADERS += \
tst_vposter.h \
@ -80,7 +81,8 @@ HEADERS += \
tst_vdetail.h \
tst_vabstractcurve.h \
tst_findpoint.h \
tst_vellipticalarc.h
tst_vellipticalarc.h \
tst_vcubicbezierpath.h
# Set using ccache. Function enable_ccache() defined in common.pri.
$$enable_ccache()

View File

@ -48,6 +48,7 @@
#include "tst_vdetail.h"
#include "tst_findpoint.h"
#include "tst_vabstractcurve.h"
#include "tst_vcubicbezierpath.h"
int main(int argc, char** argv)
{
@ -82,6 +83,7 @@ int main(int argc, char** argv)
ASSERT_TEST(new TST_VCommandLine());
ASSERT_TEST(new TST_TSTranslation());
ASSERT_TEST(new TST_VAbstractCurve());
ASSERT_TEST(new TST_VCubicBezierPath());
return status;
}

View File

@ -0,0 +1,122 @@
/************************************************************************
**
** @file tst_vcubicbezierpath.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 19 3, 2016
**
** @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) 2016 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 "tst_vcubicbezierpath.h"
#include "../vgeometry/vcubicbezierpath.h"
#include <QtTest>
//---------------------------------------------------------------------------------------------------------------------
TST_VCubicBezierPath::TST_VCubicBezierPath(QObject *parent) :
QObject(parent)
{
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VCubicBezierPath::TestCountSubSpl_data() const
{
QTest::addColumn<qint32>("points");
QTest::addColumn<qint32>("countSubSpl");
QTest::newRow("Empty") << 0 << 0;
QTest::newRow("1 point") << 1 << 0;
QTest::newRow("2 points") << 2 << 0;
QTest::newRow("3 points") << 3 << 0;
QTest::newRow("4 points") << 4 << 1;
QTest::newRow("5 points") << 5 << 1;
QTest::newRow("6 points") << 6 << 1;
QTest::newRow("7 points") << 7 << 2;
QTest::newRow("8 points") << 8 << 2;
QTest::newRow("9 points") << 9 << 2;
QTest::newRow("10 points") << 10 << 3;
QTest::newRow("11 points") << 11 << 3;
QTest::newRow("12 points") << 12 << 3;
QTest::newRow("13 points") << 13 << 4;
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VCubicBezierPath::TestCountSubSpl() const
{
QFETCH(qint32, points);
QFETCH(qint32, countSubSpl);
const qint32 res = VCubicBezierPath::CountSubSpl(points);
QCOMPARE(res, countSubSpl);
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VCubicBezierPath::TestSubSplOffset_data() const
{
QTest::addColumn<qint32>("subSplIndex");
QTest::addColumn<qint32>("offset");
QTest::newRow("Wrong index") << -1 << -1;
QTest::newRow("Wrong index") << 0 << -1;
QTest::newRow("1 subSpl") << 1 << 0;
QTest::newRow("2 subSpls") << 2 << 3;
QTest::newRow("3 subSpls") << 3 << 6;
QTest::newRow("4 subSpls") << 4 << 9;
QTest::newRow("5 subSpls") << 5 << 12;
QTest::newRow("6 subSpls") << 6 << 15;
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VCubicBezierPath::TestSubSplOffset() const
{
QFETCH(qint32, subSplIndex);
QFETCH(qint32, offset);
const qint32 res = VCubicBezierPath::SubSplOffset(subSplIndex);
QCOMPARE(res, offset);
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VCubicBezierPath::TestSubSplPointsCount_data() const
{
QTest::addColumn<qint32>("countSubSpl");
QTest::addColumn<qint32>("points");
QTest::newRow("Wrong count") << -1 << 0;
QTest::newRow("Wrong count") << 0 << 0;
QTest::newRow("1 subSpl") << 1 << 4;
QTest::newRow("2 subSpls") << 2 << 7;
QTest::newRow("3 subSpls") << 3 << 10;
QTest::newRow("4 subSpls") << 4 << 13;
QTest::newRow("5 subSpls") << 5 << 16;
QTest::newRow("6 subSpls") << 6 << 19;
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VCubicBezierPath::TestSubSplPointsCount() const
{
QFETCH(qint32, countSubSpl);
QFETCH(qint32, points);
const qint32 res = VCubicBezierPath::SubSplPointsCount(countSubSpl);
QCOMPARE(res, points);
}

View File

@ -0,0 +1,53 @@
/************************************************************************
**
** @file tst_vcubicbezierpath.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 19 3, 2016
**
** @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) 2016 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/>.
**
*************************************************************************/
#ifndef TST_VCUBICBEZIERPATH_H
#define TST_VCUBICBEZIERPATH_H
#include <QObject>
class TST_VCubicBezierPath : public QObject
{
Q_OBJECT
public:
explicit TST_VCubicBezierPath(QObject *parent = nullptr);
private slots:
void TestCountSubSpl_data() const;
void TestCountSubSpl() const;
void TestSubSplOffset_data() const;
void TestSubSplOffset() const;
void TestSubSplPointsCount_data() const;
void TestSubSplPointsCount() const;
private:
Q_DISABLE_COPY(TST_VCubicBezierPath)
};
#endif // TST_VCUBICBEZIERPATH_H