File indexing completed on 2024-11-24 04:39:32

0001 /*
0002     This file is part of Contact Editor.
0003 
0004     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "dateeditwidget.h"
0010 
0011 #include <KDatePicker>
0012 #include <KDatePickerPopup>
0013 
0014 #include <KLocalizedString>
0015 #include <QContextMenuEvent>
0016 #include <QHBoxLayout>
0017 #include <QLocale>
0018 #include <QToolButton>
0019 
0020 DateView::DateView(QWidget *parent)
0021     : QLineEdit(parent)
0022 {
0023     setPlaceholderText(i18n("Click to add date"));
0024     setReadOnly(true);
0025 }
0026 
0027 void DateView::contextMenuEvent(QContextMenuEvent *event)
0028 {
0029     if (text().isEmpty()) {
0030         return;
0031     }
0032 
0033     QMenu menu;
0034     menu.addAction(i18n("Remove"), this, &DateView::emitSignal);
0035 
0036     menu.exec(event->globalPos());
0037 }
0038 
0039 void DateView::emitSignal()
0040 {
0041     Q_EMIT resetDate();
0042 }
0043 
0044 DateEditWidget::DateEditWidget(Type type, QWidget *parent)
0045     : QWidget(parent)
0046     , mReadOnly(false)
0047 {
0048     auto layout = new QHBoxLayout(this);
0049     layout->setContentsMargins({});
0050 
0051     mView = new DateView;
0052     layout->addWidget(mView);
0053 
0054     mClearButton = new QToolButton;
0055     if (layoutDirection() == Qt::LeftToRight) {
0056         mClearButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-locationbar-rtl")));
0057     } else {
0058         mClearButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-locationbar-ltr")));
0059     }
0060     layout->addWidget(mClearButton);
0061 
0062     mSelectButton = new QToolButton;
0063     mSelectButton->setPopupMode(QToolButton::InstantPopup);
0064     switch (type) {
0065     case General:
0066         mSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("view-calendar-day")));
0067         break;
0068     case Birthday:
0069         mSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("view-calendar-birthday")));
0070         break;
0071     case Anniversary:
0072         mSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("view-calendar-wedding-anniversary")));
0073         break;
0074     }
0075 
0076     layout->addWidget(mSelectButton);
0077     setFocusProxy(mSelectButton);
0078     setFocusPolicy(Qt::StrongFocus);
0079 
0080     mMenu = new KDatePickerPopup(KDatePickerPopup::DatePicker, QDate(), this);
0081     mSelectButton->setMenu(mMenu);
0082 
0083     connect(mClearButton, &QToolButton::clicked, this, &DateEditWidget::resetDate);
0084     connect(mMenu, &KDatePickerPopup::dateChanged, this, &DateEditWidget::dateSelected);
0085     connect(mView, &DateView::resetDate, this, &DateEditWidget::resetDate);
0086 
0087     updateView();
0088 }
0089 
0090 DateEditWidget::~DateEditWidget() = default;
0091 
0092 void DateEditWidget::setDate(const QDate &date)
0093 {
0094     mDate = date;
0095     mMenu->setDate(mDate);
0096     updateView();
0097 }
0098 
0099 QDate DateEditWidget::date() const
0100 {
0101     return mDate;
0102 }
0103 
0104 void DateEditWidget::setReadOnly(bool readOnly)
0105 {
0106     mReadOnly = readOnly;
0107 
0108     mSelectButton->setEnabled(!readOnly);
0109     mClearButton->setEnabled(!readOnly);
0110 }
0111 
0112 void DateEditWidget::dateSelected(const QDate &date)
0113 {
0114     mDate = date;
0115     updateView();
0116 }
0117 
0118 void DateEditWidget::resetDate()
0119 {
0120     mDate = QDate();
0121     updateView();
0122 }
0123 
0124 void DateEditWidget::updateView()
0125 {
0126     if (mDate.isValid()) {
0127         mView->setText(QLocale().toString(mDate));
0128         mClearButton->show();
0129     } else {
0130         mView->setText(QString());
0131         mClearButton->hide();
0132     }
0133 }
0134 
0135 #include "moc_dateeditwidget.cpp"