src/widgets/doublespinbox.cpp

Fri, 04 Mar 2022 23:41:58 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 04 Mar 2022 23:41:58 +0200
changeset 155
13713fadbf14
parent 82
70c67c2c4e36
permissions
-rw-r--r--

improve rendering of the grid

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

mercurial