File indexing completed on 2025-01-05 04:49:45

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "eventdatetimewidget.h"
0008 #include <KDateComboBox>
0009 #include <KTimeComboBox>
0010 #include <QHBoxLayout>
0011 
0012 using namespace MessageViewer;
0013 
0014 EventDateTimeWidget::EventDateTimeWidget(QWidget *parent)
0015     : QWidget(parent)
0016     , mDateEdit(new KDateComboBox(this))
0017     , mTimeEdit(new KTimeComboBox(this))
0018 {
0019     auto mainLayout = new QHBoxLayout(this);
0020     mainLayout->setSpacing(0);
0021     mainLayout->setContentsMargins({});
0022     mDateEdit->setObjectName(QLatin1StringView("eventdatecombobox"));
0023     mainLayout->addWidget(mDateEdit);
0024     mTimeEdit->setObjectName(QLatin1StringView("eventtimecombobox"));
0025     mainLayout->addWidget(mTimeEdit);
0026     const QDateTime currentDateTime = QDateTime::currentDateTime();
0027     setDateTime(currentDateTime);
0028     connect(mDateEdit, &KDateComboBox::dateChanged, this, &EventDateTimeWidget::slotDateTimeChanged);
0029     connect(mTimeEdit, &KTimeComboBox::timeChanged, this, &EventDateTimeWidget::slotDateTimeChanged);
0030     connect(mDateEdit, &KDateComboBox::dateEdited, this, &EventDateTimeWidget::slotDateTimeChanged);
0031     connect(mTimeEdit, &KTimeComboBox::timeEdited, this, &EventDateTimeWidget::slotDateTimeChanged);
0032 }
0033 
0034 EventDateTimeWidget::~EventDateTimeWidget() = default;
0035 
0036 void EventDateTimeWidget::slotDateTimeChanged()
0037 {
0038     Q_EMIT dateTimeChanged(dateTime());
0039 }
0040 
0041 void EventDateTimeWidget::setMinimumDateTime(const QDateTime &dtime)
0042 {
0043     if (dateTime() != dtime) {
0044         mDateEdit->setMinimumDate(dtime.date());
0045         mTimeEdit->setMinimumTime(dtime.time());
0046     }
0047 }
0048 
0049 void EventDateTimeWidget::setDateTime(const QDateTime &dTime)
0050 {
0051     if (dateTime() != dTime) {
0052         blockSignals(true);
0053         mDateEdit->setDate(dTime.date());
0054         mTimeEdit->setTime(dTime.time());
0055         blockSignals(false);
0056         slotDateTimeChanged();
0057     }
0058 }
0059 
0060 QDateTime EventDateTimeWidget::dateTime() const
0061 {
0062     QDateTime dateTime = QDateTime::currentDateTime();
0063     dateTime.setTime(mTimeEdit->time());
0064     dateTime.setDate(mDateEdit->date());
0065     return dateTime;
0066 }
0067 
0068 QDate EventDateTimeWidget::date() const
0069 {
0070     return mDateEdit->date();
0071 }
0072 
0073 QTime EventDateTimeWidget::time() const
0074 {
0075     return mTimeEdit->time();
0076 }
0077 
0078 void EventDateTimeWidget::setTime(QTime time)
0079 {
0080     mTimeEdit->setTime(time);
0081 }
0082 
0083 void EventDateTimeWidget::setDate(QDate date)
0084 {
0085     mDateEdit->setDate(date);
0086 }
0087 
0088 #include "moc_eventdatetimewidget.cpp"