Improve visualization for tool Elliptical Arc.

This commit is contained in:
Roman Telezhynskyi 2022-08-24 18:53:25 +03:00
parent cdc291a40f
commit 1c0885e766
10 changed files with 308 additions and 33 deletions

View File

@ -289,7 +289,7 @@ void DialogArc::ShowDialog(bool click)
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
{
QLineF correction = line;
correction.setAngle(VisToolArc::CorrectAngle(correction.angle()));
correction.setAngle(Visualization::CorrectAngle(correction.angle()));
return correction.angle();
}

View File

@ -48,6 +48,8 @@
#include "../vgeometry/vellipticalarc.h"
#include "../qmuparser/qmudef.h"
#include "../vpatterndb/vcontainer.h"
#include "../vwidgets/global.h"
#include "../vwidgets/vabstractmainwindow.h"
//---------------------------------------------------------------------------------------------------------------------
/**
@ -542,6 +544,105 @@ void DialogEllipticalArc::DeployRotationAngleTextEdit()
DeployFormula(this, ui->plainTextEditRotationAngle, ui->pushButtonGrowLengthRotationAngle,formulaBaseHeightRotationAngle);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogEllipticalArc::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<VPointF> center = data->GeometricObject<VPointF>(GetCenter());
QLineF line = QLineF(static_cast<QPointF>(*center), scene->getScenePos());
auto Angle = [line]()
{
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
{
QLineF correction = line;
correction.setAngle(Visualization::CorrectAngle(correction.angle()));
return correction.angle();
}
return line.angle();
};
if (m_stage == 0) // radius 1
{
//Radius of point circle, but little bigger. Need to handle with hover sizes.
if (line.length() <= ScaledRadius(SceneScale(VAbstractValApplication::VApp()->getCurrentScene()))*1.5)
{
return;
}
SetRadius1(QString::number(FromPixel(line.length(), *data->GetPatternUnit())));
vis->RefreshGeometry();
++m_stage;
return;
}
if (m_stage == 1) // radius 2
{
QLineF radius2Line(center->x(), center->y(), center->x(), center->y() - 100);
QPointF p = VGObject::ClosestPoint(radius2Line, scene->getScenePos());
line = QLineF(static_cast<QPointF>(*center), p);
//Radius of point circle, but little bigger. Need to handle with hover sizes.
if (line.length() <= ScaledRadius(SceneScale(VAbstractValApplication::VApp()->getCurrentScene()))*1.5)
{
return;
}
SetRadius2(QString::number(FromPixel(line.length(), *data->GetPatternUnit())));
vis->RefreshGeometry();
++m_stage;
return;
}
if (m_stage == 2) // f1
{
SetF1(QString::number(Angle()));
vis->RefreshGeometry();
++m_stage;
return;
}
if (m_stage == 3) // f2
{
SetF2(QString::number(Angle()));
vis->RefreshGeometry();
++m_stage;
return;
}
auto *path = qobject_cast<VisToolEllipticalArc *>(vis);
SCASSERT(path != nullptr)
SetRotationAngle(QString::number(Angle() - path->StartingRotationAngle()));
}
vis->SetMode(Mode::Show);
vis->RefreshGeometry();
emit ToolTip(QString());
setModal(true);
show();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
@ -550,21 +651,27 @@ void DialogEllipticalArc::DeployRotationAngleTextEdit()
*/
void DialogEllipticalArc::ChosenObject(quint32 id, const SceneObject &type)
{
if (prepare == false)// After first choose we ignore all objects
if (prepare)// After first choose we ignore all objects
{
if (type == SceneObject::Point)
return;
}
if (type != SceneObject::Point)
{
return;
}
if (SetObject(id, ui->comboBoxBasePoint, QString()))
{
if (vis != nullptr)
{
if (SetObject(id, ui->comboBoxBasePoint, QString()))
{
if (vis != nullptr)
{
vis->VisualMode(id);
}
prepare = true;
this->setModal(true);
this->show();
}
auto *window = qobject_cast<VAbstractMainWindow *>(VAbstractValApplication::VApp()->getMainWindow());
SCASSERT(window != nullptr)
connect(vis, &Visualization::ToolTip, window, &VAbstractMainWindow::ShowToolTip);
vis->VisualMode(id);
}
prepare = true;
}
}

View File

