Improve creating an arc by length.

This commit is contained in:
Roman Telezhynskyi 2022-05-11 19:08:08 +03:00
parent b8872a0319
commit 43aee75f64
6 changed files with 296 additions and 115 deletions

View File

@ -301,28 +301,20 @@ void DialogArc::ShowDialog(bool click)
return;
}
QString radius = QString::number(VAbstractValApplication::VApp()->fromPixel(line.length()));
arcVis->setRadius(radius);
SetRadius(radius);
SetRadius(QString::number(VAbstractValApplication::VApp()->fromPixel(line.length())));
arcVis->RefreshGeometry();
stageRadius = false;
stageF1 = true;
}
else if (stageF1)
{
QString f1 = QString::number(Angle());
arcVis->setF1(f1);
SetF1(f1);
SetF1(QString::number(Angle()));
arcVis->RefreshGeometry();
stageF1 = false;
}
else
{
QString f2 = QString::number(arcVis->StickyEnd(Angle()));
arcVis->setF2(f2);
SetF2(f2);
SetF2(QString::number(arcVis->StickyEnd(Angle())));
FinishCreating();
}
@ -395,7 +387,8 @@ void DialogArc::ChosenObject(quint32 id, const SceneObject &type)
{
if (vis != nullptr)
{
auto *window = qobject_cast<VAbstractMainWindow *>(VAbstractValApplication::VApp()->getMainWindow());
auto *window = qobject_cast<VAbstractMainWindow *>(
VAbstractValApplication::VApp()->getMainWindow());
SCASSERT(window != nullptr)
connect(vis.data(), &Visualization::ToolTip, window, &VAbstractMainWindow::ShowToolTip);

View File

@ -49,23 +49,15 @@
#include "ui_dialogarcwithlength.h"
#include "../vgeometry/varc.h"
#include "../qmuparser/qmudef.h"
#include "../vwidgets/vabstractmainwindow.h"
//---------------------------------------------------------------------------------------------------------------------
DialogArcWithLength::DialogArcWithLength(const VContainer *data, quint32 toolId, QWidget *parent)
: DialogTool(data, toolId, parent),
ui(new Ui::DialogArcWithLength),
flagRadius(false),
flagF1(false),
flagLength(false),
timerRadius(new QTimer(this)),
timerF1(new QTimer(this)),
timerLength(new QTimer(this)),
radius(),
f1(),
length(),
formulaBaseHeightRadius(0),
formulaBaseHeightF1(0),
formulaBaseHeightLength(0)
timerLength(new QTimer(this))
{
ui->setupUi(this);
@ -132,7 +124,7 @@ DialogArcWithLength::~DialogArcWithLength()
}
//---------------------------------------------------------------------------------------------------------------------
quint32 DialogArcWithLength::GetCenter() const
auto DialogArcWithLength::GetCenter() const -> quint32
{
return getCurrentObjectId(ui->comboBoxCenter);
}
@ -145,10 +137,9 @@ void DialogArcWithLength::SetCenter(const quint32 &value)
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogArcWithLength::GetRadius() const
auto DialogArcWithLength::GetRadius() const -> QString
{
return VAbstractApplication::VApp()->TrVars()
->TryFormulaFromUser(radius, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
return VTranslateVars::TryFormulaFromUser(radius, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
}
//---------------------------------------------------------------------------------------------------------------------
@ -163,7 +154,7 @@ void DialogArcWithLength::SetRadius(const QString &value)
}
ui->plainTextEditRadius->setPlainText(radius);
VisToolArcWithLength *path = qobject_cast<VisToolArcWithLength *>(vis);
auto *path = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(path != nullptr)
path->setRadius(radius);
@ -171,12 +162,12 @@ void DialogArcWithLength::SetRadius(const QString &value)
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogArcWithLength::GetF1() const
auto DialogArcWithLength::GetF1() const -> QString
{
return VAbstractApplication::VApp()->TrVars()
->TryFormulaFromUser(f1, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
return VTranslateVars::TryFormulaFromUser(f1, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
}
//---------------------------------------------------------------------------------------------------------------------
void DialogArcWithLength::SetF1(const QString &value)
{
f1 = VAbstractApplication::VApp()->TrVars()
@ -188,7 +179,7 @@ void DialogArcWithLength::SetF1(const QString &value)
}
ui->plainTextEditF1->setPlainText(f1);
VisToolArcWithLength *path = qobject_cast<VisToolArcWithLength *>(vis);
auto *path = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(path != nullptr)
path->setF1(f1);
@ -196,10 +187,9 @@ void DialogArcWithLength::SetF1(const QString &value)
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogArcWithLength::GetLength() const
auto DialogArcWithLength::GetLength() const -> QString
{
return VAbstractApplication::VApp()->TrVars()
->TryFormulaFromUser(length, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
return VTranslateVars::TryFormulaFromUser(length, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
}
//---------------------------------------------------------------------------------------------------------------------
@ -214,15 +204,15 @@ void DialogArcWithLength::SetLength(const QString &value)
}
ui->plainTextEditLength->setPlainText(length);
VisToolArcWithLength *path = qobject_cast<VisToolArcWithLength *>(vis);
auto *path = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(path != nullptr)
path->setLength(radius);
path->setLength(length);
MoveCursorToEnd(ui->plainTextEditLength);
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogArcWithLength::GetPenStyle() const
auto DialogArcWithLength::GetPenStyle() const -> QString
{
return GetComboBoxCurrentData(ui->comboBoxPenStyle, TypeLineLine);
}
@ -234,7 +224,7 @@ void DialogArcWithLength::SetPenStyle(const QString &value)
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogArcWithLength::GetColor() const
auto DialogArcWithLength::GetColor() const -> QString
{
return GetComboBoxCurrentData(ui->comboBoxColor, ColorBlack);
}
@ -246,7 +236,7 @@ void DialogArcWithLength::SetColor(const QString &value)
}
//---------------------------------------------------------------------------------------------------------------------
qreal DialogArcWithLength::GetApproximationScale() const
auto DialogArcWithLength::GetApproximationScale() const -> qreal
{
return ui->doubleSpinBoxApproximationScale->value();
}
@ -256,7 +246,7 @@ void DialogArcWithLength::SetApproximationScale(qreal value)
{
ui->doubleSpinBoxApproximationScale->setValue(value);
VisToolArcWithLength *path = qobject_cast<VisToolArcWithLength *>(vis);
auto *path = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(path != nullptr)
path->setApproximationScale(value);
}
@ -268,7 +258,7 @@ void DialogArcWithLength::SetNotes(const QString &notes)
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogArcWithLength::GetNotes() const
auto DialogArcWithLength::GetNotes() const -> QString
{
return ui->plainTextEditToolNotes->toPlainText();
}
@ -282,15 +272,101 @@ void DialogArcWithLength::SetAliasSuffix(const QString &alias)
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogArcWithLength::GetAliasSuffix() const
auto DialogArcWithLength::GetAliasSuffix() const -> QString
{
return ui->lineEditAlias->text();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogArcWithLength::ShowDialog(bool click)
{
if (prepare)
{
auto *arcVis = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(arcVis != nullptr)
auto FinishCreating = [this, arcVis]()
{
arcVis->SetMode(Mode::Show);
arcVis->RefreshGeometry();
emit ToolTip(QString());
setModal(true);
show();
};
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*/
auto *scene = qobject_cast<VMainGraphicsScene *>(VAbstractValApplication::VApp()->getCurrentScene());
SCASSERT(scene != nullptr)
const QSharedPointer<VPointF> point = data->GeometricObject<VPointF>(GetCenter());
QLineF line = QLineF(static_cast<QPointF>(*point), scene->getScenePos());
auto Angle = [line]()
{
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
{
QLineF correction = line;
correction.setAngle(VisToolArcWithLength::CorrectAngle(correction.angle()));
return correction.angle();
}
return line.angle();
};
if (stageRadius)
{
//Radius of point circle, but little bigger. Need handle with hover sizes.
if (line.length() <= ScaledRadius(SceneScale(VAbstractValApplication::VApp()->getCurrentScene()))*1.5)
{
return;
}
SetRadius(QString::number(VAbstractValApplication::VApp()->fromPixel(line.length())));
arcVis->RefreshGeometry();
stageRadius = false;
stageF1 = true;
}
else if (stageF1)
{
SetF1(QString::number(Angle()));
arcVis->RefreshGeometry();
stageF1 = false;
}
else
{
VArc arc(*point, VAbstractValApplication::VApp()->toPixel(radius.toDouble()), f1.toDouble(),
line.angle());
SetLength(QString::number(VAbstractValApplication::VApp()->fromPixel(arc.GetLength())));
FinishCreating();
}
return;
}
FinishCreating();
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogArcWithLength::ChosenObject(quint32 id, const SceneObject &type)
{
if (prepare == false)// After first choose we ignore all objects
if (not prepare)// After first choose we ignore all objects
{
if (type == SceneObject::Point)
{
@ -298,11 +374,16 @@ void DialogArcWithLength::ChosenObject(quint32 id, const SceneObject &type)
{
if (vis != nullptr)
{
auto *window = qobject_cast<VAbstractMainWindow *>(
VAbstractValApplication::VApp()->getMainWindow());
SCASSERT(window != nullptr)
connect(vis.data(), &Visualization::ToolTip, window, &VAbstractMainWindow::ShowToolTip);
vis->VisualMode(id);
vis->RefreshToolTip();
}
prepare = true;
this->setModal(true);
this->show();
}
}
}
@ -329,7 +410,7 @@ void DialogArcWithLength::DeployLengthTextEdit()
//---------------------------------------------------------------------------------------------------------------------
void DialogArcWithLength::FXRadius()
{
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, toolId, this);
auto *dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit radius"));
dialog->SetFormula(GetRadius());
dialog->setPostfix(UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true));
@ -343,7 +424,7 @@ void DialogArcWithLength::FXRadius()
//---------------------------------------------------------------------------------------------------------------------
void DialogArcWithLength::FXF1()
{
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, toolId, this);
auto *dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit the first angle"));
dialog->SetFormula(GetF1());
dialog->setPostfix(degreeSymbol);
@ -357,7 +438,7 @@ void DialogArcWithLength::FXF1()
//---------------------------------------------------------------------------------------------------------------------
void DialogArcWithLength::FXLength()
{
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data, toolId, this);
auto *dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit the arc length"));
dialog->SetFormula(GetLength());
dialog->setPostfix(UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true));
@ -381,7 +462,7 @@ void DialogArcWithLength::SaveData()
f1 = ui->plainTextEditF1->toPlainText();
length = ui->plainTextEditLength->toPlainText();
VisToolArcWithLength *path = qobject_cast<VisToolArcWithLength *>(vis);
auto *path = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(path != nullptr)
path->setObject1Id(GetCenter());

View File

@ -36,6 +36,7 @@
#include <QtGlobal>
#include "../vmisc/def.h"
#include "defglobal.h"
#include "dialogtool.h"
namespace Ui
@ -45,40 +46,42 @@ namespace Ui
class DialogArcWithLength final : public DialogTool
{
Q_OBJECT
Q_OBJECT // NOLINT
public:
DialogArcWithLength(const VContainer *data, quint32 toolId, QWidget *parent = nullptr);
~DialogArcWithLength();
~DialogArcWithLength() override;
quint32 GetCenter() const;
auto GetCenter() const -> quint32;
void SetCenter(const quint32 &value);
QString GetRadius() const;
auto GetRadius() const -> QString;
void SetRadius(const QString &value);
QString GetF1() const;
auto GetF1() const -> QString;
void SetF1(const QString &value);
QString GetLength() const;
auto GetLength() const -> QString;
void SetLength(const QString &value);
QString GetPenStyle() const;
auto GetPenStyle() const -> QString;
void SetPenStyle(const QString &value);
QString GetColor() const;
auto GetColor() const -> QString;
void SetColor(const QString &value);
qreal GetApproximationScale() const;
auto GetApproximationScale() const -> qreal;
void SetApproximationScale(qreal value);
void SetNotes(const QString &notes);
QString GetNotes() const;
auto GetNotes() const -> QString;
void SetAliasSuffix(const QString &alias);
QString GetAliasSuffix() const;
auto GetAliasSuffix() const -> QString;
void ShowDialog(bool click) override;
public slots:
virtual void ChosenObject(quint32 id, const SceneObject &type) override;
void ChosenObject(quint32 id, const SceneObject &type) override;
/**
* @brief DeployFormulaTextEdit grow or shrink formula input
*/
@ -91,28 +94,28 @@ public slots:
void FXLength();
protected:
virtual void ShowVisualization() override;
void ShowVisualization() override;
/**
* @brief SaveData Put dialog data in local variables
*/
virtual void SaveData() override;
virtual void closeEvent(QCloseEvent *event) override;
virtual bool IsValid() const final;
void SaveData() override;
void closeEvent(QCloseEvent *event) override;
auto IsValid() const -> bool final;
private slots:
void ValidateAlias();
private:
Q_DISABLE_COPY(DialogArcWithLength)
Q_DISABLE_COPY_MOVE(DialogArcWithLength) // NOLINT
Ui::DialogArcWithLength *ui;
/** @brief flagRadius true if value of radius is correct */
bool flagRadius;
bool flagRadius{false};
/** @brief flagF1 true if value of first angle is correct */
bool flagF1;
bool flagF1{false};
bool flagLength;
bool flagLength{false};
bool flagAlias{true};
@ -125,27 +128,32 @@ private:
QTimer *timerLength;
/** @brief radius formula of radius */
QString radius;
QString radius{};
/** @brief f1 formula of first angle */
QString f1;
QString f1{};
QString length;
QString length{};
/** @brief formulaBaseHeight base height defined by dialogui */
int formulaBaseHeightRadius;
int formulaBaseHeightF1;
int formulaBaseHeightLength;
int formulaBaseHeightRadius{0};
int formulaBaseHeightF1{0};
int formulaBaseHeightLength{0};
QString originAliasSuffix{};
bool stageRadius{true};
bool stageF1{false};
bool m_firstRelease{false};
void Radius();
void Length();
void EvalF();
};
//---------------------------------------------------------------------------------------------------------------------
inline bool DialogArcWithLength::IsValid() const
inline auto DialogArcWithLength::IsValid() const -> bool
{
return flagRadius && flagF1 && flagLength && flagAlias;
}

View File

@ -347,7 +347,7 @@ void VToolArcWithLength::SetVisualization()
if (not vis.isNull())
{
const QSharedPointer<VArc> arc = VAbstractTool::data.GeometricObject<VArc>(m_id);
VisToolArcWithLength *visual = qobject_cast<VisToolArcWithLength *>(vis);
auto *visual = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(visual != nullptr)
const VTranslateVars *trVars = VAbstractApplication::VApp()->TrVars();
@ -360,6 +360,7 @@ void VToolArcWithLength::SetVisualization()
VAbstractApplication::VApp()->Settings()->GetOsSeparator()));
visual->setLineStyle(LineStyleToPenStyle(arc->GetPenStyle()));
visual->setApproximationScale(arc->GetApproximationScale());
visual->SetMode(Mode::Show);
visual->RefreshGeometry();
}
}

View File

@ -42,12 +42,14 @@
#include "../visualization.h"
#include "../vwidgets/scalesceneitems.h"
#include "vispath.h"
#include "../vmisc/vmodifierkey.h"
//---------------------------------------------------------------------------------------------------------------------
VisToolArcWithLength::VisToolArcWithLength(const VContainer *data, QGraphicsItem *parent)
:VisPath(data, parent), arcCenter(nullptr), radius(0), f1(0), length(0)
:VisPath(data, parent)
{
arcCenter = InitPoint(mainColor, this);
f1Point = InitPoint(supportColor, this);
}
//---------------------------------------------------------------------------------------------------------------------
@ -55,15 +57,95 @@ void VisToolArcWithLength::RefreshGeometry()
{
if (object1Id > NULL_ID)
{
f1Point->setVisible(false);
const QSharedPointer<VPointF> first = Visualization::data->GeometricObject<VPointF>(object1Id);
DrawPoint(arcCenter, static_cast<QPointF>(*first), supportColor);
if (mode == Mode::Creation)
{
QLineF r = QLineF(static_cast<QPointF>(*first), Visualization::scenePos);
auto Angle = [r]()
{
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
{
QLineF correction = r;
correction.setAngle(CorrectAngle(correction.angle()));
return correction.angle();
}
return r.angle();
};
auto NumberToUser = [](qreal value)
{
return VAbstractApplication::VApp()->TrVars()
->FormulaToUser(QString::number(VAbstractValApplication::VApp()->fromPixel(value)),
VAbstractApplication::VApp()->Settings()->GetOsSeparator());
};
static const QString prefix = UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true);
if (qFuzzyIsNull(radius))
{
VArc arc = VArc (*first, r.length(), r.angle(), r.angle());
arc.SetApproximationScale(m_approximationScale);
DrawPath(this, arc.GetPath(), QVector<DirectionArrow>(), supportColor, Qt::DashLine, Qt::RoundCap);
Visualization::toolTip = tr("<b>Arc</b>: radius = %1%2; "
"<b>Mouse click</b> - finish selecting the radius, "
"<b>%3</b> - skip")
.arg(NumberToUser(r.length()), prefix, VModifierKey::EnterKey());
}
else if (f1 < 0)
{
qreal f1Angle = Angle();
VArc arc = VArc (*first, radius, f1Angle, f1Angle);
arc.SetApproximationScale(m_approximationScale);
DrawPath(this, arc.GetPath(), QVector<DirectionArrow>(), supportColor, Qt::DashLine, Qt::RoundCap);
QLineF f1Line = r;
f1Line.setLength(radius);
f1Line.setAngle(f1Angle);
DrawPoint(f1Point, f1Line.p2(), supportColor);
Visualization::toolTip = tr("<b>Arc</b>: radius = %1%2, first angle = %3°; "
"<b>Mouse click</b> - finish selecting the first angle, "
"<b>%4</b> - sticking angle, "
"<b>%5</b> - skip")
.arg(NumberToUser(radius), prefix)
.arg(f1Angle)
.arg(VModifierKey::Shift(), VModifierKey::EnterKey());
}
else if (f1 >= 0)
{
VArc arc = VArc (*first, radius, f1, r.angle());
arc.SetApproximationScale(m_approximationScale);
DrawPath(this, arc.GetPath(), arc.DirectionArrows(), mainColor, lineStyle, Qt::RoundCap);
Visualization::toolTip = tr("<b>Arc</b>: radius = %1%2, first angle = %3°, arc length = %4%2; "
"<b>Mouse click</b> - finish creating, "
"<b>%5</b> - skip")
.arg(NumberToUser(radius), prefix)
.arg(f1)
.arg(NumberToUser(arc.GetLength()), VModifierKey::EnterKey());
}
}
else
{
if (not qFuzzyIsNull(radius) && f1 >= 0 && not qFuzzyIsNull(length))
{
VArc arc = VArc (length, *first, radius, f1);
arc.SetApproximationScale(m_approximationScale);
DrawPath(this, arc.GetPath(), arc.DirectionArrows(), mainColor, lineStyle, Qt::RoundCap);
}
else
{
DrawPath(this, QPainterPath(), QVector<DirectionArrow>(), mainColor, lineStyle, Qt::RoundCap);
}
}
}
}
@ -84,3 +166,15 @@ void VisToolArcWithLength::setLength(const QString &expression)
{
length = FindLengthFromUser(expression, Visualization::data->DataVariables());
}
//---------------------------------------------------------------------------------------------------------------------
auto VisToolArcWithLength::CorrectAngle(qreal angle) -> qreal
{
qreal ang = angle;
if (angle > 360)
{
ang = angle - 360.0 * qFloor(angle/360);
}
return (qFloor(qAbs(ang)/5.)) * 5;
}

View File

@ -38,26 +38,30 @@
#include "../vmisc/def.h"
#include "vispath.h"
#include "defglobal.h"
class VisToolArcWithLength final : public VisPath
{
Q_OBJECT
Q_OBJECT // NOLINT
public:
explicit VisToolArcWithLength(const VContainer *data, QGraphicsItem *parent = nullptr);
virtual ~VisToolArcWithLength() Q_DECL_EQ_DEFAULT;
~VisToolArcWithLength() override =default;
virtual void RefreshGeometry() override;
void RefreshGeometry() override;
void setRadius(const QString &expression);
void setF1(const QString &expression);
void setLength(const QString &expression);
virtual int type() const override {return Type;}
enum { Type = UserType + static_cast<int>(Vis::ToolArcWithLength)};
auto type() const -> int override {return Type;}
enum {Type = UserType + static_cast<int>(Vis::ToolArcWithLength)};
static auto CorrectAngle(qreal angle) -> qreal;
private:
Q_DISABLE_COPY(VisToolArcWithLength)
VScaledEllipse *arcCenter;
qreal radius;
qreal f1;
qreal length;
Q_DISABLE_COPY_MOVE(VisToolArcWithLength) // NOLINT
VScaledEllipse *arcCenter{nullptr};
VScaledEllipse *f1Point{nullptr};
qreal radius{0};
qreal f1{-1};
qreal length{0};
};
#endif // VISTOOLARCWITHLENGTH_H