summaryrefslogtreecommitdiffstats
path: root/kplato/kptdurationwidget.ui.h
blob: 837114d8256a126ed92a0d6dee40338a48de1da1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//
// ui.h extension file, included from the uic-generated form implementation.
//
// If you wish to add, delete or rename functions or slots use
// TQt Designer which will update this file, preserving your code. Create an
// init() function in place of a constructor, and a destroy() function in
// place of a destructor.
//

/* This file is part of the KDE project
   Copyright (C) 2004 - 2006 Dag Andersen <danders@get2net.dk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation;
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>

#include <cmath>

namespace KPlato
{

/**
 * This structure describes one of the fields shown.
 */
struct FieldDescriptor
{
    // Which field is to my left, and what conversion factor separates us?
    TQLineEdit *left;
    double leftScale;
    
    // Which field am I, and how am I formatted?
    TQLineEdit *current;
    const char *format;
    
    // Which field is to my right, and what conversion factor separates us?
    TQLineEdit *right;
    double rightScale;
    
    // If I am hidden, who else hides with me?
    TQLabel *separator;
    
    // Used for calculating a correct duration
    double fullScale;
    double scale;
    
    // Used for displaying a unit behind each field
    TQLabel *unit;
};

#define setField(f, l, ls, c, fmt, r, rs, s, fs, sc, u) \
do \
{ \
    m_fields[f].left = l; \
    m_fields[f].leftScale = ls; \
    m_fields[f].current = c; \
    m_fields[f].format = fmt; \
    m_fields[f].right = r; \
    m_fields[f].rightScale = rs; \
    m_fields[f].separator = s; \
    m_fields[f].fullScale = fs; \
    m_fields[f].scale = sc; \
    m_fields[f].unit = u; \
} while (0)
    
void DurationWidget::init()
{
    // Use the user's decimal point!
    m_decimalPoint = TDEGlobal::locale()->decimalSymbol();
    
    //NOTE: 
    //      This isn't as flexible/general as Shaheed once made it.
    //      It's now a long list of hacks.
    //      Introducing double in scales and allowing leftscale/scale/rightscale
    //      *NOT* to be multiples of each other increases the complexity and also
    //      introduces rounding errors. (eg. hour = 60 minutes, day = 7,5 hours)
    //
    //      If you know how, please make a better solution!
    //
    // Any field can be entered as an integer or a floating point value. Whatever
    // is entered is treated as follows:
    //
    //    - any fractional part is moved right one field
    //
    //    - any overflow from the integer part is carried left one field
    //
    // and the process repeated until the rightmost and leftmost fields are reached.
    TQRegExp re(TQString("\\d{1,10}|\\d{1,7}\\") + m_decimalPoint + 
        TQString("\\d{0,10}|\\d{0,7}\\") + m_decimalPoint + 
        TQString("\\d{1,3}"));
    m_validator = new TQRegExpValidator(re, TQT_TQOBJECT(this));
    m_ddd->setValidator(m_validator);
    m_hh->setValidator(m_validator);
    m_mm->setValidator(m_validator);
    m_ss->setValidator(m_validator);
    m_ms->setValidator(m_validator);
    
    m_ddUnit->hide();
    m_hhUnit->hide();
    m_mmUnit->hide();
    m_ssUnit->hide();
    m_msUnit->hide();
    
    m_fields = new FieldDescriptor[5];    
    setField(0, NULL, 0, m_ddd, "%u", m_hh, 24, m_hhSpace, 24, 24, m_ddUnit);
    setField(1, m_ddd, 24, m_hh, "%02u", m_mm, 60, m_mmColon, 60, 60, m_hhUnit);
    setField(2, m_hh, 60, m_mm, "%02u", m_ss, 60, NULL, 60, 60, m_mmUnit);
    setField(3, m_mm, 60, m_ss, "%02u", m_ms, 1000, m_ssColon, 60, 60, m_ssUnit);
    setField(4, m_ss, 1000, m_ms, "%03u", NULL, 0, m_dot, 1000, 1000, m_msUnit);
}

void DurationWidget::destroy()
{
    delete m_fields;
    //delete m_validator;  //TQWidget takes care of this
}

TQ_INT64 DurationWidget::setValueMilliseconds(TQ_INT64 milliseconds)
{
    unsigned sc = (unsigned)m_fields[4].leftScale;
    TQ_INT64 secs = milliseconds / sc;
    TQ_INT64 ms = milliseconds % sc;
    TQString tmp;
    tmp.sprintf(m_fields[4].format, ms);
    m_fields[4].current->setText(tmp);
    return secs;
}

TQ_INT64 DurationWidget::setValueSeconds(TQ_INT64 seconds)
{
    unsigned sc = (unsigned)m_fields[3].leftScale;
    TQ_INT64 mins = seconds / sc;
    TQ_INT64 s = seconds % sc;
    TQString tmp;
    tmp.sprintf(m_fields[3].format, s);
    m_fields[3].current->setText(tmp);
    return mins;
}

