66#define setField(f, l, ls, c, fmt, r, rs, s, fs, sc, u) \
69 m_fields[f].left = l; \
70 m_fields[f].leftScale = ls; \
71 m_fields[f].current = c; \
72 m_fields[f].format = fmt; \
73 m_fields[f].right = r; \
74 m_fields[f].rightScale = rs; \
75 m_fields[f].separator = s; \
76 m_fields[f].fullScale = fs; \
77 m_fields[f].scale = sc; \
78 m_fields[f].unit = u; \
81void DurationWidget::init()
84 m_decimalPoint = TDEGlobal::locale()->decimalSymbol();
103 TQRegExp re(TQString(
"\\d{1,10}|\\d{1,7}\\") + m_decimalPoint +
104 TQString(
"\\d{0,10}|\\d{0,7}\\") + m_decimalPoint +
105 TQString(
"\\d{1,3}"));
106 m_validator =
new TQRegExpValidator(re,
this);
107 m_ddd->setValidator(m_validator);
108 m_hh->setValidator(m_validator);
109 m_mm->setValidator(m_validator);
110 m_ss->setValidator(m_validator);
111 m_ms->setValidator(m_validator);
120 setField(0, NULL, 0, m_ddd,
"%u", m_hh, 24, m_hhSpace, 24, 24, m_ddUnit);
121 setField(1, m_ddd, 24, m_hh,
"%02u", m_mm, 60, m_mmColon, 60, 60, m_hhUnit);
122 setField(2, m_hh, 60, m_mm,
"%02u", m_ss, 60, NULL, 60, 60, m_mmUnit);
123 setField(3, m_mm, 60, m_ss,
"%02u", m_ms, 1000, m_ssColon, 60, 60, m_ssUnit);
124 setField(4, m_ss, 1000, m_ms,
"%03u", NULL, 0, m_dot, 1000, 1000, m_msUnit);
127void DurationWidget::destroy()
133TQ_INT64 DurationWidget::setValueMilliseconds(TQ_INT64 milliseconds)
135 unsigned sc = (unsigned)m_fields[4].leftScale;
136 TQ_INT64 secs = milliseconds / sc;
137 TQ_INT64 ms = milliseconds % sc;
139 tmp.sprintf(m_fields[4].format, ms);
140 m_fields[4].current->setText(tmp);
144TQ_INT64 DurationWidget::setValueSeconds(TQ_INT64 seconds)
146 unsigned sc = (unsigned)m_fields[3].leftScale;
147 TQ_INT64 mins = seconds / sc;
148 TQ_INT64 s = seconds % sc;
150 tmp.sprintf(m_fields[3].format, s);
151 m_fields[3].current->setText(tmp);
155TQ_INT64 DurationWidget::setValueMinutes(TQ_INT64 mins)
157 unsigned sc = (unsigned)m_fields[2].leftScale;
158 TQ_INT64 hours = mins / sc;
159 TQ_INT64 m = mins % sc;
161 tmp.sprintf(m_fields[2].format, m);
162 m_fields[2].current->setText(tmp);
167TQ_INT64 DurationWidget::setValueHours(TQ_INT64 mins)
169 if (m_fields[1].current->isHidden())
171 unsigned sc = (unsigned)m_fields[1].rightScale;
172 TQ_INT64 hours = (TQ_INT64)(mins / sc);
173 TQ_INT64 m = mins - (TQ_INT64)(hours * sc);
176 tmp.sprintf(m_fields[1].format, hours);
177 m_fields[1].current->setText(tmp);
182TQ_INT64 DurationWidget::setValueDays(TQ_INT64 mins)
184 if (m_fields[0].current->isHidden())
186 double sc = m_fields[1].rightScale * m_fields[0].rightScale;
187 TQ_INT64 days = (TQ_INT64)(mins / sc);
188 TQ_INT64 m = mins - (TQ_INT64)(days * sc);
191 tmp.sprintf(m_fields[0].format, days);
192 m_fields[0].current->setText(tmp);
196void DurationWidget::setValue(
const KPlato::Duration &newDuration)
198 TQ_INT64 value = newDuration.milliseconds();
200 value = setValueMilliseconds(value);
201 value = setValueSeconds(value);
203 value = setValueDays(value);
204 value = setValueHours(value);
205 value = setValueMinutes(value);
206 if (value > 0) kdError()<<k_funcinfo<<
"Remainder > 0: "<<value<<endl;
211Duration DurationWidget::value()
const
215 if (!m_fields[i].current->isHidden() &&
216 m_fields[i].scale > 0 &&
217 m_fields[i].scale <= m_fields[i].fullScale)
219 double v = m_fields[i].current->text().toDouble();
220 v = v * m_fields[i].scale / m_fields[i].fullScale;;
221 d.addMilliseconds((TQ_INT64)(v*(1000*60*60*24)));
224 if (!m_fields[i].current->isHidden() &&
225 m_fields[i].scale > 0 &&
226 m_fields[i].scale <= m_fields[i].fullScale)
228 double v = m_fields[i].current->text().toDouble();
229 v = v * m_fields[i].scale / m_fields[i].fullScale;;
230 d.addMilliseconds((TQ_INT64)(v*(1000*60*60)));
233 if (!m_fields[i].current->isHidden() &&
234 m_fields[i].scale > 0 &&
235 m_fields[i].scale <= m_fields[i].fullScale)
237 double v = m_fields[i].current->text().toDouble();
238 v = v * m_fields[i].scale / m_fields[i].fullScale;;
239 d.addMilliseconds((TQ_INT64)(v*(1000*60)));
242 if (!m_fields[i].current->isHidden() &&
243 m_fields[i].scale > 0 &&
244 m_fields[i].scale <= m_fields[i].fullScale)
246 double v = m_fields[i].current->text().toDouble();
247 v = v * m_fields[i].scale / m_fields[i].fullScale;;
248 d.addMilliseconds((TQ_INT64)(v*(1000)));
251 if (!m_fields[i].current->isHidden())
253 TQ_INT64 v = m_fields[i].current->text().toUInt();
254 d.addMilliseconds(v);
259void DurationWidget::dddLostFocus()
265void DurationWidget::hhLostFocus( )
271void DurationWidget::mmLostFocus()
277void DurationWidget::ssLostFocus()
283void DurationWidget::msLostFocus()
289void DurationWidget::handleLostFocus(
293 TQLineEdit *left = m_fields[field].left;
294 double leftScale = m_fields[field].leftScale;
295 const char *leftFormat = left ? m_fields[field - 1].format : NULL;
296 TQLineEdit *current = m_fields[field].current;
297 const char *currentFormat = m_fields[field].format;
298 TQLineEdit *right = m_fields[field].right;
299 double rightScale = m_fields[field].rightScale;
300 const char *rightFormat = right ? m_fields[field + 1].format : NULL;
307 TQString newValue(current->text());
308 double v = TDEGlobal::locale()->readNumber(newValue);
309 unsigned currentValue = 0;
312 if (left && v >= leftScale)
316 tmp.sprintf(leftFormat, (
unsigned)(v / leftScale));
318 handleLostFocus(field - 1);
321 v = v - (tmp.toUInt() * leftScale);
322 newValue = TDEGlobal::locale()->formatNumber(v);
324 int point = newValue.find(m_decimalPoint);
329 double frac = fraction(newValue, &p);
330 if (right && frac > 0.0)
334 v = rightScale * (frac*power(10.0, -p));
335 frac = fraction(TDEGlobal::locale()->formatNumber(v, 19), 0);
339 tmp = TDEGlobal::locale()->formatNumber(v, 19);
341 handleLostFocus(field + 1);
343 tmp.sprintf(rightFormat, (
unsigned)(v));
349 newValue = newValue.left(point);
351 currentValue = newValue.toUInt();
352 tmp.sprintf(currentFormat, currentValue);
353 current->setText(tmp);
357void DurationWidget::setVisibleFields(
int fieldMask )
360 for (i = 0; i < 5; i++)
362 bool visible = ((fieldMask >> i) & 1) == 1;
367 m_fields[i].current->show();
368 if (m_fields[i].separator)
370 m_fields[i].separator->show();
372 if (m_fields[i].unit)
374 m_fields[i].unit->show();
379 m_fields[i].current->hide();
380 if (m_fields[i].separator)
382 m_fields[i].separator->hide();
384 if (m_fields[i].unit)
386 m_fields[i].unit->hide();
393int DurationWidget::visibleFields()
397 for (i = 0; i < 5; i++)
399 if (m_fields[i].current->isHidden())
401 fieldMask |= (1 << i);
407void DurationWidget::setFieldLeftscale(
int f,
double ls)
409 m_fields[f].leftScale = ls;
412void DurationWidget::setFieldRightscale(
int f,
double rs)
414 m_fields[f].rightScale = rs;
417void DurationWidget::setFieldScale(
int f,
double scale)
419 m_fields[f].scale = scale;
422void DurationWidget::setFieldUnit(
int f, TQString unit)
424 if (m_fields[f].unit)
426 m_fields[f].unit->setText(unit);
430double DurationWidget::power(
double m,
int e) {
431 int c = e > 0 ? e : -e;
433 for (
int i=0; i < c; ++i) {
434 value = e > 0 ? value * m : value / m;
439double DurationWidget::fraction(TQString number,
int *exp) {
440 int point = number.find(m_decimalPoint);
446 v = number.mid(point+m_decimalPoint.length());
450 v = number.mid(point);
452 return TDEGlobal::locale()->readNumber(v);
The duration class can be used to store a timespan in a convenient format.
This structure describes one of the fields shown.