# HG changeset patch # User Teemu Piippo # Date 1584479627 -7200 # Node ID 70c67c2c4e368b521aba5728117b66886175328b # Parent 62373840e33abbe05c0e04abf07fa398e112da76 added doublespinbox diff -r 62373840e33a -r 70c67c2c4e36 src/widgets/doublespinbox.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/doublespinbox.cpp Tue Mar 17 23:13:47 2020 +0200 @@ -0,0 +1,58 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 - 2020 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 . + */ + +#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"}); + this->setRange(-1e6, 1e6); + this->setDecimals(4); +} + +/* + * 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); +} diff -r 62373840e33a -r 70c67c2c4e36 src/widgets/doublespinbox.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/doublespinbox.h Tue Mar 17 23:13:47 2020 +0200 @@ -0,0 +1,34 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 - 2020 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 . + */ + +#pragma once +#include + +/* + * 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; +}; +