File indexing completed on 2024-12-15 04:51:46

0001 /*******************************************************************
0002  KNotes -- Notes for the KDE project
0003 
0004  SPDX-FileCopyrightText: 2005 Michael Brade <brade@kde.org>
0005 
0006  SPDX-License-Identifier: GPL-2.0-or-later
0007 *******************************************************************/
0008 
0009 #include "notealarmdialog.h"
0010 
0011 #include <KDateComboBox>
0012 #include <KLocalizedString>
0013 #include <KTimeComboBox>
0014 #include <QDateTime>
0015 #include <QVBoxLayout>
0016 
0017 #include <QButtonGroup>
0018 #include <QDialogButtonBox>
0019 #include <QGroupBox>
0020 #include <QHBoxLayout>
0021 #include <QPushButton>
0022 #include <QRadioButton>
0023 
0024 using namespace NoteShared;
0025 
0026 NoteAlarmDialog::NoteAlarmDialog(const QString &caption, QWidget *parent)
0027     : QDialog(parent)
0028     , m_buttons(new QButtonGroup(this))
0029 {
0030     setWindowTitle(caption);
0031     auto mainLayout = new QVBoxLayout(this);
0032     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0033     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0034     okButton->setDefault(true);
0035     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0036     connect(buttonBox, &QDialogButtonBox::rejected, this, &NoteAlarmDialog::reject);
0037     auto page = new QWidget(this);
0038     auto pageVBoxLayout = new QVBoxLayout(page);
0039     pageVBoxLayout->setContentsMargins(0, 0, 0, 0);
0040 
0041     auto group = new QGroupBox(i18n("Scheduled Alarm"), page);
0042     pageVBoxLayout->addWidget(group);
0043     auto layout = new QVBoxLayout;
0044     auto none = new QRadioButton(i18n("&No alarm"));
0045     layout->addWidget(none);
0046     m_buttons->addButton(none, 0);
0047 
0048     group->setLayout(layout);
0049 
0050     auto at = new QWidget;
0051     auto atHBoxLayout = new QHBoxLayout(at);
0052     atHBoxLayout->setContentsMargins(0, 0, 0, 0);
0053     auto label_at = new QRadioButton(i18n("Alarm &at:"), at);
0054     atHBoxLayout->addWidget(label_at);
0055     m_atDate = new KDateComboBox(at);
0056     atHBoxLayout->addWidget(m_atDate);
0057     m_atTime = new KTimeComboBox(at);
0058     atHBoxLayout->addWidget(m_atTime);
0059     const QDateTime dateTime = QDateTime::currentDateTime();
0060     m_atDate->setMinimumDate(dateTime.date());
0061     m_atTime->setMinimumTime(dateTime.time());
0062     atHBoxLayout->setStretchFactor(m_atDate, 1);
0063     layout->addWidget(at);
0064     m_buttons->addButton(label_at, 1);
0065 
0066     connect(m_buttons, &QButtonGroup::buttonClicked, this, &NoteAlarmDialog::slotButtonChanged);
0067     connect(okButton, &QPushButton::clicked, this, &NoteAlarmDialog::accept);
0068     m_buttons->button(0)->setChecked(true);
0069     slotButtonChanged(m_buttons->checkedButton());
0070     mainLayout->addWidget(page);
0071     mainLayout->addWidget(buttonBox);
0072 }
0073 
0074 NoteAlarmDialog::~NoteAlarmDialog() = default;
0075 
0076 void NoteAlarmDialog::setAlarm(const QDateTime &dateTime)
0077 {
0078     if (dateTime.isValid()) {
0079         m_buttons->button(1)->setChecked(true);
0080         m_atDate->setDate(dateTime.date());
0081         m_atTime->setTime(dateTime.time());
0082     } else {
0083         m_buttons->button(0)->setChecked(true);
0084     }
0085     slotButtonChanged(m_buttons->checkedButton());
0086 }
0087 
0088 void NoteAlarmDialog::slotButtonChanged(QAbstractButton *button)
0089 {
0090     if (button) {
0091         const int id = m_buttons->id(button);
0092         switch (id) {
0093         case 0:
0094             m_atDate->setEnabled(false);
0095             m_atTime->setEnabled(false);
0096             break;
0097         case 1:
0098             m_atDate->setEnabled(true);
0099             m_atTime->setEnabled(true);
0100             break;
0101         }
0102     }
0103 }
0104 
0105 QDateTime NoteAlarmDialog::alarm() const
0106 {
0107     if (m_buttons->checkedId() == 1) {
0108         return QDateTime(m_atDate->date(), m_atTime->time());
0109     } else {
0110         return {};
0111     }
0112 }
0113 
0114 #include "moc_notealarmdialog.cpp"