Akonadi Contacts

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

KDE's Doxygen guidelines are available online.