Working with Spline Path.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2016-02-16 14:00:56 +02:00
parent be3fc296f4
commit c36f1eb8ef
8 changed files with 125 additions and 34 deletions

View File

@ -319,6 +319,7 @@ QVector<VSplinePoint> VSplinePath::GetSplinePath() const
void VSplinePath::Clear() void VSplinePath::Clear()
{ {
d->path.clear(); d->path.clear();
SetDuplicate(0);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -52,6 +52,8 @@ DialogSpline::DialogSpline(const VContainer *data, const quint32 &toolId, QWidge
FillComboBoxPoints(ui->comboBoxP4); FillComboBoxPoints(ui->comboBoxP4);
FillComboBoxLineColors(ui->comboBoxColor); FillComboBoxLineColors(ui->comboBoxColor);
CheckState();
connect(ui->comboBoxP1, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), connect(ui->comboBoxP1, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
this, &DialogSpline::PointNameChanged); this, &DialogSpline::PointNameChanged);
connect(ui->comboBoxP4, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), connect(ui->comboBoxP4, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
@ -132,14 +134,7 @@ void DialogSpline::SaveData()
const quint32 d = spl.GetDuplicate();//Save previous value const quint32 d = spl.GetDuplicate();//Save previous value
spl = VSpline(*GetP1(), *GetP4(), angle1, angle2, kAsm1, kAsm2, kCurve); spl = VSpline(*GetP1(), *GetP4(), angle1, angle2, kAsm1, kAsm2, kCurve);
if (newDuplicate <= -1) newDuplicate <= -1 ? spl.SetDuplicate(d) : spl.SetDuplicate(static_cast<quint32>(newDuplicate));
{
spl.SetDuplicate(d);
}
else
{
spl.SetDuplicate(static_cast<quint32>(newDuplicate));
}
auto path = qobject_cast<VisToolSpline *>(vis); auto path = qobject_cast<VisToolSpline *>(vis);
SCASSERT(path != nullptr); SCASSERT(path != nullptr);
@ -167,20 +162,6 @@ const QSharedPointer<VPointF> DialogSpline::GetP4() const
return data->GeometricObject<VPointF>(getCurrentObjectId(ui->comboBoxP4)); return data->GeometricObject<VPointF>(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() void DialogSpline::PointNameChanged()
{ {
@ -194,7 +175,7 @@ void DialogSpline::PointNameChanged()
flagError = false; flagError = false;
color = errorColor; color = errorColor;
ui->lineEditSplineName->setText(QString()); ui->lineEditSplineName->setText(tr("Invalid spline"));
} }
else else
{ {
@ -216,11 +197,11 @@ void DialogSpline::PointNameChanged()
{ {
newDuplicate = DNumber(spline.name()); newDuplicate = DNumber(spline.name());
spline.SetDuplicate(newDuplicate); spline.SetDuplicate(newDuplicate);
ui->lineEditSplineName->setText(spline.name());
} }
ui->lineEditSplineName->setText(spline.name());
} }
} }
ChangeColor(ui->labelName, color);
ChangeColor(ui->labelFirstPoint, color); ChangeColor(ui->labelFirstPoint, color);
ChangeColor(ui->labelSecondPoint, color); ChangeColor(ui->labelSecondPoint, color);
CheckState(); CheckState();

View File

@ -75,8 +75,6 @@ private:
const QSharedPointer<VPointF> GetP1() const; const QSharedPointer<VPointF> GetP1() const;
const QSharedPointer<VPointF> GetP4() const; const QSharedPointer<VPointF> GetP4() const;
quint32 DNumber(const QString &baseName) const;
}; };
#endif // DIALOGSPLINE_H #endif // DIALOGSPLINE_H

View File

