From 425423e1134a21aa9b8a247e2fe0882686350cd5 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 9 Jun 2020 03:34:24 +0300 Subject: [PATCH] Added ability to change time in schedule box with wheel. --- .../view/history_view_schedule_box.cpp | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp b/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp index 13abb00e78..be82309020 100644 --- a/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp +++ b/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp @@ -70,12 +70,14 @@ public: using MaskedInputField::MaskedInputField; void setMaxValue(int value); + void setWheelStep(int value); rpl::producer<> erasePrevious() const; rpl::producer putNext() const; protected: void keyPressEvent(QKeyEvent *e) override; + void wheelEvent(QWheelEvent *e) override; void correctValue( const QString &was, @@ -86,11 +88,21 @@ protected: private: int _maxValue = 0; int _maxDigits = 0; + int _wheelStep = 0; rpl::event_stream<> _erasePrevious; rpl::event_stream _putNext; }; +int Number(not_null field) { + const auto text = field->getLastText(); + auto ref = text.midRef(0); + while (!ref.isEmpty() && ref.at(0) == '0') { + ref = ref.mid(1); + } + return ref.toInt(); +} + class TimeInput final : public Ui::RpWidget { public: TimeInput(QWidget *parent, const QString &value); @@ -121,7 +133,6 @@ private: int hour() const; int minute() const; - int number(const object_ptr &field) const; object_ptr _hour; object_ptr> _separator1; @@ -181,6 +192,10 @@ void TimePart::setMaxValue(int value) { } } +void TimePart::setWheelStep(int value) { + _wheelStep = value; +} + rpl::producer<> TimePart::erasePrevious() const { return _erasePrevious.events(); } @@ -199,6 +214,25 @@ void TimePart::keyPressEvent(QKeyEvent *e) { } } +void TimePart::wheelEvent(QWheelEvent *e) { + // Only a mouse wheel is accepted. + constexpr auto step = static_cast(QWheelEvent::DefaultDeltasPerStep); + const auto delta = e->angleDelta().y(); + const auto absDelta = std::abs(delta); + if (absDelta != step) { + return; + } + + auto time = Number(this) + ((delta / absDelta) * _wheelStep); + const auto max = _maxValue + 1; + if (time < 0) { + time += max; + } else if (time >= max) { + time -= max; + } + setText(QString::number(time)); +} + void TimePart::correctValue( const QString &was, int wasCursor, @@ -290,10 +324,12 @@ TimeInput::TimeInput(QWidget *parent, const QString &value) connect(_hour, &Ui::MaskedInputField::changed, changed); connect(_minute, &Ui::MaskedInputField::changed, changed); _hour->setMaxValue(23); + _hour->setWheelStep(1); _hour->putNext() | rpl::start_with_next([=](QChar ch) { putNext(_minute, ch); }, lifetime()); _minute->setMaxValue(59); + _minute->setWheelStep(10); _minute->erasePrevious() | rpl::start_with_next([=] { erasePrevious(_hour); }, lifetime()); @@ -356,21 +392,12 @@ bool TimeInput::setFocusFast() { return true; } -int TimeInput::number(const object_ptr &field) const { - const auto text = field->getLastText(); - auto ref = text.midRef(0); - while (!ref.isEmpty() && ref.at(0) == '0') { - ref = ref.mid(1); - } - return ref.toInt(); -} - int TimeInput::hour() const { - return number(_hour); + return Number(_hour); } int TimeInput::minute() const { - return number(_minute); + return Number(_minute); } QString TimeInput::valueCurrent() const {