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 <QKeyEvent>
14 #include <QHBoxLayout>
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) : ComposerLineEdit(parent)
34 {
35  setExpandIntern(false);
36 }
37 
38 void RecipientLineEdit::keyPressEvent(QKeyEvent *ev)
39 {
40  //Laurent Bug:280153
41  /*if ( ev->key() == Qt::Key_Backspace && text().isEmpty() ) {
42  ev->accept();
43  Q_EMIT deleteMe();
44  } else */
45  if (ev->key() == Qt::Key_Left && cursorPosition() == 0
46  && !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(0, 0, 0, 0);
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,
80  this, &RecipientLineNG::analyzeLine);
81  connect(mEdit, &RecipientLineEdit::focusUp, this, &RecipientLineNG::slotFocusUp);
82  connect(mEdit, &RecipientLineEdit::focusDown, this, &RecipientLineNG::slotFocusDown);
83  connect(mEdit, &RecipientLineEdit::rightPressed, this, &RecipientLineNG::rightPressed);
84  connect(mEdit, &RecipientLineEdit::iconClicked, this, &RecipientLineNG::iconClicked);
85 
86  connect(mEdit, &RecipientLineEdit::leftPressed, mCombo, qOverload<>(&QWidget::setFocus));
87  connect(mEdit, &RecipientLineEdit::editingFinished, this, &RecipientLineNG::slotEditingFinished);
88  connect(mEdit, &RecipientLineEdit::clearButtonClicked, this, &RecipientLineNG::slotPropagateDeletion);
89  connect(mCombo, &RecipientComboBox::rightPressed, mEdit, qOverload<>(&QWidget::setFocus));
90 
91  connect(mCombo, qOverload<int>(&RecipientComboBox::activated),
92  this, &RecipientLineNG::slotTypeModified);
93 
94  connect(mEdit, &RecipientLineEdit::addAddress, this, &RecipientLineNG::slotAddRecipient);
95 }
96 
97 void RecipientLineNG::slotEditingFinished()
98 {
99  if (mEdit->text().isEmpty()) {
100  Q_EMIT deleteLine(this);
101  }
102 }
103 
104 void RecipientLineNG::slotAddRecipient(const QString &email)
105 {
106  Q_EMIT addRecipient(this, email);
107  slotReturnPressed();
108 }
109 
110 void RecipientLineNG::slotTypeModified()
111 {
112  mModified = true;
113 
114  Q_EMIT typeModified(this);
115 }
116 
117 void RecipientLineNG::analyzeLine(const QString &text)
118 {
120  mRecipientsCount = r.count();
121  mModified = true;
122  Q_EMIT countChanged();
123 }
124 
125 int RecipientLineNG::recipientsCount() const
126 {
127  return mRecipientsCount;
128 }
129 
130 void RecipientLineNG::setData(const MultiplyingLineData::Ptr &data)
131 {
132  Recipient::Ptr rec = qSharedPointerDynamicCast<Recipient>(data);
133  if (!rec) {
134  return;
135  }
136  mData = rec;
137  fieldsFromData();
138 }
139 
140 MultiplyingLineData::Ptr RecipientLineNG::data() const
141 {
142  if (isModified()) {
143  const_cast<RecipientLineNG *>(this)->dataFromFields();
144  }
145  return mData;
146 }
147 
148 void RecipientLineNG::dataFromFields()
149 {
150  if (!mData) {
151  return;
152  }
153  const QString editStr(mEdit->text());
154  QString displayName, addrSpec, 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-2020 The KDE developers.
Generated on Wed Aug 12 2020 23:12:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.