Changeset View
Changeset View
Standalone View
Standalone View
libs/ui/widgets/kis_intparsespinbox.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "kis_intparsespinbox.h" | ||||
20 | | ||||
21 | #include "kis_numparser.h" | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | #include <iostream> | ||||
25 | | ||||
26 | KisIntParseSpinBox::KisIntParseSpinBox(QWidget *parent) : | ||||
27 | QSpinBox(parent), | ||||
28 | _isLastValid(true) | ||||
29 | { | ||||
30 | _lastExprParsed = new QString("0"); | ||||
31 | | ||||
32 | connect(this, SIGNAL(noMoreParsingError()), | ||||
33 | this, SLOT(clearErrorStyle())); | ||||
34 | | ||||
35 | //hack to let the clearError be called, even if the value changed method is the one from QSpinBox. | ||||
36 | connect(this, SIGNAL(valueChanged(int)), | ||||
37 | this, SLOT(clearError())); | ||||
38 | | ||||
39 | connect(this, SIGNAL(errorWhileParsing(QString)), | ||||
40 | this, SLOT(setErrorStyle())); | ||||
41 | | ||||
42 | } | ||||
43 | | ||||
44 | KisIntParseSpinBox::~KisIntParseSpinBox() | ||||
45 | { | ||||
46 | | ||||
47 | //needed to avoid a segfault during destruction. | ||||
48 | delete _lastExprParsed; | ||||
49 | | ||||
50 | } | ||||
51 | | ||||
52 | int KisIntParseSpinBox::valueFromText(const QString & text) const | ||||
53 | { | ||||
54 | | ||||
55 | *_lastExprParsed = text; | ||||
56 | | ||||
57 | bool ok; | ||||
58 | | ||||
59 | int val = KisNumericParser::parseIntegerMathExpr(text, &ok); | ||||
60 | | ||||
61 | if (text.trimmed().isEmpty()) { //an empty text is considered valid in this case. | ||||
62 | ok = true; | ||||
63 | } | ||||
64 | | ||||
65 | if (!ok) { | ||||
66 | | ||||
67 | if (_isLastValid == true) { | ||||
68 | _oldVal = value(); | ||||
69 | } | ||||
70 | | ||||
71 | _isLastValid = false; | ||||
72 | //emit errorWhileParsing(text); //if uncommented become red everytime the string is wrong. | ||||
73 | val = _oldVal; | ||||
74 | } else { | ||||
75 | | ||||
76 | if (_isLastValid == false) { | ||||
77 | _oldVal = val; | ||||
78 | } | ||||
79 | | ||||
80 | _isLastValid = true; | ||||
81 | //emit noMoreParsingError(); | ||||
82 | } | ||||
83 | | ||||
84 | return val; | ||||
85 | | ||||
86 | } | ||||
87 | | ||||
88 | QString KisIntParseSpinBox::textFromValue(int val) const | ||||
89 | { | ||||
90 | | ||||
91 | if (!_isLastValid) { | ||||
92 | emit errorWhileParsing(*_lastExprParsed); | ||||
93 | return *_lastExprParsed; | ||||
94 | } | ||||
95 | | ||||
96 | emit noMoreParsingError(); | ||||
97 | return QSpinBox::textFromValue(val); | ||||
98 | | ||||
99 | } | ||||
100 | | ||||
101 | QValidator::State KisIntParseSpinBox::validate ( QString & input, int & pos ) const | ||||
102 | { | ||||
103 | | ||||
104 | Q_UNUSED(input); | ||||
105 | Q_UNUSED(pos); | ||||
106 | | ||||
107 | //this simple definition is sufficient for the moment | ||||
108 | //TODO: see if needed to get something more complex. | ||||
109 | return QValidator::Acceptable; | ||||
110 | | ||||
111 | } | ||||
112 | | ||||
113 | void KisIntParseSpinBox::stepBy(int steps) | ||||
114 | { | ||||
115 | | ||||
116 | _isLastValid = true; | ||||
117 | emit noMoreParsingError(); | ||||
118 | | ||||
119 | QSpinBox::stepBy(steps); | ||||
120 | | ||||
121 | } | ||||
122 | | ||||
123 | void KisIntParseSpinBox::setValue(int val) | ||||
124 | { | ||||
125 | | ||||
126 | if (!hasFocus()) { | ||||
127 | clearError(); | ||||
128 | } | ||||
129 | | ||||
130 | QSpinBox::setValue(val); | ||||
131 | } | ||||
132 | | ||||
133 | void KisIntParseSpinBox::setErrorStyle() | ||||
134 | { | ||||
135 | if (!_isLastValid) { | ||||
136 | setStyleSheet("Background: red; color: white;"); | ||||
137 | } | ||||
138 | } | ||||
139 | | ||||
140 | void KisIntParseSpinBox::clearErrorStyle() | ||||
141 | { | ||||
142 | if (_isLastValid) { | ||||
143 | setStyleSheet(""); | ||||
144 | } | ||||
145 | } | ||||
146 | | ||||
147 | | ||||
148 | void KisIntParseSpinBox::clearError() | ||||
149 | { | ||||
150 | _isLastValid = true; | ||||
151 | emit noMoreParsingError(); | ||||
152 | _oldVal = value(); | ||||
153 | clearErrorStyle(); | ||||
154 | } |