OpenToonz - An open-source full-featured 2D animation creation software
C
fix: preserve double values in DoubleLineEdit
Anderson Prado40a457231927
diff --git a/toonz/sources/image/pli/pli_io.cpp b/toonz/sources/image/pli/pli_io.cppindex 985391cba..f608c909b 100644--- a/toonz/sources/image/pli/pli_io.cpp+++ b/toonz/sources/image/pli/pli_io.cpp@@ -1469,9 +1469,10 @@ void ParsedPliImp::readFloatData(double &val, TUINT32 &bufOffs) {isNegative = readDynamicData(valInt, bufOffs);readDynamicData(valDec, bufOffs);- val = valInt + (double)valDec / 65536.0; // 2^16-- if (valInt == 0 && isNegative) val = -val;+ if (isNegative)+ val = (double)valInt - (double)valDec / 65536.0; // 2^16+ else+ val = (double)valInt + (double)valDec / 65536.0; // 2^16// m_currDynamicTypeBytesNum = currDynamicTypeBytesNumSaved;}diff --git a/toonz/sources/toonzqt/doublefield.cpp b/toonz/sources/toonzqt/doublefield.cppindex 1f7aea5ea..2e0ba97c4 100644--- a/toonz/sources/toonzqt/doublefield.cpp+++ b/toonz/sources/toonzqt/doublefield.cpp@@ -89,25 +89,25 @@ void DoubleValueLineEdit::mouseReleaseEvent(QMouseEvent *e) {//-----------------------------------------------------------------------------void DoubleValueLineEdit::keyPressEvent(QKeyEvent *event) {- // Allow users to type comma as decimal separator by converting it to a dot.- if (event->text() == ",") {- QKeyEvent dotEvent(QEvent::KeyPress, Qt::Key_Period, Qt::NoModifier, ".");- QLineEdit::keyPressEvent(&dotEvent);- } else if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {- // Validate value before emitting signal to prevent crashes- double value = getValue();- double minValue, maxValue;- getRange(minValue, maxValue);-- // Clamp value to valid range before processing- if (!std::isfinite(value) || value < minValue || value > maxValue) {- setValue(std::clamp(value, minValue, maxValue));- }- emit editingFinished();- clearFocus();- } else {- QLineEdit::keyPressEvent(event);+ // Allow users to type comma as decimal separator by converting it to a dot.+ if (event->text() == ",") {+ QKeyEvent dotEvent(QEvent::KeyPress, Qt::Key_Period, Qt::NoModifier, ".");+ QLineEdit::keyPressEvent(&dotEvent);+ } else if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {+ // Validate value before emitting signal to prevent crashes+ double value = getValue();+ double minValue, maxValue;+ getRange(minValue, maxValue);++ // Clamp value to valid range before processing+ if (!std::isfinite(value) || value < minValue || value > maxValue) {+ setValue(std::clamp(value, minValue, maxValue));}+ emit editingFinished();+ clearFocus();+ } else {+ QLineEdit::keyPressEvent(event);+ }}//=============================================================================// DoubleValueField@@ -130,11 +130,11 @@ DoubleValueField::DoubleValueField(QWidget *parent,//---- layoutQHBoxLayout *layout = new QHBoxLayout(this);- layout->setContentsMargins(0,0,0,0);+ layout->setContentsMargins(0, 0, 0, 0);layout->setSpacing(5);{QVBoxLayout *vLayout = new QVBoxLayout(field);- vLayout->setContentsMargins(0,0,0,0);+ vLayout->setContentsMargins(0, 0, 0, 0);vLayout->setSpacing(0);{vLayout->addWidget(m_lineEdit);@@ -148,16 +148,16 @@ DoubleValueField::DoubleValueField(QWidget *parent,//---- signal/slot connectionsbool ret = true;ret = ret && connect(m_lineEdit, SIGNAL(valueChanged()),- SLOT(onLineEditValueChanged()));+ SLOT(onLineEditValueChanged()));ret = ret && connect(m_roller, SIGNAL(valueChanged(bool)),- SLOT(onRollerValueChanged(bool)));+ SLOT(onRollerValueChanged(bool)));ret = ret && connect(m_slider, SIGNAL(valueChanged(int)),- SLOT(onSliderChanged(int)));- ret = ret && connect(m_slider, SIGNAL(sliderReleased()),- SLOT(onSliderReleased()));- ret = ret && connect(m_lineEdit, SIGNAL(editingFinished()), this,+ SLOT(onSliderChanged(int)));+ ret = ret &&+ connect(m_slider, SIGNAL(sliderReleased()), SLOT(onSliderReleased()));+ ret = ret && connect(m_lineEdit, SIGNAL(editingFinished()), this,SIGNAL(valueEditedByHand()));- ret = ret && connect(m_slider, SIGNAL(sliderReleased()), this,+ ret = ret && connect(m_slider, SIGNAL(sliderReleased()), this,SIGNAL(valueEditedByHand()));assert(ret);@@ -180,27 +180,30 @@ double DoubleValueField::pos2value(int x) const {// nonlinear slider case - use constexpr where is possiblestatic constexpr std::array<double, 4> thresholds{0.5, 0.75, 0.9, 1.0};- const double rangeSize = static_cast<double>(m_slider->maximum() - m_slider->minimum());+ const double rangeSize =+ static_cast<double>(m_slider->maximum() - m_slider->minimum());// Safety check for zero rangeif (std::abs(rangeSize) < std::numeric_limits<double>::epsilon()) {- return static_cast<double>(x) * std::pow(0.1, decimal);+ return static_cast<double>(x) * std::pow(0.1, decimal);}- const double posRatio = static_cast<double>(x - m_slider->minimum()) / rangeSize;+ const double posRatio =+ static_cast<double>(x - m_slider->minimum()) / rangeSize;double t = 0.0;if (posRatio <= thresholds[0]) {- t = 0.04 * posRatio;+ t = 0.04 * posRatio;} else if (posRatio <= thresholds[1]) {- t = -0.02 + 0.08 * posRatio;+ t = -0.02 + 0.08 * posRatio;} else if (posRatio <= thresholds[2]) {- t = -0.26 + 0.4 * posRatio;+ t = -0.26 + 0.4 * posRatio;} else {- t = -8.0 + 9.0 * posRatio;+ t = -8.0 + 9.0 * posRatio;}- const double sliderValue = std::round(static_cast<double>(m_slider->minimum()) + rangeSize * t);+ const double sliderValue =+ std::round(static_cast<double>(m_slider->minimum()) + rangeSize * t);return sliderValue * std::pow(0.1, decimal);}@@ -251,7 +254,9 @@ void DoubleValueField::setRange(double minValue, double maxValue) {//-----------------------------------------------------------------------------void DoubleValueField::setValue(double value) {- if (!std::isfinite(value) || qFuzzyCompare(m_lineEdit->getValue() + 1.0, value + 1.0)) return;+ if (!std::isfinite(value) ||+ qFuzzyCompare(m_lineEdit->getValue() + 1.0, value + 1.0))+ return;double minValue, maxValue;getRange(minValue, maxValue);// Safer clamping@@ -263,8 +268,8 @@ void DoubleValueField::setValue(double value) {int newSliderPos = value2pos(value);if (m_slider->value() != newSliderPos) {m_slider->setValue(newSliderPos);- // Force repaint()... sometimes it doesn't update and update doesn't seem to solve the problem!!!- // try use update() for now+ // Force repaint()... sometimes it doesn't update and update doesn't seem to+ // solve the problem!!! try use update() for nowm_slider->update();}m_slider->blockSignals(signalsBlocked);@@ -319,13 +324,17 @@ bool DoubleValueField::isRollerEnabled() { return m_roller->isEnabled(); }void DoubleValueField::onSliderChanged(int sliderPos) {double val = pos2value(sliderPos);- // Control necessary to prevent the change signal from being emitted more than once.+ // Control necessary to prevent the change signal from being emitted more than+ // once.if (qFuzzyCompare(m_lineEdit->getValue() + 1.0, val + 1.0) ||- (qFuzzyCompare(m_roller->getValue() + 1.0, val + 1.0) && m_roller->isVisible()))+ (qFuzzyCompare(m_roller->getValue() + 1.0, val + 1.0) &&+ m_roller->isVisible()))return;m_lineEdit->setValue(val);m_roller->setValue(val);- // Make the cursor on the first digit, so if the string to display is longer than the field the digits that are truncated are the last ones and not the first (they should be the ones after the decimal point).+ // Make the cursor on the first digit, so if the string to display is longer+ // than the field the digits that are truncated are the last ones and not the+ // first (they should be the ones after the decimal point).m_lineEdit->setCursorPosition(0);emit valueChanged(true);@@ -337,9 +346,12 @@ void DoubleValueField::onLineEditValueChanged() {double value = m_lineEdit->getValue();int decimal = m_lineEdit->getDecimals();- // Control necessary to prevent the change signal from being emitted more than once.- if ((qFuzzyCompare(pos2value(m_slider->value()) + 1.0, value + 1.0) && m_slider->isVisible()) ||- (qFuzzyCompare(m_roller->getValue() + 1.0, value + 1.0) && m_roller->isVisible()))+ // Control necessary to prevent the change signal from being emitted more than+ // once.+ if ((qFuzzyCompare(pos2value(m_slider->value()) + 1.0, value + 1.0) &&+ m_slider->isVisible()) ||+ (qFuzzyCompare(m_roller->getValue() + 1.0, value + 1.0) &&+ m_roller->isVisible()))return;m_slider->setValue(value2pos(value));m_roller->setValue(value);@@ -352,8 +364,10 @@ void DoubleValueField::onRollerValueChanged(bool isDragging) {double value = m_roller->getValue();if (qFuzzyCompare(value + 1.0, m_lineEdit->getValue() + 1.0)) {- assert(qFuzzyCompare(pos2value(m_slider->value()) + 1.0, value + 1.0) || !m_slider->isVisible());- // If isDragging is false, it's right that the change notification is emitted.+ assert(qFuzzyCompare(pos2value(m_slider->value()) + 1.0, value + 1.0) ||+ !m_slider->isVisible());+ // If isDragging is false, it's right that the change notification is+ // emitted.if (!isDragging) emit valueChanged(isDragging);return;}@@ -362,7 +376,8 @@ void DoubleValueField::onRollerValueChanged(bool isDragging) {// Make the cursor on the first digit, so if the string to display// is longer than the field the digits that are truncated are the- // last ones and not the first (they should be the ones after the decimal point).+ // last ones and not the first (they should be the ones after the decimal+ // point).m_lineEdit->setCursorPosition(0);emit valueChanged(isDragging);@@ -381,7 +396,7 @@ DoubleLineEdit::DoubleLineEdit(QWidget *parent, double value)setValidator(m_validator);// Limit maximum characters to prevent extreme values- setMaxLength(7);+ setMaxLength(9);setValue(value);@@ -399,18 +414,26 @@ void DoubleLineEdit::setValue(double value) {QString str;str.setNum(value);int pos = 0;- if (m_validator->validate(str, pos) == QValidator::Acceptable) {- setText(str);- // Ensures only decimals are truncated, not leading digits.- setCursorPosition(0);+ if (m_validator->validate(str, pos) != QValidator::Acceptable) {+ // If it fails, check for too many decimal places and truncate if needed+ int decimals = getDecimals();+ int decimalPos = str.indexOf('.');+ if (decimals != -1 && decimalPos != -1 &&+ (str.length() - decimalPos - 1) > decimals) {+ double factor = std::pow(10.0, getDecimals());+ value = std::round(value * factor) / factor;+ str.setNum(value);+ }}+ setText(str);++ // Ensures only decimals are truncated, not leading digits.+ setCursorPosition(0);}//------------------------------------------------------------------------------double DoubleLineEdit::getValue() {- return QLocale::c().toDouble(text());-}+double DoubleLineEdit::getValue() { return QLocale::c().toDouble(text()); }//-----------------------------------------------------------------------------@@ -465,7 +488,7 @@ MeasuredDoubleLineEdit::MeasuredDoubleLineEdit(QWidget *parent), m_decimals(7) {setObjectName("ValueLineEdit");// Limit character input to prevent extreme values- setMaxLength(7);+ setMaxLength(9);m_value = new TMeasuredValue("length");valueToText();bool ret =@@ -617,7 +640,8 @@ void MeasuredDoubleLineEdit::mousePressEvent(QMouseEvent *e) {//-----------------------------------------------------------------------------void MeasuredDoubleLineEdit::mouseMoveEvent(QMouseEvent *e) {- if ((m_mouseDragEditing && (e->buttons() & Qt::MiddleButton)) || m_labelClicked) {+ if ((m_mouseDragEditing && (e->buttons() & Qt::MiddleButton)) ||+ m_labelClicked) {int precision = getDecimals();m_value->modifyValue((e->x() - m_xMouse) / 2, precision);m_xMouse = e->x();@@ -672,7 +696,8 @@ MeasuredDoubleField::MeasuredDoubleField(QWidget *parent, bool isRollerHide)//-----------------------------------------------------------------------------void MeasuredDoubleField::setMeasure(std::string measureName) {- MeasuredDoubleLineEdit *lineEdit = qobject_cast<MeasuredDoubleLineEdit *>(m_lineEdit);+ MeasuredDoubleLineEdit *lineEdit =+ qobject_cast<MeasuredDoubleLineEdit *>(m_lineEdit);assert(lineEdit);lineEdit->setMeasure(measureName);}@@ -684,5 +709,6 @@ void MeasuredDoubleField::setDecimals(int decimals) {qobject_cast<MeasuredDoubleLineEdit *>(m_lineEdit);if (lineEdit) lineEdit->setDecimals(decimals);// Set step for roller too- if (isRollerEnabled()) m_roller->setStep(std::pow(0.1, std::max(decimals - 1, 1)));+ if (isRollerEnabled())+ m_roller->setStep(std::pow(0.1, std::max(decimals - 1, 1)));}