Changeset View
Changeset View
Standalone View
Standalone View
src/part/KgvUnitWidgets.cpp
Show All 19 Lines | 19 | /* This file was part of the KDE project | |||
---|---|---|---|---|---|
20 | Copyright (C) 2005 Jarosław Staniek <staniek@kde.org> | 20 | Copyright (C) 2005 Jarosław Staniek <staniek@kde.org> | ||
21 | 21 | | |||
22 | This program is free software; you can redistribute it and/or | 22 | This program is free software; you can redistribute it and/or | ||
23 | modify it under the terms of the GNU Library General Public | 23 | modify it under the terms of the GNU Library General Public | ||
24 | License as published by the Free Software Foundation; either | 24 | License as published by the Free Software Foundation; either | ||
25 | version 2 of the License, or (at your option) any later version. | 25 | version 2 of the License, or (at your option) any later version. | ||
26 | */ | 26 | */ | ||
27 | 27 | | |||
28 | #include "kgraphviewerlib_debug.h" | | |||
29 | #include "KgvUnitWidgets.h" | 28 | #include "KgvUnitWidgets.h" | ||
29 | #include "kgraphviewerlib_debug.h" | ||||
30 | 30 | | |||
31 | #include <QDebug> | 31 | #include <QDebug> | ||
32 | #include <QEvent> | ||||
33 | #include <QGridLayout> | ||||
32 | #include <QLocale> | 34 | #include <QLocale> | ||
33 | #include <qpushbutton.h> | | |||
34 | #include <qlayout.h> | 35 | #include <qlayout.h> | ||
35 | #include <QGridLayout> | 36 | #include <qpushbutton.h> | ||
36 | #include <QEvent> | | |||
37 | 37 | | |||
38 | // ---------------------------------------------------------------- | 38 | // ---------------------------------------------------------------- | ||
39 | // Support classes | 39 | // Support classes | ||
40 | 40 | | |||
41 | | ||||
42 | KgvUnitDoubleValidator::KgvUnitDoubleValidator(KgvUnitDoubleBase *base, QObject *parent) | 41 | KgvUnitDoubleValidator::KgvUnitDoubleValidator(KgvUnitDoubleBase *base, QObject *parent) | ||
43 | : QDoubleValidator( parent ), m_base( base ) | 42 | : QDoubleValidator(parent) | ||
43 | , m_base(base) | ||||
44 | { | 44 | { | ||
45 | } | 45 | } | ||
46 | 46 | | |||
47 | QValidator::State | 47 | QValidator::State KgvUnitDoubleValidator::validate(QString &s, int &pos) const | ||
48 | KgvUnitDoubleValidator::validate( QString &s, int &pos ) const | | |||
49 | { | 48 | { | ||
50 | | ||||
51 | qCDebug(KGRAPHVIEWERLIB_LOG) << "KgvUnitDoubleValidator::validate : " << s << " at " << pos; | 49 | qCDebug(KGRAPHVIEWERLIB_LOG) << "KgvUnitDoubleValidator::validate : " << s << " at " << pos; | ||
52 | QValidator::State result = Acceptable; | 50 | QValidator::State result = Acceptable; | ||
53 | 51 | | |||
54 | QRegExp regexp ("([ a-zA-Z]+)$"); // Letters or spaces at end | 52 | QRegExp regexp("([ a-zA-Z]+)$"); // Letters or spaces at end | ||
55 | const int res = regexp.indexIn(s); | 53 | const int res = regexp.indexIn(s); | ||
56 | 54 | | |||
57 | if ( res == -1 ) | 55 | if (res == -1) { | ||
58 | { | | |||
59 | // Nothing like an unit? The user is probably editing the unit | 56 | // Nothing like an unit? The user is probably editing the unit | ||
60 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Intermediate (no unit)"; | 57 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Intermediate (no unit)"; | ||
61 | return Intermediate; | 58 | return Intermediate; | ||
62 | } | 59 | } | ||
63 | 60 | | |||
64 | // ### TODO: are all the QString::trimmed really necessary? | 61 | // ### TODO: are all the QString::trimmed really necessary? | ||
65 | const QString number ( s.left( res ).trimmed() ); | 62 | const QString number(s.left(res).trimmed()); | ||
66 | const QString unitName ( regexp.cap( 1 ).trimmed().toLower() ); | 63 | const QString unitName(regexp.cap(1).trimmed().toLower()); | ||
67 | 64 | | |||
68 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Split:" << number << ":" << unitName << ":"; | 65 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Split:" << number << ":" << unitName << ":"; | ||
69 | 66 | | |||
70 | bool ok = false; | 67 | bool ok = false; | ||
71 | const double value = m_base->toDouble( number, &ok ); | 68 | const double value = m_base->toDouble(number, &ok); | ||
72 | double newVal = 0.0; | 69 | double newVal = 0.0; | ||
73 | if( ok ) | 70 | if (ok) { | ||
74 | { | | |||
75 | KgvUnit::Unit unit = KgvUnit::unit( unitName, &ok ); | 71 | KgvUnit::Unit unit = KgvUnit::unit(unitName, &ok); | ||
76 | if ( ok ) | 72 | if (ok) | ||
77 | newVal = KgvUnit::fromUserValue( value, unit ); | 73 | newVal = KgvUnit::fromUserValue(value, unit); | ||
78 | else | 74 | else { | ||
79 | { | | |||
80 | // Probably the user is trying to edit the unit | 75 | // Probably the user is trying to edit the unit | ||
81 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Intermediate (unknown unit)"; | 76 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Intermediate (unknown unit)"; | ||
82 | return Intermediate; | 77 | return Intermediate; | ||
83 | } | 78 | } | ||
84 | } | 79 | } else { | ||
85 | else | | |||
86 | { | | |||
87 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Not a number:" << number; | 80 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Not a number:" << number; | ||
88 | return Invalid; | 81 | return Invalid; | ||
89 | } | 82 | } | ||
90 | 83 | | |||
91 | newVal = KgvUnit::ptToUnit( newVal, m_base->m_unit ); | 84 | newVal = KgvUnit::ptToUnit(newVal, m_base->m_unit); | ||
92 | 85 | | |||
93 | s = m_base->getVisibleText( newVal ); | 86 | s = m_base->getVisibleText(newVal); | ||
94 | 87 | | |||
95 | return result; | 88 | return result; | ||
96 | } | 89 | } | ||
97 | 90 | | |||
98 | | ||||
99 | QString KgvUnitDoubleBase::getVisibleText( double value ) const | 91 | QString KgvUnitDoubleBase::getVisibleText(double value) const | ||
100 | { | 92 | { | ||
101 | const QString num ( QString( "%1%2").arg( QLocale().toString( value, m_precision ), KgvUnit::unitName( m_unit ) ) ); | 93 | const QString num(QString("%1%2").arg(QLocale().toString(value, m_precision), KgvUnit::unitName(m_unit))); | ||
102 | qCDebug(KGRAPHVIEWERLIB_LOG) << "getVisibleText: " << QString::number( value, 'f', 12 ) << " => " << num; | 94 | qCDebug(KGRAPHVIEWERLIB_LOG) << "getVisibleText: " << QString::number(value, 'f', 12) << " => " << num; | ||
103 | return num; | 95 | return num; | ||
104 | } | 96 | } | ||
105 | 97 | | |||
106 | double KgvUnitDoubleBase::toDouble( const QString& str, bool* ok ) const | 98 | double KgvUnitDoubleBase::toDouble(const QString &str, bool *ok) const | ||
107 | { | 99 | { | ||
108 | QString str2( str ); | 100 | QString str2(str); | ||
109 | /* KLocale::readNumber wants the thousand separator exactly at 1000. | 101 | /* KLocale::readNumber wants the thousand separator exactly at 1000. | ||
110 | But when editing, it might be anywhere. So we need to remove it. */ | 102 | But when editing, it might be anywhere. So we need to remove it. */ | ||
111 | const QString sep( QLocale().groupSeparator() ); | 103 | const QString sep(QLocale().groupSeparator()); | ||
112 | if ( !sep.isEmpty() ) | 104 | if (!sep.isEmpty()) | ||
113 | str2.remove( sep ); | 105 | str2.remove(sep); | ||
114 | str2.remove( KgvUnit::unitName( m_unit ) ); | 106 | str2.remove(KgvUnit::unitName(m_unit)); | ||
115 | const double dbl = QLocale().toDouble( str2, ok ); | 107 | const double dbl = QLocale().toDouble(str2, ok); | ||
116 | if ( ok ) | 108 | if (ok) | ||
117 | qCDebug(KGRAPHVIEWERLIB_LOG) << "toDouble:" << str << ": => :" << str2 << ": => " << QString::number( dbl, 'f', 12 ); | 109 | qCDebug(KGRAPHVIEWERLIB_LOG) << "toDouble:" << str << ": => :" << str2 << ": => " << QString::number(dbl, 'f', 12); | ||
118 | else | 110 | else | ||
119 | qCWarning(KGRAPHVIEWERLIB_LOG) << "error:" << str << ": => :" << str2 << ":" << endl; | 111 | qCWarning(KGRAPHVIEWERLIB_LOG) << "error:" << str << ": => :" << str2 << ":" << endl; | ||
120 | return dbl; | 112 | return dbl; | ||
121 | } | 113 | } | ||
122 | 114 | | |||
123 | | ||||
124 | // ---------------------------------------------------------------- | 115 | // ---------------------------------------------------------------- | ||
125 | // Widget classes | 116 | // Widget classes | ||
126 | 117 | | |||
127 | | ||||
128 | KgvUnitDoubleSpinBox::KgvUnitDoubleSpinBox(QWidget *parent) | 118 | KgvUnitDoubleSpinBox::KgvUnitDoubleSpinBox(QWidget *parent) | ||
129 | : QDoubleSpinBox( parent ), KgvUnitDoubleBase( KgvUnit::U_PT, 2 ) | 119 | : QDoubleSpinBox(parent) | ||
120 | , KgvUnitDoubleBase(KgvUnit::U_PT, 2) | ||||
130 | , m_lowerInPoints( -9999 ) | 121 | , m_lowerInPoints(-9999) | ||
131 | , m_upperInPoints( 9999 ) | 122 | , m_upperInPoints(9999) | ||
132 | , m_stepInPoints( 1 ) | 123 | , m_stepInPoints(1) | ||
133 | { | 124 | { | ||
134 | QDoubleSpinBox::setDecimals( 2 ); | 125 | QDoubleSpinBox::setDecimals(2); | ||
135 | m_validator = new KgvUnitDoubleValidator( this, this ); | 126 | m_validator = new KgvUnitDoubleValidator(this, this); | ||
136 | // QSpinBox::setValidator( m_validator ); | 127 | // QSpinBox::setValidator( m_validator ); | ||
137 | //setAcceptLocalizedNumbers( true ); | 128 | // setAcceptLocalizedNumbers( true ); | ||
138 | setUnit( KgvUnit::U_PT ); | 129 | setUnit(KgvUnit::U_PT); | ||
139 | 130 | | |||
140 | connect(this, static_cast<void(KgvUnitDoubleSpinBox::*)(double)>(&KgvUnitDoubleSpinBox::valueChanged), | 131 | connect(this, static_cast<void (KgvUnitDoubleSpinBox::*)(double)>(&KgvUnitDoubleSpinBox::valueChanged), this, &KgvUnitDoubleSpinBox::privateValueChanged); | ||
141 | this, &KgvUnitDoubleSpinBox::privateValueChanged); | | |||
142 | } | 132 | } | ||
143 | 133 | | |||
144 | 134 | KgvUnitDoubleSpinBox::KgvUnitDoubleSpinBox(QWidget *parent, double lower, double upper, double step, double value, KgvUnit::Unit unit, unsigned int precision) | |||
145 | KgvUnitDoubleSpinBox::KgvUnitDoubleSpinBox( QWidget *parent, | 135 | : QDoubleSpinBox(parent) | ||
146 | double lower, double upper, | 136 | , KgvUnitDoubleBase(unit, precision) | ||
147 | double step, | 137 | , m_lowerInPoints(lower) | ||
148 | double value, | 138 | , m_upperInPoints(upper) | ||
149 | KgvUnit::Unit unit, | 139 | , m_stepInPoints(step) | ||
150 | unsigned int precision) | | |||
151 | : QDoubleSpinBox( parent), | | |||
152 | KgvUnitDoubleBase( unit, precision ), | | |||
153 | m_lowerInPoints( lower ), m_upperInPoints( upper ), m_stepInPoints( step ) | | |||
154 | { | 140 | { | ||
155 | setMinimum(lower); | 141 | setMinimum(lower); | ||
156 | setMaximum(upper); | 142 | setMaximum(upper); | ||
157 | setSingleStep(step); | 143 | setSingleStep(step); | ||
158 | setValue(value); | 144 | setValue(value); | ||
159 | setDecimals(precision); | 145 | setDecimals(precision); | ||
160 | QDoubleSpinBox::setMinimum(lower); | 146 | QDoubleSpinBox::setMinimum(lower); | ||
161 | QDoubleSpinBox::setMaximum(upper); | 147 | QDoubleSpinBox::setMaximum(upper); | ||
162 | QDoubleSpinBox::setSingleStep(step); | 148 | QDoubleSpinBox::setSingleStep(step); | ||
163 | QDoubleSpinBox::setValue(value); | 149 | QDoubleSpinBox::setValue(value); | ||
164 | m_unit = KgvUnit::U_PT; | 150 | m_unit = KgvUnit::U_PT; | ||
165 | m_validator = new KgvUnitDoubleValidator( this, this ); | 151 | m_validator = new KgvUnitDoubleValidator(this, this); | ||
166 | // QSpinBox::setValidator( m_validator ); | 152 | // QSpinBox::setValidator( m_validator ); | ||
167 | //setAcceptLocalizedNumbers( true ); | 153 | // setAcceptLocalizedNumbers( true ); | ||
168 | setUnit( unit ); | 154 | setUnit(unit); | ||
169 | changeValue( value ); | 155 | changeValue(value); | ||
170 | setLineStep( 0.5 ); | 156 | setLineStep(0.5); | ||
171 | 157 | | |||
172 | connect(this, static_cast<void(KgvUnitDoubleSpinBox::*)(double)>(&KgvUnitDoubleSpinBox::valueChanged), | 158 | connect(this, static_cast<void (KgvUnitDoubleSpinBox::*)(double)>(&KgvUnitDoubleSpinBox::valueChanged), this, &KgvUnitDoubleSpinBox::privateValueChanged); | ||
173 | this, &KgvUnitDoubleSpinBox::privateValueChanged); | | |||
174 | } | 159 | } | ||
175 | 160 | | |||
176 | void | 161 | void KgvUnitDoubleSpinBox::changeValue(double val) | ||
177 | KgvUnitDoubleSpinBox::changeValue( double val ) | | |||
178 | { | 162 | { | ||
179 | QDoubleSpinBox::setValue( KgvUnit::toUserValue( val, m_unit ) ); | 163 | QDoubleSpinBox::setValue(KgvUnit::toUserValue(val, m_unit)); | ||
180 | // TODO: emit valueChanged ONLY if the value was out-of-bounds | 164 | // TODO: emit valueChanged ONLY if the value was out-of-bounds | ||
181 | // This will allow the 'user' dialog to set a dirty bool and ensure | 165 | // This will allow the 'user' dialog to set a dirty bool and ensure | ||
182 | // a proper value is getting saved. | 166 | // a proper value is getting saved. | ||
183 | } | 167 | } | ||
184 | 168 | | |||
185 | void KgvUnitDoubleSpinBox::privateValueChanged() { | 169 | void KgvUnitDoubleSpinBox::privateValueChanged() | ||
170 | { | ||||
186 | emit valueChangedPt( value () ); | 171 | emit valueChangedPt(value()); | ||
187 | } | 172 | } | ||
188 | 173 | | |||
189 | void | 174 | void KgvUnitDoubleSpinBox::setUnit(KgvUnit::Unit unit) | ||
190 | KgvUnitDoubleSpinBox::setUnit( KgvUnit::Unit unit ) | | |||
191 | { | 175 | { | ||
192 | double oldvalue = KgvUnit::fromUserValue( QDoubleSpinBox::value(), m_unit ); | 176 | double oldvalue = KgvUnit::fromUserValue(QDoubleSpinBox::value(), m_unit); | ||
193 | QDoubleSpinBox::setMinimum( KgvUnit::toUserValue( m_lowerInPoints, unit ) ); | 177 | QDoubleSpinBox::setMinimum(KgvUnit::toUserValue(m_lowerInPoints, unit)); | ||
194 | QDoubleSpinBox::setMaximum( KgvUnit::toUserValue( m_upperInPoints, unit ) ); | 178 | QDoubleSpinBox::setMaximum(KgvUnit::toUserValue(m_upperInPoints, unit)); | ||
195 | QDoubleSpinBox::setSingleStep( KgvUnit::toUserValue( m_stepInPoints, unit ) ); | 179 | QDoubleSpinBox::setSingleStep(KgvUnit::toUserValue(m_stepInPoints, unit)); | ||
196 | QDoubleSpinBox::setValue( KgvUnit::ptToUnit( oldvalue, unit ) ); | 180 | QDoubleSpinBox::setValue(KgvUnit::ptToUnit(oldvalue, unit)); | ||
197 | m_unit = unit; | 181 | m_unit = unit; | ||
198 | setSuffix( KgvUnit::unitName( unit ).prepend( ' ' ) ); | 182 | setSuffix(KgvUnit::unitName(unit).prepend(' ')); | ||
199 | } | 183 | } | ||
200 | 184 | | |||
201 | double KgvUnitDoubleSpinBox::value( void ) const | 185 | double KgvUnitDoubleSpinBox::value(void) const | ||
202 | { | 186 | { | ||
203 | return KgvUnit::fromUserValue( QDoubleSpinBox::value(), m_unit ); | 187 | return KgvUnit::fromUserValue(QDoubleSpinBox::value(), m_unit); | ||
204 | } | 188 | } | ||
205 | 189 | | |||
206 | void KgvUnitDoubleSpinBox::setMinValue( double min ) | 190 | void KgvUnitDoubleSpinBox::setMinValue(double min) | ||
207 | { | 191 | { | ||
208 | m_lowerInPoints = min; | 192 | m_lowerInPoints = min; | ||
209 | QDoubleSpinBox::setMinimum( KgvUnit::toUserValue( m_lowerInPoints, m_unit ) ); | 193 | QDoubleSpinBox::setMinimum(KgvUnit::toUserValue(m_lowerInPoints, m_unit)); | ||
210 | } | 194 | } | ||
211 | 195 | | |||
212 | void KgvUnitDoubleSpinBox::setMaxValue( double max ) | 196 | void KgvUnitDoubleSpinBox::setMaxValue(double max) | ||
213 | { | 197 | { | ||
214 | m_upperInPoints = max; | 198 | m_upperInPoints = max; | ||
215 | QDoubleSpinBox::setMaximum( KgvUnit::toUserValue( m_upperInPoints, m_unit ) ); | 199 | QDoubleSpinBox::setMaximum(KgvUnit::toUserValue(m_upperInPoints, m_unit)); | ||
216 | } | 200 | } | ||
217 | 201 | | |||
218 | void KgvUnitDoubleSpinBox::setLineStep( double step ) | 202 | void KgvUnitDoubleSpinBox::setLineStep(double step) | ||
219 | { | 203 | { | ||
220 | m_stepInPoints = KgvUnit::toUserValue(step, KgvUnit::U_PT ); | 204 | m_stepInPoints = KgvUnit::toUserValue(step, KgvUnit::U_PT); | ||
221 | QDoubleSpinBox::setSingleStep( step ); | 205 | QDoubleSpinBox::setSingleStep(step); | ||
222 | } | 206 | } | ||
223 | 207 | | |||
224 | void KgvUnitDoubleSpinBox::setLineStepPt( double step ) | 208 | void KgvUnitDoubleSpinBox::setLineStepPt(double step) | ||
225 | { | 209 | { | ||
226 | m_stepInPoints = step; | 210 | m_stepInPoints = step; | ||
227 | QDoubleSpinBox::setSingleStep( KgvUnit::toUserValue( m_stepInPoints, m_unit ) ); | 211 | QDoubleSpinBox::setSingleStep(KgvUnit::toUserValue(m_stepInPoints, m_unit)); | ||
228 | } | 212 | } | ||
229 | 213 | | |||
230 | void KgvUnitDoubleSpinBox::setMinMaxStep( double min, double max, double step ) | 214 | void KgvUnitDoubleSpinBox::setMinMaxStep(double min, double max, double step) | ||
231 | { | 215 | { | ||
232 | setMinimum( min ); | 216 | setMinimum(min); | ||
233 | setMaximum( max ); | 217 | setMaximum(max); | ||
234 | setLineStepPt( step ); | 218 | setLineStepPt(step); | ||
235 | } | 219 | } | ||
236 | 220 | | |||
237 | // ---------------------------------------------------------------- | 221 | // ---------------------------------------------------------------- | ||
238 | 222 | | |||
239 | | ||||
240 | KgvUnitDoubleLineEdit::KgvUnitDoubleLineEdit(QWidget *parent) | 223 | KgvUnitDoubleLineEdit::KgvUnitDoubleLineEdit(QWidget *parent) | ||
241 | : QLineEdit( parent ), KgvUnitDoubleBase( KgvUnit::U_PT, 2 ), m_value( 0.0 ), m_lower( 0.0 ), m_upper( 9999.99 ), | 224 | : QLineEdit(parent) | ||
242 | m_lowerInPoints( 0.0 ), m_upperInPoints( 9999.99 ) | 225 | , KgvUnitDoubleBase(KgvUnit::U_PT, 2) | ||
226 | , m_value(0.0) | ||||
227 | , m_lower(0.0) | ||||
228 | , m_upper(9999.99) | ||||
229 | , m_lowerInPoints(0.0) | ||||
230 | , m_upperInPoints(9999.99) | ||||
243 | { | 231 | { | ||
244 | setAlignment( Qt::AlignRight ); | 232 | setAlignment(Qt::AlignRight); | ||
245 | m_validator = new KgvUnitDoubleValidator( this, this ); | 233 | m_validator = new KgvUnitDoubleValidator(this, this); | ||
246 | setValidator( m_validator ); | 234 | setValidator(m_validator); | ||
247 | setUnit( KgvUnit::U_PT ); | 235 | setUnit(KgvUnit::U_PT); | ||
248 | changeValue( KgvUnit::ptToUnit( 0.0, KgvUnit::U_PT ) ); | 236 | changeValue(KgvUnit::ptToUnit(0.0, KgvUnit::U_PT)); | ||
249 | } | 237 | } | ||
250 | 238 | | |||
251 | KgvUnitDoubleLineEdit::KgvUnitDoubleLineEdit( QWidget *parent, double lower, double upper, double value, KgvUnit::Unit unit, | 239 | KgvUnitDoubleLineEdit::KgvUnitDoubleLineEdit(QWidget *parent, double lower, double upper, double value, KgvUnit::Unit unit, unsigned int precision) | ||
252 | unsigned int precision) | 240 | : QLineEdit(parent) | ||
253 | : QLineEdit( parent ), KgvUnitDoubleBase( unit, precision ), m_value( value ), m_lower( lower ), m_upper( upper ), | 241 | , KgvUnitDoubleBase(unit, precision) | ||
254 | m_lowerInPoints( lower ), m_upperInPoints( upper ) | 242 | , m_value(value) | ||
243 | , m_lower(lower) | ||||
244 | , m_upper(upper) | ||||
245 | , m_lowerInPoints(lower) | ||||
246 | , m_upperInPoints(upper) | ||||
255 | { | 247 | { | ||
256 | setAlignment( Qt::AlignRight ); | 248 | setAlignment(Qt::AlignRight); | ||
257 | m_validator = new KgvUnitDoubleValidator( this, this ); | 249 | m_validator = new KgvUnitDoubleValidator(this, this); | ||
258 | setValidator( m_validator ); | 250 | setValidator(m_validator); | ||
259 | setUnit( unit ); | 251 | setUnit(unit); | ||
260 | changeValue( KgvUnit::ptToUnit( value, unit ) ); | 252 | changeValue(KgvUnit::ptToUnit(value, unit)); | ||
261 | } | 253 | } | ||
262 | 254 | | |||
263 | void | 255 | void KgvUnitDoubleLineEdit::changeValue(double value) | ||
264 | KgvUnitDoubleLineEdit::changeValue( double value ) | | |||
265 | { | 256 | { | ||
266 | m_value = value < m_lower ? m_lower : ( value > m_upper ? m_upper : value ); | 257 | m_value = value < m_lower ? m_lower : (value > m_upper ? m_upper : value); | ||
267 | setText( getVisibleText( m_value ) ); | 258 | setText(getVisibleText(m_value)); | ||
268 | } | 259 | } | ||
269 | 260 | | |||
270 | void | 261 | void KgvUnitDoubleLineEdit::setUnit(KgvUnit::Unit unit) | ||
271 | KgvUnitDoubleLineEdit::setUnit( KgvUnit::Unit unit ) | | |||
272 | { | 262 | { | ||
273 | KgvUnit::Unit old = m_unit; | 263 | KgvUnit::Unit old = m_unit; | ||
274 | m_unit = unit; | 264 | m_unit = unit; | ||
275 | m_lower = KgvUnit::ptToUnit( m_lowerInPoints, unit ); | 265 | m_lower = KgvUnit::ptToUnit(m_lowerInPoints, unit); | ||
276 | m_upper = KgvUnit::ptToUnit( m_upperInPoints, unit ); | 266 | m_upper = KgvUnit::ptToUnit(m_upperInPoints, unit); | ||
277 | changeValue( KgvUnit::ptToUnit( KgvUnit::fromUserValue( m_value, old ), unit ) ); | 267 | changeValue(KgvUnit::ptToUnit(KgvUnit::fromUserValue(m_value, old), unit)); | ||
278 | } | 268 | } | ||
279 | 269 | | |||
280 | bool | 270 | bool KgvUnitDoubleLineEdit::eventFilter(QObject *o, QEvent *ev) | ||
281 | KgvUnitDoubleLineEdit::eventFilter( QObject* o, QEvent* ev ) | | |||
282 | { | 271 | { | ||
283 | #if 0 | 272 | #if 0 | ||
284 | if( ev->type() == QEvent::FocusOut || ev->type() == QEvent::Leave || ev->type() == QEvent::Hide ) | 273 | if( ev->type() == QEvent::FocusOut || ev->type() == QEvent::Leave || ev->type() == QEvent::Hide ) | ||
285 | { | 274 | { | ||
286 | bool ok; | 275 | bool ok; | ||
287 | double value = toDouble( text(), &ok ); | 276 | double value = toDouble( text(), &ok ); | ||
288 | changeValue( value ); | 277 | changeValue( value ); | ||
289 | return false; | 278 | return false; | ||
290 | } | 279 | } | ||
291 | else | 280 | else | ||
292 | #endif | 281 | #endif | ||
293 | return QLineEdit::eventFilter( o, ev ); | 282 | return QLineEdit::eventFilter(o, ev); | ||
294 | } | 283 | } | ||
295 | 284 | | |||
296 | double KgvUnitDoubleLineEdit::value( void ) const | 285 | double KgvUnitDoubleLineEdit::value(void) const | ||
297 | { | 286 | { | ||
298 | return KgvUnit::fromUserValue( m_value, m_unit ); | 287 | return KgvUnit::fromUserValue(m_value, m_unit); | ||
299 | } | 288 | } | ||
300 | 289 | | |||
301 | | ||||
302 | // ---------------------------------------------------------------- | 290 | // ---------------------------------------------------------------- | ||
303 | 291 | | |||
304 | | ||||
305 | KgvUnitDoubleComboBox::KgvUnitDoubleComboBox(QWidget *parent) | 292 | KgvUnitDoubleComboBox::KgvUnitDoubleComboBox(QWidget *parent) | ||
306 | : QComboBox( parent ), KgvUnitDoubleBase( KgvUnit::U_PT, 2 ), m_value( 0.0 ), m_lower( 0.0 ), m_upper( 9999.99 ), m_lowerInPoints( 0.0 ), m_upperInPoints( 9999.99 ) | 293 | : QComboBox(parent) | ||
294 | , KgvUnitDoubleBase(KgvUnit::U_PT, 2) | ||||
295 | , m_value(0.0) | ||||
296 | , m_lower(0.0) | ||||
297 | , m_upper(9999.99) | ||||
298 | , m_lowerInPoints(0.0) | ||||
299 | , m_upperInPoints(9999.99) | ||||
307 | { | 300 | { | ||
308 | lineEdit()->setAlignment( Qt::AlignRight ); | 301 | lineEdit()->setAlignment(Qt::AlignRight); | ||
309 | m_validator = new KgvUnitDoubleValidator( this, this ); | 302 | m_validator = new KgvUnitDoubleValidator(this, this); | ||
310 | lineEdit()->setValidator( m_validator ); | 303 | lineEdit()->setValidator(m_validator); | ||
311 | setUnit( KgvUnit::U_PT ); | 304 | setUnit(KgvUnit::U_PT); | ||
312 | changeValue( KgvUnit::ptToUnit( 0.0, KgvUnit::U_PT ) ); | 305 | changeValue(KgvUnit::ptToUnit(0.0, KgvUnit::U_PT)); | ||
313 | connect(this, static_cast<void(KgvUnitDoubleComboBox::*)(int)>(&KgvUnitDoubleComboBox::activated), | 306 | connect(this, static_cast<void (KgvUnitDoubleComboBox::*)(int)>(&KgvUnitDoubleComboBox::activated), this, &KgvUnitDoubleComboBox::slotActivated); | ||
314 | this, &KgvUnitDoubleComboBox::slotActivated); | | |||
315 | } | 307 | } | ||
316 | 308 | | |||
317 | KgvUnitDoubleComboBox::KgvUnitDoubleComboBox( QWidget *parent, double lower, double upper, double value, KgvUnit::Unit unit, | 309 | KgvUnitDoubleComboBox::KgvUnitDoubleComboBox(QWidget *parent, double lower, double upper, double value, KgvUnit::Unit unit, unsigned int precision) | ||
318 | unsigned int precision) | 310 | : QComboBox(parent) | ||
319 | : QComboBox( parent ), KgvUnitDoubleBase( unit, precision ), m_value( value ), m_lower( lower ), m_upper( upper ), | 311 | , KgvUnitDoubleBase(unit, precision) | ||
320 | m_lowerInPoints( lower ), m_upperInPoints( upper ) | 312 | , m_value(value) | ||
313 | , m_lower(lower) | ||||
314 | , m_upper(upper) | ||||
315 | , m_lowerInPoints(lower) | ||||
316 | , m_upperInPoints(upper) | ||||
321 | { | 317 | { | ||
322 | lineEdit()->setAlignment( Qt::AlignRight ); | 318 | lineEdit()->setAlignment(Qt::AlignRight); | ||
323 | m_validator = new KgvUnitDoubleValidator( this, this ); | 319 | m_validator = new KgvUnitDoubleValidator(this, this); | ||
324 | lineEdit()->setValidator( m_validator ); | 320 | lineEdit()->setValidator(m_validator); | ||
325 | setUnit( unit ); | 321 | setUnit(unit); | ||
326 | changeValue( KgvUnit::ptToUnit( value, unit ) ); | 322 | changeValue(KgvUnit::ptToUnit(value, unit)); | ||
327 | connect(this, static_cast<void(KgvUnitDoubleComboBox::*)(int)>(&KgvUnitDoubleComboBox::activated), | 323 | connect(this, static_cast<void (KgvUnitDoubleComboBox::*)(int)>(&KgvUnitDoubleComboBox::activated), this, &KgvUnitDoubleComboBox::slotActivated); | ||
328 | this, &KgvUnitDoubleComboBox::slotActivated); | | |||
329 | } | 324 | } | ||
330 | 325 | | |||
331 | void | 326 | void KgvUnitDoubleComboBox::changeValue(double value) | ||
332 | KgvUnitDoubleComboBox::changeValue( double value ) | | |||
333 | { | 327 | { | ||
334 | QString oldLabel = lineEdit()->text(); | 328 | QString oldLabel = lineEdit()->text(); | ||
335 | updateValue( value ); | 329 | updateValue(value); | ||
336 | if( lineEdit()->text() != oldLabel ) | 330 | if (lineEdit()->text() != oldLabel) | ||
337 | emit valueChanged( m_value ); | 331 | emit valueChanged(m_value); | ||
338 | } | 332 | } | ||
339 | 333 | | |||
340 | void | 334 | void KgvUnitDoubleComboBox::updateValue(double value) | ||
341 | KgvUnitDoubleComboBox::updateValue( double value ) | | |||
342 | { | 335 | { | ||
343 | m_value = value < m_lower ? m_lower : ( value > m_upper ? m_upper : value ); | 336 | m_value = value < m_lower ? m_lower : (value > m_upper ? m_upper : value); | ||
344 | lineEdit()->setText( getVisibleText( m_value ) ); | 337 | lineEdit()->setText(getVisibleText(m_value)); | ||
345 | } | 338 | } | ||
346 | 339 | | |||
347 | void | 340 | void KgvUnitDoubleComboBox::insertItem(double value, int index) | ||
348 | KgvUnitDoubleComboBox::insertItem( double value, int index ) | | |||
349 | { | 341 | { | ||
350 | QComboBox::insertItem(index, getVisibleText( value )); | 342 | QComboBox::insertItem(index, getVisibleText(value)); | ||
351 | } | 343 | } | ||
352 | 344 | | |||
353 | void | 345 | void KgvUnitDoubleComboBox::slotActivated(int index) | ||
354 | KgvUnitDoubleComboBox::slotActivated( int index ) | | |||
355 | { | 346 | { | ||
356 | double oldvalue = m_value; | 347 | double oldvalue = m_value; | ||
357 | bool ok; | 348 | bool ok; | ||
358 | double value = toDouble( itemText( index ), &ok ); | 349 | double value = toDouble(itemText(index), &ok); | ||
359 | m_value = value < m_lower ? m_lower : ( value > m_upper ? m_upper : value ); | 350 | m_value = value < m_lower ? m_lower : (value > m_upper ? m_upper : value); | ||
360 | if( m_value != oldvalue ) | 351 | if (m_value != oldvalue) | ||
361 | emit valueChanged( m_value ); | 352 | emit valueChanged(m_value); | ||
362 | } | 353 | } | ||
363 | 354 | | |||
364 | void | 355 | void KgvUnitDoubleComboBox::setUnit(KgvUnit::Unit unit) | ||
365 | KgvUnitDoubleComboBox::setUnit( KgvUnit::Unit unit ) | | |||
366 | { | 356 | { | ||
367 | KgvUnit::Unit old = m_unit; | 357 | KgvUnit::Unit old = m_unit; | ||
368 | m_unit = unit; | 358 | m_unit = unit; | ||
369 | m_lower = KgvUnit::ptToUnit( m_lowerInPoints, unit ); | 359 | m_lower = KgvUnit::ptToUnit(m_lowerInPoints, unit); | ||
370 | m_upper = KgvUnit::ptToUnit( m_upperInPoints, unit ); | 360 | m_upper = KgvUnit::ptToUnit(m_upperInPoints, unit); | ||
371 | changeValue( KgvUnit::ptToUnit( KgvUnit::fromUserValue( m_value, old ), unit ) ); | 361 | changeValue(KgvUnit::ptToUnit(KgvUnit::fromUserValue(m_value, old), unit)); | ||
372 | } | 362 | } | ||
373 | 363 | | |||
374 | bool | 364 | bool KgvUnitDoubleComboBox::eventFilter(QObject *o, QEvent *ev) | ||
375 | KgvUnitDoubleComboBox::eventFilter( QObject* o, QEvent* ev ) | | |||
376 | { | 365 | { | ||
377 | #if 0 | 366 | #if 0 | ||
378 | if( ev->type() == QEvent::FocusOut || ev->type() == QEvent::Leave || ev->type() == QEvent::Hide ) | 367 | if( ev->type() == QEvent::FocusOut || ev->type() == QEvent::Leave || ev->type() == QEvent::Hide ) | ||
379 | { | 368 | { | ||
380 | bool ok; | 369 | bool ok; | ||
381 | double value = toDouble( lineEdit()->text(), &ok ); | 370 | double value = toDouble( lineEdit()->text(), &ok ); | ||
382 | changeValue( value ); | 371 | changeValue( value ); | ||
383 | return false; | 372 | return false; | ||
384 | } | 373 | } | ||
385 | else | 374 | else | ||
386 | #endif | 375 | #endif | ||
387 | return QComboBox::eventFilter( o, ev ); | 376 | return QComboBox::eventFilter(o, ev); | ||
388 | } | 377 | } | ||
389 | 378 | | |||
390 | double KgvUnitDoubleComboBox::value( void ) const | 379 | double KgvUnitDoubleComboBox::value(void) const | ||
391 | { | 380 | { | ||
392 | return KgvUnit::fromUserValue( m_value, m_unit ); | 381 | return KgvUnit::fromUserValue(m_value, m_unit); | ||
393 | } | 382 | } | ||
394 | 383 | | |||
395 | | ||||
396 | // ---------------------------------------------------------------- | 384 | // ---------------------------------------------------------------- | ||
397 | 385 | | |||
398 | | ||||
399 | KgvUnitDoubleSpinComboBox::KgvUnitDoubleSpinComboBox(QWidget *parent) | 386 | KgvUnitDoubleSpinComboBox::KgvUnitDoubleSpinComboBox(QWidget *parent) | ||
400 | : QWidget( parent ), m_step( 1.0 ) | 387 | : QWidget(parent) | ||
388 | , m_step(1.0) | ||||
401 | { | 389 | { | ||
402 | QGridLayout *layout = new QGridLayout( this ); | 390 | QGridLayout *layout = new QGridLayout(this); | ||
403 | //layout->setMargin( 2 ); | 391 | // layout->setMargin( 2 ); | ||
404 | QPushButton *up = new QPushButton( "+", this ); | 392 | QPushButton *up = new QPushButton("+", this); | ||
405 | //up->setFlat( true ); | 393 | // up->setFlat( true ); | ||
406 | up->setMaximumHeight( 15 ); | 394 | up->setMaximumHeight(15); | ||
407 | up->setMaximumWidth( 15 ); | 395 | up->setMaximumWidth(15); | ||
408 | layout->addWidget( up, 0, 0 ); | 396 | layout->addWidget(up, 0, 0); | ||
409 | connect(up, &QPushButton::clicked, | 397 | connect(up, &QPushButton::clicked, this, &KgvUnitDoubleSpinComboBox::slotUpClicked); | ||
410 | this, &KgvUnitDoubleSpinComboBox::slotUpClicked); | | |||
411 | 398 | | |||
412 | QPushButton *down = new QPushButton( "-", this ); | 399 | QPushButton *down = new QPushButton("-", this); | ||
413 | down->setMaximumHeight( 15 ); | 400 | down->setMaximumHeight(15); | ||
414 | down->setMaximumWidth( 15 ); | 401 | down->setMaximumWidth(15); | ||
415 | layout->addWidget( down, 1, 0 ); | 402 | layout->addWidget(down, 1, 0); | ||
416 | connect(down, &QPushButton::clicked, | 403 | connect(down, &QPushButton::clicked, this, &KgvUnitDoubleSpinComboBox::slotDownClicked); | ||
417 | this, &KgvUnitDoubleSpinComboBox::slotDownClicked); | | |||
418 | 404 | | |||
419 | m_combo = new KgvUnitDoubleComboBox(this, KgvUnit::ptToUnit(0.0, KgvUnit::U_PT), KgvUnit::ptToUnit(9999.99, KgvUnit::U_PT), 0.0, KgvUnit::U_PT, 2); | 405 | m_combo = new KgvUnitDoubleComboBox(this, KgvUnit::ptToUnit(0.0, KgvUnit::U_PT), KgvUnit::ptToUnit(9999.99, KgvUnit::U_PT), 0.0, KgvUnit::U_PT, 2); | ||
420 | connect(m_combo, &KgvUnitDoubleComboBox::valueChanged, | 406 | connect(m_combo, &KgvUnitDoubleComboBox::valueChanged, this, &KgvUnitDoubleSpinComboBox::valueChanged); | ||
421 | this, &KgvUnitDoubleSpinComboBox::valueChanged); | | |||
422 | layout->addWidget( m_combo, 0, 2, 2, 1 ); | 407 | layout->addWidget(m_combo, 0, 2, 2, 1); | ||
423 | } | 408 | } | ||
424 | 409 | | |||
425 | KgvUnitDoubleSpinComboBox::KgvUnitDoubleSpinComboBox( QWidget *parent, double lower, double upper, double step, double value, | 410 | KgvUnitDoubleSpinComboBox::KgvUnitDoubleSpinComboBox(QWidget *parent, double lower, double upper, double step, double value, KgvUnit::Unit unit, unsigned int precision) | ||
426 | KgvUnit::Unit unit, unsigned int precision) | 411 | : QWidget(parent) | ||
427 | : QWidget( parent ), m_step( step )//, m_lowerInPoints( lower ), m_upperInPoints( upper ) | 412 | , m_step(step) //, m_lowerInPoints( lower ), m_upperInPoints( upper ) | ||
428 | { | 413 | { | ||
429 | QGridLayout *layout = new QGridLayout( this ); | 414 | QGridLayout *layout = new QGridLayout(this); | ||
430 | //layout->setMargin( 2 ); | 415 | // layout->setMargin( 2 ); | ||
431 | QPushButton *up = new QPushButton( "+", this ); | 416 | QPushButton *up = new QPushButton("+", this); | ||
432 | //up->setFlat( true ); | 417 | // up->setFlat( true ); | ||
433 | up->setMaximumHeight( 15 ); | 418 | up->setMaximumHeight(15); | ||
434 | up->setMaximumWidth( 15 ); | 419 | up->setMaximumWidth(15); | ||
435 | layout->addWidget( up, 0, 0 ); | 420 | layout->addWidget(up, 0, 0); | ||
436 | connect(up, &QPushButton::clicked, | 421 | connect(up, &QPushButton::clicked, this, &KgvUnitDoubleSpinComboBox::slotUpClicked); | ||
437 | this, &KgvUnitDoubleSpinComboBox::slotUpClicked); | | |||
438 | 422 | | |||
439 | QPushButton *down = new QPushButton( "-", this ); | 423 | QPushButton *down = new QPushButton("-", this); | ||
440 | down->setMaximumHeight( 15 ); | 424 | down->setMaximumHeight(15); | ||
441 | down->setMaximumWidth( 15 ); | 425 | down->setMaximumWidth(15); | ||
442 | layout->addWidget( down, 1, 0 ); | 426 | layout->addWidget(down, 1, 0); | ||
443 | connect(down, &QPushButton::clicked, | 427 | connect(down, &QPushButton::clicked, this, &KgvUnitDoubleSpinComboBox::slotDownClicked); | ||
444 | this, &KgvUnitDoubleSpinComboBox::slotDownClicked); | | |||
445 | 428 | | |||
446 | m_combo = new KgvUnitDoubleComboBox(this, KgvUnit::ptToUnit(lower, unit), KgvUnit::ptToUnit(upper, unit), value, unit, precision); | 429 | m_combo = new KgvUnitDoubleComboBox(this, KgvUnit::ptToUnit(lower, unit), KgvUnit::ptToUnit(upper, unit), value, unit, precision); | ||
447 | connect(m_combo, &KgvUnitDoubleComboBox::valueChanged, | 430 | connect(m_combo, &KgvUnitDoubleComboBox::valueChanged, this, &KgvUnitDoubleSpinComboBox::valueChanged); | ||
448 | this, &KgvUnitDoubleSpinComboBox::valueChanged); | | |||
449 | layout->addWidget( m_combo, 0, 2, 2, 1 ); | 431 | layout->addWidget(m_combo, 0, 2, 2, 1); | ||
450 | } | 432 | } | ||
451 | 433 | | |||
452 | void | 434 | void KgvUnitDoubleSpinComboBox::slotUpClicked() | ||
453 | KgvUnitDoubleSpinComboBox::slotUpClicked() | | |||
454 | { | 435 | { | ||
455 | m_combo->changeValue( m_combo->value() + m_step ); | 436 | m_combo->changeValue(m_combo->value() + m_step); | ||
456 | } | 437 | } | ||
457 | 438 | | |||
458 | void | 439 | void KgvUnitDoubleSpinComboBox::slotDownClicked() | ||
459 | KgvUnitDoubleSpinComboBox::slotDownClicked() | | |||
460 | { | 440 | { | ||
461 | m_combo->changeValue( m_combo->value() - m_step ); | 441 | m_combo->changeValue(m_combo->value() - m_step); | ||
462 | } | 442 | } | ||
463 | 443 | | |||
464 | void | 444 | void KgvUnitDoubleSpinComboBox::insertItem(double value, int index) | ||
465 | KgvUnitDoubleSpinComboBox::insertItem( double value, int index ) | | |||
466 | { | 445 | { | ||
467 | m_combo->insertItem( value, index ); | 446 | m_combo->insertItem(value, index); | ||
468 | } | 447 | } | ||
469 | 448 | | |||
470 | void | 449 | void KgvUnitDoubleSpinComboBox::updateValue(double value) | ||
471 | KgvUnitDoubleSpinComboBox::updateValue( double value ) | | |||
472 | { | 450 | { | ||
473 | m_combo->updateValue( value ); | 451 | m_combo->updateValue(value); | ||
474 | } | 452 | } | ||
475 | 453 | | |||
476 | double | 454 | double KgvUnitDoubleSpinComboBox::value() const | ||
477 | KgvUnitDoubleSpinComboBox::value() const | | |||
478 | { | 455 | { | ||
479 | return m_combo->value(); | 456 | return m_combo->value(); | ||
480 | } | 457 | } |