forked from mpc-qt/mpc-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotowindow.cpp
109 lines (97 loc) · 3.19 KB
/
gotowindow.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
#include <QKeyEvent>
#include <QLineEdit>
#include <QRegularExpressionValidator>
#include <QtGlobal>
#include <helpers.h>
#include "gotowindow.h"
#include "ui_gotowindow.h"
GoToWindow::GoToWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::GoToWindow)
{
ui->setupUi(this);
}
GoToWindow::~GoToWindow()
{
delete ui;
}
void GoToWindow::init(double currentTime, double maxTime, double fps)
{
maxTime_ = maxTime;
fps_ = fps == 0 ? 24 : fps;
ui->goToTime->setText(Helpers::toDateFormatWithZero(currentTime));
QRegularExpression regexTime(R"(^([0-9]?[0-9]):([0-9]?[0-9]):([0-9]?[0-9])\.[0-9]{3}$)");
QRegularExpressionValidator* validatorTime = new QRegularExpressionValidator(regexTime, this);
ui->goToTime->setValidator(validatorTime);
ui->goToTime->installEventFilter(this);
QRegularExpression regexFrame(R"(^([0-9]{1,})$)");
QRegularExpressionValidator* validatorFrame = new QRegularExpressionValidator(regexFrame, this);
ui->goToFrame->setValidator(validatorFrame);
ui->goToFrame->setText(QString::number(currentTime * fps, 'f', 0));
setFixedSize(size());
show();
}
void GoToWindow::on_goToTime_textChanged(const QString &text)
{
double time = Helpers::fromDateFormat(text);
if (time < 0 || time > maxTime_)
ui->goToTime->setStyleSheet("QLineEdit { background-color: #903736; }");
else
ui->goToTime->setStyleSheet("");
}
void GoToWindow::on_goToFrame_textChanged(const QString &text)
{
double time = text.toDouble() / fps_;
if (time < 0 || time > maxTime_)
ui->goToFrame->setStyleSheet("QLineEdit { background-color: #903736; }");
else
ui->goToFrame->setStyleSheet("");
}
void GoToWindow::on_goToTimeButton_clicked()
{
double time = Helpers::fromDateFormat(ui->goToTime->text());
if (time < 0 || time > maxTime_)
return;
emit goTo(time);
close();
}
void GoToWindow::on_goToFrameButton_clicked()
{
double time = ui->goToFrame->text().toDouble() / fps_;
if (time < 0 || time > maxTime_)
return;
emit goTo(time);
close();
}
bool GoToWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->goToTime && event->type() == QEvent::KeyPress) {
auto keyEvent = reinterpret_cast<QKeyEvent*>(event);
int cursorPos = ui->goToTime->cursorPosition();
int key = keyEvent->key();
if (key == Qt::Key_Backspace) {
ui->goToTime->deselect();
if (cursorPos > 0) {
if (ui->goToTime->text()[cursorPos - 1].isDigit()) {
ui->goToTime->cursorBackward(true);
ui->goToTime->insert("0");
ui->goToTime->setCursorPosition(cursorPos - 1);
}
else
ui->goToTime->cursorBackward(false);
return true;
}
}
else if (key == Qt::Key_Delete)
return true;
else if (key >= Qt::Key_0 && key <= Qt::Key_9) {
if (ui->goToTime->text()[cursorPos].isDigit())
ui->goToTime->cursorForward(true);
else {
ui->goToTime->cursorForward(false);
return true;
}
}
}
return false;
}