|
| 1 | +/* |
| 2 | + * expressionspinbox.cpp |
| 3 | + * Copyright 2025, dogboydog |
| 4 | + * |
| 5 | + * This file is part of Tiled. |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify it |
| 8 | + * under the terms of the GNU General Public License as published by the Free |
| 9 | + * Software Foundation; either version 2 of the License, or (at your option) |
| 10 | + * any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | + * more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License along with |
| 18 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "expressionspinbox.h" |
| 22 | + |
| 23 | +#include <QJSEngine> |
| 24 | + |
| 25 | +namespace Tiled { |
| 26 | + |
| 27 | +QJSEngine *ExpressionEvaluator::mEngine; |
| 28 | + |
| 29 | +void ExpressionEvaluator::deleteInstance() |
| 30 | +{ |
| 31 | + delete mEngine; |
| 32 | + mEngine = nullptr; |
| 33 | +} |
| 34 | + |
| 35 | +QJSValue ExpressionEvaluator::evaluate(const QString &program) |
| 36 | +{ |
| 37 | + if (!mEngine) |
| 38 | + mEngine = new QJSEngine; |
| 39 | + return mEngine->evaluate(program); |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +// ExpressionSpinBox |
| 44 | + |
| 45 | +ExpressionSpinBox::ExpressionSpinBox(QWidget *parent) |
| 46 | + : QSpinBox(parent) |
| 47 | +{} |
| 48 | + |
| 49 | +int ExpressionSpinBox::valueFromText(const QString &text) const |
| 50 | +{ |
| 51 | + const QJSValue result = ExpressionEvaluator::evaluate(text); |
| 52 | + if (result.isNumber()) |
| 53 | + return result.toNumber(); |
| 54 | + |
| 55 | + return value(); |
| 56 | +} |
| 57 | + |
| 58 | +QValidator::State ExpressionSpinBox::validate(QString &/*text*/, int &/*pos*/) const |
| 59 | +{ |
| 60 | + return QValidator::Acceptable; |
| 61 | +} |
| 62 | + |
| 63 | +void ExpressionSpinBox::focusInEvent(QFocusEvent *event) |
| 64 | +{ |
| 65 | + // Remember current prefix/suffix and remove them |
| 66 | + mPrefix = prefix(); |
| 67 | + mSuffix = suffix(); |
| 68 | + if (!mPrefix.isEmpty()) |
| 69 | + setPrefix(QString()); |
| 70 | + if (!mSuffix.isEmpty()) |
| 71 | + setSuffix(QString()); |
| 72 | + |
| 73 | + QSpinBox::focusInEvent(event); |
| 74 | +} |
| 75 | + |
| 76 | +void ExpressionSpinBox::focusOutEvent(QFocusEvent *event) |
| 77 | +{ |
| 78 | + QSpinBox::focusOutEvent(event); |
| 79 | + |
| 80 | + // Restore any previously removed prefix/suffix |
| 81 | + if (!mPrefix.isEmpty()) |
| 82 | + setPrefix(mPrefix); |
| 83 | + if (!mSuffix.isEmpty()) |
| 84 | + setSuffix(mSuffix); |
| 85 | + mPrefix.clear(); |
| 86 | + mSuffix.clear(); |
| 87 | +} |
| 88 | + |
| 89 | +// ExpressionDoubleSpinBox |
| 90 | + |
| 91 | +ExpressionDoubleSpinBox::ExpressionDoubleSpinBox(QWidget *parent) |
| 92 | + : QDoubleSpinBox(parent) |
| 93 | +{} |
| 94 | + |
| 95 | +double ExpressionDoubleSpinBox::valueFromText(const QString &text) const |
| 96 | +{ |
| 97 | + const QJSValue result = ExpressionEvaluator::evaluate(text); |
| 98 | + if (result.isNumber()) |
| 99 | + return result.toNumber(); |
| 100 | + |
| 101 | + return value(); |
| 102 | +} |
| 103 | + |
| 104 | +QValidator::State ExpressionDoubleSpinBox::validate(QString &/*text*/, int &/*pos*/) const |
| 105 | +{ |
| 106 | + return QValidator::Acceptable; |
| 107 | +} |
| 108 | + |
| 109 | +void ExpressionDoubleSpinBox::focusInEvent(QFocusEvent *event) |
| 110 | +{ |
| 111 | + // Remember current prefix/suffix and remove them |
| 112 | + mPrefix = prefix(); |
| 113 | + mSuffix = suffix(); |
| 114 | + if (!mPrefix.isEmpty()) |
| 115 | + setPrefix(QString()); |
| 116 | + if (!mSuffix.isEmpty()) |
| 117 | + setSuffix(QString()); |
| 118 | + |
| 119 | + QDoubleSpinBox::focusInEvent(event); |
| 120 | +} |
| 121 | + |
| 122 | +void ExpressionDoubleSpinBox::focusOutEvent(QFocusEvent *event) |
| 123 | +{ |
| 124 | + QDoubleSpinBox::focusOutEvent(event); |
| 125 | + |
| 126 | + // Restore any previously removed prefix/suffix |
| 127 | + if (!mPrefix.isEmpty()) |
| 128 | + setPrefix(mPrefix); |
| 129 | + if (!mSuffix.isEmpty()) |
| 130 | + setSuffix(mSuffix); |
| 131 | + mPrefix.clear(); |
| 132 | + mSuffix.clear(); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace Tiled |
| 136 | + |
| 137 | +#include "moc_expressionspinbox.cpp" |
0 commit comments