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

443 lines
16 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file dialogendline.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <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-07-25 14:00:51 +02:00
#include "dialogendline.h"
#include <QDialog>
#include <QLineEdit>
#include <QLineF>
#include <QPlainTextEdit>
#include <QPointF>
#include <QPointer>
#include <QPushButton>
#include <QSharedPointer>
#include <QTimer>
#include <QToolButton>
#include <new>
2013-07-25 14:00:51 +02:00
#include "../vgeometry/vpointf.h"
#include "../vpatterndb/vcontainer.h"
#include "../vpatterndb/vtranslatevars.h"
#include "../vwidgets/vmaingraphicsscene.h"
#include "../vwidgets/vabstractmainwindow.h"
#include "../../tools/vabstracttool.h"
#include "../../visualization/line/vistoolendline.h"
#include "../../visualization/visualization.h"
#include "../ifc/xml/vabstractpattern.h"
#include "../ifc/xml/vdomdocument.h"
#include "../support/dialogeditwrongformula.h"
#include "../vmisc/vabstractapplication.h"
#include "../vmisc/vcommonsettings.h"
#include "ui_dialogendline.h"
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief DialogEndLine create dialog
* @param data container with data
* @param parent parent widget
*/
DialogEndLine::DialogEndLine(const VContainer *data, quint32 toolId, QWidget *parent)
: DialogTool(data, toolId, parent),
ui(new Ui::DialogEndLine),
formulaLength(),
formulaAngle(),
formulaBaseHeight(0),
formulaBaseHeightAngle(0),
pointName(),
m_firstRelease(false),
timerFormulaLength(new QTimer(this)),
timerFormulaAngle(new QTimer(this)),
flagFormula(false),
flagError(false),
flagName(true)
{
2013-07-25 14:00:51 +02:00
ui->setupUi(this);
timerFormulaLength->setSingleShot(true);
timerFormulaAngle->setSingleShot(true);
ui->lineEditNamePoint->setClearButtonEnabled(true);
ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
this->formulaBaseHeight = ui->plainTextEditFormula->height();
this->formulaBaseHeightAngle = ui->plainTextEditAngle->height();
2014-03-06 14:28:46 +01:00
ui->plainTextEditFormula->installEventFilter(this);
ui->plainTextEditAngle->installEventFilter(this);
InitOkCancelApply(ui);
2013-07-25 14:00:51 +02:00
flagFormula = false;
FillComboBoxPoints(ui->comboBoxBasePoint);
FillComboBoxTypeLine(ui->comboBoxLineType, LineStylesPics());
FillComboBoxLineColors(ui->comboBoxLineColor);
2013-07-25 14:00:51 +02:00
connect(ui->toolButtonExprLength, &QPushButton::clicked, this, &DialogEndLine::FXLength);
connect(ui->toolButtonExprAngle, &QPushButton::clicked, this, &DialogEndLine::FXAngle);
connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, [this]()
{
CheckPointLabel(this, ui->lineEditNamePoint, ui->labelEditNamePoint, pointName, this->data, flagName);
CheckState();
});
connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, [this]()
{
timerFormulaLength->start(formulaTimerTimeout);
});
connect(ui->plainTextEditAngle, &QPlainTextEdit::textChanged, this, [this]()
{
timerFormulaAngle->start(formulaTimerTimeout);
});
connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogEndLine::DeployFormulaTextEdit);
connect(ui->pushButtonGrowLengthAngle, &QPushButton::clicked, this, &DialogEndLine::DeployAngleTextEdit);
connect(timerFormulaLength, &QTimer::timeout, this, &DialogEndLine::EvalLength);
connect(timerFormulaAngle, &QTimer::timeout, this, &DialogEndLine::EvalAngle);
vis = new VisToolEndLine(data);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief EvalAngle calculate value of angle
*/
void DialogEndLine::EvalAngle()
{
FormulaData formulaData;
formulaData.formula = ui->plainTextEditAngle->toPlainText();
formulaData.variables = data->DataVariables();
formulaData.labelEditFormula = ui->labelEditAngle;
formulaData.labelResult = ui->labelResultCalculationAngle;
formulaData.postfix = degreeSymbol;
formulaData.checkZero = false;
Eval(formulaData, flagError);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::EvalLength()
{
FormulaData formulaData;
formulaData.formula = ui->plainTextEditFormula->toPlainText();
formulaData.variables = data->DataVariables();
formulaData.labelEditFormula = ui->labelEditFormula;
formulaData.labelResult = ui->labelResultCalculation;
formulaData.postfix = UnitsToStr(qApp->patternUnit(), true);
formulaData.checkLessThanZero = true;
Eval(formulaData, flagFormula);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::DeployFormulaTextEdit()
{
DeployFormula(this, ui->plainTextEditFormula, ui->pushButtonGrowLength, formulaBaseHeight);
2013-07-25 14:00:51 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::DeployAngleTextEdit()
{
DeployFormula(this, ui->plainTextEditAngle, ui->pushButtonGrowLengthAngle, formulaBaseHeightAngle);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::FXAngle()
{
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit angle"));
dialog->SetFormula(GetAngle());
dialog->setPostfix(degreeSymbol);
if (dialog->exec() == QDialog::Accepted)
{
SetAngle(dialog->GetFormula());
}
delete dialog;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::FXLength()
{
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit length"));
dialog->SetFormula(GetFormula());
dialog->setPostfix(UnitsToStr(qApp->patternUnit(), true));
if (dialog->exec() == QDialog::Accepted)
{
SetFormula(dialog->GetFormula());
}
delete dialog;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @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 DialogEndLine::ChosenObject(quint32 id, const SceneObject &type)
{
if (prepare == false)// After first choose we ignore all objects
{
if (type == SceneObject::Point)
{
if (SetObject(id, ui->comboBoxBasePoint, QString()))
2014-08-17 20:49:29 +02:00
{
vis->VisualMode(id);
VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow());
SCASSERT(window != nullptr)
2016-07-26 15:51:38 +02:00
connect(vis.data(), &Visualization::ToolTip, window, &VAbstractMainWindow::ShowToolTip);
2014-08-17 20:49:29 +02:00
prepare = true;
}
}
2013-07-25 14:00:51 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief SetPointName set name of point
* @param value name
*/
2015-02-03 11:17:04 +01:00
void DialogEndLine::SetPointName(const QString &value)
{
2013-07-25 14:00:51 +02:00
pointName = value;
ui->lineEditNamePoint->setText(pointName);
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief SetTypeLine set type of line
* @param value type
*/
2015-02-03 11:17:04 +01:00
void DialogEndLine::SetTypeLine(const QString &value)
{
ChangeCurrentData(ui->comboBoxLineType, value);
vis->setLineStyle(LineStyleToPenStyle(value));
2013-07-25 14:00:51 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief SetFormula set string of formula
* @param value formula
*/
2015-02-03 11:17:04 +01:00
void DialogEndLine::SetFormula(const QString &value)
{
formulaLength = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
// increase height if needed. TODO : see if I can get the max number of caracters in one line
// of this PlainTextEdit to change 80 to this value
if (formulaLength.length() > 80)
{
this->DeployFormulaTextEdit();
}
ui->plainTextEditFormula->setPlainText(formulaLength);
VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
SCASSERT(line != nullptr)
line->setLength(formulaLength);
MoveCursorToEnd(ui->plainTextEditFormula);
2013-07-25 14:00:51 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief SetAngle set angle of line
* @param value angle in degree
*/
2015-02-03 11:17:04 +01:00
void DialogEndLine::SetAngle(const QString &value)
{
formulaAngle = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
// increase height if needed. TODO : see if I can get the max number of caracters in one line
// of this PlainTextEdit to change 80 to this value
if (formulaAngle.length() > 80)
{
this->DeployAngleTextEdit();
}
ui->plainTextEditAngle->setPlainText(formulaAngle);
VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
SCASSERT(line != nullptr)
2015-02-03 11:17:04 +01:00
line->SetAngle(formulaAngle);
MoveCursorToEnd(ui->plainTextEditAngle);
2013-07-25 14:00:51 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief SetBasePointId set id base point of line
* @param value id
*/
2015-02-03 11:17:04 +01:00
void DialogEndLine::SetBasePointId(const quint32 &value)
{
setCurrentPointId(ui->comboBoxBasePoint, value);
VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
SCASSERT(line != nullptr)
line->setObject1Id(value);
}
2015-02-03 10:27:33 +01:00
//---------------------------------------------------------------------------------------------------------------------
2015-02-03 11:17:04 +01:00
QString DialogEndLine::GetLineColor() const
2015-02-03 10:27:33 +01:00
{
return GetComboBoxCurrentData(ui->comboBoxLineColor, ColorBlack);
2015-02-03 10:27:33 +01:00
}
//---------------------------------------------------------------------------------------------------------------------
2015-02-03 11:17:04 +01:00
void DialogEndLine::SetLineColor(const QString &value)
2015-02-03 10:27:33 +01:00
{
ChangeCurrentData(ui->comboBoxLineColor, value);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief DialogEndLine::ShowDialog show dialog after finish working with visual part
* @param click true if need show dialog after click mouse
*/
void DialogEndLine::ShowDialog(bool click)
{
if (prepare)
{
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;
}
/*We will ignore click if pointer is in point circle*/
VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
SCASSERT(scene != nullptr)
2015-02-03 11:17:04 +01:00
const QSharedPointer<VPointF> point = data->GeometricObject<VPointF>(GetBasePointId());
QLineF line = QLineF(static_cast<QPointF>(*point), scene->getScenePos());
//Radius of point circle, but little bigger. Need handle with hover sizes.
if (line.length() <= ScaledRadius(SceneScale(qApp->getCurrentScene()))*1.5)
{
return;
}
}
this->setModal(true);
VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
SCASSERT(line != nullptr)
2015-02-03 11:17:04 +01:00
this->SetAngle(line->Angle());//Show in dialog angle what user choose
this->SetFormula(line->Length());
emit ToolTip(QString());
timerFormulaLength->start();
this->show();
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::ShowVisualization()
{
AddVisualization<VisToolEndLine>();
2013-07-25 14:00:51 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::SaveData()
{
2013-07-25 14:00:51 +02:00
pointName = ui->lineEditNamePoint->text();
formulaLength = ui->plainTextEditFormula->toPlainText();
formulaAngle = ui->plainTextEditAngle->toPlainText();
VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
SCASSERT(line != nullptr)
line->setObject1Id(GetBasePointId());
line->setLength(formulaLength);
2015-02-03 11:17:04 +01:00
line->SetAngle(formulaAngle);
line->setLineStyle(LineStyleToPenStyle(GetTypeLine()));
line->RefreshGeometry();
2013-07-25 14:00:51 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEndLine::closeEvent(QCloseEvent *event)
{
ui->plainTextEditFormula->blockSignals(true);
ui->plainTextEditAngle->blockSignals(true);
DialogTool::closeEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
DialogEndLine::~DialogEndLine()
{
2013-07-25 14:00:51 +02:00
delete ui;
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogEndLine::GetPointName() const
{
return pointName;
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief GetTypeLine return type of line
* @return type
*/
2015-02-03 11:17:04 +01:00
QString DialogEndLine::GetTypeLine() const
{
return GetComboBoxCurrentData(ui->comboBoxLineType, TypeLineLine);
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief GetFormula return string of formula
* @return formula
*/
2015-02-03 11:17:04 +01:00
QString DialogEndLine::GetFormula() const
{
return qApp->TrVars()->TryFormulaFromUser(formulaLength, qApp->Settings()->GetOsSeparator());
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief GetAngle return formula angle of line
* @return angle formula
*/
2015-02-03 11:17:04 +01:00
QString DialogEndLine::GetAngle() const
{
return qApp->TrVars()->TryFormulaFromUser(formulaAngle, qApp->Settings()->GetOsSeparator());
}
//---------------------------------------------------------------------------------------------------------------------
/**
2015-02-03 11:17:04 +01:00
* @brief GetBasePointId return id base point of line
* @return id
*/
2015-02-03 11:17:04 +01:00
quint32 DialogEndLine::GetBasePointId() const
{
return getCurrentObjectId(ui->comboBoxBasePoint);
}