From 0f747ede7ef1d667946d90a6513bb66ef780afc0 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 12 Feb 2016 20:10:01 +0200 Subject: [PATCH] The first implementation for tool Spline. --HG-- branch : feature --- src/app/valentina/xml/vpattern.cpp | 1 + .../vtools/dialogs/tools/dialogspline.cpp | 64 +++++++----- src/libs/vtools/dialogs/tools/dialogspline.h | 1 + .../vtools/dialogs/tools/dialogsplinepath.cpp | 14 +-- src/libs/vtools/dialogs/tools/dialogtool.h | 1 + .../drawTools/toolcurve/vabstractspline.cpp | 2 + .../drawTools/toolcurve/vabstractspline.h | 12 +-- .../tools/drawTools/toolcurve/vtoolspline.cpp | 42 ++++---- .../tools/drawTools/toolcurve/vtoolspline.h | 2 + .../drawTools/toolcurve/vtoolsplinepath.cpp | 41 ++++---- .../drawTools/toolcurve/vtoolsplinepath.h | 4 + .../vtools/visualization/vistoolspline.cpp | 99 +++++++++++++++++-- src/libs/vtools/visualization/vistoolspline.h | 19 ++++ .../visualization/vistoolsplinepath.cpp | 15 +-- .../vtools/visualization/vistoolsplinepath.h | 5 - .../vtools/visualization/visualization.cpp | 25 ++++- src/libs/vtools/visualization/visualization.h | 6 ++ src/libs/vwidgets/vcontrolpointspline.cpp | 69 +++++++++---- src/libs/vwidgets/vcontrolpointspline.h | 18 +++- src/libs/vwidgets/vmaingraphicsscene.cpp | 16 ++- src/libs/vwidgets/vmaingraphicsscene.h | 10 +- 21 files changed, 323 insertions(+), 143 deletions(-) diff --git a/src/app/valentina/xml/vpattern.cpp b/src/app/valentina/xml/vpattern.cpp index 9a1fdcb42..4c8f714de 100644 --- a/src/app/valentina/xml/vpattern.cpp +++ b/src/app/valentina/xml/vpattern.cpp @@ -42,6 +42,7 @@ #include "../vmisc/vsettings.h" #include "../qmuparser/qmuparsererror.h" #include "../vgeometry/varc.h" +#include "../vgeometry/vsplinepath.h" #include "../core/vapplication.h" #include "../vpatterndb/calculator.h" diff --git a/src/libs/vtools/dialogs/tools/dialogspline.cpp b/src/libs/vtools/dialogs/tools/dialogspline.cpp index 0af494282..ed6913d19 100644 --- a/src/libs/vtools/dialogs/tools/dialogspline.cpp +++ b/src/libs/vtools/dialogs/tools/dialogspline.cpp @@ -30,8 +30,10 @@ #include "ui_dialogspline.h" #include "../vgeometry/vpointf.h" +#include "../vgeometry/vspline.h" #include "../vpatterndb/vcontainer.h" #include "../../visualization/vistoolspline.h" + #include //--------------------------------------------------------------------------------------------------------------------- @@ -41,8 +43,7 @@ * @param parent parent widget */ DialogSpline::DialogSpline(const VContainer *data, const quint32 &toolId, QWidget *parent) - :DialogTool(data, toolId, parent), ui(new Ui::DialogSpline), angle1(0), angle2(0), kAsm1(1), kAsm2(1), - kCurve(1) + :DialogTool(data, toolId, parent), ui(new Ui::DialogSpline), angle1(0), angle2(0), kAsm1(1), kAsm2(1), kCurve(1) { ui->setupUi(this); InitOkCancelApply(ui); @@ -57,6 +58,13 @@ DialogSpline::DialogSpline(const VContainer *data, const quint32 &toolId, QWidge this, &DialogSpline::PointNameChanged); vis = new VisToolSpline(data); + auto path = qobject_cast(vis); + SCASSERT(path != nullptr); + + auto scene = qobject_cast(qApp->getCurrentScene()); + SCASSERT(scene != nullptr); + connect(scene, &VMainGraphicsScene::MouseLeftPressed, path, &VisToolSpline::MouseLeftPressed); + connect(scene, &VMainGraphicsScene::MouseLeftReleased, path, &VisToolSpline::MouseLeftReleased); } //--------------------------------------------------------------------------------------------------------------------- @@ -88,7 +96,7 @@ void DialogSpline::ChosenObject(quint32 id, const SceneObject &type) { if (type == SceneObject::Point) { - VisToolSpline *path = qobject_cast(vis); + auto *path = qobject_cast(vis); SCASSERT(path != nullptr); switch (number) @@ -96,7 +104,7 @@ void DialogSpline::ChosenObject(quint32 id, const SceneObject &type) case 0: if (SetObject(id, ui->comboBoxP1, tr("Select last point of curve"))) { - number++; + ++number; path->VisualMode(id); } break; @@ -104,29 +112,13 @@ void DialogSpline::ChosenObject(quint32 id, const SceneObject &type) { if (getCurrentObjectId(ui->comboBoxP1) != id) { - const QSharedPointer point = data->GeometricObject(id); - qint32 index = ui->comboBoxP4->findText(point->name()); - if ( index != -1 ) - { // -1 for not found - ui->comboBoxP4->setCurrentIndex(index); - emit ToolTip(""); - index = ui->comboBoxP1->currentIndex(); - quint32 p1Id = qvariant_cast(ui->comboBoxP1->itemData(index)); - - QPointF p1 = data->GeometricObject(p1Id)->toQPointF(); - QPointF p4 = data->GeometricObject(id)->toQPointF(); - - ui->spinBoxAngle1->setValue(static_cast(QLineF(p1, p4).angle())); - ui->spinBoxAngle2->setValue(static_cast(QLineF(p4, p1).angle())); + if (SetObject(id, ui->comboBoxP4, "")) + { + ++number; path->setObject4Id(id); path->RefreshGeometry(); prepare = true; - DialogAccepted(); - } - else - { - qWarning()<<"Can't find object by name"<name(); } } break; @@ -183,6 +175,32 @@ void DialogSpline::PointNameChanged() CheckState(); } +//--------------------------------------------------------------------------------------------------------------------- +void DialogSpline::ShowDialog(bool click) +{ + if (prepare && click) + { + const auto p1 = data->GeometricObject(getCurrentObjectId(ui->comboBoxP1)); + const auto p4 = data->GeometricObject(getCurrentObjectId(ui->comboBoxP4)); + + auto *path = qobject_cast(vis); + SCASSERT(path != nullptr); + + const QPointF p2 = path->GetP2(); + const QPointF p3 = path->GetP3(); + + VSpline spline(*p1, p2, p3, *p4, kCurve); + + ui->spinBoxAngle1->setValue(static_cast(spline.GetStartAngle())); + ui->spinBoxAngle2->setValue(static_cast(spline.GetEndAngle())); + + ui->doubleSpinBoxKasm1->setValue(spline.GetKasm1()); + ui->doubleSpinBoxKasm2->setValue(spline.GetKasm2()); + + DialogAccepted(); + } +} + //--------------------------------------------------------------------------------------------------------------------- void DialogSpline::ShowVisualization() { diff --git a/src/libs/vtools/dialogs/tools/dialogspline.h b/src/libs/vtools/dialogs/tools/dialogspline.h index ce36e4bb0..2d899aa64 100644 --- a/src/libs/vtools/dialogs/tools/dialogspline.h +++ b/src/libs/vtools/dialogs/tools/dialogspline.h @@ -72,6 +72,7 @@ public: public slots: virtual void ChosenObject(quint32 id, const SceneObject &type) Q_DECL_OVERRIDE; virtual void PointNameChanged() Q_DECL_OVERRIDE; + virtual void ShowDialog(bool click) Q_DECL_OVERRIDE; protected: virtual void ShowVisualization() Q_DECL_OVERRIDE; /** diff --git a/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp b/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp index 57b9a0bfd..2bb688d12 100644 --- a/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp +++ b/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp @@ -271,19 +271,7 @@ void DialogSplinePath::PathUpdated(const VSplinePath &path) //--------------------------------------------------------------------------------------------------------------------- void DialogSplinePath::ShowVisualization() { - if (prepare == false) - { - VMainGraphicsScene *scene = qobject_cast(qApp->getCurrentScene()); - SCASSERT(scene != nullptr); - - VisToolSplinePath *visPath = qobject_cast(vis); - SCASSERT(visPath != nullptr); - - connect(scene, &VMainGraphicsScene::NewFactor, visPath, &Visualization::SetFactor); - scene->addItem(visPath); - visPath->setMode(Mode::Show); - visPath->RefreshGeometry(); - } + AddVisualization(); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/dialogs/tools/dialogtool.h b/src/libs/vtools/dialogs/tools/dialogtool.h index c2cdec515..c95109718 100644 --- a/src/libs/vtools/dialogs/tools/dialogtool.h +++ b/src/libs/vtools/dialogs/tools/dialogtool.h @@ -349,6 +349,7 @@ inline void DialogTool::AddVisualization() scene->addItem(toolVis); } + toolVis->SetMode(Mode::Show); toolVis->RefreshGeometry(); } } diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp b/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp index ca01fb50b..6c32892c1 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp +++ b/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.cpp @@ -27,6 +27,8 @@ *************************************************************************/ #include "vabstractspline.h" +#include "../vwidgets/vcontrolpointspline.h" + #include const QString VAbstractSpline::TagName = QStringLiteral("spline"); diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.h b/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.h index 54129b110..e6ca060ba 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.h +++ b/src/libs/vtools/tools/drawTools/toolcurve/vabstractspline.h @@ -30,10 +30,11 @@ #define VABSTRACTSPLINE_H #include "../vdrawtool.h" -#include "../vwidgets/vcontrolpointspline.h" #include +class VControlPointSpline; + class VAbstractSpline:public VDrawTool, public QGraphicsPathItem { Q_OBJECT @@ -56,15 +57,6 @@ public slots: virtual void Disable(bool disable, const QString &namePP) Q_DECL_OVERRIDE; void DetailsMode(bool mode); signals: - /** - * @brief RefreshLine refresh control line. - * @param indexSpline position spline in spline list. - * @param pos position point in spline. - * @param controlPoint new position control point. - * @param splinePoint new position spline point. - */ - void RefreshLine(const qint32 &indexSpline, SplinePointPosition pos, - const QPointF &controlPoint, const QPointF &splinePoint); /** * @brief setEnabledPoint disable control points. * @param enable enable or diasable points. diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp b/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp index b2ef1b955..729b8b804 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp +++ b/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp @@ -31,6 +31,7 @@ #include "../../../dialogs/tools/dialogspline.h" #include "../../../undocommands/movespline.h" #include "../../../visualization/vistoolspline.h" +#include "../vwidgets/vcontrolpointspline.h" #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0) # include "../vmisc/vmath.h" @@ -64,23 +65,20 @@ VToolSpline::VToolSpline(VAbstractPattern *doc, VContainer *data, quint32 id, co this->setAcceptHoverEvents(true); this->setPath(ToolPath()); - const QSharedPointer spl = VAbstractTool::data.GeometricObject(id); - VControlPointSpline *controlPoint1 = new VControlPointSpline(1, SplinePointPosition::FirstPoint, spl->GetP2(), - spl->GetP1().toQPointF(), *data->GetPatternUnit(), - this); + const auto spl = VAbstractTool::data.GeometricObject(id); + + auto *controlPoint1 = new VControlPointSpline(1, SplinePointPosition::FirstPoint, spl->GetP2(), + spl->GetP1().toQPointF(), *data->GetPatternUnit(), this); connect(controlPoint1, &VControlPointSpline::ControlPointChangePosition, this, &VToolSpline::ControlPointChangePosition); - connect(this, &VToolSpline::RefreshLine, controlPoint1, &VControlPointSpline::RefreshLine); connect(this, &VToolSpline::setEnabledPoint, controlPoint1, &VControlPointSpline::setEnabledPoint); connect(controlPoint1, &VControlPointSpline::ShowContextMenu, this, &VToolSpline::contextMenuEvent); controlPoints.append(controlPoint1); - VControlPointSpline *controlPoint2 = new VControlPointSpline(1, SplinePointPosition::LastPoint, spl->GetP3(), - spl->GetP4().toQPointF(), *data->GetPatternUnit(), - this); + auto *controlPoint2 = new VControlPointSpline(1, SplinePointPosition::LastPoint, spl->GetP3(), + spl->GetP4().toQPointF(), *data->GetPatternUnit(), this); connect(controlPoint2, &VControlPointSpline::ControlPointChangePosition, this, &VToolSpline::ControlPointChangePosition); - connect(this, &VToolSpline::RefreshLine, controlPoint2, &VControlPointSpline::RefreshLine); connect(this, &VToolSpline::setEnabledPoint, controlPoint2, &VControlPointSpline::setEnabledPoint); connect(controlPoint2, &VControlPointSpline::ShowContextMenu, this, &VToolSpline::contextMenuEvent); controlPoints.append(controlPoint2); @@ -488,6 +486,7 @@ void VToolSpline::SetVisualization() visual->SetKAsm1(spl->GetKasm1()); visual->SetKAsm2(spl->GetKasm2()); visual->SetKCurve(spl->GetKcurve()); + visual->SetMode(Mode::Show); visual->RefreshGeometry(); } } @@ -499,7 +498,7 @@ void VToolSpline::SetVisualization() void VToolSpline::RefreshGeometry() { // Very important to disable control points. Without it the pogram can't move the curve. - foreach (VControlPointSpline *point, controlPoints) + foreach (auto *point, controlPoints) { point->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false); } @@ -515,26 +514,27 @@ void VToolSpline::RefreshGeometry() this->setPath(ToolPath()); } - const QSharedPointer spl = VAbstractTool::data.GeometricObject(id); - QPointF splinePoint = VAbstractTool::data.GeometricObject(spl->GetP1().id())->toQPointF(); - QPointF controlPoint = spl->GetP2(); - emit RefreshLine(1, SplinePointPosition::FirstPoint, controlPoint, splinePoint); - splinePoint = VAbstractTool::data.GeometricObject(spl->GetP4().id())->toQPointF(); - controlPoint = spl->GetP3(); - emit RefreshLine(1, SplinePointPosition::LastPoint, controlPoint, splinePoint); - controlPoints[0]->blockSignals(true); controlPoints[1]->blockSignals(true); - controlPoints[0]->setPos(spl->GetP2()); - controlPoints[1]->setPos(spl->GetP3()); + const auto spl = VAbstractTool::data.GeometricObject(id); + + { + const QPointF splinePoint = VAbstractTool::data.GeometricObject(spl->GetP1().id())->toQPointF(); + controlPoints[0]->RefreshCtrlPoint(1, SplinePointPosition::FirstPoint, spl->GetP2(), splinePoint); + } + + { + const QPointF splinePoint = VAbstractTool::data.GeometricObject(spl->GetP4().id())->toQPointF(); + controlPoints[1]->RefreshCtrlPoint(1, SplinePointPosition::LastPoint, spl->GetP3(), splinePoint); + } controlPoints[0]->blockSignals(false); controlPoints[1]->blockSignals(false); SetVisualization(); - foreach (VControlPointSpline *point, controlPoints) + foreach (auto *point, controlPoints) { point->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); } diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.h b/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.h index 19d4a937f..0204e4c69 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.h +++ b/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.h @@ -31,6 +31,8 @@ #include "vabstractspline.h" +class VSpline; + /** * @brief The VToolSpline class tool for creation spline. I mean bezier curve. */ diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp b/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp index adef7f429..04ec5fcb0 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp +++ b/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp @@ -30,6 +30,7 @@ #include "../../../dialogs/tools/dialogsplinepath.h" #include "../../../undocommands/movesplinepath.h" #include "../../../visualization/vistoolsplinepath.h" +#include "../vwidgets/vcontrolpointspline.h" #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0) # include "../vmisc/vmath.h" @@ -66,12 +67,10 @@ VToolSplinePath::VToolSplinePath(VAbstractPattern *doc, VContainer *data, quint3 for (qint32 i = 1; i<=splPath->Count(); ++i) { VSpline spl = splPath->GetSpline(i); - VControlPointSpline *controlPoint = new VControlPointSpline(i, SplinePointPosition::FirstPoint, spl.GetP2(), - spl.GetP1().toQPointF(), *data->GetPatternUnit(), - this); + auto *controlPoint = new VControlPointSpline(i, SplinePointPosition::FirstPoint, spl.GetP2(), + spl.GetP1().toQPointF(), *data->GetPatternUnit(), this); connect(controlPoint, &VControlPointSpline::ControlPointChangePosition, this, &VToolSplinePath::ControlPointChangePosition); - connect(this, &VToolSplinePath::RefreshLine, controlPoint, &VControlPointSpline::RefreshLine); connect(this, &VToolSplinePath::setEnabledPoint, controlPoint, &VControlPointSpline::setEnabledPoint); connect(controlPoint, &VControlPointSpline::ShowContextMenu, this, &VToolSplinePath::contextMenuEvent); controlPoints.append(controlPoint); @@ -80,7 +79,6 @@ VToolSplinePath::VToolSplinePath(VAbstractPattern *doc, VContainer *data, quint3 *data->GetPatternUnit(), this); connect(controlPoint, &VControlPointSpline::ControlPointChangePosition, this, &VToolSplinePath::ControlPointChangePosition); - connect(this, &VToolSplinePath::RefreshLine, controlPoint, &VControlPointSpline::RefreshLine); connect(this, &VToolSplinePath::setEnabledPoint, controlPoint, &VControlPointSpline::setEnabledPoint); connect(controlPoint, &VControlPointSpline::ShowContextMenu, this, &VToolSplinePath::contextMenuEvent); controlPoints.append(controlPoint); @@ -559,7 +557,7 @@ void VToolSplinePath::SetVisualization() QSharedPointer splPath = VAbstractTool::data.GeometricObject(id); visual->setPath(*splPath.data()); - visual->setMode(Mode::Show); + visual->SetMode(Mode::Show); visual->RefreshGeometry(); } } @@ -571,7 +569,7 @@ void VToolSplinePath::SetVisualization() void VToolSplinePath::RefreshGeometry() { // Very important to disable control points. Without it the pogram can't move the curve. - foreach (VControlPointSpline *point, controlPoints) + foreach (auto *point, controlPoints) { point->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false); } @@ -584,26 +582,29 @@ void VToolSplinePath::RefreshGeometry() { this->setPath(ToolPath()); } + this->setPen(QPen(CorrectColor(lineColor), qApp->toPixel(WidthHairLine(*VAbstractTool::data.GetPatternUnit()))/factor)); - const QSharedPointer splPath = VAbstractTool::data.GeometricObject(id); + + const auto splPath = VAbstractTool::data.GeometricObject(id); for (qint32 i = 1; i<=splPath->Count(); ++i) { - VSpline spl = splPath->GetSpline(i); - QPointF splinePoint = spl.GetP1().toQPointF(); - QPointF controlPoint = spl.GetP2(); - emit RefreshLine(i, SplinePointPosition::FirstPoint, controlPoint, splinePoint); - splinePoint = spl.GetP4().toQPointF(); - controlPoint = spl.GetP3(); - emit RefreshLine(i, SplinePointPosition::LastPoint, controlPoint, splinePoint); - - qint32 j = i*2; + const qint32 j = i*2; controlPoints[j-2]->blockSignals(true); controlPoints[j-1]->blockSignals(true); - controlPoints[j-2]->setPos(spl.GetP2()); - controlPoints[j-1]->setPos(spl.GetP3()); + const auto spl = splPath->GetSpline(i); + + { + const auto splinePoint = spl.GetP1().toQPointF(); + controlPoints[j-2]->RefreshCtrlPoint(i, SplinePointPosition::FirstPoint, spl.GetP2(), splinePoint); + } + + { + const auto splinePoint = spl.GetP4().toQPointF(); + controlPoints[j-1]->RefreshCtrlPoint(i, SplinePointPosition::LastPoint, spl.GetP3(), splinePoint); + } controlPoints[j-2]->blockSignals(false); controlPoints[j-1]->blockSignals(false); @@ -611,7 +612,7 @@ void VToolSplinePath::RefreshGeometry() SetVisualization(); - foreach (VControlPointSpline *point, controlPoints) + foreach (auto *point, controlPoints) { point->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); } diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.h b/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.h index f09e09233..ab6ca7db6 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.h +++ b/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.h @@ -31,6 +31,10 @@ #include "vabstractspline.h" +class VSplinePath; +class VSplinePoint; +class VSpline; + /** * @brief The VToolSplinePath class tool for creation spline path. */ diff --git a/src/libs/vtools/visualization/vistoolspline.cpp b/src/libs/vtools/visualization/vistoolspline.cpp index 229c959ed..91ebe9da6 100644 --- a/src/libs/vtools/visualization/vistoolspline.cpp +++ b/src/libs/vtools/visualization/vistoolspline.cpp @@ -30,17 +30,40 @@ #include "../../vgeometry/vpointf.h" #include "../../vgeometry/vspline.h" #include "../../vpatterndb/vcontainer.h" +#include "../vwidgets/vcontrolpointspline.h" const int EMPTY_ANGLE = -1; //--------------------------------------------------------------------------------------------------------------------- VisToolSpline::VisToolSpline(const VContainer *data, QGraphicsItem *parent) - : VisPath(data, parent), object4Id(NULL_ID), lineP1(nullptr), lineP4(nullptr), line(nullptr), angle1(EMPTY_ANGLE), - angle2(EMPTY_ANGLE), kAsm1(1), kAsm2(1), kCurve(1) + : VisPath(data, parent), + object4Id(NULL_ID), + lineP1(nullptr), + lineP4(nullptr), + line(nullptr), + angle1(EMPTY_ANGLE), + angle2(EMPTY_ANGLE), + kAsm1(1), + kAsm2(1), + kCurve(1), + isLeftMousePressed(false), + p2Selected(false), + p3Selected(false), + p2(), + p3(), + controlPoints() { lineP1 = InitPoint(supportColor, this); lineP4 = InitPoint(supportColor, this); //-V656 line = InitItem(mainColor, this); + + auto *controlPoint1 = new VControlPointSpline(1, SplinePointPosition::FirstPoint, *data->GetPatternUnit(), this); + controlPoint1->hide(); + controlPoints.append(controlPoint1); + + auto *controlPoint2 = new VControlPointSpline(1, SplinePointPosition::LastPoint, *data->GetPatternUnit(), this); + controlPoint2->hide(); + controlPoints.append(controlPoint2); } //--------------------------------------------------------------------------------------------------------------------- @@ -52,21 +75,54 @@ void VisToolSpline::RefreshGeometry() { if (object1Id > NULL_ID) { - const QSharedPointer first = Visualization::data->GeometricObject(object1Id); + const auto first = Visualization::data->GeometricObject(object1Id); DrawPoint(lineP1, first->toQPointF(), supportColor); + if (mode == Mode::Creation) + { + if (isLeftMousePressed && not p2Selected) + { + p2 = Visualization::scenePos; + controlPoints[0]->RefreshCtrlPoint(1, SplinePointPosition::FirstPoint, p2, first->toQPointF()); + controlPoints[0]->show(); + } + else + { + p2Selected = true; + } + } + if (object4Id <= NULL_ID) { - DrawLine(line, QLineF(first->toQPointF(), Visualization::scenePos), mainColor); + VSpline spline(*first, p2, Visualization::scenePos, VPointF(Visualization::scenePos), kCurve); + DrawPath(this, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap); } else { - const QSharedPointer second = Visualization::data->GeometricObject(object4Id); + const auto second = Visualization::data->GeometricObject(object4Id); DrawPoint(lineP4, second->toQPointF(), supportColor); + if (mode == Mode::Creation) + { + if (isLeftMousePressed && not p3Selected) + { + QLineF ctrlLine (second->toQPointF(), Visualization::scenePos); + ctrlLine.setAngle(ctrlLine.angle()+180); + p3 = ctrlLine.p2(); + + controlPoints[1]->RefreshCtrlPoint(1, SplinePointPosition::LastPoint, p3, second->toQPointF()); + controlPoints[1]->show(); + } + else + { + p3Selected = true; + } + } + if (qFuzzyCompare(angle1, EMPTY_ANGLE) || qFuzzyCompare(angle2, EMPTY_ANGLE)) { - DrawLine(line, QLineF(first->toQPointF(), second->toQPointF()), mainColor); + VSpline spline(*first, p2, p3, *second, kCurve); + DrawPath(this, spline.GetPath(PathDirection::Hide), mainColor, Qt::SolidLine, Qt::RoundCap); } else { @@ -112,3 +168,34 @@ void VisToolSpline::SetKCurve(const qreal &value) { kCurve = value; } + +//--------------------------------------------------------------------------------------------------------------------- +QPointF VisToolSpline::GetP2() const +{ + return p2; +} + +//--------------------------------------------------------------------------------------------------------------------- +QPointF VisToolSpline::GetP3() const +{ + return p3; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VisToolSpline::MouseLeftPressed() +{ + if (mode == Mode::Creation) + { + isLeftMousePressed = true; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VisToolSpline::MouseLeftReleased() +{ + if (mode == Mode::Creation) + { + isLeftMousePressed = false; + RefreshGeometry(); + } +} diff --git a/src/libs/vtools/visualization/vistoolspline.h b/src/libs/vtools/visualization/vistoolspline.h index fb6f780d3..ebd193cc8 100644 --- a/src/libs/vtools/visualization/vistoolspline.h +++ b/src/libs/vtools/visualization/vistoolspline.h @@ -31,6 +31,8 @@ #include "vispath.h" +class VControlPointSpline; + class VisToolSpline : public VisPath { Q_OBJECT @@ -47,8 +49,16 @@ public: void SetKAsm2(const qreal &value); void SetKCurve(const qreal &value); + QPointF GetP2() const; + QPointF GetP3() const; + virtual int type() const Q_DECL_OVERRIDE {return Type;} enum { Type = UserType + static_cast(Vis::ToolSpline)}; + +public slots: + void MouseLeftPressed(); + void MouseLeftReleased(); + protected: Q_DISABLE_COPY(VisToolSpline) quint32 object4Id; @@ -60,6 +70,15 @@ protected: qreal kAsm1; qreal kAsm2; qreal kCurve; + + bool isLeftMousePressed; + bool p2Selected; + bool p3Selected; + + QPointF p2; + QPointF p3; + + QVector controlPoints; }; #endif // VISTOOLSPLINE_H diff --git a/src/libs/vtools/visualization/vistoolsplinepath.cpp b/src/libs/vtools/visualization/vistoolsplinepath.cpp index d0f42833b..0e1af6107 100644 --- a/src/libs/vtools/visualization/vistoolsplinepath.cpp +++ b/src/libs/vtools/visualization/vistoolsplinepath.cpp @@ -30,8 +30,7 @@ //--------------------------------------------------------------------------------------------------------------------- VisToolSplinePath::VisToolSplinePath(const VContainer *data, QGraphicsItem *parent) - : VisPath(data, parent), points(QVector()), line(nullptr), path(VSplinePath()), - mode(Mode::Creation) + : VisPath(data, parent), points(QVector()), line(nullptr), path(VSplinePath()) { line = InitItem(mainColor, this); } @@ -107,18 +106,6 @@ VSplinePath VisToolSplinePath::getPath() return path; } -//--------------------------------------------------------------------------------------------------------------------- -Mode VisToolSplinePath::getMode() const -{ - return mode; -} - -//--------------------------------------------------------------------------------------------------------------------- -void VisToolSplinePath::setMode(const Mode &value) -{ - mode = value; -} - //--------------------------------------------------------------------------------------------------------------------- QGraphicsEllipseItem *VisToolSplinePath::getPoint(quint32 i) { diff --git a/src/libs/vtools/visualization/vistoolsplinepath.h b/src/libs/vtools/visualization/vistoolsplinepath.h index b1f69fb5d..d37178326 100644 --- a/src/libs/vtools/visualization/vistoolsplinepath.h +++ b/src/libs/vtools/visualization/vistoolsplinepath.h @@ -32,8 +32,6 @@ #include "vispath.h" #include "../../vgeometry/vsplinepath.h" -enum class Mode : char {Creation, Show}; - class VisToolSplinePath : public VisPath { Q_OBJECT @@ -49,8 +47,6 @@ public: virtual int type() const Q_DECL_OVERRIDE {return Type;} enum { Type = UserType + static_cast(Vis::ToolSplinePath)}; - Mode getMode() const; - void setMode(const Mode &value); signals: void PathChanged(const VSplinePath &path); @@ -59,7 +55,6 @@ protected: QVector points; QGraphicsLineItem *line; VSplinePath path; - Mode mode; QGraphicsEllipseItem * getPoint(quint32 i); }; diff --git a/src/libs/vtools/visualization/visualization.cpp b/src/libs/vtools/visualization/visualization.cpp index 021fe639a..a9f872d80 100644 --- a/src/libs/vtools/visualization/visualization.cpp +++ b/src/libs/vtools/visualization/visualization.cpp @@ -37,8 +37,16 @@ Q_LOGGING_CATEGORY(vVis, "v.visualization") //--------------------------------------------------------------------------------------------------------------------- Visualization::Visualization(const VContainer *data) - :QObject(), data(data), factor(VDrawTool::factor), scenePos(QPointF()), - mainColor(Qt::red), supportColor(Qt::magenta), lineStyle(Qt::SolidLine), object1Id(NULL_ID), toolTip(QString()) + :QObject(), + data(data), + factor(VDrawTool::factor), + scenePos(QPointF()), + mainColor(Qt::red), + supportColor(Qt::magenta), + lineStyle(Qt::SolidLine), + object1Id(NULL_ID), + toolTip(QString()), + mode(Mode::Creation) {} //--------------------------------------------------------------------------------------------------------------------- @@ -196,3 +204,16 @@ void Visualization::DrawPath(QGraphicsPathItem *pathItem, const QPainterPath &pa pathItem->setPath(path); pathItem->setVisible(true); } + + +//--------------------------------------------------------------------------------------------------------------------- +Mode Visualization::GetMode() const +{ + return mode; +} + +//--------------------------------------------------------------------------------------------------------------------- +void Visualization::SetMode(const Mode &value) +{ + mode = value; +} diff --git a/src/libs/vtools/visualization/visualization.h b/src/libs/vtools/visualization/visualization.h index a43f0bd36..e2cff633c 100644 --- a/src/libs/vtools/visualization/visualization.h +++ b/src/libs/vtools/visualization/visualization.h @@ -39,6 +39,8 @@ Q_DECLARE_LOGGING_CATEGORY(vVis) +enum class Mode : char {Creation, Show}; + class Visualization : public QObject { Q_OBJECT @@ -53,6 +55,9 @@ public: void setScenePos(const QPointF &value); virtual void VisualMode(const quint32 &pointId); void setMainColor(const QColor &value); + + Mode GetMode() const; + void SetMode(const Mode &value); signals: void ToolTip(const QString &toolTip); public slots: @@ -67,6 +72,7 @@ protected: Qt::PenStyle lineStyle; quint32 object1Id; QString toolTip; + Mode mode; virtual void InitPen()=0; virtual void AddOnScene()=0; diff --git a/src/libs/vwidgets/vcontrolpointspline.cpp b/src/libs/vwidgets/vcontrolpointspline.cpp index 1047608e9..2ed632242 100644 --- a/src/libs/vwidgets/vcontrolpointspline.cpp +++ b/src/libs/vwidgets/vcontrolpointspline.cpp @@ -36,6 +36,19 @@ #include "vmaingraphicsview.h" #include "vmaingraphicsscene.h" +//--------------------------------------------------------------------------------------------------------------------- +VControlPointSpline::VControlPointSpline(const qint32 &indexSpline, SplinePointPosition position, Unit patternUnit, + QGraphicsItem *parent) + :QGraphicsEllipseItem(parent), + radius(CircleRadius()), + controlLine(nullptr), + indexSpline(indexSpline), + position(position), + patternUnit(patternUnit) +{ + Init(); +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief VControlPointSpline constructor. @@ -48,28 +61,22 @@ VControlPointSpline::VControlPointSpline(const qint32 &indexSpline, SplinePointPosition position, const QPointF &controlPoint, const QPointF &splinePoint, Unit patternUnit, QGraphicsItem *parent) - :QGraphicsEllipseItem(parent), radius(0), controlLine(nullptr), indexSpline(indexSpline), position(position), + :QGraphicsEllipseItem(parent), + radius(CircleRadius()), + controlLine(nullptr), + indexSpline(indexSpline), + position(position), patternUnit(patternUnit) { - //create circle - radius = (1.5/*mm*/ / 25.4) * PrintDPI; - QRectF rec = QRectF(0, 0, radius*2, radius*2); - rec.translate(-rec.center().x(), -rec.center().y()); - this->setRect(rec); - this->setPen(QPen(Qt::black, ToPixel(WidthHairLine(patternUnit), patternUnit))); - this->setBrush(QBrush(Qt::NoBrush)); + Init(); + this->setFlag(QGraphicsItem::ItemIsSelectable, true); this->setFlag(QGraphicsItem::ItemIsMovable, true); this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); this->setAcceptHoverEvents(true); this->setPos(controlPoint); - this->setZValue(100); - QPointF p1, p2; - VGObject::LineIntersectCircle(QPointF(), radius, QLineF( QPointF(), splinePoint-controlPoint), p1, p2); - controlLine = new QGraphicsLineItem(QLineF(splinePoint-controlPoint, p1), this); - controlLine->setPen(QPen(Qt::red, ToPixel(WidthHairLine(patternUnit), patternUnit))); - controlLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true); + SetCtrlLine(controlPoint, splinePoint); } //--------------------------------------------------------------------------------------------------------------------- @@ -196,22 +203,44 @@ void VControlPointSpline::contextMenuEvent(QGraphicsSceneContextMenuEvent *event emit ShowContextMenu(event); } +//--------------------------------------------------------------------------------------------------------------------- +void VControlPointSpline::Init() +{ + auto rec = QRectF(0, 0, radius*2, radius*2); + rec.translate(-rec.center().x(), -rec.center().y()); + this->setRect(rec); + this->setPen(QPen(Qt::black, ToPixel(WidthHairLine(patternUnit), patternUnit))); + this->setBrush(QBrush(Qt::NoBrush)); + this->setZValue(100); + + controlLine = new QGraphicsLineItem(this); + controlLine->setPen(QPen(Qt::red, ToPixel(WidthHairLine(patternUnit), patternUnit))); + controlLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VControlPointSpline::SetCtrlLine(const QPointF &controlPoint, const QPointF &splinePoint) +{ + QPointF p1, p2; + VGObject::LineIntersectCircle(QPointF(), radius, QLineF( QPointF(), splinePoint-controlPoint), p1, p2); + controlLine->setLine(QLineF(splinePoint-controlPoint, p1)); +} + //--------------------------------------------------------------------------------------------------------------------- /** - * @brief RefreshLine refresh line control point. + * @brief RefreshCtrlPoint refresh the control point. * @param indexSpline index spline in list. * @param pos position point in spline. * @param controlPoint control point. * @param splinePoint spline point. */ -void VControlPointSpline::RefreshLine(const qint32 &indexSpline, SplinePointPosition pos, - const QPointF &controlPoint, const QPointF &splinePoint) +void VControlPointSpline::RefreshCtrlPoint(const qint32 &indexSpline, SplinePointPosition pos, + const QPointF &controlPoint, const QPointF &splinePoint) { if (this->indexSpline == indexSpline && this->position == pos) { - QPointF p1, p2; - VGObject::LineIntersectCircle(QPointF(), radius, QLineF( QPointF(), splinePoint-controlPoint), p1, p2); - controlLine->setLine(QLineF(splinePoint-controlPoint, p1)); + this->setPos(controlPoint); + SetCtrlLine(controlPoint, splinePoint); } } diff --git a/src/libs/vwidgets/vcontrolpointspline.h b/src/libs/vwidgets/vcontrolpointspline.h index 21b9eb629..a15e33756 100644 --- a/src/libs/vwidgets/vcontrolpointspline.h +++ b/src/libs/vwidgets/vcontrolpointspline.h @@ -41,6 +41,8 @@ class VControlPointSpline : public QObject, public QGraphicsEllipseItem { Q_OBJECT public: + VControlPointSpline(const qint32 &indexSpline, SplinePointPosition position, Unit patternUnit, + QGraphicsItem * parent = nullptr); VControlPointSpline(const qint32 &indexSpline, SplinePointPosition position, const QPointF &controlPoint, const QPointF &splinePoint, Unit patternUnit, QGraphicsItem * parent = nullptr); virtual ~VControlPointSpline() Q_DECL_OVERRIDE; @@ -63,12 +65,12 @@ signals: */ void ShowContextMenu(QGraphicsSceneContextMenuEvent *event); public slots: - void RefreshLine(const qint32 &indexSpline, SplinePointPosition pos, const QPointF &controlPoint, - const QPointF &splinePoint); + void RefreshCtrlPoint(const qint32 &indexSpline, SplinePointPosition pos, const QPointF &controlPoint, + const QPointF &splinePoint); void setEnabledPoint(bool enable); protected: /** @brief radius radius circle. */ - qreal radius; + const qreal radius; /** @brief controlLine pointer to line control point. */ QGraphicsLineItem *controlLine; @@ -88,6 +90,16 @@ private: SplinePointPosition position; Unit patternUnit; + + inline qreal CircleRadius() const; + void Init(); + void SetCtrlLine(const QPointF &controlPoint, const QPointF &splinePoint); }; +//--------------------------------------------------------------------------------------------------------------------- +qreal VControlPointSpline::CircleRadius() const +{ + return (1.5/*mm*/ / 25.4) * PrintDPI; +} + #endif // VCONTROLPOINTSPLINE_H diff --git a/src/libs/vwidgets/vmaingraphicsscene.cpp b/src/libs/vwidgets/vmaingraphicsscene.cpp index 2642ddc4f..b0c6f709a 100644 --- a/src/libs/vwidgets/vmaingraphicsscene.cpp +++ b/src/libs/vwidgets/vmaingraphicsscene.cpp @@ -74,7 +74,11 @@ void VMainGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event) */ void VMainGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { - emit mousePress(event->scenePos()); + if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) + { + emit MouseLeftPressed(); + } + QGraphicsScene::mousePressEvent(event); if (QGuiApplication::keyboardModifiers() == Qt::ControlModifier) { @@ -86,6 +90,16 @@ void VMainGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event) } } +//--------------------------------------------------------------------------------------------------------------------- +void VMainGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick) + { + emit MouseLeftReleased(); + } + QGraphicsScene::mouseReleaseEvent(event); +} + //--------------------------------------------------------------------------------------------------------------------- void VMainGraphicsScene::InitOrigins() { diff --git a/src/libs/vwidgets/vmaingraphicsscene.h b/src/libs/vwidgets/vmaingraphicsscene.h index faa9fbd4b..20526b9d2 100644 --- a/src/libs/vwidgets/vmaingraphicsscene.h +++ b/src/libs/vwidgets/vmaingraphicsscene.h @@ -61,17 +61,17 @@ public slots: protected: virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; + virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; signals: /** * @brief mouseMove send new mouse position. * @param scenePos new mouse position. */ void mouseMove(const QPointF &scenePos); - /** - * @brief mousePress send new mouse press position. - * @param scenePos new mouse press position. - */ - void mousePress(QPointF scenePos); + + void MouseLeftPressed(); + void MouseLeftReleased(); + /** * @brief ChoosedObject send option choosed object. * @param id object id.