Fri, 04 Mar 2022 23:41:58 +0200
improve rendering of the grid
82 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include "doublespinbox.h" | |
20 | ||
21 | /* | |
22 | * Constructs a new double spin box. The locale is fixed to system default "C". | |
23 | */ | |
24 | DoubleSpinBox::DoubleSpinBox(QWidget* parent) : | |
25 | QDoubleSpinBox {parent} | |
26 | { | |
27 | this->setLocale({"C"}); | |
28 | this->setRange(-1e6, 1e6); | |
29 | this->setDecimals(4); | |
30 | } | |
31 | ||
32 | /* | |
33 | * Reimplementation of QDoubleSpinBox::textFromValue to remove trailing zeros. | |
34 | */ | |
35 | QString DoubleSpinBox::textFromValue(double value) const | |
36 | { | |
37 | QString result = QDoubleSpinBox::textFromValue(value); | |
38 | if (result.contains(".")) | |
39 | { | |
40 | // Remove trailing zeros | |
41 | while (result.endsWith("0")) | |
42 | result.chop(1); | |
43 | // Remove trailing decimal point if we just removed all the zeros. | |
44 | if (result.endsWith(".")) | |
45 | result.chop(1); | |
46 | } | |
47 | return result; | |
48 | } | |
49 | ||
50 | /* | |
51 | * Reimplementation of QDoubleSpinBox::validate to fix the decimal point if the locale-specific | |
52 | * decimal point was used. | |
53 | */ | |
54 | QValidator::State DoubleSpinBox::validate(QString& input, int& pos) const | |
55 | { | |
56 | input.replace(QLocale().decimalPoint(), this->locale().decimalPoint()); | |
57 | return QDoubleSpinBox::validate(input, pos); | |
58 | } |