| 1 | // QCodeEditor |
| 2 | #include <QLineNumberArea> |
| 3 | #include <QSyntaxStyle> |
| 4 | #include <QCodeEditor> |
| 5 | |
| 6 | // Qt |
| 7 | #include <QTextEdit> |
| 8 | #include <QPainter> |
| 9 | #include <QPaintEvent> |
| 10 | #include <QTextBlock> |
| 11 | #include <QScrollBar> |
| 12 | #include <QAbstractTextDocumentLayout> |
| 13 | |
| 14 | QLineNumberArea::QLineNumberArea(QCodeEditor* parent) : |
| 15 | QWidget(parent), |
| 16 | m_syntaxStyle(nullptr), |
| 17 | m_codeEditParent(parent) |
| 18 | { |
| 19 | |
| 20 | } |
| 21 | |
| 22 | QSize QLineNumberArea::sizeHint() const |
| 23 | { |
| 24 | if (m_codeEditParent == nullptr) |
| 25 | { |
| 26 | return QWidget::sizeHint(); |
| 27 | } |
| 28 | |
| 29 | // Calculating width |
| 30 | int digits = 1; |
| 31 | int max = qMax(1, m_codeEditParent->document()->blockCount()); |
| 32 | while (max >= 10) { |
| 33 | max /= 10; |
| 34 | ++digits; |
| 35 | } |
| 36 | |
| 37 | #if QT_VERSION >= 0x050B00 |
| 38 | int space = 13 + m_codeEditParent->fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits; |
| 39 | #else |
| 40 | int space = 13 + m_codeEditParent->fontMetrics().width(QLatin1Char('9')) * digits; |
| 41 | #endif |
| 42 | |
| 43 | return {space, 0}; |
| 44 | } |
| 45 | |
| 46 | void QLineNumberArea::setSyntaxStyle(QSyntaxStyle* style) |
| 47 | { |
| 48 | m_syntaxStyle = style; |
| 49 | } |
| 50 | |
| 51 | QSyntaxStyle* QLineNumberArea::syntaxStyle() const |
| 52 | { |
| 53 | return m_syntaxStyle; |
| 54 | } |
| 55 | |
| 56 | void QLineNumberArea::paintEvent(QPaintEvent* event) |
| 57 | { |
| 58 | QPainter painter(this); |
| 59 | |
| 60 | // Clearing rect to update |
| 61 | painter.fillRect( |
| 62 | event->rect(), |
| 63 | m_syntaxStyle->getFormat("Text").background().color() |
| 64 | ); |
| 65 | |
| 66 | auto blockNumber = m_codeEditParent->getFirstVisibleBlock(); |
| 67 | auto block = m_codeEditParent->document()->findBlockByNumber(blockNumber); |
| 68 | auto top = (int) m_codeEditParent->document()->documentLayout()->blockBoundingRect(block).translated(0, -m_codeEditParent->verticalScrollBar()->value()).top(); |
| 69 | auto bottom = top + (int) m_codeEditParent->document()->documentLayout()->blockBoundingRect(block).height(); |
| 70 | |
| 71 | auto currentLine = m_syntaxStyle->getFormat("CurrentLineNumber").foreground().color(); |
| 72 | auto otherLines = m_syntaxStyle->getFormat("LineNumber").foreground().color(); |
| 73 | |
| 74 | painter.setFont(m_codeEditParent->font()); |
| 75 | |
| 76 | while (block.isValid() && top <= event->rect().bottom()) |
| 77 | { |
| 78 | if (block.isVisible() && bottom >= event->rect().top()) |
| 79 | { |
| 80 | QString number = QString::number(blockNumber + 1); |
| 81 | |
| 82 | auto isCurrentLine = m_codeEditParent->textCursor().blockNumber() == blockNumber; |
| 83 | painter.setPen(isCurrentLine ? currentLine : otherLines); |
| 84 | |
| 85 | painter.drawText( |
| 86 | -5, |
| 87 | top, |
| 88 | sizeHint().width(), |
| 89 | m_codeEditParent->fontMetrics().height(), |
| 90 | Qt::AlignRight, |
| 91 | number |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | block = block.next(); |
| 96 | top = bottom; |
| 97 | bottom = top + (int) m_codeEditParent->document()->documentLayout()->blockBoundingRect(block).height(); |
| 98 | ++blockNumber; |
| 99 | } |
| 100 | } |