better representation of fractions and large numbers

Tue, 27 Mar 2018 14:13:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 27 Mar 2018 14:13:58 +0300
changeset 1350
eb2d3bc4fc73
parent 1349
45d8eec8bd52
child 1351
b2994da4b852

better representation of fractions and large numbers

src/basics.cpp file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/canvas.cpp file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
--- a/src/basics.cpp	Tue Mar 27 13:01:02 2018 +0300
+++ b/src/basics.cpp	Tue Mar 27 14:13:58 2018 +0300
@@ -87,3 +87,61 @@
 	};
 	return settings;
 }
+
+QString largeNumberRep(int number)
+{
+	if (number < 0)
+	{
+		return "-" + largeNumberRep(-number);
+	}
+	else
+	{
+		QString rep;
+
+		while (number >= 1000)
+		{
+			rep = " " + QString::number(number % 1000);
+			number /= 1000;
+		}
+
+		return QString::number(number) + rep;
+	}
+}
+
+static const QString superscripts = "⁰¹²³⁴⁵⁶⁷⁸⁹";
+static const QString subscripts = "₀₁₂₃₄₅₆₇₈₉";
+
+static QString customNumberRep(int number, const QString& script, const QString& minus)
+{
+	if (number < 0)
+	{
+		return minus + customNumberRep(-number, script, minus);
+	}
+	else
+	{
+		QString rep = "";
+		for (QChar character : QString::number(number))
+		{
+			if (character >= '0' and character <= '9')
+				rep += script[character.unicode() - '0'];
+			else
+				rep += character;
+		}
+		return rep;
+	}
+}
+
+QString superscript(int number)
+{
+	return customNumberRep(number, superscripts, "⁻");
+}
+
+QString subscript(int number)
+{
+	return customNumberRep(number, subscripts, "₋");
+}
+
+QString fractionRep(int numerator, int denominator)
+{
+	return superscript(numerator) + "⁄" + subscript(denominator);
+}
--- a/src/basics.h	Tue Mar 27 13:01:02 2018 +0300
+++ b/src/basics.h	Tue Mar 27 14:13:58 2018 +0300
@@ -90,6 +90,10 @@
 QString formatFileSize(qint64 size);
 int gcd(int a, int b);
 QString joinStrings(const QList<class StringFormatArg>& values, QString delimeter = " ");
+QString largeNumberRep(int number);
 double roundToDecimals(double value, int decimals);
 class QSettings& settingsObject();
 void simplify(int& numerator, int& denominator);
+QString superscript(int number);
+QString subscript(int number);
+QString fractionRep(int numerator, int denominator);
--- a/src/canvas.cpp	Tue Mar 27 13:01:02 2018 +0300
+++ b/src/canvas.cpp	Tue Mar 27 14:13:58 2018 +0300
@@ -57,7 +57,7 @@
 	// Render triangle count
 	{
 		QPoint renderPoint {4, height() - 4 - metrics.height() - metrics.descent()};
-		painter.drawText(renderPoint, format("△ %1", m_document.triangleCount()));
+		painter.drawText(renderPoint, format("△ %1", largeNumberRep(m_document.triangleCount())));
 	}
 
 	// Message log
--- a/src/mainwindow.cpp	Tue Mar 27 13:01:02 2018 +0300
+++ b/src/mainwindow.cpp	Tue Mar 27 14:13:58 2018 +0300
@@ -839,7 +839,7 @@
 	int numerator = this->ringToolSegments();
 	int denominator = this->ringToolDivisions();
 	simplify (numerator, denominator);
-	ui.ringToolSegmentsLabel->setText (format ("%1 / %2", numerator, denominator));
+	ui.ringToolSegmentsLabel->setText(fractionRep(numerator, denominator));
 }
 
 // ---------------------------------------------------------------------------------------------------------------------

mercurial