add support for add a highlight for the currently active input line to the line number display
This commit is contained in:
@ -28,7 +28,7 @@
|
||||
#include <QTextBlock>
|
||||
#include <QUrl>
|
||||
|
||||
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
|
||||
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent), highlight(-1)
|
||||
{
|
||||
help_action = new QShortcut(QKeySequence::fromString("Ctrl+?"), parent);
|
||||
connect(help_action, &QShortcut::activated, this, &CodeEditor::get_help);
|
||||
@ -83,7 +83,7 @@ int CodeEditor::lineNumberAreaWidth()
|
||||
++digits;
|
||||
}
|
||||
|
||||
int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
|
||||
int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * (digits + 2);
|
||||
|
||||
return space;
|
||||
}
|
||||
@ -94,6 +94,12 @@ void CodeEditor::setFont(const QFont &newfont)
|
||||
document()->setDefaultFont(newfont);
|
||||
}
|
||||
|
||||
void CodeEditor::setHighlight(int block)
|
||||
{
|
||||
highlight = block;
|
||||
repaint();
|
||||
}
|
||||
|
||||
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
|
||||
{
|
||||
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
|
||||
@ -167,15 +173,22 @@ void CodeEditor::highlightCurrentLine()
|
||||
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(lineNumberArea);
|
||||
painter.fillRect(event->rect(), Qt::lightGray);
|
||||
QTextBlock block = firstVisibleBlock();
|
||||
int blockNumber = block.blockNumber();
|
||||
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
|
||||
int bottom = top + qRound(blockBoundingRect(block).height());
|
||||
|
||||
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
|
||||
int bottom = top + qRound(blockBoundingRect(block).height());
|
||||
while (block.isValid() && top <= event->rect().bottom()) {
|
||||
if (block.isVisible() && bottom >= event->rect().top()) {
|
||||
QString number = QString::number(blockNumber + 1);
|
||||
painter.setPen(Qt::black);
|
||||
QString number = QString::number(blockNumber + 1) + " ";
|
||||
if ((highlight < 0) || (blockNumber != highlight)) {
|
||||
painter.setPen(Qt::black);
|
||||
} else {
|
||||
number = QString(">") + QString::number(blockNumber + 1) + "<";
|
||||
painter.fillRect(0, top, lineNumberArea->width(), fontMetrics().height(),
|
||||
Qt::darkRed);
|
||||
painter.setPen(Qt::white);
|
||||
}
|
||||
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
|
||||
Qt::AlignRight, number);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user