-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathframedTextAttr.cpp
More file actions
64 lines (50 loc) · 1.72 KB
/
framedTextAttr.cpp
File metadata and controls
64 lines (50 loc) · 1.72 KB
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
/**
* example code to complete my answer to my question on StackOverflow: see
* http://stackoverflow.com/questions/19232882/nice-text-formatting-in-qtextedit-like-qtcreator-does
* author: CapelliC @2015
* license: MIT
*/
#include "framedTextAttr.h"
#include <QDebug>
framedTextAttr::framedTextAttr(QObject *parent) : QObject(parent) {
}
QSizeF framedTextAttr::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) {
Q_UNUSED(doc)
Q_UNUSED(posInDocument)
Q_ASSERT(format.type() == format.CharFormat);
return QSizeF(0, 0);
}
void framedTextAttr::drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) {
Q_UNUSED(doc)
Q_UNUSED(posInDocument)
Q_ASSERT(format.type() == format.CharFormat);
const QTextCharFormat &tf = reinterpret_cast<const QTextCharFormat&>(format);
QFont fn = tf.font();
QFontMetrics fm(fn);
QString s = format.property(prop()).toString();
QSizeF sz = fm.boundingRect(s).size();
QRectF Rect(rect.topLeft(), sz);
Rect.moveTop(rect.top() - sz.height());
Rect.adjust(0, 3, 0, 3);
painter->drawRoundedRect(Rect, 4, 4);
}
void framedTextAttr::frame(QTextCursor c) {
QTextCharFormat f;
f.setObjectType(type());
f.setProperty(prop(), c.selectedText());
//c.clearSelection();
if (c.selectionEnd() > c.selectionStart()) {
c.setPosition(c.selectionStart());
} else {
c.setPosition(c.selectionEnd());
}
c.insertText(QString(QChar::ObjectReplacementCharacter), f);
}
bool framedTextAttr::unframe(QTextCursor c) {
auto f = c.charFormat();
if (f.type() == type()) {
c.deleteChar();
return true;
}
return false;
}