forked from InvictusInnovations/keyhotee
/
ContactListEdit.cpp
377 lines (318 loc) · 11.2 KB
/
ContactListEdit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "ContactListEdit.hpp"
#include "public_key_address.hpp"
#include "AddressBook/Contact.hpp"
#include <QCompleter>
#include <QAbstractItemView>
#include <QKeyEvent>
#include <QScrollBar>
#include <QPainter>
#include <QTextBlock>
#include <fc/log/logger.hpp>
#include <bts/profile.hpp>
ContactListEdit::ContactListEdit(QWidget* parent)
: QTextEdit(parent)
{
_completer = nullptr;
connect(this, &QTextEdit::textChanged, this, &ContactListEdit::fitHeightToDocument);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
fitHeightToDocument();
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setTabChangesFocus(true);
}
ContactListEdit::~ContactListEdit()
{}
void ContactListEdit::setCompleter(QCompleter* completer)
{
if (_completer)
QObject::disconnect(_completer, 0, this, 0);
_completer = completer;
if (!_completer)
return;
_completer->setWidget(this);
_completer->setCompletionMode(QCompleter::PopupCompletion);
_completer->setCaseSensitivity(Qt::CaseInsensitive);
connect(_completer, SIGNAL(activated(const QModelIndex&)),
this, SLOT(insertCompletion(const QModelIndex&)));
}
void ContactListEdit::insertCompletion( const QModelIndex& completionIndex )
{
if( !completionIndex.isValid())
return;
QString completion = completionIndex.data().toString();
int row = completionIndex.data(Qt::UserRole).toInt();
row = row / 2;
auto addressbook = bts::get_profile()->get_addressbook();
auto contacts = addressbook->get_contacts();
auto contact = contacts[row];
insertCompletion(completion, contact);
}
void ContactListEdit::insertCompletion( const QString& completion, const bts::addressbook::contact& c)
{
ilog( "insertCompletion ${c}", ("c", completion.toStdString() ) );
// remove existing text
// create image, attach meta data for on-click menus
if (_completer->widget() != this)
return;
addContactEntry(completion, c);
}
void ContactListEdit::onCompleterRequest()
{
setFocus();
showCompleter(QString());
}
//! [5]
QString ContactListEdit::textUnderCursor() const
{
QTextCursor text_cursor = textCursor();
text_cursor.select(QTextCursor::WordUnderCursor);
return text_cursor.selectedText();
}
QStringList ContactListEdit::getListOfImageNames() const
{
QStringList image_names;
QTextBlock block = document()->begin();
while (block.isValid())
{
for (QTextBlock::iterator i = block.begin(); !i.atEnd(); ++i)
{
QTextCharFormat format = i.fragment().charFormat();
bool isImage = format.isImageFormat();
if (isImage)
image_names << format.toImageFormat().name();
}
block = block.next();
}
return image_names;
}
void ContactListEdit::addContactEntry(const QString& contactText, const bts::addressbook::contact& c)
{
QFont default_font;
default_font.setPointSize( default_font.pointSize() - 1 );
QFontMetrics font_metrics(default_font);
QRect bounding = font_metrics.boundingRect(contactText);
int completion_width = font_metrics.width(contactText);
int completion_height = bounding.height();
completion_width += 20;
QImage completion_image(completion_width, completion_height + 4, QImage::Format_ARGB32);
completion_image.fill(QColor(0, 0, 0, 0) );
QPainter painter;
painter.begin(&completion_image);
painter.setFont(default_font);
painter.setRenderHint(QPainter::Antialiasing);
QBrush brush(Qt::SolidPattern);
brush.setColor( QColor( 205, 220, 241 ) );
QPen pen;
bool isKeyhoteeFounder = Contact::isKeyhoteeFounder(c);
if (isKeyhoteeFounder)
{
QLinearGradient grad(QPointF(0, 0), QPointF(0, 1));
grad.setCoordinateMode(QGradient::ObjectBoundingMode);
grad.setColorAt(0.3, QColor(231, 190, 66));
grad.setColorAt(1.0, QColor(103, 51, 1));
brush = QBrush(grad);
pen.setColor( QColor( 103, 51, 1 ) );
}
else
{
brush.setColor( QColor( 205, 220, 241 ) );
pen.setColor( QColor( 105,110,180 ) );
}
painter.setBrush(brush);
painter.setPen(pen);
painter.drawRoundedRect(0, 0, completion_width - 1, completion_image.height() - 1, 8, 8,
Qt::AbsoluteSize);
painter.setPen(QPen());
painter.drawText(QPoint(10, completion_height - 2), contactText);
QTextCursor text_cursor = textCursor();
uint32_t prefix_len = _completer->completionPrefix().length();
for (uint32_t i = 0; i < prefix_len; ++i)
text_cursor.deletePreviousChar();
// int extra = completion.length() -
// tc.movePosition(QTextCursor::Left);
// tc.movePosition(QTextCursor::EndOfWord);
// tc.insertText(completion.right(extra));
text_cursor.insertImage(completion_image, contactText);
text_cursor.insertText(" ");
setTextCursor(text_cursor);
}
QString ContactListEdit::toString(const bts::addressbook::wallet_identity& id) const
{
return toStringImpl(id);
}
QString ContactListEdit::toString(const bts::addressbook::wallet_contact& id) const
{
return id.get_display_name().c_str();//toStringImpl(id);
}
template <class TContactStorage>
inline
QString ContactListEdit::toStringImpl(const TContactStorage& id) const
{
/** Use trivial encoding (as kID only) until some underlying storage for added contact
data will be implemented. Right now built text must allow successfull reverse parsing
what is completely wrong
*/
bool noAlias = true;//id.first_name.empty() && id.last_name.empty();
std::string identity_label;
if(noAlias == false)
identity_label = id.first_name + " " + id.last_name;
std::string entry(identity_label);
if(noAlias == false)
entry += '(';
entry += id.dac_id_string;
if(noAlias == false)
entry += ')';
return QString(entry.c_str());
}
//! [5]
//! [6]
void ContactListEdit::focusInEvent(QFocusEvent* focus_event)
{
if (_completer)
_completer->setWidget(this);
QTextEdit::focusInEvent(focus_event);
}
//! [6]
bool ContactListEdit::focusNextPrevChild(bool next)
{
if (_completer && _completer->popup()->isVisible())
return false;
return QTextEdit::focusNextPrevChild(next);
}
//! [7]
void ContactListEdit::keyPressEvent(QKeyEvent* key_event)
{
if (_completer && _completer->popup()->isVisible())
// The following keys are forwarded by the completer to the widget
switch (key_event->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
key_event->ignore();
return; // let the completer do default behavior
default:
break;
}
bool isShortcut = ((key_event->modifiers() & Qt::ControlModifier) && key_event->key() == Qt::Key_E); // CTRL+E
if (!_completer || !isShortcut) // do not process the shortcut when we have a completer
QTextEdit::keyPressEvent(key_event);
//! [7]
//! [8]
const bool ctrlOrShift = key_event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!_completer || (ctrlOrShift && key_event->text().isEmpty()))
return;
static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="); // end of word
bool hasModifier = (key_event->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();
if (!isShortcut && (hasModifier || key_event->text().isEmpty() || completionPrefix.length() == 0
|| eow.contains(key_event->text().right(1))))
{
_completer->popup()->hide();
return;
}
showCompleter(completionPrefix);
}
void ContactListEdit::showCompleter(const QString& completionPrefix)
{
if (completionPrefix != _completer->completionPrefix())
{
_completer->setCompletionPrefix(completionPrefix);
_completer->popup()->setCurrentIndex(_completer->completionModel()->index(0, 0));
}
QRect cr = cursorRect();
cr.setWidth(_completer->popup()->sizeHintForColumn(0)
+ _completer->popup()->verticalScrollBar()->sizeHint().width());
_completer->complete(cr); // popup it up!
}
void ContactListEdit::SetCollectedContacts(const IMailProcessor::TRecipientPublicKeys& storage)
{
auto profile = bts::get_profile();
auto aBook = profile->get_addressbook();
for(const auto& recipient : storage)
{
assert(recipient.valid());
auto contact = aBook->get_contact_by_public_key(recipient);
if(contact)
{
/** Use kID as completion here - it is slight violation against source list but we don't know
here how it was originally entered (by kID or alias: fName lName)
*/
QString entryText(toString(*contact));
addContactEntry(entryText, *contact);
}
else
{
bool known = false;
/// If no contact found try one of registered identities.
std::vector<bts::addressbook::wallet_identity> identities = profile->identities();
for(const auto& identity : identities)
{
assert(identity.public_key.valid());
if(identity.public_key == recipient)
{
QString entryText(toString(identity));
addContactEntry(entryText, identity);
known = true;
break;
}
}
if(known == false)
{
/// Some unknown contact. Lets show its public key in the list.
public_key_address pkAddress(recipient);
std::string textPK(pkAddress);
bts::addressbook::wallet_contact wc;
wc.public_key = recipient;
addContactEntry(QString(textPK.c_str()), wc);
}
}
}
}
void ContactListEdit::GetCollectedContacts(IMailProcessor::TRecipientPublicKeys* storage) const
{
/** FIXME - this code generally looks bad. It will don't work when in AB will be conflict between
kID and fName lName alias.
Whole completer <-> contact edit communication is wrong, since it should operate on Contact
objects not just strings (where noone knows it is a kID or alias).
Another problem is synchronization against AB changes. It is also did wrong. To do it correclty
instantiated completer should use dedicated model operating DIRECTLY on AB not copied string
list.
*/
auto addressbook = bts::get_profile()->get_addressbook();
QStringList recipient_image_names = getListOfImageNames();
storage->reserve(recipient_image_names.size());
for(const auto& recipient : recipient_image_names)
{
std::string to_string = recipient.toStdString();
//check first to see if we have a dac_id
auto to_contact = addressbook->get_contact_by_dac_id(to_string);
if (!to_contact.valid()) // if not dac_id, check if we have a full name
to_contact = addressbook->get_contact_by_display_name(to_string);
assert(to_contact.valid());
storage->push_back(to_contact->public_key);
}
}
//! [8]
QSize ContactListEdit::sizeHint() const
{
QSize sizehint = QTextEdit::sizeHint();
// sizehint.setHeight(_fitted_height);
return sizehint;
}
void ContactListEdit::fitHeightToDocument()
{
document()->setTextWidth(width());
QSize document_size(document()->size().toSize());
// _fitted_height = document_size.height();
setMaximumHeight(document_size.height());
updateGeometry();
}
void ContactListEdit::resizeEvent(QResizeEvent* resize_event)
{
fitHeightToDocument();
QTextEdit::resizeEvent(resize_event);
}