Changeset View
Changeset View
Standalone View
Standalone View
libs/widgets/kis_double_parse_unit_spin_box.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_double_parse_unit_spin_box.h" | ||||
20 | | ||||
21 | | ||||
22 | class Q_DECL_HIDDEN KisDoubleParseUnitSpinBox::Private | ||||
23 | { | ||||
24 | public: | ||||
25 | Private(double low, double up, double step) | ||||
26 | : lowerInPoints(low), | ||||
27 | upperInPoints(up), | ||||
28 | stepInPoints(step), | ||||
29 | unit(KoUnit(KoUnit::Point)) | ||||
30 | { | ||||
31 | } | ||||
32 | | ||||
33 | double lowerInPoints; ///< lowest value in points | ||||
34 | double upperInPoints; ///< highest value in points | ||||
35 | double stepInPoints; ///< step in points | ||||
36 | KoUnit unit; | ||||
37 | }; | ||||
38 | | ||||
39 | KisDoubleParseUnitSpinBox::KisDoubleParseUnitSpinBox(QWidget *parent) : | ||||
40 | KisDoubleParseSpinBox(parent), | ||||
41 | d(new Private(-9999, 9999, 1)) | ||||
42 | { | ||||
43 | setUnit( KoUnit(KoUnit::Point) ); | ||||
44 | setAlignment( Qt::AlignRight ); | ||||
45 | | ||||
46 | connect(this, SIGNAL(valueChanged( double )), SLOT(privateValueChanged())); | ||||
47 | } | ||||
48 | | ||||
49 | KisDoubleParseUnitSpinBox::~KisDoubleParseUnitSpinBox() | ||||
50 | { | ||||
51 | delete d; | ||||
52 | } | ||||
53 | | ||||
54 | void KisDoubleParseUnitSpinBox::changeValue( double newValue ) | ||||
55 | { | ||||
56 | if (d->unit.toUserValue(newValue) == oldValue) { | ||||
57 | return; | ||||
58 | } | ||||
59 | | ||||
60 | KisDoubleParseSpinBox::setValue( d->unit.toUserValue(newValue) ); | ||||
61 | } | ||||
62 | | ||||
63 | void KisDoubleParseUnitSpinBox::setUnit( const KoUnit & unit) | ||||
64 | { | ||||
65 | if( unit == d->unit) return; | ||||
66 | double oldValue = d->unit.fromUserValue( KisDoubleParseSpinBox::value() ); | ||||
67 | | ||||
68 | KisDoubleParseSpinBox::setMinimum( unit.toUserValue( d->lowerInPoints ) ); | ||||
69 | KisDoubleParseSpinBox::setMaximum( unit.toUserValue( d->upperInPoints ) ); | ||||
70 | | ||||
71 | qreal step = unit.toUserValue( d->stepInPoints ); | ||||
72 | | ||||
73 | if (unit.type() == KoUnit::Pixel) { | ||||
74 | // limit the pixel step by 1.0 | ||||
75 | step = qMax(qreal(1.0), step); | ||||
76 | } | ||||
77 | | ||||
78 | KisDoubleParseSpinBox::setSingleStep( step ); | ||||
79 | d->unit = unit; | ||||
80 | KisDoubleParseSpinBox::setValue( KoUnit::ptToUnit( oldValue, unit ) ); | ||||
81 | setSuffix( unit.symbol().prepend(QLatin1Char(' ')) ); | ||||
82 | } | ||||
83 | | ||||
84 | double KisDoubleParseUnitSpinBox::value( ) const | ||||
85 | { | ||||
86 | return d->unit.fromUserValue( KisDoubleParseSpinBox::value() ); | ||||
87 | } | ||||
88 | | ||||
89 | void KisDoubleParseUnitSpinBox::setMinimum(double min) | ||||
90 | { | ||||
91 | d->lowerInPoints = min; | ||||
92 | KisDoubleParseSpinBox::setMinimum( d->unit.toUserValue( min ) ); | ||||
93 | } | ||||
94 | | ||||
95 | void KisDoubleParseUnitSpinBox::setMaximum(double max) | ||||
96 | { | ||||
97 | d->upperInPoints = max; | ||||
98 | KisDoubleParseSpinBox::setMaximum( d->unit.toUserValue( max ) ); | ||||
99 | } | ||||
100 | | ||||
101 | void KisDoubleParseUnitSpinBox::setLineStep(double step) | ||||
102 | { | ||||
103 | d->stepInPoints = KoUnit(KoUnit::Point).toUserValue(step); | ||||
104 | KisDoubleParseSpinBox::setSingleStep( step ); | ||||
105 | } | ||||
106 | | ||||
107 | void KisDoubleParseUnitSpinBox::setLineStepPt(double step) | ||||
108 | { | ||||
109 | d->stepInPoints = step; | ||||
110 | KisDoubleParseSpinBox::setSingleStep( d->unit.toUserValue( step ) ); | ||||
111 | } | ||||
112 | | ||||
113 | | ||||
114 | void KisDoubleParseUnitSpinBox::setMinMaxStep( double min, double max, double step ) | ||||
115 | { | ||||
116 | setMinimum( min ); | ||||
117 | setMaximum( max ); | ||||
118 | setLineStepPt( step ); | ||||
119 | } | ||||
120 | | ||||
121 | | ||||
122 | QValidator::State KisDoubleParseUnitSpinBox::validate(QString &input, int &pos) const | ||||
123 | { | ||||
124 | Q_UNUSED(pos); | ||||
125 | | ||||
126 | QRegExp regexp ("([ a-zA-Z]+)$"); // Letters or spaces at end | ||||
127 | const int res = input.indexOf( regexp ); | ||||
128 | | ||||
129 | if ( res == -1 ) { | ||||
130 | // Nothing like an unit? The user is probably editing the unit | ||||
131 | return QValidator::Intermediate; | ||||
132 | } | ||||
133 | | ||||
134 | QString expr ( input.left( res ) ); | ||||
135 | const QString unitName ( regexp.cap( 1 ).trimmed().toLower() ); | ||||
136 | | ||||
137 | bool ok = true; | ||||
138 | bool interm = false; | ||||
139 | | ||||
140 | QValidator::State exprState = KisDoubleParseSpinBox::validate(expr, pos); | ||||
141 | | ||||
142 | if (exprState == QValidator::Invalid) { | ||||
143 | return exprState; | ||||
144 | } else if (exprState == QValidator::Intermediate) { | ||||
145 | interm = true; | ||||
146 | } | ||||
147 | | ||||
148 | //check if we can parse the unit. | ||||
149 | KoUnit::fromSymbol(unitName, &ok); | ||||
150 | | ||||
151 | if (!ok || interm) { | ||||
152 | return QValidator::Intermediate; | ||||
153 | } | ||||
154 | | ||||
155 | return QValidator::Acceptable; | ||||
156 | } | ||||
157 | | ||||
158 | QString KisDoubleParseUnitSpinBox::textFromValue( double value ) const | ||||
159 | { | ||||
160 | return KisDoubleParseSpinBox::textFromValue(value); | ||||
161 | } | ||||
162 | | ||||
163 | double KisDoubleParseUnitSpinBox::valueFromText( const QString& str ) const | ||||
164 | { | ||||
165 | //KisDoubleParseSpinBox is supposed to remove the suffix and prefix by itself. | ||||
166 | return KisDoubleParseSpinBox::valueFromText(str); | ||||
167 | } | ||||
168 | | ||||
169 | void KisDoubleParseUnitSpinBox::privateValueChanged() | ||||
170 | { | ||||
171 | emit valueChangedPt( value() ); | ||||
172 | } |