/************************************************************************ ** ** @file dialogbisector.cpp ** @author Roman Telezhynskyi ** @date November 15, 2013 ** ** @brief ** @copyright ** This source code is part of the Valentina project, a pattern making ** program, whose allow create and modeling patterns of clothing. ** Copyright (C) 2013-2015 Valentina project ** 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 . ** *************************************************************************/ #include "dialogbisector.h" #include #include #include #include #include #include #include #include #include #include #include #include "../../visualization/line/vistoolbisector.h" #include "../../visualization/visualization.h" #include "../ifc/xml/vabstractpattern.h" #include "../support/dialogeditwrongformula.h" #include "../vmisc/vabstractapplication.h" #include "../vmisc/vcommonsettings.h" #include "../vpatterndb/vtranslatevars.h" #include "../vpatterndb/vcontainer.h" #include "ui_dialogbisector.h" #include "../vwidgets/vabstractmainwindow.h" #include "../vtools/tools/drawTools/toolpoint/toolsinglepoint/toollinepoint/vtoolbisector.h" #include "../vgeometry/vpointf.h" //--------------------------------------------------------------------------------------------------------------------- /** * @brief DialogBisector create dialog * @param data container with data * @param parent parent widget */ DialogBisector::DialogBisector(const VContainer *data, quint32 toolId, QWidget *parent) : DialogTool(data, toolId, parent), ui(new Ui::DialogBisector), m_timerFormula(new QTimer(this)) { ui->setupUi(this); m_timerFormula->setSingleShot(true); connect(m_timerFormula, &QTimer::timeout, this, &DialogBisector::EvalFormula); ui->lineEditNamePoint->setClearButtonEnabled(true); ui->lineEditNamePoint->setText( VAbstractValApplication::VApp()->getCurrentDocument()->GenerateLabel(LabelType::NewLabel)); this->m_formulaBaseHeight = ui->plainTextEditFormula->height(); ui->plainTextEditFormula->installEventFilter(this); InitOkCancelApply(ui); FillComboBoxPoints(ui->comboBoxFirstPoint); FillComboBoxPoints(ui->comboBoxSecondPoint); FillComboBoxTypeLine(ui->comboBoxLineType, LineStylesPics()); FillComboBoxPoints(ui->comboBoxThirdPoint); FillComboBoxLineColors(ui->comboBoxLineColor); connect(ui->toolButtonExprLength, &QPushButton::clicked, this, &DialogBisector::FXLength); connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, [this]() { CheckPointLabel(this, ui->lineEditNamePoint, ui->labelEditNamePoint, m_pointName, this->data, m_flagName); CheckState(); }); connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, [this]() { m_timerFormula->start(formulaTimerTimeout); }); connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogBisector::DeployFormulaTextEdit); connect(ui->comboBoxFirstPoint, &QComboBox::currentTextChanged, this, &DialogBisector::PointNameChanged); connect(ui->comboBoxSecondPoint, &QComboBox::currentTextChanged, this, &DialogBisector::PointNameChanged); connect(ui->comboBoxThirdPoint, &QComboBox::currentTextChanged, this, &DialogBisector::PointNameChanged); vis = new VisToolBisector(data); ui->tabWidget->setCurrentIndex(0); SetTabStopDistance(ui->plainTextEditToolNotes); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::PointNameChanged() { QSet set; set.insert(getCurrentObjectId(ui->comboBoxFirstPoint)); set.insert(getCurrentObjectId(ui->comboBoxSecondPoint)); set.insert(getCurrentObjectId(ui->comboBoxThirdPoint)); QColor color; if (set.size() != 3) { m_flagError = false; color = errorColor; } else { m_flagError = true; color = OkColor(this); } ChangeColor(ui->labelFirstPoint, color); ChangeColor(ui->labelSecondPoint, color); ChangeColor(ui->labelThirdPoint, color); CheckState(); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::FXLength() { auto *dialog = new DialogEditWrongFormula(data, toolId, this); dialog->setWindowTitle(tr("Edit length")); dialog->SetFormula(GetFormula()); dialog->setPostfix(UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true)); if (dialog->exec() == QDialog::Accepted) { SetFormula(dialog->GetFormula()); } delete dialog; } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::EvalFormula() { FormulaData formulaData; formulaData.formula = ui->plainTextEditFormula->toPlainText(); formulaData.variables = data->DataVariables(); formulaData.labelEditFormula = ui->labelEditFormula; formulaData.labelResult = ui->labelResultCalculation; formulaData.postfix = UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true); formulaData.checkZero = false; Eval(formulaData, m_flagFormula); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::ShowVisualization() { AddVisualization(); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::DeployFormulaTextEdit() { DeployFormula(this, ui->plainTextEditFormula, ui->pushButtonGrowLength, m_formulaBaseHeight); } //--------------------------------------------------------------------------------------------------------------------- DialogBisector::~DialogBisector() { delete ui; } //--------------------------------------------------------------------------------------------------------------------- auto DialogBisector::GetPointName() const -> QString { 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 DialogBisector::ChosenObject(quint32 id, const SceneObject &type) { if (prepare)// After first choose we ignore all objects { return; } if (type == SceneObject::Point) { auto *line = qobject_cast(vis); SCASSERT(line != nullptr) switch (m_number) { case 0: if (SetObject(id, ui->comboBoxFirstPoint, tr("Select second point of angle"))) { m_number++; line->VisualMode(id); } break; case 1: if (getCurrentObjectId(ui->comboBoxFirstPoint) != id) { if (SetObject(id, ui->comboBoxSecondPoint, tr("Select third point of angle"))) { m_number++; line->SetPoint2Id(id); line->RefreshGeometry(); } } break; case 2: ChosenThirdPoint(id); break; default: break; } } } //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetPointName set name of point * @param value name */ void DialogBisector::SetPointName(const QString &value) { m_pointName = value; ui->lineEditNamePoint->setText(m_pointName); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetTypeLine set type of line * @param value type */ void DialogBisector::SetTypeLine(const QString &value) { ChangeCurrentData(ui->comboBoxLineType, value); vis->SetLineStyle(LineStyleToPenStyle(value)); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetFormula set string of formula * @param value formula */ void DialogBisector::SetFormula(const QString &value) { m_formula = VAbstractApplication::VApp()->TrVars() ->FormulaToUser(value, VAbstractApplication::VApp()->Settings()->GetOsSeparator()); // increase height if needed. if (m_formula.length() > 80) { this->DeployFormulaTextEdit(); } ui->plainTextEditFormula->setPlainText(m_formula); auto *line = qobject_cast(vis); SCASSERT(line != nullptr) line->SetLength(m_formula); MoveCursorToEnd(ui->plainTextEditFormula); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetFirstPointId set id of first point * @param value id */ void DialogBisector::SetFirstPointId(const quint32 &value) { setCurrentPointId(ui->comboBoxFirstPoint, value); auto *line = qobject_cast(vis); SCASSERT(line != nullptr) line->SetPoint1Id(value); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetSecondPointId set id of second point * @param value id */ void DialogBisector::SetSecondPointId(const quint32 &value) { setCurrentPointId(ui->comboBoxSecondPoint, value); auto *line = qobject_cast(vis); SCASSERT(line != nullptr) line->SetPoint2Id(value); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief SetThirdPointId set id of third point * @param value id */ void DialogBisector::SetThirdPointId(const quint32 &value) { setCurrentPointId(ui->comboBoxThirdPoint, value); auto *line = qobject_cast(vis); SCASSERT(line != nullptr) line->SetPoint3Id(value); } //--------------------------------------------------------------------------------------------------------------------- auto DialogBisector::GetLineColor() const -> QString { return GetComboBoxCurrentData(ui->comboBoxLineColor, ColorBlack); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::SetLineColor(const QString &value) { ChangeCurrentData(ui->comboBoxLineColor, value); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::SetNotes(const QString ¬es) { ui->plainTextEditToolNotes->setPlainText(notes); } //--------------------------------------------------------------------------------------------------------------------- auto DialogBisector::GetNotes() const -> QString { return ui->plainTextEditToolNotes->toPlainText(); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::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(VAbstractValApplication::VApp()->getCurrentScene()); SCASSERT(scene != nullptr) const QSharedPointer p1 = data->GeometricObject(GetFirstPointId()); const QSharedPointer p2 = data->GeometricObject(GetSecondPointId()); const QSharedPointer p3 = data->GeometricObject(GetThirdPointId()); qreal angle = VToolBisector::BisectorAngle(static_cast(*p1), static_cast(*p2), static_cast(*p3)); QLineF baseLine(static_cast(*p2), static_cast(*p3)); baseLine.setAngle(angle); QLineF line(static_cast(*p2), scene->getScenePos()); qreal len = line.length(); qreal angleTo = baseLine.angleTo(line); if (angleTo > 90 && angleTo < 270) { len *= -1; } SetFormula(QString::number(FromPixel(len, *data->GetPatternUnit()))); } FinishCreating(); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::SaveData() { m_pointName = ui->lineEditNamePoint->text(); m_formula = ui->plainTextEditFormula->toPlainText(); auto *line = qobject_cast(vis); SCASSERT(line != nullptr) line->SetPoint1Id(GetFirstPointId()); line->SetPoint2Id(GetSecondPointId()); line->SetPoint3Id(GetThirdPointId()); line->SetLength(m_formula); line->SetLineStyle(LineStyleToPenStyle(GetTypeLine())); line->RefreshGeometry(); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::closeEvent(QCloseEvent *event) { ui->plainTextEditFormula->blockSignals(true); DialogTool::closeEvent(event); } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::ChosenThirdPoint(quint32 id) { QSet set; set.insert(getCurrentObjectId(ui->comboBoxFirstPoint)); set.insert(getCurrentObjectId(ui->comboBoxSecondPoint)); set.insert(id); if (set.size() == 3) { if (SetObject(id, ui->comboBoxThirdPoint, QString())) { auto *window = qobject_cast( VAbstractValApplication::VApp()->getMainWindow()); SCASSERT(window != nullptr) auto *line = qobject_cast(vis); SCASSERT(line != nullptr) connect(line, &Visualization::ToolTip, window, &VAbstractMainWindow::ShowToolTip); line->SetPoint3Id(id); line->RefreshGeometry(); prepare = true; if (not VAbstractValApplication::VApp()->Settings()->IsInteractiveTools()) { FinishCreating(); } } } } //--------------------------------------------------------------------------------------------------------------------- void DialogBisector::FinishCreating() { vis->SetMode(Mode::Show); vis->RefreshGeometry(); emit ToolTip(QString()); setModal(true); show(); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetTypeLine return type of line * @return type */ auto DialogBisector::GetTypeLine() const -> QString { return GetComboBoxCurrentData(ui->comboBoxLineType, TypeLineLine); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetFormula return string of formula * @return formula */ auto DialogBisector::GetFormula() const -> QString { return VTranslateVars::TryFormulaFromUser(m_formula, VAbstractApplication::VApp()->Settings()->GetOsSeparator()); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetFirstPointId return id of first point * @return id */ auto DialogBisector::GetFirstPointId() const -> quint32 { return getCurrentObjectId(ui->comboBoxFirstPoint); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetSecondPointId return id of second point * @return id */ auto DialogBisector::GetSecondPointId() const -> quint32 { return getCurrentObjectId(ui->comboBoxSecondPoint); } //--------------------------------------------------------------------------------------------------------------------- /** * @brief GetThirdPointId return id of third point * @return id */ auto DialogBisector::GetThirdPointId() const -> quint32 { return getCurrentObjectId(ui->comboBoxThirdPoint); }