|
| 1 | +#include <QtWidgets> |
| 2 | + |
| 3 | +#include "codeeditor.h" |
| 4 | + |
| 5 | + |
| 6 | +Highlighter::Highlighter(QTextDocument *parent) |
| 7 | + : QSyntaxHighlighter(parent) |
| 8 | +{ |
| 9 | + HighlightingRule rule; |
| 10 | + |
| 11 | + keywordFormat.setForeground(Qt::darkBlue); |
| 12 | + keywordFormat.setFontWeight(QFont::Bold); |
| 13 | + QStringList keywordPatterns; |
| 14 | + keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b" |
| 15 | + << "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b" |
| 16 | + << "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b" |
| 17 | + << "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b" |
| 18 | + << "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b" |
| 19 | + << "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b" |
| 20 | + << "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b" |
| 21 | + << "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b" |
| 22 | + << "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b" |
| 23 | + << "\\bvoid\\b" << "\\bvolatile\\b" << "\\bbool\\b"; |
| 24 | + foreach (const QString &pattern, keywordPatterns) { |
| 25 | + rule.pattern = QRegularExpression(pattern); |
| 26 | + rule.format = keywordFormat; |
| 27 | + highlightingRules.append(rule); |
| 28 | + } |
| 29 | + |
| 30 | + classFormat.setFontWeight(QFont::Bold); |
| 31 | + classFormat.setForeground(Qt::darkMagenta); |
| 32 | + rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b"); |
| 33 | + rule.format = classFormat; |
| 34 | + highlightingRules.append(rule); |
| 35 | + |
| 36 | + quotationFormat.setForeground(Qt::darkGreen); |
| 37 | + rule.pattern = QRegularExpression("\".*\""); |
| 38 | + rule.format = quotationFormat; |
| 39 | + highlightingRules.append(rule); |
| 40 | + |
| 41 | + functionFormat.setFontItalic(true); |
| 42 | + functionFormat.setForeground(Qt::blue); |
| 43 | + rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()"); |
| 44 | + rule.format = functionFormat; |
| 45 | + highlightingRules.append(rule); |
| 46 | + |
| 47 | + singleLineCommentFormat.setForeground(Qt::gray); |
| 48 | + rule.pattern = QRegularExpression("//[^\n]*"); |
| 49 | + rule.format = singleLineCommentFormat; |
| 50 | + highlightingRules.append(rule); |
| 51 | + |
| 52 | + multiLineCommentFormat.setForeground(Qt::gray); |
| 53 | + |
| 54 | + commentStartExpression = QRegularExpression("/\\*"); |
| 55 | + commentEndExpression = QRegularExpression("\\*/"); |
| 56 | +} |
| 57 | + |
| 58 | +void Highlighter::highlightBlock(const QString &text) |
| 59 | +{ |
| 60 | + foreach (const HighlightingRule &rule, highlightingRules) { |
| 61 | + QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); |
| 62 | + while (matchIterator.hasNext()) { |
| 63 | + QRegularExpressionMatch match = matchIterator.next(); |
| 64 | + setFormat(match.capturedStart(), match.capturedLength(), rule.format); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + setCurrentBlockState(0); |
| 69 | + |
| 70 | + int startIndex = 0; |
| 71 | + if (previousBlockState() != 1) |
| 72 | + startIndex = text.indexOf(commentStartExpression); |
| 73 | + |
| 74 | + while (startIndex >= 0) { |
| 75 | + QRegularExpressionMatch match = commentEndExpression.match(text, startIndex); |
| 76 | + int endIndex = match.capturedStart(); |
| 77 | + int commentLength = 0; |
| 78 | + if (endIndex == -1) { |
| 79 | + setCurrentBlockState(1); |
| 80 | + commentLength = text.length() - startIndex; |
| 81 | + } else { |
| 82 | + commentLength = endIndex - startIndex |
| 83 | + + match.capturedLength(); |
| 84 | + } |
| 85 | + setFormat(startIndex, commentLength, multiLineCommentFormat); |
| 86 | + startIndex = text.indexOf(commentStartExpression, startIndex + commentLength); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) |
| 92 | +{ |
| 93 | + lineNumberArea = new LineNumberArea(this); |
| 94 | + highlighter = new Highlighter(this->document()); |
| 95 | + mErrorPosition = -1; |
| 96 | + |
| 97 | + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); |
| 98 | + connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); |
| 99 | + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); |
| 100 | + |
| 101 | + updateLineNumberAreaWidth(0); |
| 102 | +} |
| 103 | + |
| 104 | +static int getPos(const QString &fileData, int lineNumber) |
| 105 | +{ |
| 106 | + for (int pos = 0, line = 1; pos < fileData.size(); ++pos) { |
| 107 | + if (fileData[pos] != '\n') |
| 108 | + continue; |
| 109 | + ++line; |
| 110 | + if (line == lineNumber) |
| 111 | + return pos + 1; |
| 112 | + } |
| 113 | + return fileData.size(); |
| 114 | +} |
| 115 | + |
| 116 | +void CodeEditor::setErrorLine(int errorLine) |
| 117 | +{ |
| 118 | + mErrorPosition = getPos(toPlainText(), errorLine); |
| 119 | + QTextCursor tc = textCursor(); |
| 120 | + tc.setPosition(mErrorPosition); |
| 121 | + setTextCursor(tc); |
| 122 | + centerCursor(); |
| 123 | +} |
| 124 | + |
| 125 | +int CodeEditor::lineNumberAreaWidth() |
| 126 | +{ |
| 127 | + int digits = 1; |
| 128 | + int max = qMax(1, blockCount()); |
| 129 | + while (max >= 10) { |
| 130 | + max /= 10; |
| 131 | + ++digits; |
| 132 | + } |
| 133 | + |
| 134 | + int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; |
| 135 | + return space; |
| 136 | +} |
| 137 | + |
| 138 | +void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) |
| 139 | +{ |
| 140 | + setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); |
| 141 | +} |
| 142 | + |
| 143 | +void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) |
| 144 | +{ |
| 145 | + if (dy) |
| 146 | + lineNumberArea->scroll(0, dy); |
| 147 | + else |
| 148 | + lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); |
| 149 | + |
| 150 | + if (rect.contains(viewport()->rect())) |
| 151 | + updateLineNumberAreaWidth(0); |
| 152 | +} |
| 153 | + |
| 154 | +void CodeEditor::resizeEvent(QResizeEvent *event) |
| 155 | +{ |
| 156 | + QPlainTextEdit::resizeEvent(event); |
| 157 | + QRect cr = contentsRect(); |
| 158 | + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); |
| 159 | +} |
| 160 | + |
| 161 | +void CodeEditor::highlightCurrentLine() |
| 162 | +{ |
| 163 | + QTextCursor tc = textCursor(); |
| 164 | + tc.setPosition(mErrorPosition); |
| 165 | + setTextCursor(tc); |
| 166 | + |
| 167 | + QList<QTextEdit::ExtraSelection> extraSelections; |
| 168 | + |
| 169 | + QTextEdit::ExtraSelection selection; |
| 170 | + |
| 171 | + QColor lineColor = QColor(255,220,220); |
| 172 | + |
| 173 | + selection.format.setBackground(lineColor); |
| 174 | + selection.format.setProperty(QTextFormat::FullWidthSelection, true); |
| 175 | + selection.cursor = textCursor(); |
| 176 | + selection.cursor.clearSelection(); |
| 177 | + extraSelections.append(selection); |
| 178 | + |
| 179 | + setExtraSelections(extraSelections); |
| 180 | +} |
| 181 | + |
| 182 | +void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) |
| 183 | +{ |
| 184 | + QPainter painter(lineNumberArea); |
| 185 | + painter.fillRect(event->rect(), QColor(240,240,240)); |
| 186 | + |
| 187 | + QTextBlock block = firstVisibleBlock(); |
| 188 | + int blockNumber = block.blockNumber(); |
| 189 | + int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); |
| 190 | + int bottom = top + (int) blockBoundingRect(block).height(); |
| 191 | + |
| 192 | + while (block.isValid() && top <= event->rect().bottom()) { |
| 193 | + if (block.isVisible() && bottom >= event->rect().top()) { |
| 194 | + QString number = QString::number(blockNumber + 1); |
| 195 | + painter.setPen(Qt::black); |
| 196 | + painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), |
| 197 | + Qt::AlignRight, number); |
| 198 | + } |
| 199 | + |
| 200 | + block = block.next(); |
| 201 | + top = bottom; |
| 202 | + bottom = top + (int) blockBoundingRect(block).height(); |
| 203 | + ++blockNumber; |
| 204 | + } |
| 205 | +} |
0 commit comments