[LyX/master] Allow entering localized lengths with unit (#11852)

Juergen Spitzmueller spitz at lyx.org
Mon Aug 8 15:49:57 UTC 2022


commit 1487b198033c74d976b110a3a024c66fd027a20c
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Mon Aug 8 18:34:34 2022 +0200

    Allow entering localized lengths with unit (#11852)
---
 src/frontends/qt/Validator.cpp  |    8 ++++++--
 src/frontends/qt/qt_helpers.cpp |   14 ++++++++++++++
 src/frontends/qt/qt_helpers.h   |    3 +++
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/frontends/qt/Validator.cpp b/src/frontends/qt/Validator.cpp
index ad1c4f6..baf1011 100644
--- a/src/frontends/qt/Validator.cpp
+++ b/src/frontends/qt/Validator.cpp
@@ -72,8 +72,12 @@ QValidator::State LengthValidator::validate(QString & qtext, int &) const
 		GlueLength gl;
 		if (unsigned_ && gl.len().value() < 0)
 			return QValidator::Invalid;
-		return (isValidGlueLength(text, &gl)) ?
-			QValidator::Acceptable : QValidator::Intermediate;
+		if (isValidGlueLength(text, &gl))
+			return QValidator::Acceptable;
+		// Also check for localized variant
+		if (isValidGlueLength(fromqstr(unlocString(qtext)), &gl))
+			return QValidator::Acceptable;
+		return QValidator::Intermediate;
 	}
 
 	Length l;
diff --git a/src/frontends/qt/qt_helpers.cpp b/src/frontends/qt/qt_helpers.cpp
index 8372fd9..0cf9c9a 100644
--- a/src/frontends/qt/qt_helpers.cpp
+++ b/src/frontends/qt/qt_helpers.cpp
@@ -99,6 +99,9 @@ string widgetsToLength(QLineEdit const * input, LengthCombo const * combo)
 	// Don't return unit-from-choice if the input(field) contains a unit
 	if (isValidGlueLength(fromqstr(length)))
 		return fromqstr(length);
+	// Also try with localized version
+	if (isValidGlueLength(fromqstr(unlocString(length))))
+		return fromqstr(unlocString(length));
 
 	Length::UNIT const unit = combo->currentLengthItem();
 
@@ -115,6 +118,9 @@ Length widgetsToLength(QLineEdit const * input, QComboBox const * combo)
 	// don't return unit-from-choice if the input(field) contains a unit
 	if (isValidGlueLength(fromqstr(length)))
 		return Length(fromqstr(length));
+	// Also try with localized version
+	if (isValidGlueLength(fromqstr(unlocString(length))))
+		return Length(fromqstr(unlocString(length)));
 
 	Length::UNIT unit = Length::UNIT_NONE;
 	QString const item = combo->currentText();
@@ -208,6 +214,14 @@ QString formatLocFPNumber(double d)
 }
 
 
+QString unlocString(QString const & str)
+{
+	QLocale loc;
+	QString res = str;
+	return res.replace(loc.decimalPoint(), QString("."));
+}
+
+
 bool SortLocaleAware(QString const & lhs, QString const & rhs)
 {
 	return QString::localeAwareCompare(lhs, rhs) < 0;
diff --git a/src/frontends/qt/qt_helpers.h b/src/frontends/qt/qt_helpers.h
index 08d50fa..8248de8 100644
--- a/src/frontends/qt/qt_helpers.h
+++ b/src/frontends/qt/qt_helpers.h
@@ -72,6 +72,9 @@ void doubleToWidget(QLineEdit * input, std::string const & value,
  */
 QString formatLocFPNumber(double d);
 
+// Method to replace localized decimal separator by dot
+QString unlocString(QString const & str);
+
 /// Method to sort QStrings locale-aware (e.g. in combo widgets)
 bool SortLocaleAware(QString const & lhs, QString const & rhs);
 


More information about the lyx-cvs mailing list