TQ_INT64 DurationWidget::setValueMinutes(TQ_INT64 mins)
{
    unsigned sc = (unsigned)m_fields[2].leftScale;
    TQ_INT64 hours = mins / sc;
    TQ_INT64 m = mins % sc;
    TQString tmp;
    tmp.sprintf(m_fields[2].format, m);
    m_fields[2].current->setText(tmp);
    return hours;
}

// NOTE: Input is minutes and also returns minutes!
TQ_INT64 DurationWidget::setValueHours(TQ_INT64 mins)
{
    if (m_fields[1].current->isHidden())
        return mins;
    unsigned sc = (unsigned)m_fields[1].rightScale;
    TQ_INT64 hours = (TQ_INT64)(mins / sc);
    TQ_INT64 m = mins - (TQ_INT64)(hours * sc);
    //kdDebug()<<k_funcinfo<<"mins="<<mins<<" -> hours="<<hours<<" rem="<<m<<endl;
    TQString tmp;
    tmp.sprintf(m_fields[1].format, hours);
    m_fields[1].current->setText(tmp);
    return m;
}

// NOTE: Input is minutes and also returns minutes!
TQ_INT64 DurationWidget::setValueDays(TQ_INT64 mins)
{
    if (m_fields[0].current->isHidden())
        return mins;
    double sc = m_fields[1].rightScale * m_fields[0].rightScale;
    TQ_INT64 days = (TQ_INT64)(mins / sc);
    TQ_INT64 m = mins - (TQ_INT64)(days * sc);
    //kdDebug()<<k_funcinfo<<"mins="<<mins<<" -> days="<<days<<" rem="<<m<<endl;
    TQString tmp;
    tmp.sprintf(m_fields[0].format, days);
    m_fields[0].current->setText(tmp);
    return m;
}

void DurationWidget::setValue(const KPlato::Duration &newDuration)
{
    TQ_INT64 value = newDuration.milliseconds();
    //kdDebug()<<k_funcinfo<<f<<": value="<<value<<endl;
    value = setValueMilliseconds(value); // returns seconds
    value = setValueSeconds(value); // returns minutes
    // Now call days first to allow for fractional scales
    value = setValueDays(value); // NOTE  returns minutes
    value = setValueHours(value); // NOTE  returns minutes
    value = setValueMinutes(value); // returns hours: Should be 0
    if (value > 0) kdError()<<k_funcinfo<<"Remainder > 0: "<<value<<endl;
    
    emit valueChanged();
}

Duration DurationWidget::value() const
{
    Duration d;
    int i=0;
    if (!m_fields[i].current->isHidden() &&
        m_fields[i].scale > 0 &&
        m_fields[i].scale <= m_fields[i].fullScale)
    {
        double v = m_fields[i].current->text().toDouble();
        v = v * m_fields[i].scale / m_fields[i].fullScale;;
        d.addMilliseconds((TQ_INT64)(v*(1000*60*60*24)));
    }
    ++i;
    if (!m_fields[i].current->isHidden() &&
        m_fields[i].scale > 0 &&
        m_fields[i].scale <= m_fields[i].fullScale)
    {
        double v = m_fields[i].current->text().toDouble();
        v = v * m_fields[i].scale / m_fields[i].fullScale;;
        d.addMilliseconds((TQ_INT64)(v*(1000*60*60)));
    }
    ++i;
    if (!m_fields[i].current->isHidden() &&
        m_fields[i].scale > 0 &&
        m_fields[i].scale <= m_fields[i].fullScale)
    {
        double v = m_fields[i].current->text().toDouble();
        v = v * m_fields[i].scale / m_fields[i].fullScale;;
        d.addMilliseconds((TQ_INT64)(v*(1000*60)));
    }
    ++i;
    if (!m_fields[i].current->isHidden() &&
        m_fields[i].scale > 0 &&
        m_fields[i].scale <= m_fields[i].fullScale)
    {
        double v = m_fields[i].current->text().toDouble();
        v = v * m_fields[i].scale / m_fields[i].fullScale;;
        d.addMilliseconds((TQ_INT64)(v*(1000)));
    }
    ++i;
    if (!m_fields[i].current->isHidden())
    {
        TQ_INT64 v = m_fields[i].current->text().toUInt();
        d.addMilliseconds(v);
    }
    return d;
}

void DurationWidget::dddLostFocus()
{
    handleLostFocus(0);
    emit valueChanged();
}

void DurationWidget::hhLostFocus( )
{
    handleLostFocus(1);
    emit valueChanged();
}

void DurationWidget::mmLostFocus()
{
    handleLostFocus(2);
    emit valueChanged();
}

void DurationWidget::ssLostFocus()
{
    handleLostFocus(3);
    emit valueChanged();
}

void DurationWidget::msLostFocus()
{
    handleLostFocus(4);
    emit valueChanged();
}

