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
|
/* -------------------------------------------------------------
timeedit.cpp
(C) 2003 by Daniel Teske (teske@bigfoot.com)
------------------------------------------------------------- */
#include <klocale.h>
#include <qlabel.h>
#include <qlayout.h>
#include "timeedit.h"
#include "timeedit.moc"
WrappingSpinBox::WrappingSpinBox(int minValue, int maxValue, int step, QWidget *parent, const char *name)
: QSpinBox(minValue, maxValue, step, parent, name)
{
}
WrappingSpinBox::~WrappingSpinBox()
{
}
/** Overloaded QSpinBox method */
void WrappingSpinBox::stepUp()
{
bool wrap = false;
if (value() == 59)
wrap = true;
if (wrap)
emit wrapUp(); // must wrap first (to avoid double-step-up)
QSpinBox::stepUp();
}
/** Overloaded QSpinBox method */
void WrappingSpinBox::stepDown()
{
bool wrap = false;
if (value() == 0)
wrap = true;
QSpinBox::stepDown();
if (wrap)
emit wrapDown();
}
// -------------------------------------------------------------------------
TimeEdit::TimeEdit(QWidget* parent, const char* name)
: QWidget(parent, name)
{
layout = new QHBoxLayout(this);
minuteBox = new QSpinBox(0, 300, 1, this);
// minuteBox->setFixedSize(minuteBox->sizeHint());
QLabel* min = new QLabel(i18n(" min"), this);
min->setFixedSize(min->sizeHint());
secondBox = new WrappingSpinBox(0, 59, 1, this);
secondBox->setWrapping(true);
// secondBox->setFixedSize(secondBox->sizeHint());
QLabel* sec = new QLabel(i18n(" sec"),this);
sec->setFixedSize(sec->sizeHint());
layout->addWidget(minuteBox);
layout->addWidget(min);
layout->addWidget(secondBox);
layout->addWidget(sec);
connect(minuteBox, SIGNAL(valueChanged(int)), SLOT(spinBoxValueChanged(int)) );
connect(secondBox, SIGNAL(valueChanged(int)), SLOT(spinBoxValueChanged(int)) );
connect(secondBox, SIGNAL(wrapUp()), SLOT(wrappedUp()));
connect(secondBox, SIGNAL(wrapDown()), SLOT(wrappedDown()));
}
TimeEdit::~TimeEdit()
{
}
/** Set to specified number of seconds. */
void TimeEdit::setValue(int val)
{
if (val < 0)
return;
// block signals to avoid receiption of valueChanged()
// between changing of minutes and seconds
secondBox->blockSignals(true);
minuteBox->blockSignals(true);
secondBox->setValue(val % 60);
minuteBox->setValue(val / 60);
secondBox->blockSignals(false);
minuteBox->blockSignals(false);
emit valueChanged(value());
}
/** Return current value in seconds. */
int TimeEdit::value()
{
return minuteBox->value()*60 + secondBox->value();
}
/** SLOT: Handle wrap-up of seconds-box */
void TimeEdit::wrappedUp()
{
if (minuteBox->value() != minuteBox->maxValue()) {
minuteBox->stepUp();
} else {
secondBox->setValue(58); // ugly: must cater for wrapping-first
}
}
/** SLOT: Handle wrap-down of seconds-box */
void TimeEdit::wrappedDown()
{
// well, the "if" should always be true
if (minuteBox->value() != minuteBox->minValue()) {
minuteBox->stepDown();
} else {
secondBox->setValue(0);
}
}
/** SLOT: Handle any change in minutes of seconds */
void TimeEdit::spinBoxValueChanged(int)
{
if (value() == 0) {
secondBox->stepUp(); // this will give another spinBoxValueChanged() invocation
return;
}
emit valueChanged(value());
}
/** SLOT (overloading QSpinBox): set focus */
void TimeEdit::setFocus()
{
minuteBox->setFocus();
}
|