@ -80,6 +80,8 @@ public:
void SetAliasSuffix(const QString &alias);
QString GetAliasSuffix() const;
void ShowDialog(bool click) override;
public slots:
virtual void ChosenObject(quint32 id, const SceneObject &type) override;
/**
@ -175,6 +177,10 @@ private:
QString originAliasSuffix{};
bool m_firstRelease{false};
int m_stage{0};
void EvalRadiuses();
void EvalAngles();
};

View File

@ -430,6 +430,7 @@ void VToolEllipticalArc::SetVisualization()
visual->SetF2(trVars->FormulaToUser(elArc->GetFormulaF2(), osSeparator));
visual->SetRotationAngle(trVars->FormulaToUser(elArc->GetFormulaRotationAngle(), osSeparator));
visual->SetLineStyle(LineStyleToPenStyle(elArc->GetPenStyle()));
visual->SetMode(Mode::Show);
visual->RefreshGeometry();
}
}

View File

@ -178,18 +178,6 @@ void VisToolArc::VisualMode(quint32 id)
StartVisualMode();
}
//---------------------------------------------------------------------------------------------------------------------
auto VisToolArc::CorrectAngle(qreal angle) -> qreal
{
qreal ang = angle;
if (angle > 360)
{
ang = angle - 360.0 * qFloor(angle/360);
}
return (qFloor(qAbs(ang)/5.)) * 5;
}
//---------------------------------------------------------------------------------------------------------------------
auto VisToolArc::StickyEnd(qreal angle) const -> qreal
{

View File

@ -58,7 +58,6 @@ public:
auto type() const -> int override {return Type;}
enum {Type = UserType + static_cast<int>(Vis::ToolArc)};
static auto CorrectAngle(qreal angle) -> qreal;
auto StickyEnd(qreal angle) const -> qreal;
private:

View File

@ -39,12 +39,31 @@
#include "../visualization.h"
#include "../vwidgets/scalesceneitems.h"
#include "vispath.h"
#include "../vmisc/vmodifierkey.h"
namespace
{
auto Angle(const QLineF &radius) -> qreal
{
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
{
QLineF correction = radius;
correction.setAngle(Visualization::CorrectAngle(correction.angle()));
return correction.angle();
}
return radius.angle();
}
} // namespace
//---------------------------------------------------------------------------------------------------------------------
VisToolEllipticalArc::VisToolEllipticalArc(const VContainer *data, QGraphicsItem *parent)
:VisPath(data, parent)
{
m_arcCenter = InitPoint(Color(VColor::MainColor), this);
m_radius1Line = InitItem<VScaledLine>(Color(VColor::SupportColor), this);
m_radius2Line = InitItem<VScaledLine>(Color(VColor::SupportColor), this);
m_f1Point = InitPoint(Color(VColor::SupportColor), this);
}
//---------------------------------------------------------------------------------------------------------------------
@ -55,11 +74,104 @@ void VisToolEllipticalArc::RefreshGeometry()
const QSharedPointer<VPointF> first = GetData()->GeometricObject<VPointF>(m_centerId);
DrawPoint(m_arcCenter, static_cast<QPointF>(*first), Color(VColor::SupportColor));
if (not qFuzzyIsNull(m_radius1) && not qFuzzyIsNull(m_radius2) && m_f1 >= 0 && m_f2 >= 0 && m_rotationAngle >= 0)
static const QString prefix = UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true);
QLineF radius (static_cast<QPointF>(*first), ScenePos());
auto center = static_cast<QPointF>(*first);
if (GetMode() == Mode::Creation)
{
VEllipticalArc elArc = VEllipticalArc(*first, m_radius1, m_radius2, m_f1, m_f2, m_rotationAngle);
DrawPath(this, elArc.GetPath(), elArc.DirectionArrows(), Color(VColor::MainColor), LineStyle(),
Qt::RoundCap);
if (qFuzzyIsNull(m_radius1))
{
DrawRadius1Line(center, radius.length());
SetToolTip(tr("<b>Elliptical arc</b>: radius1 = %1%2; "
"<b>Mouse click</b> - finish selecting the first radius, "
"<b>%3</b> - skip").arg(LengthToUser(radius.length()), prefix, VModifierKey::EnterKey()));
}
else if (qFuzzyIsNull(m_radius2))
{
DrawRadius2Line(center, m_radius1);
QLineF radius2Line(center.x(), center.y(), center.x(), center.y() - 100);
QPointF p = VGObject::ClosestPoint(radius2Line, ScenePos());
radius = QLineF(static_cast<QPointF>(*first), p);
DrawRadius2Line(center, radius.length());
DrawElArc(*first, m_radius1, radius.length(), 0, 0);
SetToolTip(tr("<b>Elliptical arc</b>: radius1 = %1%2, "
"radius2 = %3%2; "
"<b>Mouse click</b> - finish selecting the second radius, "
"<b>%4</b> - skip")
.arg(LengthToUser(m_radius1), prefix, LengthToUser(radius.length()),
VModifierKey::EnterKey()));
}
else if (m_f1 < 0)
{
DrawRadius1Line(center, m_radius1);
DrawRadius2Line(center, m_radius2);
qreal f1Angle = Angle(radius);
VEllipticalArc elArc = DrawElArc(*first, m_radius1, m_radius2, f1Angle, f1Angle);
DrawPoint(m_f1Point, elArc.GetP1(), Color(VColor::SupportColor));
SetToolTip(tr("<b>Elliptical arc</b>: radius1 = %1%2, "
"radius2 = %3%2, angle1 = %4°; "
"<b>Mouse click</b> - finish selecting the second radius, "
"<b>%5</b> - sticking angle, "
"<b>%6</b> - skip")
.arg(LengthToUser(m_radius1), prefix, LengthToUser(m_radius2),
AngleToUser(f1Angle), VModifierKey::Shift(), VModifierKey::EnterKey()));
}
else if (m_f2 < 0)
{
DrawRadius1Line(center, m_radius1);
DrawRadius2Line(center, m_radius2);
const qreal f2Angle = Angle(radius);
VEllipticalArc elArc = DrawElArc(*first, m_radius1, m_radius2, m_f1, f2Angle);
DrawPoint(m_f1Point, elArc.GetP1(), Color(VColor::SupportColor));
SetToolTip(tr("<b>Elliptical arc</b>: radius1 = %1%2, "
"radius2 = %3%2, angle1 = %4°, angle2 = %5°; "
"<b>Mouse click</b> - finish selecting the second radius, "
"<b>%6</b> - sticking angle, "
"<b>%7</b> - skip")
.arg(LengthToUser(m_radius1), prefix, LengthToUser(m_radius2),
AngleToUser(m_f1), AngleToUser(f2Angle), VModifierKey::Shift(),
VModifierKey::EnterKey()));
}
else if (VFuzzyComparePossibleNulls(m_rotationAngle, INT_MAX))
{
if (VFuzzyComparePossibleNulls(m_startingRotationAngle, INT_MAX))
{
m_startingRotationAngle = radius.angle();
}
qreal rotationAngle = Angle(radius) - m_startingRotationAngle;
DrawRadius1Line(center, m_radius1, rotationAngle);
DrawRadius2Line(center, m_radius2, rotationAngle);
VEllipticalArc elArc = DrawElArc(*first, m_radius1, m_radius2, m_f1, m_f2, rotationAngle);
DrawPoint(m_f1Point, elArc.GetP1(), Color(VColor::SupportColor));
SetToolTip(tr("<b>Elliptical arc</b>: radius1 = %1%2, "
"radius2 = %3%2, angle1 = %4°, angle2 = %5°, rotation = %6°; "
"<b>Mouse click</b> - finish selecting the second radius, "
"<b>%7</b> - sticking angle, "
"<b>%8</b> - skip")
.arg(LengthToUser(m_radius1), prefix, LengthToUser(radius.length()),
AngleToUser(m_f1), AngleToUser(m_f2), AngleToUser(rotationAngle),
VModifierKey::Shift(), VModifierKey::EnterKey()));
}
}
else if (not qFuzzyIsNull(m_radius1) && not qFuzzyIsNull(m_radius2) && m_f1 >= 0 && m_f2 >= 0 &&
not VFuzzyComparePossibleNulls(m_rotationAngle, INT_MAX))
{
DrawElArc(*first, m_radius1, m_radius2, m_f1, m_f2, m_rotationAngle);
}
}
}
@ -100,3 +212,31 @@ void VisToolEllipticalArc::SetRotationAngle(const QString &expression)
{
m_rotationAngle = FindValFromUser(expression, GetData()->DataVariables());
}
//---------------------------------------------------------------------------------------------------------------------
void VisToolEllipticalArc::DrawRadius1Line(const QPointF &center, qreal radius, qreal rotationAngle)
{
QLineF radiusLine(center.x(), center.y(), center.x() + 100, center.y());
radiusLine.setLength(radius);
radiusLine.setAngle(radiusLine.angle() + rotationAngle);
DrawLine(m_radius1Line, radiusLine, Color(VColor::SupportColor));
}
//---------------------------------------------------------------------------------------------------------------------
void VisToolEllipticalArc::DrawRadius2Line(const QPointF &center, qreal radius, qreal rotationAngle)
{
QLineF radiusLine(center.x(), center.y(), center.x(), center.y() - 100);
radiusLine.setLength(radius);
radiusLine.setAngle(radiusLine.angle() + rotationAngle);
DrawLine(m_radius2Line, radiusLine, Color(VColor::SupportColor));
}
//---------------------------------------------------------------------------------------------------------------------
auto VisToolEllipticalArc::DrawElArc(const VPointF &center, qreal radius1, qreal radius2, qreal f1, qreal f2,
qreal rotationAngle) -> VEllipticalArc
{
VEllipticalArc elArc(center, radius1, radius2, f1, f2, rotationAngle);
DrawPath(this, elArc.GetPath(), elArc.DirectionArrows(), Color(VColor::MainColor), LineStyle(), Qt::RoundCap);
return elArc;
}

View File

@ -38,6 +38,9 @@
#include "../vmisc/def.h"
#include "vispath.h"
class VPointF;
class VEllipticalArc;
class VisToolEllipticalArc : public VisPath
{
Q_OBJECT // NOLINT
@ -55,17 +58,28 @@ public:
void SetF2(const QString &expression);
void SetRotationAngle(const QString &expression);
auto StartingRotationAngle() const -> qreal;
auto type() const -> int override {return Type;}
enum {Type = UserType + static_cast<int>(Vis::ToolEllipticalArc)};
private:
Q_DISABLE_COPY_MOVE(VisToolEllipticalArc) // NOLINT
VScaledEllipse *m_arcCenter{nullptr};
VScaledLine *m_radius1Line{nullptr};
VScaledLine *m_radius2Line{nullptr};
VScaledEllipse *m_f1Point{nullptr};
qreal m_radius1{0};
qreal m_radius2{0};
qreal m_f1{0};
qreal m_f2{0};
qreal m_rotationAngle{0};
qreal m_f1{-1};
qreal m_f2{-1};
qreal m_startingRotationAngle{INT_MAX};
qreal m_rotationAngle{INT_MAX};
quint32 m_centerId{NULL_ID};
void DrawRadius1Line(const QPointF &center, qreal radius, qreal rotationAngle = 0);
void DrawRadius2Line(const QPointF &center, qreal radius, qreal rotationAngle = 0);
auto DrawElArc(const VPointF &center, qreal radius1, qreal radius2, qreal f1, qreal f2,
qreal rotationAngle = 0) -> VEllipticalArc;
};
//---------------------------------------------------------------------------------------------------------------------
@ -74,4 +88,10 @@ inline void VisToolEllipticalArc::SetCenterId(quint32 newCenterId)
m_centerId = newCenterId;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VisToolEllipticalArc::StartingRotationAngle() const -> qreal
{
return m_startingRotationAngle;
}
#endif // VISTOOLELLIPTICALARC_H

View File

@ -43,6 +43,7 @@
#include <QString>
#include <Qt>
#include <QtDebug>
#include <QtMath>
#include "../vpatterndb/calculator.h"
#include "../vpatterndb/vtranslatevars.h"
@ -199,6 +200,18 @@ auto Visualization::FindValFromUser(const QString &expression,
return val;
}
//---------------------------------------------------------------------------------------------------------------------
qreal Visualization::CorrectAngle(qreal angle)
{
qreal ang = angle;
if (angle > 360)
{
ang = angle - 360.0 * qFloor(angle/360);
}
return (qFloor(qAbs(ang)/5.)) * 5;
}
//---------------------------------------------------------------------------------------------------------------------
void Visualization::RefreshToolTip() const
{

View File

@ -84,6 +84,7 @@ public:
QSharedPointer<VInternalVariable> > *vars, bool fromUser = true) -> qreal;
static auto FindValFromUser(const QString &expression, const QHash<QString,
QSharedPointer<VInternalVariable> > *vars, bool fromUser = true) -> qreal;
static auto CorrectAngle(qreal angle) -> qreal;
auto CurrentToolTip() const -> QString;