@ -39,7 +39,7 @@
* @param parent parent widget * @param parent parent widget
*/ */
DialogSplinePath::DialogSplinePath(const VContainer *data, const quint32 &toolId, QWidget *parent) 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); ui->setupUi(this);
InitOkCancelApply(ui); InitOkCancelApply(ui);
@ -94,6 +94,7 @@ void DialogSplinePath::SetPath(const VSplinePath &value)
} }
ui->listWidget->setFocus(Qt::OtherFocusReason); ui->listWidget->setFocus(Qt::OtherFocusReason);
ui->doubleSpinBoxKcurve->setValue(path.GetKCurve()); ui->doubleSpinBoxKcurve->setValue(path.GetKCurve());
ui->lineEditSplPathName->setText(path.name());
auto visPath = qobject_cast<VisToolSplinePath *>(vis); auto visPath = qobject_cast<VisToolSplinePath *>(vis);
SCASSERT(visPath != nullptr); SCASSERT(visPath != nullptr);
@ -123,7 +124,7 @@ void DialogSplinePath::ChosenObject(quint32 id, const SceneObject &type)
{ {
if (type == SceneObject::Point) if (type == SceneObject::Point)
{ {
if (path.CountPoint() >= 2 && path.at(path.CountPoint()-1).P().id() == id) if (AllIds().contains(id))
{ {
return; return;
} }
@ -153,7 +154,9 @@ void DialogSplinePath::ChosenObject(quint32 id, const SceneObject &type)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void DialogSplinePath::SaveData() void DialogSplinePath::SaveData()
{ {
const quint32 d = path.GetDuplicate();//Save previous value
SavePath(); SavePath();
newDuplicate <= -1 ? path.SetDuplicate(d) : path.SetDuplicate(static_cast<quint32>(newDuplicate));
auto visPath = qobject_cast<VisToolSplinePath *>(vis); auto visPath = qobject_cast<VisToolSplinePath *>(vis);
SCASSERT(visPath != nullptr); SCASSERT(visPath != nullptr);
@ -195,6 +198,45 @@ void DialogSplinePath::currentPointChanged(int index)
DataPoint(p.P().id(), p.KAsm1(), p.Angle1(), p.KAsm2(), p.Angle2()); DataPoint(p.P().id(), p.KAsm1(), p.Angle1(), p.KAsm2(), p.Angle2());
EnableFields(); EnableFields();
item->setData(Qt::UserRole, QVariant::fromValue(p)); 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<VSplinePoint>(ui->listWidget->item(0)->data(Qt::UserRole));
auto last = qvariant_cast<VSplinePoint>(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) if (path.CountPoint() >= 3)
{ {
emit ToolTip(""); emit ToolTip("");
if (not data->IsUnique(path.name()))
{
path.SetDuplicate(DNumber(path.name()));
}
DialogAccepted(); DialogAccepted();
} }
} }
@ -368,10 +416,39 @@ void DialogSplinePath::EnableFields()
void DialogSplinePath::SavePath() void DialogSplinePath::SavePath()
{ {
path.Clear(); path.Clear();
path = ExtractPath();
}
//---------------------------------------------------------------------------------------------------------------------
QSet<quint32> DialogSplinePath::AllIds() const
{
QSet<quint32> ids;
for (qint32 i = 0; i < ui->listWidget->count(); ++i) for (qint32 i = 0; i < ui->listWidget->count(); ++i)
{ {
QListWidgetItem *item = ui->listWidget->item(i); ids.insert(qvariant_cast<VSplinePoint>(ui->listWidget->item(i)->data(Qt::UserRole)).P().id());
path.append( qvariant_cast<VSplinePoint>(item->data(Qt::UserRole)));
} }
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<VSplinePoint>(ui->listWidget->item(i)->data(Qt::UserRole)));
}
return path;
} }

View File

@ -77,10 +77,15 @@ private:
/** @brief path spline path */ /** @brief path spline path */
VSplinePath path; VSplinePath path;
qint32 newDuplicate;
void NewItem(quint32 id, qreal kAsm1, qreal angle1, qreal kAsm2, qreal angle2); 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 DataPoint(quint32 id, qreal kAsm1, qreal angle1, qreal kAsm2, qreal angle2);
void EnableFields(); void EnableFields();
void SavePath(); void SavePath();
QSet<quint32> AllIds() const;
bool IsPathValid() const;
VSplinePath ExtractPath() const;
}; };
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>528</width> <width>528</width>
<height>322</height> <height>353</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -28,7 +28,7 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<item> <item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="labelPoint">
<property name="text"> <property name="text">
<string>Point of curve</string> <string>Point of curve</string>
</property> </property>
@ -196,6 +196,20 @@
<item row="1" column="1"> <item row="1" column="1">
<widget class="QComboBox" name="comboBoxColor"/> <widget class="QComboBox" name="comboBoxColor"/>
</item> </item>
<item row="2" column="0">
<widget class="QLabel" name="labelName">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditSplPathName">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>

View File

@ -311,6 +311,20 @@ bool DialogTool::eventFilter(QObject *object, QEvent *event)
return false; 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 * @brief ValFormulaChanged handle change formula

View File

@ -255,6 +255,7 @@ protected:
virtual void SaveData() {} virtual void SaveData() {}
void MoveCursorToEnd(QPlainTextEdit *plainTextEdit); void MoveCursorToEnd(QPlainTextEdit *plainTextEdit);
bool eventFilter(QObject *object, QEvent *event); bool eventFilter(QObject *object, QEvent *event);
quint32 DNumber(const QString &baseName) const;
private: private:
void FillList(QComboBox *box, const QMap<QString, quint32> &list)const; void FillList(QComboBox *box, const QMap<QString, quint32> &list)const;