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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
// Copyright 2009-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "RichTextEditor.h"
#include "Log.h"
#include "MainWindow.h"
#include "XMLTools.h"
#include <QtCore/QMimeData>
#include <QtGui/QImageReader>
#include <QtGui/QPainter>
#include <QtWidgets/QColorDialog>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QToolTip>
#ifdef Q_OS_WIN
# include <shlobj.h>
#endif
// We define a global macro called 'g'. This can lead to issues when included code uses 'g' as a type or parameter name
// (like protobuf 3.7 does). As such, for now, we have to make this our last include.
#include "Global.h"
RichTextHtmlEdit::RichTextHtmlEdit(QWidget *p) : QTextEdit(p) {
m_document = new LogDocument(this);
m_document->setDefaultStyleSheet(qApp->styleSheet());
setDocument(m_document);
}
/* On nix, some programs send utf8, some send wchar_t. Some zeroterminate once, some twice, some not at all.
*/
static QString decodeMimeString(const QByteArray &src) {
if (src.isEmpty())
return QString();
if ((src.length() >= 4) && ((src.length() % sizeof(ushort)) == 0)) {
const ushort *ptr = reinterpret_cast< const ushort * >(src.constData());
int len = static_cast< int >(src.length() / sizeof(ushort));
if ((ptr[0] > 0) && (ptr[0] < 0x7f) && (ptr[1] > 0) && (ptr[1] < 0x7f)) {
while (len && (ptr[len - 1] == 0))
--len;
return QString::fromUtf16(ptr, len);
}
}
if ((sizeof(wchar_t) != sizeof(ushort)) && (src.length() >= static_cast< int >(sizeof(wchar_t)))
&& ((src.length() % sizeof(wchar_t)) == 0)) {
const wchar_t *ptr = reinterpret_cast< const wchar_t * >(src.constData());
int len = static_cast< int >(src.length() / sizeof(wchar_t));
if (*ptr < 0x7f) {
while (len && (ptr[len - 1] == 0))
--len;
return QString::fromWCharArray(ptr, len);
}
}
const char *ptr = src.constData();
int len = src.length();
while (len && (ptr[len - 1] == 0))
--len;
return QString::fromUtf8(ptr, len);
}
/* Try really hard to properly decode Mime into something sane.
*/
void RichTextHtmlEdit::insertFromMimeData(const QMimeData *source) {
QString uri;
QString title;
QRegExp newline(QLatin1String("[\\r\\n]"));
#ifndef QT_NO_DEBUG
qWarning() << "RichTextHtmlEdit::insertFromMimeData" << source->formats();
#endif
if (source->hasImage()) {
QImage img = qvariant_cast< QImage >(source->imageData());
QString html = Log::imageToImg(img);
if (!html.isEmpty())
insertHtml(html);
return;
}
QString mozurl = decodeMimeString(source->data(QLatin1String("text/x-moz-url")));
if (!mozurl.isEmpty()) {
QStringList lines = mozurl.split(newline);
qWarning() << mozurl << lines;
if (lines.count() >= 2) {
uri = lines.at(0);
title = lines.at(1);
}
}
if (uri.isEmpty())
uri = decodeMimeString(source->data(QLatin1String("text/x-moz-url-data")));
if (title.isEmpty())
title = decodeMimeString(source->data(QLatin1String("text/x-moz-url-desc")));
if (uri.isEmpty()) {
QStringList urls;
#ifdef Q_OS_WIN
urls = decodeMimeString(
source->data(QLatin1String("application/x-qt-windows-mime;value=\"UniformResourceLocatorW\"")))
.split(newline);
if (urls.isEmpty())
#endif
urls = decodeMimeString(source->data(QLatin1String("text/uri-list"))).split(newline);
if (!urls.isEmpty())
uri = urls.at(0).trimmed();
}
if (uri.isEmpty()) {
QUrl url(source->text(), QUrl::StrictMode);
if (url.isValid() && !url.isRelative()) {
uri = url.toString();
}
}
#ifdef Q_OS_WIN
if (title.isEmpty()
&& source->hasFormat(QLatin1String("application/x-qt-windows-mime;value=\"FileGroupDescriptorW\""))) {
QByteArray qba = source->data(QLatin1String("application/x-qt-windows-mime;value=\"FileGroupDescriptorW\""));
if (qba.length() == sizeof(FILEGROUPDESCRIPTORW)) {
const FILEGROUPDESCRIPTORW *ptr = reinterpret_cast< const FILEGROUPDESCRIPTORW * >(qba.constData());
title = QString::fromWCharArray(ptr->fgd[0].cFileName);
if (title.endsWith(QLatin1String(".url"), Qt::CaseInsensitive))
title = title.left(title.length() - 4);
}
}
#endif
if (!uri.isEmpty()) {
if (title.isEmpty())
title = uri;
uri = uri.toHtmlEscaped();
title = title.toHtmlEscaped();
insertHtml(QString::fromLatin1("<a href=\"%1\">%2</a>").arg(uri, title));
return;
}
QString html = decodeMimeString(source->data(QLatin1String("text/html")));
if (!html.isEmpty()) {
insertHtml(html);
return;
}
QTextEdit::insertFromMimeData(source);
}
RichTextEditorLink::RichTextEditorLink(const QString &txt, QWidget *p) : QDialog(p) {
setupUi(this);
if (!txt.isEmpty()) {
qleText->setText(txt);
}
}
QString RichTextEditorLink::text() const {
QUrl url(qleUrl->text(), QUrl::StrictMode);
QString txt = qleText->text();
txt = txt.toHtmlEscaped();
if (url.isValid() && !url.isRelative() && !txt.isEmpty()) {
return QString::fromLatin1("<a href=\"%1\">%2</a>").arg(url.toString(), txt);
}
return QString();
}
RichTextEditor::RichTextEditor(QWidget *p) : QTabWidget(p) {
bChanged = false;
bModified = false;
bReadOnly = false;
setupUi(this);
qtbToolBar->addAction(qaBold);
qtbToolBar->addAction(qaItalic);
qtbToolBar->addAction(qaUnderline);
qtbToolBar->addAction(qaColor);
qtbToolBar->addSeparator();
qtbToolBar->addAction(qaLink);
qtbToolBar->addAction(qaImage);
connect(this, SIGNAL(currentChanged(int)), this, SLOT(onCurrentChanged(int)));
updateActions();
qteRichText->setFocus();
qteRichText->installEventFilter(this);
qptePlainText->installEventFilter(this);
}
bool RichTextEditor::isModified() const {
return bModified;
}
void RichTextEditor::on_qaBold_triggered(bool on) {
qteRichText->setFontWeight(on ? QFont::Bold : QFont::Normal);
}
void RichTextEditor::on_qaItalic_triggered(bool on) {
qteRichText->setFontItalic(on);
}
void RichTextEditor::on_qaUnderline_triggered(bool on) {
qteRichText->setFontUnderline(on);
}
void RichTextEditor::on_qaColor_triggered() {
QColor c = QColorDialog::getColor();
if (c.isValid())
qteRichText->setTextColor(c);
}
void RichTextEditor::on_qaLink_triggered() {
QTextCursor qtc = qteRichText->textCursor();
RichTextEditorLink *rtel = new RichTextEditorLink(qtc.selectedText(), this);
if (rtel->exec() == QDialog::Accepted) {
QString html = rtel->text();
if (!html.isEmpty())
qteRichText->insertHtml(html);
}
delete rtel;
}
void RichTextEditor::on_qaImage_triggered() {
QPair< QByteArray, QImage > choice = g.mw->openImageFile();
QByteArray &qba = choice.first;
if (qba.isEmpty())
return;
if ((g.uiImageLength > 0) && (static_cast< unsigned int >(qba.length()) > g.uiImageLength)) {
QMessageBox::warning(this, tr("Failed to load image"),
tr("Image file too large to embed in document. Please use images smaller than %1 kB.")
.arg(g.uiImageLength / 1024));
return;
}
QBuffer qb(&qba);
qb.open(QIODevice::ReadOnly);
QByteArray format = QImageReader::imageFormat(&qb);
qb.close();
qteRichText->insertHtml(Log::imageToImg(format, qba));
}
void RichTextEditor::onCurrentChanged(int index) {
if (!bChanged)
return;
if (index == 1)
richToPlain();
else
qteRichText->setHtml(qptePlainText->toPlainText());
bChanged = false;
}
void RichTextEditor::on_qptePlainText_textChanged() {
bModified = true;
bChanged = true;
}
void RichTextEditor::on_qteRichText_textChanged() {
bModified = true;
bChanged = true;
updateActions();
if (!g.uiMessageLength)
return;
richToPlain();
const QString &plainText = qptePlainText->toPlainText();
bool over = true;
unsigned int imagelength = plainText.length();
if (g.uiMessageLength && imagelength <= g.uiMessageLength) {
over = false;
} else if (g.uiImageLength && imagelength > g.uiImageLength) {
over = true;
} else {
QString qsOut;
QXmlStreamReader qxsr(QString::fromLatin1("<document>%1</document>").arg(plainText));
QXmlStreamWriter qxsw(&qsOut);
while (!qxsr.atEnd()) {
switch (qxsr.readNext()) {
case QXmlStreamReader::Invalid:
return;
case QXmlStreamReader::StartElement: {
if (qxsr.name() == QLatin1String("img")) {
QXmlStreamAttributes attr = qxsr.attributes();
qxsw.writeStartElement(qxsr.namespaceUri().toString(), qxsr.name().toString());
foreach (const QXmlStreamAttribute &a, qxsr.attributes())
if (a.name() != QLatin1String("src"))
qxsw.writeAttribute(a);
} else {
qxsw.writeCurrentToken(qxsr);
}
} break;
default:
qxsw.writeCurrentToken(qxsr);
break;
}
}
over = (static_cast< unsigned int >(qsOut.length()) > g.uiMessageLength);
}
QString tooltip = tr("Message is too long.");
if (!over) {
if (QToolTip::text() == tooltip)
QToolTip::hideText();
} else {
QPoint p = QCursor::pos();
const QRect &r = qteRichText->rect();
if (!r.contains(qteRichText->mapFromGlobal(p)))
p = qteRichText->mapToGlobal(r.center());
QToolTip::showText(p, tooltip, qteRichText);
}
}
void RichTextEditor::on_qteRichText_cursorPositionChanged() {
updateActions();
}
void RichTextEditor::on_qteRichText_currentCharFormatChanged() {
updateActions();
}
void RichTextEditor::updateColor(const QColor &col) {
if (col == qcColor)
return;
qcColor = col;
QRect r(0, 0, 24, 24);
QPixmap qpm(r.size());
QPainter qp(&qpm);
qp.fillRect(r, col);
qp.setPen(col.darker());
qp.drawRect(r.adjusted(0, 0, -1, -1));
qaColor->setIcon(qpm);
}
void RichTextEditor::updateActions() {
qaBold->setChecked(qteRichText->fontWeight() == QFont::Bold);
qaItalic->setChecked(qteRichText->fontItalic());
qaUnderline->setChecked(qteRichText->fontUnderline());
updateColor(qteRichText->textColor());
}
void RichTextEditor::richToPlain() {
QXmlStreamReader reader(qteRichText->toHtml());
QString qsOutput;
QXmlStreamWriter writer(&qsOutput);
int paragraphs = 0;
QMap< QString, QString > def;
def.insert(QLatin1String("margin-top"), QLatin1String("0px"));
def.insert(QLatin1String("margin-bottom"), QLatin1String("0px"));
def.insert(QLatin1String("margin-left"), QLatin1String("0px"));
def.insert(QLatin1String("margin-right"), QLatin1String("0px"));
def.insert(QLatin1String("-qt-block-indent"), QLatin1String("0"));
def.insert(QLatin1String("text-indent"), QLatin1String("0px"));
XMLTools::recurseParse(reader, writer, paragraphs, def);
qsOutput = qsOutput.trimmed();
bool changed;
do {
// Make sure the XML has a root element (would be invalid XML otherwise)
// The "unduplicate" element will be dropped by XMLTools::unduplciateTags
qsOutput = QString::fromLatin1("<unduplicate>%1</unduplicate>").arg(qsOutput);
QXmlStreamReader r(qsOutput);
qsOutput = QString();
QXmlStreamWriter w(&qsOutput);
changed = XMLTools::unduplicateTags(r, w);
qsOutput = qsOutput.trimmed();
} while (changed);
qptePlainText->setPlainText(qsOutput);
}
void RichTextEditor::setText(const QString &txt, bool readonly) {
qtbToolBar->setEnabled(!readonly && g.bAllowHTML);
qtbToolBar->setVisible(!readonly && g.bAllowHTML);
qptePlainText->setReadOnly(readonly || !g.bAllowHTML);
qteRichText->setReadOnly(readonly);
qteRichText->setHtml(txt);
qptePlainText->setPlainText(txt);
bChanged = false;
bModified = false;
bReadOnly = readonly;
}
QString RichTextEditor::text() {
if (bChanged) {
if (currentIndex() == 0)
richToPlain();
else
qteRichText->setHtml(qptePlainText->toPlainText());
}
bChanged = false;
return qptePlainText->toPlainText();
}
bool RichTextEditor::eventFilter(QObject *obj, QEvent *evt) {
if (obj != qptePlainText && obj != qteRichText)
return false;
if (evt->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast< QKeyEvent * >(evt);
if (((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
&& (keyEvent->modifiers() == Qt::ControlModifier)) {
emit accept();
return true;
}
}
return false;
}
bool RichTextImage::isValidImage(const QByteArray &ba, QByteArray &fmt) {
QBuffer qb;
qb.setData(ba);
if (!qb.open(QIODevice::ReadOnly)) {
return false;
}
QByteArray detectedFormat = QImageReader::imageFormat(&qb).toLower();
if (detectedFormat == QByteArray("png") || detectedFormat == QByteArray("jpg")
|| detectedFormat == QByteArray("jpeg") || detectedFormat == QByteArray("gif")) {
fmt = detectedFormat;
return true;
}
return false;
}
|