configdatetimedlg.cpp
4.29 KB
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
#include <QDateTime>
#include <QKeyEvent>
#include "configdatetimedlg.h"
#include "ui_configdatetimedlg.h"
#include "system.h"
#include "soundplayer.h"
ConfigDateTimeDlg::ConfigDateTimeDlg(QWidget *parent) :
QDialog(parent),
ui(new Ui::ConfigDateTimeDlg)
{
ui->setupUi(this);
this->setWindowFlags( Qt::FramelessWindowHint);
this->setAttribute( Qt::WA_DeleteOnClose);
qApp->setActiveWindow(this);
this->setFocus();
foreach (QPushButton *button, findChildren<QPushButton *>())
connect(button, &QPushButton::pressed, SoundPlayer::playClick);
ui->ctrSpBxYear->setFormatterWidth(4);
QDateTime dt_tm = QDateTime::currentDateTime();
QDate dt_ = dt_tm.date();
QTime tm_ = dt_tm.time();
ui->ctrSpBxYear->setValue(dt_.year());
ui->ctrSpBxMonth->setValue(dt_.month());
ui->ctrSpBxDay->setValue(dt_.day());
ui->ctrSpBxHour->setValue(tm_.hour());
ui->ctrSpBxMin->setValue(tm_.minute());
ui->ctrSpBxYear->setFocus();
m_bInputState = true;
connect(ui->keyboardwidget, SIGNAL(onOkKeyClicked()), this, SLOT(keyEnter_clicked()));
connect(ui->keyboardwidget, SIGNAL(onCancelKeyClicked()), this, SLOT(keyCancel_clicked()));
m_pMapperSpins = new QSignalMapper(this);
m_pMapperSpins->setMapping(ui->ctrSpBxYear, ui->ctrSpBxYear);
m_pMapperSpins->setMapping(ui->ctrSpBxMonth, ui->ctrSpBxMonth);
m_pMapperSpins->setMapping(ui->ctrSpBxDay, ui->ctrSpBxDay);
m_pMapperSpins->setMapping(ui->ctrSpBxHour, ui->ctrSpBxHour);
m_pMapperSpins->setMapping(ui->ctrSpBxMin, ui->ctrSpBxMin);
connect(ui->ctrSpBxYear, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
connect(ui->ctrSpBxMonth, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
connect(ui->ctrSpBxDay, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
connect(ui->ctrSpBxHour, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
connect(ui->ctrSpBxMin, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
connect(m_pMapperSpins, SIGNAL(mapped(QWidget*)), this, SLOT(focusInSpinBox(QWidget*)));
}
ConfigDateTimeDlg::~ConfigDateTimeDlg()
{
delete ui;
}
void ConfigDateTimeDlg::on_ctrBtnOk_clicked()
{
time_t tm_t;
QDate dt_(ui->ctrSpBxYear->value(), ui->ctrSpBxMonth->value(), ui->ctrSpBxDay->value());
QTime tm_(ui->ctrSpBxHour->value(), ui->ctrSpBxMin->value());
QDateTime dt_tm;
dt_tm.setTime(tm_);
dt_tm.setDate(dt_);
tm_t = (time_t)dt_tm.toTime_t();
System::setRtcTime(dt_tm);
stime(&tm_t);
this->accept();
}
void ConfigDateTimeDlg::on_ctrBtnCancel_clicked()
{
this->reject();
}
void ConfigDateTimeDlg::keyPressEvent(QKeyEvent *event){
switch (event->key())
{
case 0x01000032: // Turn left
if(focusWidget() == ui->ctrSpBxYear) ui->ctrBtnCancel->setFocus();
else focusPreviousChild();
break;
case 0x01000031: // Push
break;
case 0x01000030: // Turn right
if(focusWidget() == ui->ctrBtnCancel) ui->ctrSpBxYear->setFocus();
else focusNextChild();
break;
}
}
void ConfigDateTimeDlg::keyReleaseEvent(QKeyEvent *event){
switch (event->key())
{
case 0x01000032: // Turn left
if(focusWidget() == ui->ctrSpBxYear) ui->ctrBtnCancel->setFocus();
else focusPreviousChild();
break;
case 0x01000031: // Push
{
QPushButton *btn = qobject_cast<QPushButton*>(focusWidget());
if(btn != NULL){
btn->click();
}
else{
QSpinBox *spbx = qobject_cast<QSpinBox*>(focusWidget());
qDebug() << "grab keyboard";
if(spbx != NULL) ui->keyboardwidget->focusInKeyboard();
}
break;
}
case 0x01000030: // Turn right
if(focusWidget() == ui->ctrBtnCancel) ui->ctrSpBxYear->setFocus();
else focusNextChild();
break;
}
}
void ConfigDateTimeDlg::keyCancel_clicked(){
QSpinBox *spbx = qobject_cast<QSpinBox*>(focusWidget());
if(spbx != NULL){
spbx->setValue(m_nFocusValue);
}
ui->keyboardwidget->focusOutKeyboard();
}
void ConfigDateTimeDlg::keyEnter_clicked(){
ui->keyboardwidget->focusOutKeyboard();
}
void ConfigDateTimeDlg::focusInSpinBox(QWidget *widget){
QSpinBox *spbx = qobject_cast<QSpinBox*>(widget);
if(spbx != NULL){
m_nFocusValue = spbx->value();
}
}