Messagelib

recipientline.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
3 SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
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
16using namespace MessageComposer;
17using namespace KPIM;
18
19RecipientComboBox::RecipientComboBox(QWidget *parent)
20 : QComboBox(parent)
21{
22}
23
24void RecipientComboBox::keyPressEvent(QKeyEvent *ev)
25{
26 if (ev->key() == Qt::Key_Right) {
27 Q_EMIT rightPressed();
28 } else {
30 }
31}
32
33RecipientLineEdit::RecipientLineEdit(QWidget *parent)
34 : ComposerLineEdit(parent)
35{
36 setExpandIntern(false);
37}
38
39void 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
56RecipientLineNG::RecipientLineNG(QWidget *parent)
57 : MultiplyingLine(parent)
58 , mCombo(new RecipientComboBox(this))
59 , mEdit(new RecipientLineEdit(this))
60 , mData(new Recipient)
61{
63
64 auto topLayout = new QHBoxLayout(this);
65 topLayout->setContentsMargins({});
66
67 const QStringList recipientTypes = Recipient::allTypeLabels();
68
69 mCombo->addItems(recipientTypes);
70 topLayout->addWidget(mCombo);
71 mCombo->setToolTip(i18nc("@label:listbox", "Select type of recipient"));
72 mEdit->setToolTip(i18nc("@info:tooltip", "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, &RecipientComboBox::activated, this, &RecipientLineNG::slotTypeModified);
91
92 connect(mEdit, &RecipientLineEdit::addAddress, this, &RecipientLineNG::slotAddRecipient);
93}
94
95void RecipientLineNG::slotEditingFinished()
96{
97 if (mEdit->text().isEmpty()) {
98 Q_EMIT deleteLine(this);
99 }
100}
101
102void RecipientLineNG::slotAddRecipient(const QString &email)
103{
104 Q_EMIT addRecipient(this, email);
105 slotReturnPressed();
106}
107
108void RecipientLineNG::slotTypeModified()
109{
110 mModified = true;
111
112 Q_EMIT typeModified(this);
113}
114
115void RecipientLineNG::analyzeLine(const QString &text)
116{
118 mRecipientsCount = r.count();
119 mModified = true;
120 Q_EMIT countChanged();
121}
122
123int RecipientLineNG::recipientsCount() const
124{
125 return mRecipientsCount;
126}
127
128void RecipientLineNG::setData(const MultiplyingLineData::Ptr &data)
129{
130 Recipient::Ptr rec = qSharedPointerDynamicCast<Recipient>(data);
131 if (rec.isNull()) {
132 return;
133 }
134 mData = rec;
135 fieldsFromData();
136}
137
138MultiplyingLineData::Ptr RecipientLineNG::data() const
139{
140 if (isModified()) {
141 const_cast<RecipientLineNG *>(this)->dataFromFields();
142 }
143 return mData;
144}
145
146void RecipientLineNG::dataFromFields()
147{
148 if (!mData) {
149 return;
150 }
151 const QString editStr(mEdit->text());
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
163void RecipientLineNG::fieldsFromData()
164{
165 if (!mData) {
166 return;
167 }
168 mCombo->setCurrentIndex(Recipient::typeToId(mData->type()));
169 mEdit->setText(mData->email());
170}
171
172void RecipientLineNG::activate()
173{
174 mEdit->setFocus();
175}
176
177bool RecipientLineNG::isActive() const
178{
179 return mEdit->hasFocus();
180}
181
182bool RecipientLineNG::isEmpty() const
183{
184 return mEdit->text().isEmpty();
185}
186
187bool RecipientLineNG::isModified() const
188{
189 return mModified || mEdit->isModified();
190}
191
192void RecipientLineNG::clearModified()
193{
194 mModified = false;
195 mEdit->setModified(false);
196}
197
198int RecipientLineNG::setColumnWidth(int w)
199{
200 w = qMax(w, mCombo->sizeHint().width());
201 mCombo->setFixedWidth(w);
202 mCombo->updateGeometry();
204 return w;
205}
206
207void RecipientLineNG::fixTabOrder(QWidget *previous)
208{
209 setTabOrder(previous, mCombo);
210 setTabOrder(mCombo, mEdit);
211}
212
213QWidget *RecipientLineNG::tabOut() const
214{
215 return mEdit;
216}
217
218void RecipientLineNG::clear()
219{
220 mRecipientsCount = 0;
221 mEdit->clear();
222}
223
224bool RecipientLineNG::canDeleteLineEdit() const
225{
226 return mEdit->canDeleteLineEdit();
227}
228
229void RecipientLineNG::setCompletionMode(KCompletion::CompletionMode mode)
230{
231 mEdit->setCompletionMode(mode);
232}
233
234Recipient::Type RecipientLineNG::recipientType() const
235{
236 return Recipient::idToType(mCombo->currentIndex());
237}
238
239void RecipientLineNG::setRecipientType(Recipient::Type type)
240{
241 mCombo->setCurrentIndex(Recipient::typeToId(type));
242 slotTypeModified();
243}
244
246{
247 mEdit->setRecentAddressConfig(config);
248}
249
250Recipient::Ptr RecipientLineNG::recipient() const
251{
252 return qSharedPointerDynamicCast<Recipient>(data());
253}
254
255void RecipientLineNG::setIcon(const QIcon &icon, const QString &tooltip)
256{
257 mEdit->setIcon(icon, tooltip);
258}
259
260void RecipientLineNG::setEnableIndexSearch(bool enableIndexSearch)
261{
262 mEdit->setEnableBalooSearch(enableIndexSearch);
263}
264
265bool RecipientLineNG::enableIndexSearch() const
266{
267 return mEdit->enableBalooSearch();
268}
269
270void RecipientLineNG::setEnableAkonadiSearch(bool enableAkonadiSearch)
271{
272 mEdit->setEnableAkonadiSearch(enableAkonadiSearch);
273}
274
275bool RecipientLineNG::enableAkonadiSearch() const
276{
277 return mEdit->enableAkonadiSearch();
278}
279
280QString RecipientLineNG::rawData() const
281{
282 return mEdit->text();
283}
284
285bool 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}
295
296#include "moc_recipientline.cpp"
void deleteLine(KPIM::MultiplyingLine *)
The ComposerLineEdit class.
The RecipientLineEdit class.
The RecipientLineNG class.
void setRecentAddressConfig(KConfig *config)
Sets the config file used for storing recent addresses.
The Recipient class.
Definition recipient.h:28
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
KCODECS_EXPORT EmailParseResult splitAddress(const QByteArray &address, QByteArray &displayName, QByteArray &addrSpec, QByteArray &comment)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT QString displayName(Akonadi::ETMCalendar *calendar, const Akonadi::Collection &collection)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
virtual void keyPressEvent(QKeyEvent *e) override
virtual QSize sizeHint() const const override
int key() const const
Qt::KeyboardModifiers modifiers() const const
void textChanged(const QString &text)
qsizetype count() const const
Q_EMITQ_EMIT
bool isNull() const const
int width() const const
Key_Right
ShiftModifier
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual bool event(QEvent *event) override
QWidget * parentWidget() const const
void setFixedWidth(int w)
void setFocus()
void setTabOrder(QWidget *first, QWidget *second)
void updateGeometry()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.