2023-08-02 13:14:21 +03:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ktimeedit.h"
|
|
|
|
#include "klocale.h"
|
|
|
|
#include "kdebug.h"
|
|
|
|
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
2023-08-03 01:33:35 +03:00
|
|
|
class KTimeEditPrivate;
|
|
|
|
|
|
|
|
class KTimeBox : public QSpinBox
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
KTimeBox(KTimeEditPrivate *privateparent, QWidget *parent);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QValidator::State validate(QString &input, int &pos) const final;
|
2024-04-02 17:35:20 +03:00
|
|
|
QAbstractSpinBox::StepEnabled stepEnabled() const final;
|
2023-08-03 01:33:35 +03:00
|
|
|
|
|
|
|
private:
|
2024-04-02 17:35:20 +03:00
|
|
|
QTime getTime() const;
|
|
|
|
|
2023-08-03 01:33:35 +03:00
|
|
|
KTimeEditPrivate* ktimeeditprivate;
|
|
|
|
};
|
|
|
|
|
|
|
|
KTimeBox::KTimeBox(KTimeEditPrivate *privateparent, QWidget *parent)
|
|
|
|
: QSpinBox(parent),
|
|
|
|
ktimeeditprivate(privateparent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-02 13:14:21 +03:00
|
|
|
class KTimeEditPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KTimeEditPrivate(KTimeEdit *parent);
|
|
|
|
|
2023-08-03 01:33:35 +03:00
|
|
|
void updateWidgets();
|
2023-08-02 13:14:21 +03:00
|
|
|
void slotValueChanged(const int value);
|
|
|
|
|
|
|
|
KTimeEdit* ktimeedit;
|
2023-08-03 01:33:35 +03:00
|
|
|
KTimeBox* hourbox;
|
|
|
|
KTimeBox* minutebox;
|
|
|
|
KTimeBox* secondbox;
|
|
|
|
QTime mintime;
|
|
|
|
QTime maxtime;
|
2023-08-02 13:14:21 +03:00
|
|
|
};
|
|
|
|
|
2023-08-03 01:33:35 +03:00
|
|
|
|
|
|
|
QValidator::State KTimeBox::validate(QString &input, int &pos) const
|
|
|
|
{
|
2024-04-02 17:35:20 +03:00
|
|
|
const QTime currenttime = getTime();
|
2023-08-03 01:33:35 +03:00
|
|
|
if (!currenttime.isValid()) {
|
|
|
|
return QValidator::Invalid;
|
|
|
|
}
|
|
|
|
if (currenttime < ktimeeditprivate->mintime || currenttime > ktimeeditprivate->maxtime) {
|
|
|
|
return QValidator::Invalid;
|
|
|
|
}
|
|
|
|
return QValidator::Acceptable;
|
|
|
|
}
|
|
|
|
|
2024-04-02 17:35:20 +03:00
|
|
|
QAbstractSpinBox::StepEnabled KTimeBox::stepEnabled() const
|
|
|
|
{
|
|
|
|
QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
|
|
|
|
const QTime currenttime = getTime();
|
|
|
|
if (!currenttime.isValid()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (currenttime < ktimeeditprivate->maxtime && value() < maximum()) {
|
|
|
|
result |= QAbstractSpinBox::StepUpEnabled;
|
|
|
|
}
|
|
|
|
if (currenttime > ktimeeditprivate->mintime && value() > minimum()) {
|
|
|
|
result |= QAbstractSpinBox::StepDownEnabled;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTime KTimeBox::getTime() const
|
|
|
|
{
|
|
|
|
const int hour = ktimeeditprivate->hourbox->value();
|
|
|
|
const int minute = ktimeeditprivate->minutebox->value();
|
|
|
|
const int second = ktimeeditprivate->secondbox->value();
|
|
|
|
return QTime(hour, minute, second);
|
|
|
|
}
|
|
|
|
|
2023-08-03 01:33:35 +03:00
|
|
|
|
2023-08-02 13:14:21 +03:00
|
|
|
KTimeEditPrivate::KTimeEditPrivate(KTimeEdit *parent)
|
|
|
|
: ktimeedit(parent),
|
|
|
|
hourbox(nullptr),
|
|
|
|
minutebox(nullptr),
|
2023-08-03 01:33:35 +03:00
|
|
|
secondbox(nullptr),
|
|
|
|
mintime(0, 0, 0),
|
|
|
|
maxtime(23, 59, 59)
|
2023-08-02 13:14:21 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-03 01:33:35 +03:00
|
|
|
void KTimeEditPrivate::updateWidgets()
|
2023-08-02 13:14:21 +03:00
|
|
|
{
|
2023-08-03 01:33:35 +03:00
|
|
|
const int hour = hourbox->value();
|
|
|
|
const int minute = minutebox->value();
|
|
|
|
const int second = secondbox->value();
|
|
|
|
hourbox->setSuffix(i18np(" hour", " hours", hour));
|
|
|
|
minutebox->setSuffix(i18np(" minute", " minutes", minute));
|
|
|
|
secondbox->setSuffix(i18np(" second", " seconds", second));
|
|
|
|
if (hour == 0 && minute == 0 && second == 0) {
|
|
|
|
hourbox->setSpecialValueText(i18n("never"));
|
|
|
|
minutebox->setSpecialValueText(i18n("never"));
|
|
|
|
secondbox->setSpecialValueText(i18n("never"));
|
|
|
|
} else {
|
|
|
|
hourbox->setSpecialValueText(QString());
|
|
|
|
minutebox->setSpecialValueText(QString());
|
|
|
|
secondbox->setSpecialValueText(QString());
|
|
|
|
}
|
|
|
|
hourbox->setMinimum(mintime.hour());
|
|
|
|
hourbox->setMaximum(maxtime.hour());
|
|
|
|
if (hour > 0) {
|
|
|
|
minutebox->setMinimum(0);
|
|
|
|
} else {
|
|
|
|
minutebox->setMinimum(mintime.minute());
|
|
|
|
}
|
|
|
|
if (hour > 0 || minute > 0) {
|
|
|
|
secondbox->setMinimum(0);
|
|
|
|
} else {
|
|
|
|
secondbox->setMinimum(mintime.second());
|
|
|
|
}
|
2023-08-02 13:14:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void KTimeEditPrivate::slotValueChanged(const int value)
|
|
|
|
{
|
2023-08-03 01:33:35 +03:00
|
|
|
updateWidgets();
|
2023-08-02 13:14:21 +03:00
|
|
|
const QTime currenttime = QTime(hourbox->value(), minutebox->value(), secondbox->value());
|
|
|
|
emit ktimeedit->timeChanged(currenttime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KTimeEdit::KTimeEdit(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
d(new KTimeEditPrivate(this))
|
|
|
|
{
|
|
|
|
QHBoxLayout* timelayout = new QHBoxLayout(this);
|
2023-08-03 01:33:35 +03:00
|
|
|
d->hourbox = new KTimeBox(d, this);
|
2023-08-02 13:14:21 +03:00
|
|
|
d->hourbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2024-04-02 17:35:20 +03:00
|
|
|
d->hourbox->setRange(0, 23);
|
2023-08-02 13:14:21 +03:00
|
|
|
timelayout->addWidget(d->hourbox);
|
2023-08-03 01:33:35 +03:00
|
|
|
d->minutebox = new KTimeBox(d, this);
|
2023-08-02 13:14:21 +03:00
|
|
|
d->minutebox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2024-04-02 17:35:20 +03:00
|
|
|
d->minutebox->setRange(0, 59);
|
2023-08-02 13:14:21 +03:00
|
|
|
timelayout->addWidget(d->minutebox);
|
2023-08-03 01:33:35 +03:00
|
|
|
d->secondbox = new KTimeBox(d, this);
|
2023-08-02 13:14:21 +03:00
|
|
|
d->secondbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2024-04-02 17:35:20 +03:00
|
|
|
d->secondbox->setRange(0, 59);
|
2023-08-02 13:14:21 +03:00
|
|
|
timelayout->addWidget(d->secondbox);
|
|
|
|
setLayout(timelayout);
|
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
|
|
|
2023-08-03 01:33:35 +03:00
|
|
|
d->updateWidgets();
|
2023-08-02 13:14:21 +03:00
|
|
|
|
|
|
|
connect(d->hourbox, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
|
|
|
|
connect(d->minutebox, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
|
|
|
|
connect(d->secondbox, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
|
|
|
|
}
|
|
|
|
|
|
|
|
KTimeEdit::~KTimeEdit()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTime KTimeEdit::minimumTime() const
|
|
|
|
{
|
2023-08-03 01:33:35 +03:00
|
|
|
return d->mintime;
|
2023-08-02 13:14:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void KTimeEdit::setMinimumTime(const QTime &time)
|
|
|
|
{
|
2024-04-02 17:40:04 +03:00
|
|
|
if (Q_UNLIKELY(!time.isValid())) {
|
|
|
|
kWarning() << "invalid minimum KTimeEdit time";
|
|
|
|
return;
|
|
|
|
}
|
2023-08-03 01:33:35 +03:00
|
|
|
d->mintime = time;
|
|
|
|
d->updateWidgets();
|
2023-08-02 13:14:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QTime KTimeEdit::maximumTime() const
|
|
|
|
{
|
2023-08-03 01:33:35 +03:00
|
|
|
return d->maxtime;
|
2023-08-02 13:14:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void KTimeEdit::setMaximumTime(const QTime &time)
|
|
|
|
{
|
2024-04-02 17:40:04 +03:00
|
|
|
if (Q_UNLIKELY(!time.isValid())) {
|
|
|
|
kWarning() << "invalid maximum KTimeEdit time";
|
|
|
|
return;
|
|
|
|
}
|
2023-08-03 01:33:35 +03:00
|
|
|
d->maxtime = time;
|
|
|
|
d->updateWidgets();
|
2023-08-02 13:14:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QTime KTimeEdit::time() const
|
|
|
|
{
|
|
|
|
return QTime(d->hourbox->value(), d->minutebox->value(), d->secondbox->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
void KTimeEdit::setTime(const QTime &time)
|
|
|
|
{
|
|
|
|
d->hourbox->setValue(time.hour());
|
|
|
|
d->minutebox->setValue(time.minute());
|
|
|
|
d->secondbox->setValue(time.second());
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_ktimeedit.cpp"
|
2023-08-03 01:33:35 +03:00
|
|
|
#include "ktimeedit.moc"
|