Add validDateTime property to datefieldbackend

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-08-08 15:06:52 +08:00 committed by Matthieu Gallien
parent 7e26bbd5f0
commit 1f8ecf416c
2 changed files with 22 additions and 3 deletions

View file

@ -54,13 +54,13 @@ void DateFieldBackend::setDateTimeMsecs(const qint64 dateTimeMsecs)
QString DateFieldBackend::dateTimeString() const
{
const auto locale = QLocale::system();
return _dateTime.toString(locale.dateTimeFormat(QLocale::ShortFormat));
return _dateTime.toString(locale.dateFormat(QLocale::ShortFormat));
}
void DateFieldBackend::setDateTimeString(const QString &dateTimeString)
{
const auto locale = QLocale::system();
const auto dt = locale.toDateTime(dateTimeString, locale.dateTimeFormat(QLocale::ShortFormat));
const auto dt = locale.toDateTime(dateTimeString, locale.dateFormat(QLocale::ShortFormat));
setDateTime(dt);
}
@ -123,5 +123,20 @@ void DateFieldBackend::setMaximumDateTimeMsecs(const qint64 maximumDateTimeMsecs
const auto dt = QDateTime::fromMSecsSinceEpoch(maximumDateTimeMsecs);
setMaximumDateTime(dt);
}
bool DateFieldBackend::validDateTime() const
{
auto valid = _dateTime.isValid();
if (_minimumDateTime.isValid()) {
valid &= _dateTime >= _minimumDateTime;
}
if (_maximumDateTime.isValid()) {
valid &= _dateTime <= _maximumDateTime;
}
return valid;
}
}
}

View file

@ -36,6 +36,8 @@ class DateFieldBackend : public QObject
Q_PROPERTY(QDateTime maximumDateTime READ maximumDateTime WRITE setMaximumDateTime NOTIFY maximumDateTimeChanged)
Q_PROPERTY(qint64 maximumDateTimeMsecs READ maximumDateTimeMsecs WRITE setMaximumDateTimeMsecs NOTIFY maximumDateTimeChanged)
Q_PROPERTY(bool validDateTime READ validDateTime NOTIFY dateTimeChanged NOTIFY minimumDateTimeChanged NOTIFY maximumDateTimeChanged)
public:
explicit DateFieldBackend() = default;
@ -49,6 +51,8 @@ public:
[[nodiscard]] QDateTime maximumDateTime() const;
[[nodiscard]] qint64 maximumDateTimeMsecs() const;
[[nodiscard]] bool validDateTime() const;
public slots:
void setDateTime(const QDateTime &dateTime);
void setDateTimeMsecs(const qint64 dateTimeMsecs);
@ -66,7 +70,7 @@ signals:
void maximumDateTimeChanged();
private:
QDateTime _dateTime;
QDateTime _dateTime = QDateTime::currentDateTimeUtc();
QDateTime _minimumDateTime;
QDateTime _maximumDateTime;
};