Qt 小例子学习9 - 代码编辑器
CodeEditor.h
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QObject>
#include <QPlainTextEdit>
QT_BEGIN_NAMESPACE
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
QT_END_NAMESPACE
class LineNumberArea;
class CodeEditor : public QPlainTextEdit
{
Q_OBJECT
public:
CodeEditor(QWidget* parent = 0);
void lineNumberAreaPaintEvent(QPaintEvent* event);
int lineNumberAreaWidth();
protected:
void resizeEvent(QResizeEvent* event) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect&, int);
void onClicked(int h);
private:
LineNumberArea* lineNumberArea;
QVector<int> breakpoints;
};
class LineNumberArea : public QWidget
{
Q_OBJECT
public:
LineNumberArea(CodeEditor* editor) : QWidget(editor)
{
codeEditor = editor;
}
QSize sizeHint() const override
{
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent* event) override
{
codeEditor->lineNumberAreaPaintEvent(event);
}
void mousePressEvent(QMouseEvent* event) override
{
emit clicked(event->pos().y());
}
signals:
void clicked(int y);
private:
CodeEditor* codeEditor;
};
#endif // CODEEDITOR_H
CodeEditor.cpp
#include <QtWidgets>
#include "CodeEditor.h"
#define width_circle 10
CodeEditor::CodeEditor(QWidget* parent) : QPlainTextEdit(parent)
{
lineNumberArea = new LineNumberArea(this);
connect(this, &QPlainTextEdit::blockCountChanged, this,
&CodeEditor::updateLineNumberAreaWidth);
connect(this, &QPlainTextEdit::updateRequest, this,
&CodeEditor::updateLineNumberArea);
connect(this, &QPlainTextEdit::cursorPositionChanged, this,
&CodeEditor::highlightCurrentLine);
connect(lineNumberArea, &LineNumberArea::clicked, this,
&CodeEditor::onClicked);
updateLineNumberAreaWidth(0);
highlightCurrentLine();
}
int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while(max >= 10)
{
max /= 10;
++digits;
}
int space = width_circle + 3 + fontMetrics().width(QLatin1Char('9')) * digits;
return space;
}
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void CodeEditor::updateLineNumberArea(const QRect& rect, int dy)
{
if(dy)
{
lineNumberArea->scroll(0, dy);
}
else
{
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
}
if(rect.contains(viewport()->rect()))
{
updateLineNumberAreaWidth(0);
}
}
void CodeEditor::onClicked(int h)
{
QTextBlock block = firstVisibleBlock();
int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int)blockBoundingRect(block).height();
int blockNumber = block.blockNumber();
while(block.isValid())
{
if(block.isVisible())
{
if(h > top && h < bottom)
{
int index = breakpoints.indexOf(blockNumber);
if(index != -1)
{
breakpoints.remove(index);
}
else
{
breakpoints << blockNumber;
}
update();
return;
}
}
blockNumber++;
block = block.next();
top = bottom;
bottom = top + (int)blockBoundingRect(block).height();
}
}
void CodeEditor::resizeEvent(QResizeEvent* e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(
QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void CodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if(!isReadOnly())
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int)blockBoundingRect(block).height();
while(block.isValid() && top <= event->rect().bottom())
{
if(block.isVisible() && bottom >= event->rect().top())
{
QString number = QString::number(blockNumber + 1);
if(breakpoints.indexOf(blockNumber) != -1)
{
painter.setBrush(Qt::red);
painter.drawEllipse(0,
top + (fontMetrics().height() - width_circle) / 2,
width_circle, width_circle);
}
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + (int)blockBoundingRect(block).height();
++blockNumber;
}
}
#undef width_circle