added a doublespinbox variant that uses "." as the decimal point and omits trailing zeros

Thu, 10 May 2018 15:48:39 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 10 May 2018 15:48:39 +0300
changeset 1387
6393b6020c62
parent 1386
c59dac18b06b
child 1388
fb20489acf39

added a doublespinbox variant that uses "." as the decimal point and omits trailing zeros

CMakeLists.txt file | annotate | diff | comparison | revisions
src/dialogs/subfilereferenceeditor.cpp file | annotate | diff | comparison | revisions
src/dialogs/subfilereferenceeditor.ui file | annotate | diff | comparison | revisions
src/widgets/doublespinbox.cpp file | annotate | diff | comparison | revisions
src/widgets/doublespinbox.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Thu May 10 14:57:23 2018 +0300
+++ b/CMakeLists.txt	Thu May 10 15:48:39 2018 +0300
@@ -93,6 +93,7 @@
 	src/types/boundingbox.cpp
 	src/types/matrix.cpp
 	src/types/vertex.cpp
+	src/widgets/doublespinbox.cpp
 	src/widgets/headeredit.cpp
 	src/widgets/vertexobjecteditor.cpp
 )
@@ -170,6 +171,7 @@
 	src/types/library.h
 	src/types/matrix.h
 	src/types/vertex.h
+	src/widgets/doublespinbox.h
 	src/widgets/headeredit.h
 	src/widgets/vertexobjecteditor.h
 )
