-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[ui] Eval expression directly in specific TextFields #2836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cbentejac
merged 4 commits into
develop
from
dev/enable_math_expressions_on_text_fields
Sep 10, 2025
+220
−27
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d18e35a
Add ExpressionTextField component for float/int TextFields with expre…
Alxiice 7409178
[ui] MathEvaluator : Fix issues in the expression evaluation
Alxiice 56215fb
[ui] ExpressionTextField : fix issues with undostack, error status, n…
Alxiice 5bc6fdb
[ui] ExpressionTextField : Make sure we keep the connection to gainLa…
Alxiice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import QtQuick | ||
| import QtQuick.Controls | ||
|
|
||
| TextField { | ||
| id: root | ||
|
|
||
| // evaluated numeric value (NaN if invalid) | ||
| // It helps keeping the connection that text has so that we don't loose ability to undo/reset | ||
| property real evaluatedValue: 0 | ||
|
|
||
| property bool hasExprError: false | ||
| property bool isInt: false | ||
| property int decimals: 2 | ||
|
|
||
| // Overlay for error state (red border on top of default background) | ||
| Rectangle { | ||
| anchors.fill: parent | ||
| radius: 4 | ||
| border.color: "red" | ||
| color: "transparent" | ||
| visible: root.hasExprError | ||
| z: 1 | ||
| } | ||
|
|
||
| function raiseError() { | ||
| hasExprError = true | ||
| } | ||
|
|
||
| function clearError() { | ||
| hasExprError = false | ||
| } | ||
|
|
||
| function reset(_value) { | ||
| clearError() | ||
| evaluatedValue = _value | ||
| if (isInt) { | ||
| root.text = _value.toFixed(0) | ||
| } else { | ||
| root.text = _value.toFixed(decimals) | ||
| } | ||
| } | ||
|
|
||
| function getEvalExpression(_text) { | ||
| try { | ||
| var result = MathEvaluator.eval(_text) | ||
| if (isInt) | ||
| result = Math.round(result) | ||
| else | ||
| result = result.toFixed(decimals) | ||
| return result | ||
| } catch (err) { | ||
| console.error("Error evaluating expression (", _text, "):", err) | ||
| return NaN | ||
| } | ||
| } | ||
|
|
||
| function refreshStatus() { | ||
| if (isNaN(getEvalExpression(root.text))) { | ||
| raiseError() | ||
| } else { | ||
| clearError() | ||
| } | ||
| } | ||
|
|
||
| function updateExpression() { | ||
| var result = getEvalExpression(root.text) | ||
| if (!isNaN(result)) { | ||
| evaluatedValue = result | ||
| clearError() | ||
| return result | ||
| } else { | ||
| evaluatedValue = NaN | ||
| raiseError() | ||
| return NaN | ||
| } | ||
| } | ||
|
|
||
| // When user commits input, evaluate but do NOT overwrite text | ||
| onAccepted: { | ||
| updateExpression() | ||
| } | ||
|
|
||
| onEditingFinished: { | ||
| updateExpression() | ||
| } | ||
|
|
||
| onTextChanged: { | ||
| if (!activeFocus) { | ||
| refreshStatus() | ||
| } | ||
| } | ||
|
|
||
| Component.onCompleted: { | ||
| refreshStatus() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| .pragma library | ||
|
|
||
|
|
||
| var symbols = { | ||
| pi: Math.PI, | ||
| e: Math.E, | ||
| }; | ||
|
|
||
| var functions = { | ||
| abs: Math.abs, | ||
| min: Math.min, | ||
| max: Math.max, | ||
| sin: Math.sin, | ||
| cos: Math.cos, | ||
| tan: Math.tan, | ||
| pow: Math.pow, | ||
| sqrt: Math.sqrt, | ||
| exp: Math.exp, | ||
| log: Math.log | ||
| }; | ||
|
|
||
|
|
||
| function removeLeadingZeros(str) { | ||
| return str.replace(/\b0*(\d+(?:\.\d*)?)/g, (match, number) => { | ||
| // If the number starts with a decimal, add a leading zero | ||
| if (number.startsWith('.')) { | ||
| return '0' + number; | ||
| } | ||
| // Otherwise just return the number without leading zeros | ||
| return number; | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Evaluate an expression | ||
| * | ||
| * @param {*} expr Math expression | ||
| * @returns float or int | ||
| */ | ||
| function eval(expr) { | ||
| // Warning : commas are for separating function args and dot to indicate decimals | ||
|
|
||
| // Only allow numbers, operators, parentheses, and function names | ||
| if (!/^[0-9+\-*/^()e.,\s]*$/.test(expr.replace(/\b[a-zA-Z]+\b/g, ""))) { | ||
| throw "Invalid characters in expression"; | ||
| } | ||
|
|
||
| expr = removeLeadingZeros(expr); | ||
|
|
||
| // Replace symbols and functions | ||
| for (var symbol in symbols) { | ||
| expr = expr.replace(new RegExp("\\b" + symbol + "\\b", "g"), symbols[symbol]); | ||
| } | ||
| for (var func in functions) { // Warning : not really a map | ||
| expr = expr.replace(new RegExp("\\b" + func + "\\b", "g"), "Math." + func); | ||
| } | ||
|
|
||
| // Eval with function to avoid issues with undeclared variables | ||
| return Function('"use strict"; return (' + expr + ')')(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.