-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessibledatetimeedit.cpp
293 lines (251 loc) · 9.41 KB
/
accessibledatetimeedit.cpp
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
#include "accessibledatetimeedit.h"
#include <QApplication>
#include "../Lib/alertsound.h"
// Displays a date and time in the following format:
//
// DDD DD / MMM / YYYY HH : MM
//
//
// Intercepts arrow keys, and allows up/down changing of each field.
// If the field is numeric, allows direct entry.
//
// Checks values at each step, and updates all fields dynamically
//
// Intercepts tab, and directs to the next or previous sub-field
//
AccessibleDateTimeEdit::AccessibleDateTimeEdit(QWidget *parent) :
QLineEdit(parent)
{
partialok=false ;
pos=0 ;
monthdelta=-1 ;
dateprefix = "ddd " ;
dateformat = dateprefix + "dd/MMM/yyyy hh:mm" ;
QDateTime now = QDateTime::currentDateTime() ;
setDateTime(now) ;
updateDateText() ;
}
void AccessibleDateTimeEdit::setFormat(QString prefix, QString format, bool partial)
{
dateprefix = prefix ;
dateformat = dateprefix + format ;
partialok = partial ;
updateDateText() ;
}
//
// set and get the date/time
//
void AccessibleDateTimeEdit::setDateTime(QDateTime& newdatetime)
{
datetime = newdatetime.toLocalTime() ;
if (partialok && datetime.date().year()<10 && datetime.date().year()!=1 && datetime.date().year()!=4) {
datetime = datetime.addYears(4-datetime.date().year()) ;
}
updateDateText() ;
}
QDateTime& AccessibleDateTimeEdit::getDateTime()
{
return datetime ;
}
// Private
void AccessibleDateTimeEdit::updateDateText()
{
datetext = datetime.toLocalTime().toString(dateformat) ;
if (partialok) {
bool isempty = (datetime.date().year()==1 && datetime.date().month()==1 && datetime.date().day()==1) ;
bool noyear = datetime.date().year()==4 ;
for (int i=0; i<dateformat.length(); i++) {
QChar ch = dateformat.at(i) ;
if (ch.isLetter() && isempty) datetext.replace(i, 1, '-') ;
if (ch=='y' && noyear) datetext.replace(i, 1, '-') ;
}
}
this->setText(datetext) ;
this->setCursorPosition(pos) ;
}
void AccessibleDateTimeEdit::focusInEvent(QFocusEvent * event)
{
updateDateText() ;
QLineEdit::focusInEvent(event) ;
this->deselect() ;
pos=dateprefix.size() ;
this->setCursorPosition(pos) ;
}
//
// Only allow keypresses at the correct position
//
void AccessibleDateTimeEdit::keyPressEvent(QKeyEvent *event)
{
int invalid=false ;
Qt::KeyboardModifiers keyMod = QApplication::keyboardModifiers();
int key = event->key() ;
bool control = keyMod.testFlag(Qt::ControlModifier) ;
bool alt = keyMod.testFlag(Qt::AltModifier) ;
bool shift = keyMod.testFlag(Qt::ShiftModifier) ;
pos = this->cursorPosition() ;
if (pos<0 || pos>=dateformat.size()) pos=0 ;
QDateTime newtime = datetime ;
int newpos = pos ;
if ( shift || control || alt ||
key == Qt::Key_Tab ||
key == Qt::Key_Control ||
key == Qt::Key_Alt ||
key == Qt::Key_Shift ||
key == Qt::Key_Enter ||
key == Qt::Key_Escape ||
key == Qt::Key_Return) {
// default handler for control signals
QLineEdit::keyPressEvent(event);
return ;
} else if(event->key() == Qt::Key_Minus && partialok) {
// Remove date, or Set year
QChar ch = dateformat.at(pos) ;
if (!ch.isNull()) {
if (!ch.isLetter()) ch=dateformat.at(pos-1) ;
if (ch == 'd' || ch=='M' || ch=='h' || ch=='m') {
newtime = datetime.fromString("010100010000", "ddMMyyyyhhmm") ;
}
if (ch=='y') newtime = newtime.addYears(4 - datetime.date().year()) ;
}
monthdelta=-1 ;
} else if(event->key() == Qt::Key_Up) {
// next value for current field
QChar ch = dateformat.at(pos) ;
if (!ch.isNull()) {
if (!ch.isLetter()) ch=dateformat.at(pos-1) ;
if (ch == 'd') newtime = newtime.addDays(1) ;
else if (ch == 'M') newtime = newtime.addMonths(1) ;
else if (ch == 'y') newtime = newtime.addYears(1) ;
else if (ch == 'h') newtime = newtime.addSecs(3600) ;
else if (ch == 'm') newtime = newtime.addSecs(60) ;
}
monthdelta=-1 ;
}
else if(event->key() == Qt::Key_Down) {
// previous value for current field
QChar ch = dateformat.at(pos) ;
if (!ch.isNull()) {
if (!ch.isLetter()) ch=dateformat.at(pos-1) ;
if (ch == 'd') newtime = newtime.addDays(-1) ;
else if (ch == 'M') newtime = newtime.addMonths(-1) ;
else if (ch == 'y') newtime = newtime.addYears(-1) ;
else if (ch == 'h') newtime = newtime.addSecs(-3600) ;
else if (ch == 'm') newtime = newtime.addSecs(-60) ;
}
monthdelta=-1 ;
}
else if(event->key() == Qt::Key_Left) {
// move left
QChar ch = dateformat.at(pos) ;
while (pos>0 && dateformat.at(pos)==ch) pos-- ;
while (pos>0 && !dateformat.at(pos).isLetter()) pos-- ;
if (pos<dateprefix.size()) {
pos=dateformat.size()-1 ;
}
while (pos>0 && dateformat.at(pos).isLetter()) pos-- ;
if (!dateformat.at(pos).isLetter()) pos++ ;
newpos=pos ;
monthdelta=-1 ;
}
else if(event->key() == Qt::Key_Right) {
// move right
QChar ch = dateformat.at(pos) ;
while (pos<datetext.size() && dateformat.at(pos)==ch) pos++ ;
while (pos<datetext.size() && !dateformat.at(pos).isLetter()) pos++ ;
if (pos>=datetext.size()) { pos=dateprefix.size() ; }
newpos=pos ;
monthdelta=-1 ;
}
else if (key>=Qt::Key_0 && key<=Qt::Key_9 && pos<(dateformat.size())) {
// handle a numeric input to change the month
if (dateformat.at(pos)=='M') {
if (monthdelta==-1) {
// First digit
monthdelta = (10 * (key - Qt::Key_0)) ;
if (monthdelta>12) monthdelta=-1 ;
} else {
// Second digit
monthdelta += (key - Qt::Key_0) ;
if (monthdelta>=1 && monthdelta<=12) {
int month = newtime.date().month() ;
newtime = newtime.addMonths(monthdelta - month) ;
while (newpos<(dateformat.size()) &&
(dateformat.at(newpos)=='M' || !(dateformat.at(newpos).isLetter()))) newpos++ ;
}
monthdelta=-1 ;
}
}
// handle direct typing of numbers
else {
int n, v, nth ;
QChar t ;
// Add/subtract date/time numbers based on keypress and position
n = key - Qt::Key_0 ; // Current keypress
t = dateformat.at(pos) ; // Current date field type
if (datetext.at(pos)=='-') v=0 ; // Current date field value (from text)
else v=datetext.at(pos).toLatin1() - '0' ;
nth=1 ; // nth character in the date field
for (int j=pos; j<dateformat.length()-1 && dateformat.at(j+1)==t; j++) {
nth=nth*10 ;
}
switch (t.toLatin1()) {
case 'y':
if (partialok && datetime.date().year()<10 && nth==1000 && n>0) {
// First digit when the year is ----
newtime = newtime.addYears((n*nth)-newtime.date().year()) ;
} else if (partialok && datetime.date().year()<10) {
// Any other digit in the year when it is ----
invalid=true ;
} else {
newtime = newtime.addYears((n-v)*nth) ;
}
if (newtime.date().year()<0) invalid=true ;
break ;
case 'd':
if (nth==10 && n==3) {
newtime = newtime.addDays(30-newtime.date().day()) ;
} else {
newtime = newtime.addDays((n-v)*nth) ;
}
if (newtime.date().month() != datetime.date().month()) invalid=true ;
break ;
case 'h':
if (nth==10 && n==2) {
newtime = newtime.addSecs((20 - newtime.time().hour())*3600) ;
} else {
newtime = newtime.addSecs((n-v)*nth*3600) ;
}
if (newtime.date().day() != datetime.date().day()) invalid=true ;
break ;
case 'm':
newtime = newtime.addSecs((n-v)*nth*60) ;
if (nth==10 && n>5) invalid=true ;
break ;
}
// Work out the new time, and cursor position
if (newpos<(dateformat.size())) {
newpos++ ;
while (newpos<(dateformat.size()) && !(dateformat.at(newpos).isLetter())) newpos++ ;
}
monthdelta=-1 ;
}
} else {
invalid=true ;
}
// Change the year from 0001 (empty) to 0004 (no year) if the month/day have changed
if (partialok && newtime.date().year()==1 && (newtime.date().month()!=1 || newtime.date().day()!=1)) {
newtime = newtime.addYears(3) ;
}
if (partialok && newtime.date().year()<10 && newtime.date().year()!=1 && newtime.date().year()!=4) {
invalid=true ;
}
if (invalid) {
// beep! invalid input
play(Disabled) ;
monthdelta=-1 ;
} else {
pos=newpos ;
datetime = newtime ;
updateDateText() ;
}
}