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