41 connect(
this, SIGNAL(blockCountChanged(
int)),
this, SLOT(updateLineNumberAreaWidth(
int)));
42 connect(
this, SIGNAL(updateRequest(QRect,
int)),
this, SLOT(updateLineNumberArea(QRect,
int)));
43 connect(
this, SIGNAL(cursorPositionChanged()),
this, SLOT(highlightCurrentLine()));
45 updateLineNumberAreaWidth(0);
46 highlightCurrentLine();
56 int max = qMax(1, blockCount());
62 int space = 3 + fontMetrics().width(QLatin1Char(
'9')) * digits;
72 void CodeTextEdit::updateLineNumberAreaWidth(
int newBlockCount)
74 Q_UNUSED(newBlockCount);
81 void CodeTextEdit::updateLineNumberArea(
const QRect &rect,
int dy)
84 m_lineNumberArea->scroll(0, dy);
87 m_lineNumberArea->update(0, rect.y(), m_lineNumberArea->width(), rect.height());
90 if (rect.contains(viewport()->rect())) {
91 updateLineNumberAreaWidth(0);
100 QPlainTextEdit::resizeEvent(e);
102 QRect cr = contentsRect();
103 m_lineNumberArea->setGeometry(QRect(cr.left(), cr.top(),
lineNumberAreaWidth(), cr.height()));
109 void CodeTextEdit::highlightCurrentLine()
114 QTextEdit::ExtraSelection selection;
116 QColor lineColor = QColor(Qt::yellow).lighter(160);
118 selection.format.setBackground(lineColor);
119 selection.format.setProperty(QTextFormat::FullWidthSelection,
true);
120 selection.cursor = textCursor();
121 selection.cursor.clearSelection();
122 extraSelections.append(selection);
125 setExtraSelections(extraSelections);
133 QPainter painter(m_lineNumberArea);
134 painter.fillRect(event->rect(), Qt::lightGray);
136 QTextBlock block = firstVisibleBlock();
137 int blockNumber = block.blockNumber();
138 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
139 int bottom = top + (int) blockBoundingRect(block).height();
141 while (block.isValid() && top <=
event->rect().bottom()) {
142 if (block.isVisible() && bottom >=
event->rect().top()) {
143 QString number = QString::number(blockNumber + 1);
144 painter.setPen(Qt::black);
145 painter.drawText(0, top, m_lineNumberArea->width(), fontMetrics().height(),
146 Qt::AlignRight, number);
149 block = block.next();
151 bottom = top + (int) blockBoundingRect(block).height();
void resizeEvent(QResizeEvent *event)
void lineNumberAreaPaintEvent(QPaintEvent *event)
CodeTextEdit(QWidget *parent=0)
Constructor.
int lineNumberAreaWidth()
Calculates the needed width of the line number area.