src/widgets/doublespinbox.cpp

Sat, 04 Aug 2018 21:46:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 04 Aug 2018 21:46:58 +0300
changeset 1428
ece049033adc
parent 1387
6393b6020c62
permissions
-rw-r--r--

fixed a crash when trying to open a document for the 3rd time after closing it 2 times

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

mercurial