void DurationWidget::handleLostFocus(
    int field)
{
    // Get our own info, and that of our left and right neighbours.
    TQLineEdit *left = m_fields[field].left;
    double leftScale = m_fields[field].leftScale; 
    const char *leftFormat = left ? m_fields[field - 1].format : NULL; 
    TQLineEdit *current = m_fields[field].current; 
    const char *currentFormat = m_fields[field].format;
    TQLineEdit *right = m_fields[field].right;
    double rightScale = m_fields[field].rightScale; 
    const char *rightFormat = right ? m_fields[field + 1].format : NULL;
    
    // avoid possible crash
    if (leftScale == 0)
        leftScale = 1;
        
    // Get the text and start processing...
    TQString newValue(current->text());
    double v = TDEGlobal::locale()->readNumber(newValue);
    unsigned currentValue = 0;
    TQString tmp;
    //kdDebug()<<k_funcinfo<<field<<": value="<<v<<" v="<<v<<endl;
    if (left && v >= leftScale)
    {
        //kdDebug()<<k_funcinfo<<field<<": value="<<v<<" leftScale="<<leftScale<<endl;
        // Carry overflow, recurse as required.
        tmp.sprintf(leftFormat, (unsigned)(v / leftScale));
        left->setText(tmp);
        handleLostFocus(field - 1);

        // Get remainder.
        v = v - (tmp.toUInt() * leftScale);
        newValue = TDEGlobal::locale()->formatNumber(v);
    }
    int point = newValue.find(m_decimalPoint);
    if (point != -1)
    {
        //HACK doubles may be rounded(at fractions > 6 digits on my system)
        int p;
        double frac = fraction(newValue, &p);
        if (right && frac > 0.0)
        {
            //kdDebug()<<k_funcinfo<<field<<": value="<<newValue<<" rightScale="<<rightScale<<" frac="<<frac<<" ("<<newValue.mid(point)<<")"<<endl;
            // Propagate fraction
            v = rightScale * (frac*power(10.0, -p));
            frac = fraction(TDEGlobal::locale()->formatNumber(v, 19), 0);
            //kdDebug()<<k_funcinfo<<field<<": v="<<v<<" ("<<(unsigned)v<<") rest="<<frac<<endl;
            if (frac > 0.0)
            {
                tmp = TDEGlobal::locale()->formatNumber(v, 19);
                right->setText(tmp);
                handleLostFocus(field + 1);
            } else {
                tmp.sprintf(rightFormat, (unsigned)(v));
                right->setText(tmp);
            }
            
        }
        // TODO keep fraction for last shown field
        newValue = newValue.left(point);
    }
    currentValue = newValue.toUInt();
    tmp.sprintf(currentFormat, currentValue);
    current->setText(tmp);
}

// Set which fields are visible.
void DurationWidget::setVisibleFields( int fieldMask )
{
    int i;
    for (i = 0; i < 5; i++)
    {
        bool visible = ((fieldMask >> i) & 1) == 1;
        
        // Set the visibility of the fields, and of any associated separator.
        if (visible)
        {
            m_fields[i].current->show();
            if (m_fields[i].separator)
            {
                m_fields[i].separator->show();
            }
            if (m_fields[i].unit)
            {
                m_fields[i].unit->show();
     }
        }
        else
        {
            m_fields[i].current->hide();
            if (m_fields[i].separator)
            {
                m_fields[i].separator->hide();
            }
            if (m_fields[i].unit)
            {
                m_fields[i].unit->hide();
     }
        }
    }
}

// Retreive the visible fields.
int DurationWidget::visibleFields()
{
    int i;
    int fieldMask = 0;
    for (i = 0; i < 5; i++)
    {
        if (m_fields[i].current->isHidden())
        {   
            fieldMask |= (1 << i);
        }
    }
    return fieldMask;
}

void DurationWidget::setFieldLeftscale(int f, double ls)
{
    m_fields[f].leftScale = ls;
}

void DurationWidget::setFieldRightscale(int f, double rs)
{
    m_fields[f].rightScale = rs;
}

void DurationWidget::setFieldScale(int f, double scale)
{
    m_fields[f].scale = scale;
}

void DurationWidget::setFieldUnit(int f, TQString unit)
{
    if (m_fields[f].unit)
    {
 m_fields[f].unit->setText(unit);
    }
}

double DurationWidget::power(double m, int e) {
    int c = e > 0 ? e : -e;
    double value = 1;
    for (int i=0; i < c; ++i) {
        value = e > 0 ? value * m : value / m;
    }
    return value;
}

double DurationWidget::fraction(TQString number, int *exp) {
    int point = number.find(m_decimalPoint);
    if (point == -1) {
        return 0.0;
    }
    TQString v;
    if (exp) {
        v = number.mid(point+m_decimalPoint.length());
        *exp = v.length();
        
    } else {
         v = number.mid(point);
    }
    return TDEGlobal::locale()->readNumber(v);
}

}  //KPlato namespace