Messagelib

recipientline.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Casey Link <[email protected]>
3  SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "recipientline.h"
9 
10 #include <KEmailAddress>
11 #include <KLocalizedString>
12 
13 #include <QHBoxLayout>
14 #include <QKeyEvent>
15 
16 using namespace MessageComposer;
17 using namespace KPIM;
18 
19 RecipientComboBox::RecipientComboBox(QWidget *parent)
20  : QComboBox(parent)
21 {
22 }
23 
24 void RecipientComboBox::keyPressEvent(QKeyEvent *ev)
25 {
26  if (ev->key() == Qt::Key_Right) {
27  Q_EMIT rightPressed();
28  } else {
30  }
31 }
32 
33 RecipientLineEdit::RecipientLineEdit(QWidget *parent)
34  : ComposerLineEdit(parent)
35 {
36  setExpandIntern(false);
37 }
38 
39 void RecipientLineEdit::keyPressEvent(QKeyEvent *ev)
40 {
41  // Laurent Bug:280153
42  /*if ( ev->key() == Qt::Key_Backspace && text().isEmpty() ) {
43  ev->accept();
44  Q_EMIT deleteMe();
45  } else */
46  if (ev->key() == Qt::Key_Left && cursorPosition() == 0 && !ev->modifiers().testFlag(Qt::ShiftModifier)) { // Shift would be pressed during selection
47  Q_EMIT leftPressed();
48  } else if (ev->key() == Qt::Key_Right && cursorPosition() == text().length()
49  && !ev->modifiers().testFlag(Qt::ShiftModifier)) { // Shift would be pressed during selection
50  Q_EMIT rightPressed();
51  } else {
52  MessageComposer::ComposerLineEdit::keyPressEvent(ev);
53  }
54 }
55 
56 RecipientLineNG::RecipientLineNG(QWidget *parent)
57  : MultiplyingLine(parent)
58  , mData(new Recipient)
59 {
61 
62  QBoxLayout *topLayout = new QHBoxLayout(this);
63  topLayout->setContentsMargins({});
64 
65  const QStringList recipientTypes = Recipient::allTypeLabels();
66 
67  mCombo = new RecipientComboBox(this);
68  mCombo->addItems(recipientTypes);
69  topLayout->addWidget(mCombo);
70  mCombo->setToolTip(i18nc("@label:listbox", "Select type of recipient"));
71  mEdit = new RecipientLineEdit(this);
72  mEdit->setToolTip(i18n("Set the list of email addresses to receive this message"));
73  mEdit->setClearButtonEnabled(true);
74  topLayout->addWidget(mEdit);
75  mEdit->installEventFilter(this);
76 
77  connect(mEdit, &RecipientLineEdit::returnPressed, this, &RecipientLineNG::slotReturnPressed);
78  connect(mEdit, &RecipientLineEdit::deleteMe, this, &RecipientLineNG::slotPropagateDeletion);
79  connect(mEdit, &QLineEdit::textChanged, this, &RecipientLineNG::analyzeLine);
80  connect(mEdit, &RecipientLineEdit::focusUp, this, &RecipientLineNG::slotFocusUp);
81  connect(mEdit, &RecipientLineEdit::focusDown, this, &RecipientLineNG::slotFocusDown);
82  connect(mEdit, &RecipientLineEdit::rightPressed, this, &RecipientLineNG::rightPressed);
83  connect(mEdit, &RecipientLineEdit::iconClicked, this, &RecipientLineNG::iconClicked);
84 
85  connect(mEdit, &RecipientLineEdit::leftPressed, mCombo, qOverload<>(&QWidget::setFocus));
86  connect(mEdit, &RecipientLineEdit::editingFinished, this, &RecipientLineNG::slotEditingFinished);
87  connect(mEdit, &RecipientLineEdit::clearButtonClicked, this, &RecipientLineNG::slotPropagateDeletion);
88  connect(mCombo, &RecipientComboBox::rightPressed, mEdit, qOverload<>(&QWidget::setFocus));
89 
90  connect(mCombo, qOverload<int>(&RecipientComboBox::activated), this, &RecipientLineNG::slotTypeModified);
91 
92  connect(mEdit, &RecipientLineEdit::addAddress, this, &RecipientLineNG::slotAddRecipient);
93 }
94 
95 void RecipientLineNG::slotEditingFinished()
96 {
97  if (mEdit->text().isEmpty()) {
98  Q_EMIT deleteLine(this);
99  }
100 }
101 
102 void RecipientLineNG::slotAddRecipient(const QString &email)
103 {
104  Q_EMIT addRecipient(this, email);
105  slotReturnPressed();
106 }
107 
108 void RecipientLineNG::slotTypeModified()
109 {
110  mModified = true;
111 
112  Q_EMIT typeModified(this);
113 }
114 
115 void RecipientLineNG::analyzeLine(const QString &text)
116 {
118  mRecipientsCount = r.count();
119  mModified = true;
120  Q_EMIT countChanged();
121 }
122 
123 int RecipientLineNG::recipientsCount() const
124 {
125  return mRecipientsCount;
126 }
127 
128 void RecipientLineNG::setData(const MultiplyingLineData::Ptr &data)
129 {
130  Recipient::Ptr rec = qSharedPointerDynamicCast<Recipient>(data);
131  if (!rec) {
132  return;
133  }
134  mData = rec;
135  fieldsFromData();
136 }
137 
138 MultiplyingLineData::Ptr RecipientLineNG::data() const
139 {
140  if (isModified()) {
141  const_cast<RecipientLineNG *>(this)->dataFromFields();
142  }
143  return mData;
144 }
145 
146 void RecipientLineNG::dataFromFields()
147 {
148  if (!mData) {
149  return;
150  }
151  const QString editStr(mEdit->text());
152  QString displayName;
153  QString addrSpec;
154  QString comment;
155  if (KEmailAddress::splitAddress(editStr, displayName, addrSpec, comment) == KEmailAddress::AddressOk) {
156  mData->setName(displayName);
157  }
158  mData->setEmail(editStr);
159  mData->setType(Recipient::idToType(mCombo->currentIndex()));
160  mModified = false;
161 }
162 
163 void RecipientLineNG::fieldsFromData()
164 {
165  if (!mData) {
166  return;
167  }
168  mCombo->setCurrentIndex(Recipient::typeToId(mData->type()));
169  mEdit->setText(mData->email());
170 }
171 
172 void RecipientLineNG::activate()
173 {
174  mEdit->setFocus();
175 }
176 
177 bool RecipientLineNG::isActive() const
178 {
179  return mEdit->hasFocus();
180 }
181 
182 bool RecipientLineNG::isEmpty() const
183 {
184  return mEdit->text().isEmpty();
185 }
186 
187 bool RecipientLineNG::isModified() const
188 {
189  return mModified || mEdit->isModified();
190 }
191 
192 void RecipientLineNG::clearModified()
193 {
194  mModified = false;
195  mEdit->setModified(false);
196 }
197 
198 int RecipientLineNG::setColumnWidth(int w)
199 {
200  w = qMax(w, mCombo->sizeHint().width());
201  mCombo->setFixedWidth(w);
202  mCombo->updateGeometry();
203  parentWidget()->updateGeometry();
204  return w;
205 }
206 
207 void RecipientLineNG::fixTabOrder(QWidget *previous)
208 {
209  setTabOrder(previous, mCombo);
210  setTabOrder(mCombo, mEdit);
211 }
212 
213 QWidget *RecipientLineNG::tabOut() const
214 {
215  return mEdit;
216 }
217 
218 void RecipientLineNG::clear()
219 {
220  mRecipientsCount = 0;
221  mEdit->clear();
222 }
223 
224 bool RecipientLineNG::canDeleteLineEdit() const
225 {
226  return mEdit->canDeleteLineEdit();
227 }
228 
229 void RecipientLineNG::setCompletionMode(KCompletion::CompletionMode mode)
230 {
231  mEdit->setCompletionMode(mode);
232 }
233 
234 Recipient::Type RecipientLineNG::recipientType() const
235 {
236  return Recipient::idToType(mCombo->currentIndex());
237 }
238 
239 void RecipientLineNG::setRecipientType(Recipient::Type type)
240 {
241  mCombo->setCurrentIndex(Recipient::typeToId(type));
242  slotTypeModified();
243 }
244 
246 {
247  mEdit->setRecentAddressConfig(config);
248 }
249 
250 Recipient::Ptr RecipientLineNG::recipient() const
251 {
252  return qSharedPointerDynamicCast<Recipient>(data());
253 }
254 
255 void RecipientLineNG::setIcon(const QIcon &icon, const QString &tooltip)
256 {
257  mEdit->setIcon(icon, tooltip);
258 }
259 
260 void RecipientLineNG::setEnableIndexSearch(bool enableIndexSearch)
261 {
262  mEdit->setEnableBalooSearch(enableIndexSearch);
263 }
264 
265 bool RecipientLineNG::enableIndexSearch() const
266 {
267  return mEdit->enableBalooSearch();
268 }
269 
270 void RecipientLineNG::setEnableAkonadiSearch(bool enableAkonadiSearch)
271 {
272  mEdit->setEnableAkonadiSearch(enableAkonadiSearch);
273 }
274 
275 bool RecipientLineNG::enableAkonadiSearch() const
276 {
277  return mEdit->enableAkonadiSearch();
278 }
279 
280 QString RecipientLineNG::rawData() const
281 {
282  return mEdit->text();
283 }
284 
285 bool RecipientLineNG::eventFilter(QObject *watched, QEvent *event)
286 {
287  if (watched == mEdit) {
288  if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) {
289  Q_EMIT activeChanged();
290  }
291  }
292 
293  return false;
294 }
ShiftModifier
Qt::KeyboardModifiers modifiers() const const
QEvent::Type type() const const
void setContentsMargins(int left, int top, int right, int bottom)
KCODECS_EXPORT EmailParseResult splitAddress(const QByteArray &address, QByteArray &displayName, QByteArray &addrSpec, QByteArray &comment)
virtual void keyPressEvent(QKeyEvent *e) override
void textChanged(const QString &text)
The ComposerLineEdit class.
The RecipientLineNG class.
Definition: recipientline.h:54
void setRecentAddressConfig(KConfig *config)
Sets the config file used for storing recent addresses.
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
int count(const T &value) const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setFocus()
bool isEmpty() const const
The RecipientLineEdit class.
Definition: recipientline.h:36
int key() const const
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
QString i18n(const char *text, const TYPE &arg...)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
The Recipient class.
Definition: recipient.h:27
Key_Right
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.