Akonadi Contacts

dateeditwidget.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "dateeditwidget.h"
10
11#include <KDatePicker>
12#include <KDatePickerPopup>
13
14#include <KLocalizedString>
15#include <QContextMenuEvent>
16#include <QHBoxLayout>
17#include <QLocale>
18#include <QToolButton>
19
20DateView::DateView(QWidget *parent)
21 : QLineEdit(parent)
22{
23 setPlaceholderText(i18nc("@info:placeholder", "Click to add date"));
24 setReadOnly(true);
25}
26
27void DateView::contextMenuEvent(QContextMenuEvent *event)
28{
29 if (text().isEmpty()) {
30 return;
31 }
32
33 QMenu menu;
34 menu.addAction(i18n("Remove"), this, &DateView::emitSignal);
35
36 menu.exec(event->globalPos());
37}
38
39void DateView::emitSignal()
40{
41 Q_EMIT resetDate();
42}
43
44DateEditWidget::DateEditWidget(Type type, QWidget *parent)
45 : QWidget(parent)
46 , mReadOnly(false)
47{
48 auto layout = new QHBoxLayout(this);
49 layout->setContentsMargins({});
50
51 mView = new DateView;
52 layout->addWidget(mView);
53
54 mClearButton = new QToolButton;
55 if (layoutDirection() == Qt::LeftToRight) {
56 mClearButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-locationbar-rtl")));
57 } else {
58 mClearButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-locationbar-ltr")));
59 }
60 layout->addWidget(mClearButton);
61
62 mSelectButton = new QToolButton;
64 switch (type) {
65 case General:
66 mSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("view-calendar-day")));
67 break;
68 case Birthday:
69 mSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("view-calendar-birthday")));
70 break;
71 case Anniversary:
72 mSelectButton->setIcon(QIcon::fromTheme(QStringLiteral("view-calendar-wedding-anniversary")));
73 break;
74 }
75
76 layout->addWidget(mSelectButton);
77 setFocusProxy(mSelectButton);
78 setFocusPolicy(Qt::StrongFocus);
79
81 mSelectButton->setMenu(mMenu);
82
83 connect(mClearButton, &QToolButton::clicked, this, &DateEditWidget::resetDate);
84 connect(mMenu, &KDatePickerPopup::dateChanged, this, &DateEditWidget::dateSelected);
85 connect(mView, &DateView::resetDate, this, &DateEditWidget::resetDate);
86
87 updateView();
88}
89
90DateEditWidget::~DateEditWidget() = default;
91
92void DateEditWidget::setDate(const QDate &date)
93{
94 mDate = date;
95 mMenu->setDate(mDate);
96 updateView();
97}
98
99QDate DateEditWidget::date() const
100{
101 return mDate;
102}
103
104void DateEditWidget::setReadOnly(bool readOnly)
105{
106 mReadOnly = readOnly;
107
108 mSelectButton->setEnabled(!readOnly);
109 mClearButton->setEnabled(!readOnly);
110}
111
112void DateEditWidget::dateSelected(const QDate &date)
113{
114 mDate = date;
115 updateView();
116}
117
118void DateEditWidget::resetDate()
119{
120 mDate = QDate();
121 updateView();
122}
123
124void DateEditWidget::updateView()
125{
126 if (mDate.isValid()) {
127 mView->setText(QLocale().toString(mDate));
128 mClearButton->show();
129 } else {
130 mView->setText(QString());
131 mClearButton->hide();
132 }
133}
134
135#include "moc_dateeditwidget.cpp"
void dateChanged(const QDate &date)
void setDate(QDate date)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
void clicked(bool checked)
bool isValid(int year, int month, int day)
QIcon fromTheme(const QString &name)
virtual bool event(QEvent *e) override
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * exec()
Q_EMITQ_EMIT
StrongFocus
LeftToRight
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setPopupMode(ToolButtonPopupMode mode)
void setEnabled(bool)
void hide()
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.