精简版的textEditor

马寒
2023-12-01
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
 
#include <QtWidgets>
#include <QPlainTextEdit>
#include <QObject>
 
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
 
class LineNumberArea;
 
 
class CodeEditor : public QPlainTextEdit
{
    Q_OBJECT
 
public:
    CodeEditor(QWidget *parent = 0);
 
    void drawLineNumbers(QPaintEvent *event);
    int lineNumberAreaWidth();
 
private slots:
    void highlightCurrentLine();
    void updateLineNumberArea(const QRect &, int);
 
private:
    QWidget *lineNumberArea;
};
 
 
class LineNumberArea : public QWidget
{
public:
    LineNumberArea(CodeEditor *editor) : QWidget(editor)
    {
        codeEditor = editor;
    }
 
 
protected:
    void paintEvent(QPaintEvent *event)
    {
        codeEditor->drawLineNumbers(event);
    }
 
private:
    CodeEditor *codeEditor;
};
 
 

#endif


//.cpp

#include "codeeditor.h"
 
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
 
    lineNumberArea = new LineNumberArea(this);
    QRect cr = contentsRect();
    lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
    lineNumberArea->show();
 
    setWordWrapMode(QTextOption::NoWrap);
 
    connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
    connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
 
    setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
 
    highlightCurrentLine();
}
 
 
int CodeEditor::lineNumberAreaWidth()
{
    return 40;
}
 
void CodeEditor::updateLineNumberArea(const QRect &rect, int)
{
 
    lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.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::drawLineNumbers(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).rightJustified(4,'0');
            painter.setPen(Qt::black);
            painter.drawText(0, top, lineNumberArea->width()-5, fontMetrics().height(),Qt::AlignRight, number);
        }
 
        block = block.next();
        top = bottom;
        bottom = top + (int) blockBoundingRect(block).height();
        ++blockNumber;
    }
    painter.setPen(Qt::green);
    painter.drawLine(lineNumberArea->width()-1,0,lineNumberArea->width()-1,lineNumberArea->height());
}
 
 
 

//main.cpp

#include <QtWidgets>
 
#include "codeeditor.h"
 
int main(int argv, char **args)
{
    QApplication app(argv, args);
 
    CodeEditor editor;
    editor.setWindowTitle(QObject::tr("Code Editor Example"));
    editor.show();
 
    return app.exec();
}
 
 

 类似资料: