Akonadi Contacts

emailaddressrequester.cpp
1 /*
2  SPDX-FileCopyrightText: 2001 Marc Mutz <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "emailaddressrequester.h"
8 
9 #include "emailaddressselectiondialog.h"
10 #include <KLineEdit>
11 #include <KLocalizedString>
12 #include <QIcon>
13 
14 #include <QHBoxLayout>
15 #include <QPushButton>
16 #include <QTreeView>
17 
18 using namespace Akonadi;
19 
20 class Q_DECL_HIDDEN Akonadi::EmailAddressRequester::Private
21 {
22 public:
23  Private(EmailAddressRequester *qq)
24  : q(qq)
25  {
26  }
27 
28  void slotAddressBook();
29 
30  EmailAddressRequester *const q;
31  KLineEdit *mLineEdit = nullptr;
32 };
33 
34 void EmailAddressRequester::Private::slotAddressBook()
35 {
37  dlg->view()->view()->setSelectionMode(QAbstractItemView::MultiSelection);
38  if (!dlg->exec()) {
39  delete dlg;
40  return;
41  }
42 
43  QStringList addressList;
44  const auto selectedAddresses = dlg->selectedAddresses();
45  addressList.reserve(selectedAddresses.size());
46  for (const Akonadi::EmailAddressSelection &selection : selectedAddresses) {
47  addressList << selection.quotedEmail();
48  }
49 
50  QString text = mLineEdit->text().trimmed();
51 
52  if (!text.isEmpty()) {
53  if (!text.endsWith(QLatin1Char(','))) {
54  text += QLatin1String(", ");
55  } else {
56  text += QLatin1Char(' ');
57  }
58  }
59 
60  mLineEdit->setText(text + addressList.join(QLatin1Char(',')));
61  delete dlg;
62 }
63 
65  : QWidget(parent)
66  , d(new Private(this))
67 {
68  auto layout = new QHBoxLayout(this);
69  layout->setSpacing(4);
70  layout->setContentsMargins(0, 0, 0, 0);
71 
72  d->mLineEdit = new KLineEdit(this);
73  d->mLineEdit->setClearButtonEnabled(true);
74  d->mLineEdit->setTrapReturnKey(true);
75  layout->addWidget(d->mLineEdit, 1);
76 
77  auto button = new QPushButton(this);
78  button->setIcon(QIcon::fromTheme(QStringLiteral("help-contents")));
79  button->setFixedHeight(d->mLineEdit->sizeHint().height());
80  button->setToolTip(i18n("Open Address Book"));
81  layout->addWidget(button);
82 
83  connect(button, &QPushButton::clicked, this, [this]() {
84  d->slotAddressBook();
85  });
87 }
88 
90 {
91  delete d;
92 }
93 
95 {
96  d->mLineEdit->clear();
97 }
98 
100 {
101  d->mLineEdit->setText(text);
102 }
103 
105 {
106  return d->mLineEdit->text();
107 }
108 
110 {
111  return d->mLineEdit;
112 }
113 
114 #include "moc_emailaddressrequester.cpp"
QLayout * layout() const const
void setContentsMargins(int left, int top, int right, int bottom)
An selection of an email address and corresponding name.
KLineEdit * lineEdit() const
Returns the line edit that is used by the email address requester.
void reserve(int alloc)
void textChanged(const QString &text)
A widget to input one or more email addresses.
QString join(const QString &separator) const const
A dialog to select email addresses from Akonadi.
void textChanged()
This signal is emitted whenever the text of the email address requester has been changed.
EmailAddressRequester(QWidget *parent=nullptr)
Creates a new email address requester.
void setText(const QString &text)
Sets the text of the email address requester.
void clear()
Clears the text of the email address requester.
bool isEmpty() const const
QString trimmed() const const
void clicked(bool checked)
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString text() const
Returns the text of the email address requester.
void addWidget(QWidget *w)
QString i18n(const char *text, const TYPE &arg...)
~EmailAddressRequester() override
Destroys the email address requester.
void setSpacing(int)
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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.