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

408 lines
14 KiB
C++
Raw Normal View History

2014-01-08 15:05:32 +01:00
/************************************************************************
**
** @file dialogcutarc.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
2014-01-08 15:05:32 +01:00
** @date 7 1, 2014
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
2014-01-08 15:05:32 +01:00
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2013-2015 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
2014-01-08 15:05:32 +01:00
**
** 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 "dialogcutarc.h"
#include <QDialog>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPointer>
#include <QPushButton>
#include <QTimer>
#include <QToolButton>
#include "../vpatterndb/vtranslatevars.h"
#include "../vpatterndb/vcontainer.h"
#include "../../visualization/path/vistoolcutarc.h"
#include "../../visualization/visualization.h"
#include "../ifc/xml/vabstractpattern.h"
#include "../support/dialogeditwrongformula.h"
#include "../vmisc/vabstractapplication.h"
#include "../vmisc/vcommonsettings.h"
#include "ui_dialogcutarc.h"
#include "../vgeometry/varc.h"
2020-11-06 13:48:37 +01:00
#include "../qmuparser/qmudef.h"
#include "../vwidgets/vabstractmainwindow.h"
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief DialogCutArc create dialog.
* @param data container with data
* @param parent parent widget
*/
DialogCutArc::DialogCutArc(const VContainer *data, quint32 toolId, QWidget *parent)
: DialogTool(data, toolId, parent),
ui(new Ui::DialogCutArc),
2022-08-20 17:49:32 +02:00
m_timerFormula(new QTimer(this))
2014-01-08 15:05:32 +01:00
{
ui->setupUi(this);
2022-08-20 17:49:32 +02:00
m_timerFormula->setSingleShot(true);
connect(m_timerFormula, &QTimer::timeout, this, &DialogCutArc::EvalFormula);
ui->lineEditNamePoint->setClearButtonEnabled(true);
2021-02-06 14:52:21 +01:00
ui->lineEditNamePoint->setText(
VAbstractValApplication::VApp()->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
2022-08-20 17:49:32 +02:00
m_formulaBaseHeight = ui->plainTextEditFormula->height();
ui->plainTextEditFormula->installEventFilter(this);
2014-03-06 14:28:46 +01:00
InitOkCancelApply(ui);
2014-01-08 15:05:32 +01:00
FillComboBoxArcs(ui->comboBoxArc);
2014-01-08 15:05:32 +01:00
connect(ui->toolButtonExprLength, &QPushButton::clicked, this, &DialogCutArc::FXLength);
connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, [this]()
{
2022-08-20 17:49:32 +02:00
CheckPointLabel(this, ui->lineEditNamePoint, ui->labelEditNamePoint, m_pointName, this->data, m_flagName);
CheckState();
});
connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, [this]()
{
2022-08-20 17:49:32 +02:00
m_timerFormula->start(formulaTimerTimeout);
});
connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogCutArc::DeployFormulaTextEdit);
connect(ui->comboBoxArc, &QComboBox::currentTextChanged, this, &DialogCutArc::ArcChanged);
2020-11-04 17:14:21 +01:00
connect(ui->lineEditAlias1, &QLineEdit::textEdited, this, &DialogCutArc::ValidateAlias);
connect(ui->lineEditAlias2, &QLineEdit::textEdited, this, &DialogCutArc::ValidateAlias);
vis = new VisToolCutArc(data);
2020-10-28 13:09:56 +01:00
ui->tabWidget->setCurrentIndex(0);
SetTabStopDistance(ui->plainTextEditToolNotes);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::FXLength()
{
2022-08-20 17:49:32 +02:00
auto *dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit length"));
dialog->SetFormula(GetFormula());
2021-02-06 14:52:21 +01:00
dialog->setPostfix(UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true));
if (dialog->exec() == QDialog::Accepted)
{
SetFormula(dialog->GetFormula());
}
delete dialog;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::EvalFormula()
{
FormulaData formulaData;
formulaData.formula = ui->plainTextEditFormula->toPlainText();
formulaData.variables = data->DataVariables();
formulaData.labelEditFormula = ui->labelEditFormula;
formulaData.labelResult = ui->labelResultCalculation;
2021-02-06 14:52:21 +01:00
formulaData.postfix = UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true);
formulaData.checkZero = false;
2022-08-20 17:49:32 +02:00
Eval(formulaData, m_flagFormula);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::ShowVisualization()
{
AddVisualization<VisToolCutArc>();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::DeployFormulaTextEdit()
{
2022-08-20 17:49:32 +02:00
DeployFormula(this, ui->plainTextEditFormula, ui->pushButtonGrowLength, m_formulaBaseHeight);
2014-01-08 15:05:32 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2014-01-08 15:05:32 +01:00
DialogCutArc::~DialogCutArc()
{
delete ui;
}
//---------------------------------------------------------------------------------------------------------------------
2022-08-20 17:49:32 +02:00
auto DialogCutArc::GetPointName() const -> QString
{
2022-08-20 17:49:32 +02:00
return m_pointName;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
* @param id id of point or detail
* @param type type of object
*/
void DialogCutArc::ChosenObject(quint32 id, const SceneObject &type)
2014-01-08 15:05:32 +01:00
{
2022-08-20 17:49:32 +02:00
if (prepare)// After first choose we ignore all objects
2014-01-08 15:05:32 +01:00
{
2022-08-20 17:49:32 +02:00
return;
}
if (type == SceneObject::Arc)
{
if (SetObject(id, ui->comboBoxArc, QString()))
2014-08-17 20:49:29 +02:00
{
2022-08-20 17:49:32 +02:00
if (vis != nullptr)
2014-08-17 20:49:29 +02:00
{
2022-08-20 17:49:32 +02:00
vis->VisualMode(id);
2014-08-17 20:49:29 +02:00
}
2022-08-20 17:49:32 +02:00
prepare = true;
auto *window = qobject_cast<VAbstractMainWindow *>(VAbstractValApplication::VApp()->getMainWindow());
SCASSERT(window != nullptr)
connect(vis.data(), &Visualization::ToolTip, window, &VAbstractMainWindow::ShowToolTip);
2022-11-17 15:42:03 +01:00
if (not VAbstractValApplication::VApp()->Settings()->IsInteractiveTools())
{
FinishCreating();
}
2014-08-17 20:49:29 +02:00
}
2014-01-08 15:05:32 +01:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::SaveData()
2014-01-08 15:05:32 +01:00
{
2022-08-20 17:49:32 +02:00
m_pointName = ui->lineEditNamePoint->text();
m_formula = ui->plainTextEditFormula->toPlainText();
2022-08-20 17:49:32 +02:00
auto *path = qobject_cast<VisToolCutArc *>(vis);
SCASSERT(path != nullptr)
2022-08-23 15:37:58 +02:00
path->SetArcId(getArcId());
path->SetLength(m_formula);
path->RefreshGeometry();
2014-01-08 15:05:32 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::closeEvent(QCloseEvent *event)
{
ui->plainTextEditFormula->blockSignals(true);
DialogTool::closeEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::ArcChanged()
{
CurrentCurveLength(getArcId(), const_cast<VContainer *> (data));
}
2020-11-04 17:14:21 +01:00
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::ValidateAlias()
{
2020-11-06 13:48:37 +01:00
QRegularExpression rx(NameRegExp());
2020-11-07 10:18:13 +01:00
2020-11-04 17:14:21 +01:00
VArc arc1;
arc1.SetAliasSuffix(GetAliasSuffix1());
2020-11-07 10:18:13 +01:00
VArc arc2;
arc2.SetAliasSuffix(GetAliasSuffix2());
2020-11-06 13:48:37 +01:00
if (not GetAliasSuffix1().isEmpty() &&
2020-11-07 10:18:13 +01:00
(not rx.match(arc1.GetAlias()).hasMatch() ||
2022-08-20 17:49:32 +02:00
(m_originAliasSuffix1 != GetAliasSuffix1() && not data->IsUnique(arc1.GetAlias())) ||
2020-11-07 10:18:13 +01:00
arc1.GetAlias() == arc2.GetAlias()))
2020-11-04 17:14:21 +01:00
{
2022-08-20 17:49:32 +02:00
m_flagAlias1 = false;
2020-11-04 17:14:21 +01:00
ChangeColor(ui->labelAlias1, errorColor);
}
else
{
2022-08-20 17:49:32 +02:00
m_flagAlias1 = true;
2020-11-04 17:14:21 +01:00
ChangeColor(ui->labelAlias1, OkColor(this));
}
2020-11-07 10:18:13 +01:00
if (not GetAliasSuffix2().isEmpty() &&
(not rx.match(arc2.GetAlias()).hasMatch() ||
2022-08-20 17:49:32 +02:00
(m_originAliasSuffix2 != GetAliasSuffix2() && not data->IsUnique(arc2.GetAlias())) ||
2020-11-07 10:18:13 +01:00
arc1.GetAlias() == arc2.GetAlias()))
2020-11-04 17:14:21 +01:00
{
2022-08-20 17:49:32 +02:00
m_flagAlias2 = false;
2020-11-04 17:14:21 +01:00
ChangeColor(ui->labelAlias2, errorColor);
}
else
{
2022-08-20 17:49:32 +02:00
m_flagAlias2 = true;
2020-11-04 17:14:21 +01:00
ChangeColor(ui->labelAlias2, OkColor(this));
}
CheckState();
}
2022-11-17 15:42:03 +01:00
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::FinishCreating()
{
vis->SetMode(Mode::Show);
vis->RefreshGeometry();
emit ToolTip(QString());
setModal(true);
show();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief setArcId set id of arc
* @param value id
*/
void DialogCutArc::setArcId(quint32 value)
2014-01-08 15:05:32 +01:00
{
setCurrentArcId(ui->comboBoxArc, value);
2022-08-20 17:49:32 +02:00
auto *path = qobject_cast<VisToolCutArc *>(vis);
SCASSERT(path != nullptr)
2022-08-23 15:37:58 +02:00
path->SetArcId(value);
2014-01-08 15:05:32 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief SetFormula set string with formula length
* @param value string with formula
*/
2015-02-03 11:17:04 +01:00
void DialogCutArc::SetFormula(const QString &value)
2014-01-08 15:05:32 +01:00
{
2022-08-20 17:49:32 +02:00
m_formula = VAbstractApplication::VApp()->TrVars()
2021-02-06 14:52:21 +01:00
->FormulaToUser(value, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
// increase height if needed.
2022-08-20 17:49:32 +02:00
if (m_formula.length() > 80)
{
this->DeployFormulaTextEdit();
}
2022-08-20 17:49:32 +02:00
ui->plainTextEditFormula->setPlainText(m_formula);
2022-08-20 17:49:32 +02:00
auto *path = qobject_cast<VisToolCutArc *>(vis);
SCASSERT(path != nullptr)
2022-08-23 15:37:58 +02:00
path->SetLength(m_formula);
MoveCursorToEnd(ui->plainTextEditFormula);
2014-01-08 15:05:32 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief SetPointName set name point on arc
* @param value name
*/
2015-02-03 11:17:04 +01:00
void DialogCutArc::SetPointName(const QString &value)
2014-01-08 15:05:32 +01:00
{
2022-08-20 17:49:32 +02:00
m_pointName = value;
ui->lineEditNamePoint->setText(m_pointName);
2014-01-08 15:05:32 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief GetFormula return string with formula length
* @return formula
*/
2022-08-20 17:49:32 +02:00
auto DialogCutArc::GetFormula() const -> QString
{
2022-08-20 17:49:32 +02:00
return VTranslateVars::TryFormulaFromUser(m_formula, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief getArcId return id of arc
* @return id
*/
2022-08-20 17:49:32 +02:00
auto DialogCutArc::getArcId() const -> quint32
{
return getCurrentObjectId(ui->comboBoxArc);
}
2020-10-28 13:09:56 +01:00
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::SetNotes(const QString &notes)
{
ui->plainTextEditToolNotes->setPlainText(notes);
}
//---------------------------------------------------------------------------------------------------------------------
2022-08-20 17:49:32 +02:00
auto DialogCutArc::GetNotes() const -> QString
2020-10-28 13:09:56 +01:00
{
return ui->plainTextEditToolNotes->toPlainText();
}
2020-11-04 17:14:21 +01:00
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::SetAliasSuffix1(const QString &alias)
{
2022-08-20 17:49:32 +02:00
m_originAliasSuffix1 = alias;
ui->lineEditAlias1->setText(m_originAliasSuffix1);
2020-11-04 17:14:21 +01:00
ValidateAlias();
}
//---------------------------------------------------------------------------------------------------------------------
2022-08-20 17:49:32 +02:00
auto DialogCutArc::GetAliasSuffix1() const -> QString
2020-11-04 17:14:21 +01:00
{
return ui->lineEditAlias1->text();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::SetAliasSuffix2(const QString &alias)
{
2022-08-20 17:49:32 +02:00
m_originAliasSuffix2 = alias;
ui->lineEditAlias2->setText(m_originAliasSuffix2);
2020-11-04 17:14:21 +01:00
ValidateAlias();
}
//---------------------------------------------------------------------------------------------------------------------
2022-08-20 17:49:32 +02:00
auto DialogCutArc::GetAliasSuffix2() const -> QString
2020-11-04 17:14:21 +01:00
{
return ui->lineEditAlias2->text();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogCutArc::ShowDialog(bool click)
{
if (not prepare)
{
return;
}
if (click)
{
// The check need to ignore first release of mouse button.
// User can select point by clicking on a label.
if (not m_firstRelease)
{
m_firstRelease = true;
return;
}
auto *scene = qobject_cast<VMainGraphicsScene *>(VAbstractValApplication::VApp()->getCurrentScene());
SCASSERT(scene != nullptr)
const QSharedPointer<VArc> arc = data->GeometricObject<VArc>(getArcId());
QPointF p = arc->ClosestPoint(scene->getScenePos());
qreal len = arc->GetLengthByPoint(p);
if (len > 0)
{
SetFormula(QString::number(FromPixel(len, *data->GetPatternUnit())));
}
}
2022-11-17 15:42:03 +01:00
FinishCreating();
}