From dfce9015fed13f530383d7b79d9050a3d05f8cf0 Mon Sep 17 00:00:00 2001 From: cpdt Date: Tue, 30 Oct 2018 18:20:18 +1000 Subject: [PATCH] only select the number portion when editing the value of a NumControl, fixes #127 --- editor/util.cpp | 17 ++++++++++++----- editor/util.h | 2 +- editor/widgets/controls/NumControlItem.cpp | 4 +++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/editor/util.cpp b/editor/util.cpp index 92eab8da..d5a8904c 100644 --- a/editor/util.cpp +++ b/editor/util.cpp @@ -98,9 +98,10 @@ QString AxiomUtil::formatChannelFull(float val, AxiomModel::FormType form) { return static_cast(formatFloatForm(val, form) % " " % getFormUnit(val, form)).trimmed(); } -QString AxiomUtil::formatNumForm(AxiomModel::NumValue value, bool includeForm) { +QString AxiomUtil::formatNumForm(AxiomModel::NumValue value, bool includeForm, int *outNumLength) { if (fabsf(value.left - value.right) < 0.01f) { auto formattedNum = formatFloatForm(value.left, value.form); + if (outNumLength) *outNumLength = formattedNum.size(); return includeForm ? static_cast(formattedNum % " " % getFormUnit(value.left, value.form)) : formattedNum; } else { @@ -111,10 +112,16 @@ QString AxiomUtil::formatNumForm(AxiomModel::NumValue value, bool includeForm) { auto leftUnit = getFormUnit(value.left, value.form); auto rightUnit = getFormUnit(value.right, value.form); - if (leftUnit == rightUnit) - return formattedLeft % " / " % formattedRight % " " % leftUnit; - else - return formattedLeft % " " % leftUnit % " / " % formattedRight % " " % rightUnit; + if (leftUnit == rightUnit) { + auto leftSide = static_cast(formattedLeft % " / " % formattedRight); + if (outNumLength) *outNumLength = leftSide.size(); + return leftSide % " " % leftUnit; + } else { + auto resultStr = + static_cast(formattedLeft % " " % leftUnit % " / " % formattedRight % " " % rightUnit); + if (outNumLength) *outNumLength = resultStr.size(); + return resultStr; + } } } diff --git a/editor/util.h b/editor/util.h index f1d2ad23..ceefcb26 100644 --- a/editor/util.h +++ b/editor/util.h @@ -33,7 +33,7 @@ namespace AxiomUtil { QString formatChannelFull(float val, AxiomModel::FormType form); - QString formatNumForm(AxiomModel::NumValue value, bool includeForm); + QString formatNumForm(AxiomModel::NumValue value, bool includeForm, int *outNumLength = nullptr); QRect makeRect(QPoint p1, QPoint p2); diff --git a/editor/widgets/controls/NumControlItem.cpp b/editor/widgets/controls/NumControlItem.cpp index eb28acd3..c5db1403 100644 --- a/editor/widgets/controls/NumControlItem.cpp +++ b/editor/widgets/controls/NumControlItem.cpp @@ -322,7 +322,9 @@ void NumControlItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { auto selectedAction = menu.exec(event->screenPos()); if (selectedAction == setValAction) { - auto editor = new FloatingValueEditor(AxiomUtil::formatNumForm(control->value(), true), event->scenePos()); + int formatNumLength; + auto formatNumString = AxiomUtil::formatNumForm(control->value(), true, &formatNumLength); + auto editor = new FloatingValueEditor(std::move(formatNumString), event->scenePos(), 0, formatNumLength); scene()->addItem(editor); connect(editor, &FloatingValueEditor::valueSubmitted, this, &NumControlItem::setStringValue); } else if (selectedAction == copyValAction) {