--- a/src/dialogs/subfilereferenceeditor.cpp	Thu May 10 14:57:23 2018 +0300
+++ b/src/dialogs/subfilereferenceeditor.cpp	Thu May 10 15:48:39 2018 +0300
@@ -185,7 +185,7 @@
 			{
 				for (int row : {0, 1, 2})
 				{
-					double cellValue = abs(this->matrixCell(row, column)->value());
+					double cellValue = this->matrixCell(row, column)->value();
 					cellValue *= newScaling / oldScaling;
 					QDoubleSpinBox* cellWidget = this->matrixCell(row, column);
 					cellWidget->blockSignals(true);
--- a/src/dialogs/subfilereferenceeditor.ui	Thu May 10 14:57:23 2018 +0300
+++ b/src/dialogs/subfilereferenceeditor.ui	Thu May 10 15:48:39 2018 +0300
@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>625</width>
+    <width>699</width>
     <height>644</height>
    </rect>
   </property>
@@ -70,7 +70,10 @@
      <item row="2" column="1">
       <layout class="QHBoxLayout" name="horizontalLayout">
        <item>
-        <widget class="QDoubleSpinBox" name="positionX">
+        <widget class="DoubleSpinBox" name="positionX">
+         <property name="prefix">
+          <string>𝑥 = </string>
+         </property>
          <property name="decimals">
           <number>5</number>
          </property>
@@ -83,7 +86,10 @@
         </widget>
        </item>
        <item>
-        <widget class="QDoubleSpinBox" name="positionY">
+        <widget class="DoubleSpinBox" name="positionY">
+         <property name="prefix">
+          <string>𝑦 = </string>
+         </property>
          <property name="decimals">
           <number>5</number>
          </property>
@@ -96,7 +102,10 @@
         </widget>
        </item>
        <item>
-        <widget class="QDoubleSpinBox" name="positionZ">
+        <widget class="DoubleSpinBox" name="positionZ">
+         <property name="prefix">
+          <string>𝑧 = </string>
+         </property>
          <property name="decimals">
           <number>5</number>
          </property>
@@ -127,7 +136,7 @@
      <item row="4" column="1">
       <layout class="QGridLayout" name="matrixLayout">
        <item row="1" column="1">
-        <widget class="QDoubleSpinBox" name="matrixE">
+        <widget class="DoubleSpinBox" name="matrixE">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -140,7 +149,7 @@
         </widget>
        </item>
        <item row="0" column="0">
-        <widget class="QDoubleSpinBox" name="matrixA">
+        <widget class="DoubleSpinBox" name="matrixA">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -153,7 +162,7 @@
         </widget>
        </item>
        <item row="2" column="0">
-        <widget class="QDoubleSpinBox" name="matrixG">
+        <widget class="DoubleSpinBox" name="matrixG">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -166,7 +175,7 @@
         </widget>
        </item>
        <item row="2" column="1">
-        <widget class="QDoubleSpinBox" name="matrixH">
+        <widget class="DoubleSpinBox" name="matrixH">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -179,7 +188,7 @@
         </widget>
        </item>
        <item row="2" column="2">
-        <widget class="QDoubleSpinBox" name="matrixI">
+        <widget class="DoubleSpinBox" name="matrixI">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -192,7 +201,7 @@
         </widget>
        </item>
        <item row="1" column="2">
-        <widget class="QDoubleSpinBox" name="matrixF">
+        <widget class="DoubleSpinBox" name="matrixF">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -205,7 +214,7 @@
         </widget>
        </item>
        <item row="0" column="2">
-        <widget class="QDoubleSpinBox" name="matrixC">
+        <widget class="DoubleSpinBox" name="matrixC">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -218,7 +227,7 @@
         </widget>
        </item>
        <item row="1" column="0">
-        <widget class="QDoubleSpinBox" name="matrixD">
+        <widget class="DoubleSpinBox" name="matrixD">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -231,7 +240,7 @@
         </widget>
        </item>
        <item row="0" column="1">
-        <widget class="QDoubleSpinBox" name="matrixB">
+        <widget class="DoubleSpinBox" name="matrixB">
          <property name="decimals">
           <number>5</number>
          </property>
@@ -255,7 +264,7 @@
      <item row="6" column="1">
       <layout class="QHBoxLayout" name="horizontalLayout_3">
        <item>
-        <widget class="QDoubleSpinBox" name="scalingX">
+        <widget class="DoubleSpinBox" name="scalingX">
          <property name="prefix">
           <string>𝑥 × </string>
          </property>
@@ -271,7 +280,7 @@
         </widget>
        </item>
        <item>
-        <widget class="QDoubleSpinBox" name="scalingY">
+        <widget class="DoubleSpinBox" name="scalingY">
          <property name="prefix">
           <string>𝑦 × </string>
          </property>
@@ -287,7 +296,7 @@
         </widget>
        </item>
        <item>
-        <widget class="QDoubleSpinBox" name="scalingZ">
+        <widget class="DoubleSpinBox" name="scalingZ">
          <property name="prefix">
           <string>𝑧 × </string>
          </property>
@@ -325,6 +334,13 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>DoubleSpinBox</class>
+   <extends>QDoubleSpinBox</extends>
+   <header>widgets/doublespinbox.h</header>
+  </customwidget>
+ </customwidgets>
  <tabstops>
   <tabstop>primitivesTreeView</tabstop>
   <tabstop>referenceName</tabstop>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/widgets/doublespinbox.cpp	Thu May 10 15:48:39 2018 +0300
@@ -0,0 +1,59 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2018 Teemu Piippo
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "doublespinbox.h"
+
+/*
+ * Constructs a new double spin box. The locale is fixed to system default "C".
+ */
+DoubleSpinBox::DoubleSpinBox(QWidget* parent) :
+	QDoubleSpinBox {parent}
+{
+	this->setLocale({"C"});
+}
+
+/*
+ * Reimplementation of QDoubleSpinBox::textFromValue to remove trailing zeros.
+ */
+QString DoubleSpinBox::textFromValue(double value) const
+{
+	QString result = QDoubleSpinBox::textFromValue(value);
+
+	if (result.contains("."))
+	{
+		// Remove trailing zeros
+		while (result.endsWith("0"))
+			result.chop(1);
+
+		// Remove trailing decimal point if we just removed all the zeros.
+		if (result.endsWith("."))
+			result.chop(1);
+	}
+
+	return result;
+}
+
+/*
+ * Reimplementation of QDoubleSpinBox::validate to fix the decimal point if the locale-specific
+ * decimal point was used.
+ */
+QValidator::State DoubleSpinBox::validate(QString& input, int& pos) const
+{
+	input.replace(QLocale().decimalPoint(), this->locale().decimalPoint());
+	return QDoubleSpinBox::validate(input, pos);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/widgets/doublespinbox.h	Thu May 10 15:48:39 2018 +0300
@@ -0,0 +1,35 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2018 Teemu Piippo
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+#include <QDoubleSpinBox>
+
+/*
+ * A version of QDoubleSpinBox that consistently uses "." as the decimal separator
+ * and does not display trailing zeros.
+ */
+class DoubleSpinBox : public QDoubleSpinBox
+{
+public:
+	DoubleSpinBox(QWidget* parent = nullptr);
+
+protected:
+	QString textFromValue(double value) const override;
+	QValidator::State validate(QString& input, int& pos) const override;
+};
+

mercurial