[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kcalc/frameworks] /: KF5 port: replace KLocale::formatNumber with custom code
From:       Christoph Feck <christoph () maxiom ! de>
Date:       2014-09-30 22:05:35
Message-ID: E1XZ5Y7-0003kV-LU () scm ! kde ! org
[Download RAW message or body]

Git commit 87e735414b31e4c685dc5ae28462841854e2647b by Christoph Feck.
Committed on 24/09/2014 at 22:40.
Pushed by cfeck into branch 'frameworks'.

KF5 port: replace KLocale::formatNumber with custom code

REVIEW: 120357

M  +55   -17   kcalcdisplay.cpp
M  +1    -0    kcalcdisplay.h

http://commits.kde.org/kcalc/87e735414b31e4c685dc5ae28462841854e2647b

diff --git a/kcalcdisplay.cpp b/kcalcdisplay.cpp
index b4e2b3b..464f0b6 100644
--- a/kcalcdisplay.cpp
+++ b/kcalcdisplay.cpp
@@ -29,7 +29,6 @@ along with this program.  If not, see \
<http://www.gnu.org/licenses/>.  #include <QStyleOption>
 #include <QTimer>
 
-#include <kglobal.h>
 #include <knotification.h>
 #include <KLocalizedString>
 
@@ -288,7 +287,7 @@ void KCalcDisplay::slotPaste(bool bClipboard) {
 	tmp_str = tmp_str.trimmed();
 	
 	if (groupdigits_) {
-		tmp_str.remove(KGlobal::locale()->thousandsSeparator());
+		tmp_str.remove(QLocale().groupSeparator());
 	}
 	
 	tmp_str = tmp_str.toLower();
@@ -496,7 +495,7 @@ bool KCalcDisplay::setAmount(const KNumber &new_amount) {
 			
 			display_str = QString::number(tmp_workaround, num_base_).toUpper();
 			if (neg) {
-				display_str.prepend(KGlobal::locale()->negativeSign());
+				display_str.prepend(QLocale().negativeSign());
 			}
 		}
 	} else {
@@ -525,18 +524,10 @@ void KCalcDisplay::setText(const QString &string)
 	// The decimal mode needs special treatment for two reasons, because: a) it uses \
KGlobal::locale() to get a localized   // format and b) it has possible numbers after \
the decimal place. Neither applies to Binary, Hexadecimal or Octal.  
-	if (groupdigits_ && !special){
+	if ((groupdigits_ || num_base_ == NB_DECIMAL) && !special){
 		switch (num_base_) {
 		case NB_DECIMAL:
-	        if (string.endsWith(QLatin1Char('.'))) {
-	            text_.chop(1);
-	            // Note: rounding happened already above!
-	            text_ = KGlobal::locale()->formatNumber(text_, false, 0);
-	            text_.append(KGlobal::locale()->decimalSymbol());
-	        } else {
-	            // Note: rounding happened already above!
-	            text_ = KGlobal::locale()->formatNumber(text_, false, 0);
-			}
+			text_ = formatDecimalNumber(text_);
 			break;
 			
 		case NB_BINARY:
@@ -566,6 +557,53 @@ void KCalcDisplay::setText(const QString &string)
 }
 
 //------------------------------------------------------------------------------
+// Name: formatDecimalNumber
+// Desc: Convert decimal number to locale-dependend format.
+//      We cannot use QLocale::formatNumber(), because the
+//      precision is limited to "double".
+//------------------------------------------------------------------------------
+QString KCalcDisplay::formatDecimalNumber(QString string)
+{
+	QLocale locale;
+
+	string.replace(QLatin1Char('.'), locale.decimalPoint());
+
+	if (groupdigits_ && !(locale.numberOptions() & QLocale::OmitGroupSeparator)) {
+		// find position after last digit
+		int pos = string.indexOf(locale.decimalPoint());
+		if (pos < 0) {
+			// do not group digits after the exponent part
+			const int expPos = string.indexOf(QLatin1Char('e'));
+			if (expPos > 0) {
+				pos = expPos;
+			} else {
+				pos = string.length();
+			}
+		}
+
+		const QChar groupSeparator = locale.groupSeparator();
+		const int groupSize = 3;
+
+		string.reserve(string.length() + (pos - 1) / groupSize);
+		while ((pos -= groupSize) > 0) {
+			string.insert(pos, groupSeparator);
+		}
+	}
+
+	string.replace(QLatin1Char('-'), locale.negativeSign());
+	string.replace(QLatin1Char('+'), locale.positiveSign());
+
+	unsigned short zero = locale.zeroDigit().unicode();
+	for (int i = 0; i < string.length(); ++i) {
+		if (string.at(i).isDigit()) {
+			string[i] = QChar(zero + string.at(i).digitValue());
+		}
+	}
+
+	return string;
+}
+
+//------------------------------------------------------------------------------
 // Name: groupDigits
 // Desc: 
 //------------------------------------------------------------------------------
@@ -767,7 +805,7 @@ void KCalcDisplay::newCharacter(const QChar new_char) {
 		break;
 
 	default:
-		if(new_char == KGlobal::locale()->decimalSymbol()[0]) {
+		if(new_char == QLocale().decimalPoint()) {
 			// Period can be set only once and only in decimal
 			// mode, also not in EE-mode
 			if (num_base_ != NB_DECIMAL || period_ || eestate_) {
@@ -788,7 +826,7 @@ void KCalcDisplay::newCharacter(const QChar new_char) {
 	// change exponent or mantissa
 	if (eestate_) {
 		// ignore '.' before 'e'. turn e.g. '123.e' into '123e'
-		if (new_char == QLatin1Char('e') && \
str_int_.endsWith(KGlobal::locale()->decimalSymbol())) { +		if (new_char == \
QLatin1Char('e') && str_int_.endsWith(QLocale().decimalPoint())) {  str_int_.chop(1);
 			period_ = false;
 		}
@@ -808,7 +846,7 @@ void KCalcDisplay::newCharacter(const QChar new_char) {
 				str_int_.append(new_char);
 				break;
 			default:
-				if(new_char == KGlobal::locale()->decimalSymbol()[0]) {
+				if(new_char == QLocale().decimalPoint()) {
 					// display "0." not just "."
 					str_int_.append(new_char);
 				} else {
@@ -845,7 +883,7 @@ void KCalcDisplay::deleteLastDigit() {
 	} else {
 		const int length = str_int_.length();
 		if (length > 1) {
-			if (str_int_[length-1] == KGlobal::locale()->decimalSymbol()[0]) {
+			if (str_int_[length-1] == QLocale().decimalPoint()) {
 				period_ = false;
 			}
 			str_int_.chop(1);
diff --git a/kcalcdisplay.h b/kcalcdisplay.h
index 807cb00..e495f3f 100644
--- a/kcalcdisplay.h
+++ b/kcalcdisplay.h
@@ -86,6 +86,7 @@ public:
     void setFixedPrecision(int precision);
     void setPrecision(int precision);
     void setText(const QString &string);
+    QString formatDecimalNumber(QString string);
     QString groupDigits(const QString &displayString, int numDigits);
     QString text() const;
     void updateDisplay();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic