-
Notifications
You must be signed in to change notification settings - Fork 1
/
DateTimePickDialog.cpp
92 lines (73 loc) · 2.89 KB
/
DateTimePickDialog.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
#include "DateTimePickDialog.h"
#include <QPushButton>
#include <QScrollBar>
//#include <QAbstractKineticScroller>
DateTimePickDialog::DateTimePickDialog(const QString &resetText, QWidget *parent) :
RotatingDialog(parent),
ui(new Ui::DateTimePickDialog),
resizeCount(0)
{
ui->setupUi(this);
QPushButton *resetButton = new QPushButton(resetText);
QPushButton *acceptButton = new QPushButton(tr("Done"));
resetButton->setAutoDefault(false);
acceptButton->setDefault(true);
ui->buttonBox->addButton(resetButton, QDialogButtonBox::ActionRole);
ui->buttonBox->addButton(acceptButton, QDialogButtonBox::ActionRole);
connect(resetButton, SIGNAL(clicked()), this, SLOT(reset()));
connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
this->setFeatures(ui->dialogLayout, ui->buttonBox);
}
void DateTimePickDialog::keyPressEvent(QKeyEvent *e)
{
// Try to map virtual keys to digits
int digit = e->nativeVirtualKey() - '0';
if (digit < 0 || digit > 9) {
// Try to map top row keys to digits
digit = e->nativeScanCode() - 24;
if (digit < 0 || digit > 9) {
// Failure, abort
RotatingDialog::keyPressEvent(e);
return;
}
digit = (digit + 1) % 10;
}
// Calculate maximum input length
int scrollersInputLength = 0;
for (int i = 0; i < scrollers.size(); i++)
scrollersInputLength += scrollers[i].width;
// Register the input
scrollersInput.append(digit);
if (scrollersInput.size() > scrollersInputLength)
scrollersInput.removeFirst();
// Forward input parts to appropriate scrollers
for (int s = 0, d = 0; s < scrollers.size() && scrollersInput.size() - d >= scrollers[s].width; d += scrollers[s].width, s++) {
int inputPart = 0;
for (int i = 0; i < scrollers[s].width; i++)
inputPart = inputPart * 10 + scrollersInput[d + i];
scrollers[s].scroll(inputPart);
}
centerView();
}
void DateTimePickDialog::resizeEvent(QResizeEvent *e)
{
// HACK: Make the scroll-to-item work by waiting for the lists to be properly resized before scrolling
if (++resizeCount == 2)
centerView();
QDialog::resizeEvent(e);
}
int DateTimePickDialog::row(QListWidget *listWidget)
{
const QList<QListWidgetItem*> selection = listWidget->selectedItems();
return selection.isEmpty() ? -1 : listWidget->row(selection.first());
}
void DateTimePickDialog::centerView(QListWidget *listWidget)
{
const int itemHeight = listWidget->visualItemRect(listWidget->item(0)).height();
#if 0
listWidget->property("kineticScroller").value<QAbstractKineticScroller*>()
->scrollTo(QPoint(0, qBound(0,
row(listWidget) * itemHeight + (itemHeight - listWidget->height()) / 2,
listWidget->verticalScrollBar()->maximum())));
#endif
}