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 Akonadi::EmailAddressRequesterPrivate
21 {
22 public:
23  explicit EmailAddressRequesterPrivate(EmailAddressRequester *qq)
24  : q(qq)
25  {
26  }
27 
28  void slotAddressBook();
29 
30  EmailAddressRequester *const q;
31  KLineEdit *mLineEdit = nullptr;
32 };
33 
34 void EmailAddressRequesterPrivate::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 EmailAddressRequesterPrivate(this))
67 {
68  auto layout = new QHBoxLayout(this);
69  layout->setSpacing(4);
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 
92 {
93  d->mLineEdit->clear();
94 }
95 
97 {
98  d->mLineEdit->setText(text);
99 }
100 
101 QString EmailAddressRequester::text() const
102 {
103  return d->mLineEdit->text();
104 }
105 
107 {
108  return d->mLineEdit;
109 }
110 
111 #include "moc_emailaddressrequester.cpp"
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
void setText(const QString &text)
Sets the text of the email address requester.
An selection of an email address and corresponding name.
QLayout * layout() const const
QString trimmed() const const
void clicked(bool checked)
QIcon fromTheme(const QString &name)
KLineEdit * lineEdit() const
Returns the line edit that is used by the email address requester.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
~EmailAddressRequester() override
Destroys the email address requester.
void reserve(int alloc)
QString i18n(const char *text, const TYPE &arg...)
void textChanged(const QString &text)
bool isEmpty() const const
void clear()
Clears the text of the email address requester.
A dialog to select email addresses from Akonadi.
QString join(const QString &separator) const const
void addWidget(QWidget *w)
void textChanged()
This signal is emitted whenever the text of the email address requester has been changed.
void setContentsMargins(int left, int top, int right, int bottom)
void setSpacing(int)
A widget to input one or more email addresses.
EmailAddressRequester(QWidget *parent=nullptr)
Creates a new email address requester.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Feb 5 2023 03:57:43 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.