From c36f1eb8ef75406c2f698bdc20a022786042d113 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 16 Feb 2016 14:00:56 +0200 Subject: [PATCH] Working with Spline Path. --HG-- branch : feature --- src/libs/vgeometry/vsplinepath.cpp | 1 + .../vtools/dialogs/tools/dialogspline.cpp | 31 ++----- src/libs/vtools/dialogs/tools/dialogspline.h | 2 - .../vtools/dialogs/tools/dialogsplinepath.cpp | 87 +++++++++++++++++-- .../vtools/dialogs/tools/dialogsplinepath.h | 5 ++ .../vtools/dialogs/tools/dialogsplinepath.ui | 18 +++- src/libs/vtools/dialogs/tools/dialogtool.cpp | 14 +++ src/libs/vtools/dialogs/tools/dialogtool.h | 1 + 8 files changed, 125 insertions(+), 34 deletions(-) diff --git a/src/libs/vgeometry/vsplinepath.cpp b/src/libs/vgeometry/vsplinepath.cpp index 214bd2c63..bde1dc103 100644 --- a/src/libs/vgeometry/vsplinepath.cpp +++ b/src/libs/vgeometry/vsplinepath.cpp @@ -319,6 +319,7 @@ QVector VSplinePath::GetSplinePath() const void VSplinePath::Clear() { d->path.clear(); + SetDuplicate(0); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/dialogs/tools/dialogspline.cpp b/src/libs/vtools/dialogs/tools/dialogspline.cpp index a276e781a..578bd12d5 100644 --- a/src/libs/vtools/dialogs/tools/dialogspline.cpp +++ b/src/libs/vtools/dialogs/tools/dialogspline.cpp @@ -52,6 +52,8 @@ DialogSpline::DialogSpline(const VContainer *data, const quint32 &toolId, QWidge FillComboBoxPoints(ui->comboBoxP4); FillComboBoxLineColors(ui->comboBoxColor); + CheckState(); + connect(ui->comboBoxP1, static_cast(&QComboBox::currentIndexChanged), this, &DialogSpline::PointNameChanged); connect(ui->comboBoxP4, static_cast(&QComboBox::currentIndexChanged), @@ -132,14 +134,7 @@ void DialogSpline::SaveData() const quint32 d = spl.GetDuplicate();//Save previous value spl = VSpline(*GetP1(), *GetP4(), angle1, angle2, kAsm1, kAsm2, kCurve); - if (newDuplicate <= -1) - { - spl.SetDuplicate(d); - } - else - { - spl.SetDuplicate(static_cast(newDuplicate)); - } + newDuplicate <= -1 ? spl.SetDuplicate(d) : spl.SetDuplicate(static_cast(newDuplicate)); auto path = qobject_cast(vis); SCASSERT(path != nullptr); @@ -167,20 +162,6 @@ const QSharedPointer DialogSpline::GetP4() const return data->GeometricObject(getCurrentObjectId(ui->comboBoxP4)); } -//--------------------------------------------------------------------------------------------------------------------- -quint32 DialogSpline::DNumber(const QString &baseName) const -{ - quint32 num = 1; - QString name; - do - { - name = baseName + QString("_%1").arg(num); - ++num; - } while (not data->IsUnique(name)); - - return num; -} - //--------------------------------------------------------------------------------------------------------------------- void DialogSpline::PointNameChanged() { @@ -194,7 +175,7 @@ void DialogSpline::PointNameChanged() flagError = false; color = errorColor; - ui->lineEditSplineName->setText(QString()); + ui->lineEditSplineName->setText(tr("Invalid spline")); } else { @@ -216,11 +197,11 @@ void DialogSpline::PointNameChanged() { newDuplicate = DNumber(spline.name()); spline.SetDuplicate(newDuplicate); - - ui->lineEditSplineName->setText(spline.name()); } + ui->lineEditSplineName->setText(spline.name()); } } + ChangeColor(ui->labelName, color); ChangeColor(ui->labelFirstPoint, color); ChangeColor(ui->labelSecondPoint, color); CheckState(); diff --git a/src/libs/vtools/dialogs/tools/dialogspline.h b/src/libs/vtools/dialogs/tools/dialogspline.h index 066f796a1..ab7160b5b 100644 --- a/src/libs/vtools/dialogs/tools/dialogspline.h +++ b/src/libs/vtools/dialogs/tools/dialogspline.h @@ -75,8 +75,6 @@ private: const QSharedPointer GetP1() const; const QSharedPointer GetP4() const; - - quint32 DNumber(const QString &baseName) const; }; #endif // DIALOGSPLINE_H diff --git a/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp b/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp index dde11226a..26f87d18a 100644 --- a/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp +++ b/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp @@ -39,7 +39,7 @@ * @param parent parent widget */ DialogSplinePath::DialogSplinePath(const VContainer *data, const quint32 &toolId, QWidget *parent) - :DialogTool(data, toolId, parent), ui(new Ui::DialogSplinePath), path(VSplinePath()) + :DialogTool(data, toolId, parent), ui(new Ui::DialogSplinePath), path(VSplinePath()), newDuplicate(-1) { ui->setupUi(this); InitOkCancelApply(ui); @@ -94,6 +94,7 @@ void DialogSplinePath::SetPath(const VSplinePath &value) } ui->listWidget->setFocus(Qt::OtherFocusReason); ui->doubleSpinBoxKcurve->setValue(path.GetKCurve()); + ui->lineEditSplPathName->setText(path.name()); auto visPath = qobject_cast(vis); SCASSERT(visPath != nullptr); @@ -123,7 +124,7 @@ void DialogSplinePath::ChosenObject(quint32 id, const SceneObject &type) { if (type == SceneObject::Point) { - if (path.CountPoint() >= 2 && path.at(path.CountPoint()-1).P().id() == id) + if (AllIds().contains(id)) { return; } @@ -153,7 +154,9 @@ void DialogSplinePath::ChosenObject(quint32 id, const SceneObject &type) //--------------------------------------------------------------------------------------------------------------------- void DialogSplinePath::SaveData() { + const quint32 d = path.GetDuplicate();//Save previous value SavePath(); + newDuplicate <= -1 ? path.SetDuplicate(d) : path.SetDuplicate(static_cast(newDuplicate)); auto visPath = qobject_cast(vis); SCASSERT(visPath != nullptr); @@ -195,6 +198,45 @@ void DialogSplinePath::currentPointChanged(int index) DataPoint(p.P().id(), p.KAsm1(), p.Angle1(), p.KAsm2(), p.Angle2()); EnableFields(); item->setData(Qt::UserRole, QVariant::fromValue(p)); + item->setText(p.P().name()); + + QColor color = okColor; + if (not IsPathValid()) + { + flagError = false; + color = errorColor; + + ui->lineEditSplPathName->setText(tr("Invalid spline path")); + } + else + { + flagError = true; + color = okColor; + + auto first = qvariant_cast(ui->listWidget->item(0)->data(Qt::UserRole)); + auto last = qvariant_cast(ui->listWidget->item(ui->listWidget->count()-1)->data(Qt::UserRole)); + + if (first.P().id() == path.at(0).P().id() && last.P().id() == path.at(path.CountPoint()-1).P().id()) + { + newDuplicate = -1; + ui->lineEditSplPathName->setText(path.name()); + } + else + { + VSplinePath newPath = ExtractPath(); + + if (not data->IsUnique(newPath.name())) + { + newDuplicate = DNumber(newPath.name()); + newPath.SetDuplicate(newDuplicate); + } + + ui->lineEditSplPathName->setText(newPath.name()); + } + } + ChangeColor(ui->labelName, color); + ChangeColor(ui->labelPoint, color); + CheckState(); } //--------------------------------------------------------------------------------------------------------------------- @@ -265,6 +307,12 @@ void DialogSplinePath::ShowDialog(bool click) if (path.CountPoint() >= 3) { emit ToolTip(""); + + if (not data->IsUnique(path.name())) + { + path.SetDuplicate(DNumber(path.name())); + } + DialogAccepted(); } } @@ -368,10 +416,39 @@ void DialogSplinePath::EnableFields() void DialogSplinePath::SavePath() { path.Clear(); + path = ExtractPath(); +} + +//--------------------------------------------------------------------------------------------------------------------- +QSet DialogSplinePath::AllIds() const +{ + QSet ids; for (qint32 i = 0; i < ui->listWidget->count(); ++i) { - QListWidgetItem *item = ui->listWidget->item(i); - path.append( qvariant_cast(item->data(Qt::UserRole))); + ids.insert(qvariant_cast(ui->listWidget->item(i)->data(Qt::UserRole)).P().id()); } - path.SetKCurve(ui->doubleSpinBoxKcurve->value()); + + return ids; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogSplinePath::IsPathValid() const +{ + if (path.CountPoint() < 3) + { + return false; + } + + return (AllIds().size() == path.CountPoint()); +} + +//--------------------------------------------------------------------------------------------------------------------- +VSplinePath DialogSplinePath::ExtractPath() const +{ + VSplinePath path(ui->doubleSpinBoxKcurve->value()); + for (qint32 i = 0; i < ui->listWidget->count(); ++i) + { + path.append( qvariant_cast(ui->listWidget->item(i)->data(Qt::UserRole))); + } + return path; } diff --git a/src/libs/vtools/dialogs/tools/dialogsplinepath.h b/src/libs/vtools/dialogs/tools/dialogsplinepath.h index c10146364..b992a697f 100644 --- a/src/libs/vtools/dialogs/tools/dialogsplinepath.h +++ b/src/libs/vtools/dialogs/tools/dialogsplinepath.h @@ -77,10 +77,15 @@ private: /** @brief path spline path */ VSplinePath path; + qint32 newDuplicate; + void NewItem(quint32 id, qreal kAsm1, qreal angle1, qreal kAsm2, qreal angle2); void DataPoint(quint32 id, qreal kAsm1, qreal angle1, qreal kAsm2, qreal angle2); void EnableFields(); void SavePath(); + QSet AllIds() const; + bool IsPathValid() const; + VSplinePath ExtractPath() const; }; //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/dialogs/tools/dialogsplinepath.ui b/src/libs/vtools/dialogs/tools/dialogsplinepath.ui index 1942bc658..140fe7fac 100644 --- a/src/libs/vtools/dialogs/tools/dialogsplinepath.ui +++ b/src/libs/vtools/dialogs/tools/dialogsplinepath.ui @@ -7,7 +7,7 @@ 0 0 528 - 322 + 353 @@ -28,7 +28,7 @@ - + Point of curve @@ -196,6 +196,20 @@ + + + + Name: + + + + + + + true + + + diff --git a/src/libs/vtools/dialogs/tools/dialogtool.cpp b/src/libs/vtools/dialogs/tools/dialogtool.cpp index 5cff1eb04..fe1040147 100644 --- a/src/libs/vtools/dialogs/tools/dialogtool.cpp +++ b/src/libs/vtools/dialogs/tools/dialogtool.cpp @@ -311,6 +311,20 @@ bool DialogTool::eventFilter(QObject *object, QEvent *event) return false; } +//--------------------------------------------------------------------------------------------------------------------- +quint32 DialogTool::DNumber(const QString &baseName) const +{ + quint32 num = 0; + QString name; + do + { + ++num; + name = baseName + QString("_%1").arg(num); + } while (not data->IsUnique(name)); + + return num; +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief ValFormulaChanged handle change formula diff --git a/src/libs/vtools/dialogs/tools/dialogtool.h b/src/libs/vtools/dialogs/tools/dialogtool.h index c95109718..a715bb1f0 100644 --- a/src/libs/vtools/dialogs/tools/dialogtool.h +++ b/src/libs/vtools/dialogs/tools/dialogtool.h @@ -255,6 +255,7 @@ protected: virtual void SaveData() {} void MoveCursorToEnd(QPlainTextEdit *plainTextEdit); bool eventFilter(QObject *object, QEvent *event); + quint32 DNumber(const QString &baseName) const; private: void FillList(QComboBox *box, const QMap &list)const;