forked from vedderb/bldc-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrichtextedit.cpp
553 lines (467 loc) · 17.6 KB
/
mrichtextedit.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
** Contact: http://www.hobrasoft.cz/
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file is under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation and appearing
** in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the
** GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
*/
#include "mrichtextedit.h"
#include <QApplication>
#include <QClipboard>
#include <QMimeData>
#include <QFontDatabase>
#include <QInputDialog>
#include <QColorDialog>
#include <QTextList>
#include <QtDebug>
#include <QFileDialog>
#include <QImageReader>
#include <QSettings>
#include <QBuffer>
#include <QUrl>
#include <QPlainTextEdit>
#include <QMenu>
#include <QDialog>
MRichTextEdit::MRichTextEdit(QWidget *parent) : QWidget(parent) {
setupUi(this);
m_lastBlockList = 0;
f_textedit->setTabStopWidth(40);
connect(f_textedit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
this, SLOT(slotCurrentCharFormatChanged(QTextCharFormat)));
connect(f_textedit, SIGNAL(cursorPositionChanged()),
this, SLOT(slotCursorPositionChanged()));
m_fontsize_h1 = 18;
m_fontsize_h2 = 16;
m_fontsize_h3 = 14;
m_fontsize_h4 = 12;
fontChanged(f_textedit->font());
bgColorChanged(f_textedit->textColor());
// paragraph formatting
m_paragraphItems << tr("Standard")
<< tr("Heading 1")
<< tr("Heading 2")
<< tr("Heading 3")
<< tr("Heading 4")
<< tr("Monospace");
f_paragraph->addItems(m_paragraphItems);
connect(f_paragraph, SIGNAL(activated(int)),
this, SLOT(textStyle(int)));
// undo & redo
f_undo->setShortcut(QKeySequence::Undo);
f_redo->setShortcut(QKeySequence::Redo);
connect(f_textedit->document(), SIGNAL(undoAvailable(bool)),
f_undo, SLOT(setEnabled(bool)));
connect(f_textedit->document(), SIGNAL(redoAvailable(bool)),
f_redo, SLOT(setEnabled(bool)));
f_undo->setEnabled(f_textedit->document()->isUndoAvailable());
f_redo->setEnabled(f_textedit->document()->isRedoAvailable());
connect(f_undo, SIGNAL(clicked()), f_textedit, SLOT(undo()));
connect(f_redo, SIGNAL(clicked()), f_textedit, SLOT(redo()));
// cut, copy & paste
f_cut->setShortcut(QKeySequence::Cut);
f_copy->setShortcut(QKeySequence::Copy);
f_paste->setShortcut(QKeySequence::Paste);
f_cut->setEnabled(false);
f_copy->setEnabled(false);
connect(f_cut, SIGNAL(clicked()), f_textedit, SLOT(cut()));
connect(f_copy, SIGNAL(clicked()), f_textedit, SLOT(copy()));
connect(f_paste, SIGNAL(clicked()), f_textedit, SLOT(paste()));
connect(f_textedit, SIGNAL(copyAvailable(bool)), f_cut, SLOT(setEnabled(bool)));
connect(f_textedit, SIGNAL(copyAvailable(bool)), f_copy, SLOT(setEnabled(bool)));
#ifndef QT_NO_CLIPBOARD
connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(slotClipboardDataChanged()));
#endif
// link
f_link->setShortcut(Qt::CTRL + Qt::Key_L);
connect(f_link, SIGNAL(clicked(bool)), this, SLOT(textLink(bool)));
// bold, italic & underline
f_bold->setShortcut(Qt::CTRL + Qt::Key_B);
f_italic->setShortcut(Qt::CTRL + Qt::Key_I);
f_underline->setShortcut(Qt::CTRL + Qt::Key_U);
connect(f_bold, SIGNAL(clicked()), this, SLOT(textBold()));
connect(f_italic, SIGNAL(clicked()), this, SLOT(textItalic()));
connect(f_underline, SIGNAL(clicked()), this, SLOT(textUnderline()));
connect(f_strikeout, SIGNAL(clicked()), this, SLOT(textStrikeout()));
QAction *removeFormat = new QAction(tr("Remove character formatting"), this);
removeFormat->setShortcut(QKeySequence("CTRL+M"));
connect(removeFormat, SIGNAL(triggered()), this, SLOT(textRemoveFormat()));
f_textedit->addAction(removeFormat);
QAction *removeAllFormat = new QAction(tr("Remove all formatting"), this);
connect(removeAllFormat, SIGNAL(triggered()), this, SLOT(textRemoveAllFormat()));
f_textedit->addAction(removeAllFormat);
QAction *textsource = new QAction(tr("Edit document source"), this);
textsource->setShortcut(QKeySequence("CTRL+O"));
connect(textsource, SIGNAL(triggered()), this, SLOT(textSource()));
f_textedit->addAction(textsource);
QMenu *menu = new QMenu(this);
menu->addAction(removeAllFormat);
menu->addAction(removeFormat);
menu->addAction(textsource);
f_menu->setMenu(menu);
f_menu->setPopupMode(QToolButton::InstantPopup);
// lists
f_list_bullet->setShortcut(Qt::CTRL + Qt::Key_Minus);
f_list_ordered->setShortcut(Qt::CTRL + Qt::Key_Equal);
connect(f_list_bullet, SIGNAL(clicked(bool)), this, SLOT(listBullet(bool)));
connect(f_list_ordered, SIGNAL(clicked(bool)), this, SLOT(listOrdered(bool)));
// indentation
f_indent_dec->setShortcut(Qt::CTRL + Qt::Key_Comma);
f_indent_inc->setShortcut(Qt::CTRL + Qt::Key_Period);
connect(f_indent_inc, SIGNAL(clicked()), this, SLOT(increaseIndentation()));
connect(f_indent_dec, SIGNAL(clicked()), this, SLOT(decreaseIndentation()));
// font size
QFontDatabase db;
foreach(int size, db.standardSizes())
f_fontsize->addItem(QString::number(size));
connect(f_fontsize, SIGNAL(activated(QString)),
this, SLOT(textSize(QString)));
f_fontsize->setCurrentIndex(f_fontsize->findText(QString::number(QApplication::font()
.pointSize())));
// text background color
QPixmap pix(16, 16);
pix.fill(QApplication::palette().background().color());
f_bgcolor->setIcon(pix);
connect(f_bgcolor, SIGNAL(clicked()), this, SLOT(textBgColor()));
// images
connect(f_image, SIGNAL(clicked()), this, SLOT(insertImage()));
}
void MRichTextEdit::textSource() {
QDialog *dialog = new QDialog(this);
QPlainTextEdit *pte = new QPlainTextEdit(dialog);
pte->setPlainText( f_textedit->toHtml() );
QGridLayout *gl = new QGridLayout(dialog);
gl->addWidget(pte,0,0,1,1);
dialog->setWindowTitle(tr("Document source"));
dialog->setMinimumWidth (400);
dialog->setMinimumHeight(600);
dialog->exec();
f_textedit->setHtml(pte->toPlainText());
delete dialog;
}
void MRichTextEdit::textRemoveFormat() {
QTextCharFormat fmt;
fmt.setFontWeight(QFont::Normal);
fmt.setFontUnderline (false);
fmt.setFontStrikeOut (false);
fmt.setFontItalic (false);
fmt.setFontPointSize (9);
// fmt.setFontFamily ("Helvetica");
// fmt.setFontStyleHint (QFont::SansSerif);
// fmt.setFontFixedPitch (true);
f_bold ->setChecked(false);
f_underline ->setChecked(false);
f_italic ->setChecked(false);
f_strikeout ->setChecked(false);
f_fontsize ->setCurrentIndex(f_fontsize->findText("9"));
// QTextBlockFormat bfmt = cursor.blockFormat();
// bfmt->setIndent(0);
fmt.clearBackground();
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textRemoveAllFormat() {
f_bold ->setChecked(false);
f_underline ->setChecked(false);
f_italic ->setChecked(false);
f_strikeout ->setChecked(false);
f_fontsize ->setCurrentIndex(f_fontsize->findText("9"));
QString text = f_textedit->toPlainText();
f_textedit->setPlainText(text);
}
void MRichTextEdit::textBold() {
QTextCharFormat fmt;
fmt.setFontWeight(f_bold->isChecked() ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::focusInEvent(QFocusEvent *) {
f_textedit->setFocus(Qt::TabFocusReason);
}
void MRichTextEdit::textUnderline() {
QTextCharFormat fmt;
fmt.setFontUnderline(f_underline->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textItalic() {
QTextCharFormat fmt;
fmt.setFontItalic(f_italic->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textStrikeout() {
QTextCharFormat fmt;
fmt.setFontStrikeOut(f_strikeout->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textSize(const QString &p) {
qreal pointSize = p.toFloat();
if (p.toFloat() > 0) {
QTextCharFormat fmt;
fmt.setFontPointSize(pointSize);
mergeFormatOnWordOrSelection(fmt);
}
}
void MRichTextEdit::textLink(bool checked) {
bool unlink = false;
QTextCharFormat fmt;
if (checked) {
QString url = f_textedit->currentCharFormat().anchorHref();
bool ok;
QString newUrl = QInputDialog::getText(this, tr("Create a link"),
tr("Link URL:"), QLineEdit::Normal,
url,
&ok);
if (ok) {
fmt.setAnchor(true);
fmt.setAnchorHref(newUrl);
fmt.setForeground(QApplication::palette().color(QPalette::Link));
fmt.setFontUnderline(true);
} else {
unlink = true;
}
} else {
unlink = true;
}
if (unlink) {
fmt.setAnchor(false);
fmt.setForeground(QApplication::palette().color(QPalette::Text));
fmt.setFontUnderline(false);
}
mergeFormatOnWordOrSelection(fmt);
}
void MRichTextEdit::textStyle(int index) {
QTextCursor cursor = f_textedit->textCursor();
cursor.beginEditBlock();
// standard
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::BlockUnderCursor);
}
QTextCharFormat fmt;
cursor.setCharFormat(fmt);
f_textedit->setCurrentCharFormat(fmt);
if (index == ParagraphHeading1
|| index == ParagraphHeading2
|| index == ParagraphHeading3
|| index == ParagraphHeading4 ) {
if (index == ParagraphHeading1) {
fmt.setFontPointSize(m_fontsize_h1);
}
if (index == ParagraphHeading2) {
fmt.setFontPointSize(m_fontsize_h2);
}
if (index == ParagraphHeading3) {
fmt.setFontPointSize(m_fontsize_h3);
}
if (index == ParagraphHeading4) {
fmt.setFontPointSize(m_fontsize_h4);
}
if (index == ParagraphHeading2 || index == ParagraphHeading4) {
fmt.setFontItalic(true);
}
fmt.setFontWeight(QFont::Bold);
}
if (index == ParagraphMonospace) {
fmt = cursor.charFormat();
fmt.setFontFamily("Monospace");
fmt.setFontStyleHint(QFont::Monospace);
fmt.setFontFixedPitch(true);
}
cursor.setCharFormat(fmt);
f_textedit->setCurrentCharFormat(fmt);
cursor.endEditBlock();
}
void MRichTextEdit::textBgColor() {
QColor col = QColorDialog::getColor(f_textedit->textBackgroundColor(), this);
QTextCursor cursor = f_textedit->textCursor();
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::WordUnderCursor);
}
QTextCharFormat fmt = cursor.charFormat();
if (col.isValid()) {
fmt.setBackground(col);
} else {
fmt.clearBackground();
}
cursor.setCharFormat(fmt);
f_textedit->setCurrentCharFormat(fmt);
bgColorChanged(col);
}
void MRichTextEdit::listBullet(bool checked) {
if (checked) {
f_list_ordered->setChecked(false);
}
list(checked, QTextListFormat::ListDisc);
}
void MRichTextEdit::listOrdered(bool checked) {
if (checked) {
f_list_bullet->setChecked(false);
}
list(checked, QTextListFormat::ListDecimal);
}
void MRichTextEdit::list(bool checked, QTextListFormat::Style style) {
QTextCursor cursor = f_textedit->textCursor();
cursor.beginEditBlock();
if (!checked) {
QTextBlockFormat obfmt = cursor.blockFormat();
QTextBlockFormat bfmt;
bfmt.setIndent(obfmt.indent());
cursor.setBlockFormat(bfmt);
} else {
QTextListFormat listFmt;
if (cursor.currentList()) {
listFmt = cursor.currentList()->format();
}
listFmt.setStyle(style);
cursor.createList(listFmt);
}
cursor.endEditBlock();
}
void MRichTextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format) {
QTextCursor cursor = f_textedit->textCursor();
if (!cursor.hasSelection()) {
cursor.select(QTextCursor::WordUnderCursor);
}
cursor.mergeCharFormat(format);
f_textedit->mergeCurrentCharFormat(format);
f_textedit->setFocus(Qt::TabFocusReason);
}
void MRichTextEdit::slotCursorPositionChanged() {
QTextList *l = f_textedit->textCursor().currentList();
if (m_lastBlockList && (l == m_lastBlockList || (l != 0 && m_lastBlockList != 0
&& l->format().style() == m_lastBlockList->format().style()))) {
return;
}
m_lastBlockList = l;
if (l) {
QTextListFormat lfmt = l->format();
if (lfmt.style() == QTextListFormat::ListDisc) {
f_list_bullet->setChecked(true);
f_list_ordered->setChecked(false);
} else if (lfmt.style() == QTextListFormat::ListDecimal) {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(true);
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
}
void MRichTextEdit::fontChanged(const QFont &f) {
f_fontsize->setCurrentIndex(f_fontsize->findText(QString::number(f.pointSize())));
f_bold->setChecked(f.bold());
f_italic->setChecked(f.italic());
f_underline->setChecked(f.underline());
f_strikeout->setChecked(f.strikeOut());
if (f.pointSize() == m_fontsize_h1) {
f_paragraph->setCurrentIndex(ParagraphHeading1);
} else if (f.pointSize() == m_fontsize_h2) {
f_paragraph->setCurrentIndex(ParagraphHeading2);
} else if (f.pointSize() == m_fontsize_h3) {
f_paragraph->setCurrentIndex(ParagraphHeading3);
} else if (f.pointSize() == m_fontsize_h4) {
f_paragraph->setCurrentIndex(ParagraphHeading4);
} else {
if (f.fixedPitch() && f.family() == "Monospace") {
f_paragraph->setCurrentIndex(ParagraphMonospace);
} else {
f_paragraph->setCurrentIndex(ParagraphStandard);
}
}
if (f_textedit->textCursor().currentList()) {
QTextListFormat lfmt = f_textedit->textCursor().currentList()->format();
if (lfmt.style() == QTextListFormat::ListDisc) {
f_list_bullet->setChecked(true);
f_list_ordered->setChecked(false);
} else if (lfmt.style() == QTextListFormat::ListDecimal) {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(true);
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
} else {
f_list_bullet->setChecked(false);
f_list_ordered->setChecked(false);
}
}
void MRichTextEdit::bgColorChanged(const QColor &c) {
QPixmap pix(16, 16);
if (c.isValid()) {
pix.fill(c);
} else {
pix.fill(QApplication::palette().background().color());
}
f_bgcolor->setIcon(pix);
}
void MRichTextEdit::slotCurrentCharFormatChanged(const QTextCharFormat &format) {
fontChanged(format.font());
bgColorChanged((format.background().isOpaque()) ? format.background().color() : QColor());
f_link->setChecked(format.isAnchor());
}
void MRichTextEdit::slotClipboardDataChanged() {
#ifndef QT_NO_CLIPBOARD
if (const QMimeData *md = QApplication::clipboard()->mimeData())
f_paste->setEnabled(md->hasText());
#endif
}
QString MRichTextEdit::toHtml() const {
QString s = f_textedit->toHtml();
// convert emails to links
s = s.replace(QRegExp("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)([a-zA-Z\\d]+@[a-zA-Z\\d]+\\.[a-zA-Z]+)"), "\\1<a href=\"mailto:\\2\">\\2</a>");
// convert links
s = s.replace(QRegExp("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)((?:https?|ftp|file)://[^\\s'\"<>]+)"), "\\1<a href=\"\\2\">\\2</a>");
return s;
}
void MRichTextEdit::increaseIndentation() {
indent(+1);
}
void MRichTextEdit::decreaseIndentation() {
indent(-1);
}
void MRichTextEdit::indent(int delta) {
QTextCursor cursor = f_textedit->textCursor();
cursor.beginEditBlock();
QTextBlockFormat bfmt = cursor.blockFormat();
int ind = bfmt.indent();
if (ind + delta >= 0) {
bfmt.setIndent(ind + delta);
}
cursor.setBlockFormat(bfmt);
cursor.endEditBlock();
}
void MRichTextEdit::setText(const QString& text) {
if (text.isEmpty()) {
setPlainText(text);
return;
}
if (text[0] == '<') {
setHtml(text);
} else {
setPlainText(text);
}
}
void MRichTextEdit::insertImage() {
QSettings s;
QString attdir = s.value("general/filedialog-path").toString();
QString file = QFileDialog::getOpenFileName(this,
tr("Select an image"),
attdir,
tr("Images (*.png *.xpm *.jpg *.gif *.bmp);; All (*)"));
QImage image = QImageReader(file).read();
f_textedit->dropImage(image, QFileInfo(file).suffix().toUpper().toLocal8Bit().data());
}