mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-08 08:04:08 +02:00
Added ability to change time in schedule box with wheel.
This commit is contained in:
parent
2743aee614
commit
425423e113
1 changed files with 39 additions and 12 deletions
|
@ -70,12 +70,14 @@ public:
|
||||||
using MaskedInputField::MaskedInputField;
|
using MaskedInputField::MaskedInputField;
|
||||||
|
|
||||||
void setMaxValue(int value);
|
void setMaxValue(int value);
|
||||||
|
void setWheelStep(int value);
|
||||||
|
|
||||||
rpl::producer<> erasePrevious() const;
|
rpl::producer<> erasePrevious() const;
|
||||||
rpl::producer<QChar> putNext() const;
|
rpl::producer<QChar> putNext() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void keyPressEvent(QKeyEvent *e) override;
|
void keyPressEvent(QKeyEvent *e) override;
|
||||||
|
void wheelEvent(QWheelEvent *e) override;
|
||||||
|
|
||||||
void correctValue(
|
void correctValue(
|
||||||
const QString &was,
|
const QString &was,
|
||||||
|
@ -86,11 +88,21 @@ protected:
|
||||||
private:
|
private:
|
||||||
int _maxValue = 0;
|
int _maxValue = 0;
|
||||||
int _maxDigits = 0;
|
int _maxDigits = 0;
|
||||||
|
int _wheelStep = 0;
|
||||||
rpl::event_stream<> _erasePrevious;
|
rpl::event_stream<> _erasePrevious;
|
||||||
rpl::event_stream<QChar> _putNext;
|
rpl::event_stream<QChar> _putNext;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int Number(not_null<TimePart*> 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 {
|
class TimeInput final : public Ui::RpWidget {
|
||||||
public:
|
public:
|
||||||
TimeInput(QWidget *parent, const QString &value);
|
TimeInput(QWidget *parent, const QString &value);
|
||||||
|
@ -121,7 +133,6 @@ private:
|
||||||
|
|
||||||
int hour() const;
|
int hour() const;
|
||||||
int minute() const;
|
int minute() const;
|
||||||
int number(const object_ptr<TimePart> &field) const;
|
|
||||||
|
|
||||||
object_ptr<TimePart> _hour;
|
object_ptr<TimePart> _hour;
|
||||||
object_ptr<Ui::PaddingWrap<Ui::FlatLabel>> _separator1;
|
object_ptr<Ui::PaddingWrap<Ui::FlatLabel>> _separator1;
|
||||||
|
@ -181,6 +192,10 @@ void TimePart::setMaxValue(int value) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TimePart::setWheelStep(int value) {
|
||||||
|
_wheelStep = value;
|
||||||
|
}
|
||||||
|
|
||||||
rpl::producer<> TimePart::erasePrevious() const {
|
rpl::producer<> TimePart::erasePrevious() const {
|
||||||
return _erasePrevious.events();
|
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<int>(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(
|
void TimePart::correctValue(
|
||||||
const QString &was,
|
const QString &was,
|
||||||
int wasCursor,
|
int wasCursor,
|
||||||
|
@ -290,10 +324,12 @@ TimeInput::TimeInput(QWidget *parent, const QString &value)
|
||||||
connect(_hour, &Ui::MaskedInputField::changed, changed);
|
connect(_hour, &Ui::MaskedInputField::changed, changed);
|
||||||
connect(_minute, &Ui::MaskedInputField::changed, changed);
|
connect(_minute, &Ui::MaskedInputField::changed, changed);
|
||||||
_hour->setMaxValue(23);
|
_hour->setMaxValue(23);
|
||||||
|
_hour->setWheelStep(1);
|
||||||
_hour->putNext() | rpl::start_with_next([=](QChar ch) {
|
_hour->putNext() | rpl::start_with_next([=](QChar ch) {
|
||||||
putNext(_minute, ch);
|
putNext(_minute, ch);
|
||||||
}, lifetime());
|
}, lifetime());
|
||||||
_minute->setMaxValue(59);
|
_minute->setMaxValue(59);
|
||||||
|
_minute->setWheelStep(10);
|
||||||
_minute->erasePrevious() | rpl::start_with_next([=] {
|
_minute->erasePrevious() | rpl::start_with_next([=] {
|
||||||
erasePrevious(_hour);
|
erasePrevious(_hour);
|
||||||
}, lifetime());
|
}, lifetime());
|
||||||
|
@ -356,21 +392,12 @@ bool TimeInput::setFocusFast() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int TimeInput::number(const object_ptr<TimePart> &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 {
|
int TimeInput::hour() const {
|
||||||
return number(_hour);
|
return Number(_hour);
|
||||||
}
|
}
|
||||||
|
|
||||||
int TimeInput::minute() const {
|
int TimeInput::minute() const {
|
||||||
return number(_minute);
|
return Number(_minute);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString TimeInput::valueCurrent() const {
|
QString TimeInput::valueCurrent() const {
|
||||||
|
|
Loading…
Add table
Reference in a new issue