valentina/src/libs/vtools/dialogs/tools/dialogsplinepath.cpp

455 lines
16 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file dialogsplinepath.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date November 15, 2013
**
** @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) 2013-2015 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/>.
**
*************************************************************************/
2013-08-09 08:49:34 +02:00
#include "dialogsplinepath.h"
#include "ui_dialogsplinepath.h"
#include "../vgeometry/vsplinepoint.h"
#include "../vpatterndb/vcontainer.h"
#include "../../visualization/vistoolsplinepath.h"
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief DialogSplinePath create dialog
* @param data container with data
* @param parent parent widget
*/
DialogSplinePath::DialogSplinePath(const VContainer *data, const quint32 &toolId, QWidget *parent)
:DialogTool(data, toolId, parent), ui(new Ui::DialogSplinePath), path(VSplinePath()), newDuplicate(-1)
{
2013-08-09 08:49:34 +02:00
ui->setupUi(this);
InitOkCancelApply(ui);
bOk->setEnabled(false);
2013-08-09 08:49:34 +02:00
FillComboBoxPoints(ui->comboBoxPoint);
FillComboBoxLineColors(ui->comboBoxColor);
2013-08-09 08:49:34 +02:00
connect(ui->listWidget, &QListWidget::currentRowChanged, this, &DialogSplinePath::PointChanged);
2013-08-09 08:49:34 +02:00
connect(ui->comboBoxPoint, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &DialogSplinePath::currentPointChanged);
connect(ui->doubleSpinBoxAngle1, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
2013-08-09 08:49:34 +02:00
this, &DialogSplinePath::Angle1Changed);
connect(ui->doubleSpinBoxAngle2, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
2013-08-09 08:49:34 +02:00
this, &DialogSplinePath::Angle2Changed);
connect(ui->doubleSpinBoxKasm1, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogSplinePath::KAsm1Changed);
connect(ui->doubleSpinBoxKasm2, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogSplinePath::KAsm2Changed);
vis = new VisToolSplinePath(data);
auto path = qobject_cast<VisToolSplinePath *>(vis);
SCASSERT(path != nullptr);
auto scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
SCASSERT(scene != nullptr);
connect(scene, &VMainGraphicsScene::MouseLeftPressed, path, &VisToolSplinePath::MouseLeftPressed);
connect(scene, &VMainGraphicsScene::MouseLeftReleased, path, &VisToolSplinePath::MouseLeftReleased);
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
DialogSplinePath::~DialogSplinePath()
{
DeleteVisualization<VisToolSplinePath>();
2013-08-09 08:49:34 +02:00
delete ui;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief SetPath set spline path
* @param value path
*/
void DialogSplinePath::SetPath(const VSplinePath &value)
{
2013-08-09 08:49:34 +02:00
this->path = value;
ui->listWidget->blockSignals(true);
ui->listWidget->clear();
for (qint32 i = 0; i < path.CountPoint(); ++i)
{
const VSplinePoint &point = path.at(i);
NewItem(point.P().id(), point.KAsm1(), point.Angle1(), point.KAsm2(), point.Angle2());
2013-08-09 08:49:34 +02:00
}
ui->listWidget->setFocus(Qt::OtherFocusReason);
2015-02-03 11:17:04 +01:00
ui->doubleSpinBoxKcurve->setValue(path.GetKCurve());
ui->lineEditSplPathName->setText(path.name());
auto visPath = qobject_cast<VisToolSplinePath *>(vis);
SCASSERT(visPath != nullptr);
visPath->setPath(path);
ui->listWidget->blockSignals(false);
2013-08-09 08:49:34 +02:00
}
2015-02-03 10:27:33 +01:00
//---------------------------------------------------------------------------------------------------------------------
2015-02-03 11:17:04 +01:00
QString DialogSplinePath::GetColor() const
2015-02-03 10:27:33 +01:00
{
2015-02-03 11:17:04 +01:00
return GetComboBoxCurrentData(ui->comboBoxColor);
2015-02-03 10:27:33 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2015-02-03 11:17:04 +01:00
void DialogSplinePath::SetColor(const QString &value)
2015-02-03 10:27:33 +01:00
{
ChangeCurrentData(ui->comboBoxColor, value);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
* @param id id of point or detail
* @param type don't show this id in list
*/
void DialogSplinePath::ChosenObject(quint32 id, const SceneObject &type)
{
if (type == SceneObject::Point)
{
if (AllIds().contains(id))
{
return;
}
NewItem(id, 1, 0, 1, 180);
emit ToolTip(tr("Select point of curve path"));
SavePath();
auto visPath = qobject_cast<VisToolSplinePath *>(vis);
SCASSERT(visPath != nullptr);
visPath->setPath(path);
if (path.CountPoint() == 1)
{
visPath->VisualMode(NULL_ID);
connect(visPath, &VisToolSplinePath::ToolTip, this, &DialogTool::ShowVisToolTip);
connect(visPath, &VisToolSplinePath::PathChanged, this, &DialogSplinePath::PathUpdated);
}
else
{
visPath->RefreshGeometry();
}
2013-08-09 08:49:34 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogSplinePath::SaveData()
{
const quint32 d = path.GetDuplicate();//Save previous value
SavePath();
newDuplicate <= -1 ? path.SetDuplicate(d) : path.SetDuplicate(static_cast<quint32>(newDuplicate));
auto visPath = qobject_cast<VisToolSplinePath *>(vis);
SCASSERT(visPath != nullptr);
visPath->setPath(path);
visPath->SetMode(Mode::Show);
visPath->RefreshGeometry();
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief PointChanged selected another point in list
* @param row number of row
*/
void DialogSplinePath::PointChanged(int row)
{
if (ui->listWidget->count() == 0)
{
2013-08-09 08:49:34 +02:00
return;
}
QListWidgetItem *item = ui->listWidget->item( row );
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
DataPoint(p.P().id(), p.KAsm1(), p.Angle1(), p.KAsm2(), p.Angle2());
2013-08-09 08:49:34 +02:00
EnableFields();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief currentPointChanged changed point in combo box
* @param index index in list
*/
void DialogSplinePath::currentPointChanged(int index)
{
quint32 id = qvariant_cast<quint32>(ui->comboBoxPoint->itemData(index));
2013-08-09 08:49:34 +02:00
qint32 row = ui->listWidget->currentRow();
QListWidgetItem *item = ui->listWidget->item( row );
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
const QSharedPointer<VPointF> point = data->GeometricObject<VPointF>(id);
p.SetP(*point);
DataPoint(p.P().id(), p.KAsm1(), p.Angle1(), p.KAsm2(), p.Angle2());
2013-08-09 08:49:34 +02:00
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<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();
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Angle1Changed changed first angle
* @param index index in list
*/
void DialogSplinePath::Angle1Changed(qreal index)
{
qint32 row = ui->listWidget->currentRow();
QListWidgetItem *item = ui->listWidget->item( row );
SCASSERT(item != nullptr);
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
p.SetAngle1(index);
DataPoint(p.P().id(), p.KAsm1(), p.Angle1(), p.KAsm2(), p.Angle2());
item->setData(Qt::UserRole, QVariant::fromValue(p));
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Angle2Changed changed second angle
* @param index index in list
*/
void DialogSplinePath::Angle2Changed(qreal index)
{
qint32 row = ui->listWidget->currentRow();
QListWidgetItem *item = ui->listWidget->item( row );
SCASSERT(item != nullptr);
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
p.SetAngle2(index);
DataPoint(p.P().id(), p.KAsm1(), p.Angle1(), p.KAsm2(), p.Angle2());
item->setData(Qt::UserRole, QVariant::fromValue(p));
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief KAsm1Changed changed first coefficient asymmetry
* @param d value
*/
void DialogSplinePath::KAsm1Changed(qreal d)
{
2013-08-09 08:49:34 +02:00
qint32 row = ui->listWidget->currentRow();
QListWidgetItem *item = ui->listWidget->item( row );
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
p.SetKAsm1(d);
item->setData(Qt::UserRole, QVariant::fromValue(p));
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief KAsm2Changed changed second coefficient asymmetry
* @param d value
*/
void DialogSplinePath::KAsm2Changed(qreal d)
{
2013-08-09 08:49:34 +02:00
qint32 row = ui->listWidget->currentRow();
QListWidgetItem *item = ui->listWidget->item( row );
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
p.SetKAsm2(d);
item->setData(Qt::UserRole, QVariant::fromValue(p));
}
//---------------------------------------------------------------------------------------------------------------------
void DialogSplinePath::ShowDialog(bool click)
{
if (click == false)
{
if (path.CountPoint() >= 3)
{
emit ToolTip("");
if (not data->IsUnique(path.name()))
{
path.SetDuplicate(DNumber(path.name()));
}
DialogAccepted();
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogSplinePath::PathUpdated(const VSplinePath &path)
{
SetPath(path);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogSplinePath::ShowVisualization()
{
AddVisualization<VisToolSplinePath>();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief NewItem add point to list
* @param id id
* @param kAsm1 first coefficient asymmetry
* @param angle1 first angle in degree
* @param kAsm2 second coefficient asymmetry
* @param angle2 second angle in degree
*/
void DialogSplinePath::NewItem(quint32 id, qreal kAsm1, qreal angle1, qreal kAsm2, qreal angle2)
{
const QSharedPointer<VPointF> point = data->GeometricObject<VPointF>(id);
QListWidgetItem *item = new QListWidgetItem(point->name());
2013-08-09 08:49:34 +02:00
item->setFont(QFont("Times", 12, QFont::Bold));
VSplinePoint p(*point.data(), kAsm1, angle1, kAsm2, angle2);
DataPoint(point->id(), kAsm1, angle1, kAsm2, angle2);
2013-08-09 08:49:34 +02:00
item->setData(Qt::UserRole, QVariant::fromValue(p));
ui->listWidget->addItem(item);
ui->listWidget->setCurrentItem(item);
if (ui->listWidget->count() >= 2)
{
bOk = ui->buttonBox->button(QDialogButtonBox::Ok);
bOk->setEnabled(true);
}
2013-08-09 08:49:34 +02:00
EnableFields();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief dataPoint show data of point in fields
* @param id id
* @param kAsm1 first coefficient asymmetry
* @param angle1 first angle of spline
* @param kAsm2 second coefficient asymmetry
* @param angle2 second angle of spline
*/
void DialogSplinePath::DataPoint(quint32 id, qreal kAsm1, qreal angle1, qreal kAsm2, qreal angle2)
{
ui->comboBoxPoint->blockSignals(true);
ui->doubleSpinBoxAngle1->blockSignals(true);
ui->doubleSpinBoxAngle2->blockSignals(true);
ui->doubleSpinBoxKasm1->blockSignals(true);
ui->doubleSpinBoxKasm2->blockSignals(true);
2013-08-09 08:49:34 +02:00
ChangeCurrentData(ui->comboBoxPoint, id);
ui->doubleSpinBoxKasm1->setValue(kAsm1);
ui->doubleSpinBoxKasm2->setValue(kAsm2);
ui->doubleSpinBoxAngle2->setValue(angle2);
ui->doubleSpinBoxAngle1->setValue(angle1);
2013-08-09 08:49:34 +02:00
ui->comboBoxPoint->blockSignals(false);
ui->doubleSpinBoxAngle1->blockSignals(false);
ui->doubleSpinBoxAngle2->blockSignals(false);
ui->doubleSpinBoxKasm1->blockSignals(false);
ui->doubleSpinBoxKasm2->blockSignals(false);
2013-08-09 08:49:34 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief EnableFields enable or disable fields
*/
void DialogSplinePath::EnableFields()
{
2013-08-09 08:49:34 +02:00
ui->doubleSpinBoxKasm1->setEnabled(true);
ui->doubleSpinBoxAngle1->setEnabled(true);
2013-08-09 08:49:34 +02:00
ui->doubleSpinBoxKasm2->setEnabled(true);
ui->doubleSpinBoxAngle2->setEnabled(true);
2013-08-09 08:49:34 +02:00
qint32 row = ui->listWidget->currentRow();
if (row == 0)
{
2013-08-09 08:49:34 +02:00
ui->doubleSpinBoxKasm1->setEnabled(false);
ui->doubleSpinBoxAngle1->setEnabled(false);
2013-08-09 08:49:34 +02:00
return;
}
if (row == ui->listWidget->count()-1)
{
2013-08-09 08:49:34 +02:00
ui->doubleSpinBoxKasm2->setEnabled(false);
ui->doubleSpinBoxAngle2->setEnabled(false);
2013-08-09 08:49:34 +02:00
return;
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogSplinePath::SavePath()
{
path.Clear();
path = ExtractPath();
}
//---------------------------------------------------------------------------------------------------------------------
QSet<quint32> DialogSplinePath::AllIds() const
{
QSet<quint32> ids;
for (qint32 i = 0; i < ui->listWidget->count(); ++i)
{
ids.insert(qvariant_cast<VSplinePoint>(ui->listWidget->item(i)->data(Qt::UserRole)).P().id());
}
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;
}