Jump by keyboard between time input fields.

This commit is contained in:
John Preston 2024-04-16 12:41:46 +04:00
parent d2420c6bf8
commit 372543de6e
3 changed files with 28 additions and 8 deletions

View file

@ -42,18 +42,32 @@ rpl::producer<> TimePart::erasePrevious() const {
return _erasePrevious.events();
}
rpl::producer<> TimePart::jumpToPrevious() const {
return _jumpToPrevious.events();
}
rpl::producer<QChar> TimePart::putNext() const {
return _putNext.events();
}
void TimePart::keyPressEvent(QKeyEvent *e) {
const auto isBackspace = (e->key() == Qt::Key_Backspace);
const auto isBeginning = (cursorPosition() == 0);
if (isBackspace && isBeginning && !hasSelectedText()) {
_erasePrevious.fire({});
} else {
MaskedInputField::keyPressEvent(e);
const auto position = cursorPosition();
const auto selection = hasSelectedText();
if (!selection && !position) {
if (e->key() == Qt::Key_Backspace) {
_erasePrevious.fire({});
return;
} else if (e->key() == Qt::Key_Left) {
_jumpToPrevious.fire({});
return;
}
} else if (!selection && position == getLastText().size()) {
if (e->key() == Qt::Key_Right) {
_putNext.fire(QChar(0));
return;
}
}
MaskedInputField::keyPressEvent(e);
}
void TimePart::wheelEvent(QWheelEvent *e) {
@ -78,10 +92,10 @@ void TimePart::correctValue(
int wasCursor,
QString &now,
int &nowCursor) {
auto newText = QString();
auto newCursor = -1;
const auto oldCursor = nowCursor;
const auto oldLength = now.size();
auto newCursor = (oldCursor > 0) ? -1 : 0;
auto newText = QString();
auto accumulated = 0;
auto limit = 0;
for (; limit != oldLength; ++limit) {

View file

@ -18,6 +18,7 @@ public:
void setWheelStep(int value);
[[nodiscard]] rpl::producer<> erasePrevious() const;
[[nodiscard]] rpl::producer<> jumpToPrevious() const;
[[nodiscard]] rpl::producer<QChar> putNext() const;
[[nodiscard]] std::optional<int> number();
@ -37,6 +38,7 @@ private:
int _maxDigits = 0;
int _wheelStep = 0;
rpl::event_stream<> _erasePrevious;
rpl::event_stream<> _jumpToPrevious;
rpl::event_stream<QChar> _putNext;
};

View file

@ -110,6 +110,10 @@ TimeInput::TimeInput(
_minute->erasePrevious() | rpl::start_with_next([=] {
erasePrevious(_hour);
}, lifetime());
_minute->jumpToPrevious() | rpl::start_with_next([=] {
_hour->setCursorPosition(_hour->getLastText().size());
_hour->setFocus();
}, lifetime());
_separator1->setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);