| 1 | // QCodeEditor |
| 2 | #include <QFramedTextAttribute> |
| 3 | #include <QSyntaxStyle> |
| 4 | |
| 5 | // Qt |
| 6 | #include <QFontMetrics> |
| 7 | #include <QPainter> |
| 8 | #include <QDebug> |
| 9 | #include <QTextBlock> |
| 10 | |
| 11 | int QFramedTextAttribute::type() |
| 12 | { |
| 13 | return QTextFormat::UserFormat + 1; |
| 14 | } |
| 15 | |
| 16 | QFramedTextAttribute::QFramedTextAttribute(QObject* parent) : |
| 17 | QObject(parent), |
| 18 | m_style(nullptr) |
| 19 | { |
| 20 | |
| 21 | } |
| 22 | |
| 23 | void QFramedTextAttribute::setSyntaxStyle(QSyntaxStyle* style) |
| 24 | { |
| 25 | m_style = style; |
| 26 | } |
| 27 | |
| 28 | QSyntaxStyle* QFramedTextAttribute::syntaxStyle() const |
| 29 | { |
| 30 | return m_style; |
| 31 | } |
| 32 | |
| 33 | QSizeF QFramedTextAttribute::intrinsicSize(QTextDocument*, int, const QTextFormat&) |
| 34 | { |
| 35 | return {0, 0}; |
| 36 | } |
| 37 | |
| 38 | void QFramedTextAttribute::drawObject(QPainter* painter, |
| 39 | const QRectF& rect, |
| 40 | QTextDocument*, |
| 41 | int, |
| 42 | const QTextFormat& format) |
| 43 | { |
| 44 | // Casting |
| 45 | auto textCharFormat = reinterpret_cast<const QTextCharFormat&>(format); |
| 46 | |
| 47 | // Getting font data |
| 48 | auto font = textCharFormat.font(); |
| 49 | QFontMetrics metrics(font); |
| 50 | |
| 51 | // Getting required size |
| 52 | auto string = format.property(FramedString).toString(); |
| 53 | auto stringSize = metrics.boundingRect(string).size(); |
| 54 | |
| 55 | // Creating frame rect |
| 56 | QRectF drawRect(rect.topLeft(), stringSize); |
| 57 | drawRect.moveTop(rect.top() - stringSize.height()); |
| 58 | drawRect.adjust(0, 4, 0, 4); |
| 59 | |
| 60 | // Drawing |
| 61 | painter->setPen(m_style->getFormat("Occurrences").background().color()); |
| 62 | painter->setRenderHint(QPainter::Antialiasing); |
| 63 | painter->drawRoundedRect(drawRect, 4, 4); |
| 64 | } |
| 65 | |
| 66 | void QFramedTextAttribute::frame(QTextCursor cursor) |
| 67 | { |
| 68 | auto text = cursor.document()->findBlockByNumber(cursor.blockNumber()).text(); |
| 69 | |
| 70 | QTextCharFormat format; |
| 71 | format.setObjectType(type()); |
| 72 | format.setProperty(FramedString, cursor.selectedText()); |
| 73 | |
| 74 | if (cursor.selectionEnd() > cursor.selectionStart()) |
| 75 | { |
| 76 | cursor.setPosition(cursor.selectionStart()); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | cursor.setPosition(cursor.selectionEnd()); |
| 81 | } |
| 82 | |
| 83 | cursor.insertText( |
| 84 | QString(QChar::ObjectReplacementCharacter), |
| 85 | format |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | void QFramedTextAttribute::clear(QTextCursor cursor) |
| 90 | { |
| 91 | auto doc = cursor.document(); |
| 92 | |
| 93 | for (auto blockIndex = 0; |
| 94 | blockIndex < doc->blockCount(); |
| 95 | ++blockIndex) |
| 96 | { |
| 97 | auto block = doc->findBlockByNumber(blockIndex); |
| 98 | |
| 99 | auto formats = block.textFormats(); |
| 100 | int offset = 0; |
| 101 | |
| 102 | for (auto& format : formats) |
| 103 | { |
| 104 | if (format.format.objectType() == type()) |
| 105 | { |
| 106 | cursor.setPosition(block.position() + format.start - offset); |
| 107 | cursor.deleteChar(); |
| 108 | ++offset; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |