关键字与保留字高亮
可以通过文件或列表的方式添加关键字,此处以文件的方式为例
创建配置文件
配置文件中添加关键字
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
qtHaveModule(printsupport): QT+=printsupport #支持打印
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
myhightlighter.cpp \
mytextedit.cpp
HEADERS += \
mainwindow.h \
myhightlighter.h \
mytextedit.h
FORMS += \
mainwindow.ui \
mytextedit.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
code.qrc \
images.qrc
#ifndef MYHIGHTLIGHTER_H
#define MYHIGHTLIGHTER_H
#include <QObject>
#include <QSyntaxHighlighter>
#include <QTextStream>
class MyHightLighter : public QSyntaxHighlighter
{
public:
explicit MyHightLighter(QTextDocument *parent = nullptr, QString fontFamily = "Consola", int fontSize = 14);
protected:
virtual void highlightBlock(const QString &text);
private:
QString mFontFamily;
int mFontSize;
struct HightLightRule {
QRegExp pattern; // 正则表达式正则表达式
QTextCharFormat format;
};
QVector<HightLightRule> hightLightRules; // 高亮规则
void AddNormalTextFormat();
void AddNumberFormat(); // 数字高亮
void AddStringFormat(); // 字串匹配
void AddCommentFormat(); // 注释高亮
void AddMultiLineCommentFormat(const QString &text);
// 关键字注释/保留字
void AddKeyWordsFormat();
// 类名高亮
void AddClassNameFormat();
// 方法名/函数名高亮
void AddFunctionFormat();
};
#endif // MYHIGHTLIGHTER_H
#include "myhightlighter.h"
MyHightLighter::MyHightLighter(QTextDocument *parent, QString fontFamily, int fontSize) : QSyntaxHighlighter(parent)
{
mFontFamily = fontFamily;
mFontSize = fontSize;
// 对于普通文本
AddNormalTextFormat();
// 数字匹配
AddNumberFormat();
// 子串匹配
AddStringFormat();
// 注释高亮
AddCommentFormat(); // 注释高亮
// 关键字注释/保留字
AddKeyWordsFormat();
// 类名高亮
AddClassNameFormat();
// 方法名/函数名高亮
AddFunctionFormat();
}
// 普通文本匹配
void MyHightLighter::AddNormalTextFormat()
{
HightLightRule rule;
rule.pattern = QRegExp("[a-z0-9A-Z]+"); // 创建正则表达式,+表示重复匹配
QTextCharFormat normalTextFormat;
normalTextFormat.setFont(QFont(mFontFamily, mFontSize));
normalTextFormat.setForeground(QColor(0, 100, 100));
rule.format = normalTextFormat;
hightLightRules.append(rule);
}
// 数字匹配
void MyHightLighter::AddNumberFormat()
{
HightLightRule rule;
rule.pattern = QRegExp("\\b\\d+|\\d+\\.\\d+\\b"); // 创建正则表达式,+表示重复匹配
QTextCharFormat numberFormat;
numberFormat.setFont(QFont(mFontFamily, mFontSize));
numberFormat.setForeground(QColor(250, 80, 50));
rule.format = numberFormat;
hightLightRules.append(rule);
}
// 子串匹配
void MyHightLighter::AddStringFormat()
{
QTextCharFormat numberFormat;
numberFormat.setFont(QFont(mFontFamily, mFontSize));
numberFormat.setForeground(QColor(0, 180, 180));
HightLightRule rule;
rule.format = numberFormat;
// ''
rule.pattern = QRegExp("'[^']*'"); // 创建正则表达式,+表示重复匹配
hightLightRules.append(rule);
// ""
rule.pattern = QRegExp("\"[^\"]*\"");
hightLightRules.append(rule);
//
// rule.pattern = QRegExp();
// hightLightRules.append(rule);
}
// 注释高亮
void MyHightLighter::AddCommentFormat()
{
QTextCharFormat commentFormat;
commentFormat.setFont(QFont(mFontFamily, mFontSize));
commentFormat.setForeground(Qt::darkGreen);
HightLightRule rule;
rule.format = commentFormat;
// "//"高亮
rule.pattern = QRegExp("\\/\\/.*$");
hightLightRules.append(rule);
// "/*xxx*/"高亮
// 已经包含在多行注释中实现,此处不再需要
// rule.pattern = QRegExp("\\/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*\\/");
// hightLightRules.append(rule);
}
void MyHightLighter::AddMultiLineCommentFormat(const QString &text)
{
setCurrentBlockState(0);
// /*
QRegExp commentStartRegExp("\\/\\*");
// */
QRegExp commentEndRegExp("\\*\\/");
// 高亮格式
QTextCharFormat multiLineCommentFormat;
multiLineCommentFormat.setFont(QFont(mFontFamily, mFontSize));
multiLineCommentFormat.setForeground(Qt::darkGreen);
int startIndex = 0;
if (previousBlockState() != 1) {
// 前一行的状态
startIndex = commentStartRegExp.indexIn(text);
}commentEndRegExp.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndRegExp.indexIn(text, startIndex);
int commentLength = 0;
if (endIndex == -1) {
// 没有结束位置
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
// 有结束位置
commentLength = endIndex - startIndex + commentEndRegExp.matchedLength();
}
// 设置格式
setFormat(startIndex,
text.length() - startIndex,
multiLineCommentFormat);
startIndex = commentStartRegExp.indexIn(text, commentLength + startIndex);
}
}
// 关键字注释/保留字
void MyHightLighter::AddKeyWordsFormat()
{
QFile file(":/notepad/config/codeConfig/keyWords.txt.txt");
QTextStream keyWordsStream(&file);
HightLightRule rule;
QTextCharFormat keyWordsFormat;
keyWordsFormat.setFont(QFont(mFontFamily, mFontSize));
keyWordsFormat.setForeground(Qt::darkMagenta);
rule.format = keyWordsFormat;
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
keyWordsStream.seek(0);
QString line;
while (!keyWordsStream.atEnd()) {
line = keyWordsStream.readLine();
if (line != "") {
rule.pattern = QRegExp("\\b" + line + "\\b");
hightLightRules.append(rule);
}
}
file.close();
}
}
// 类名高亮
void MyHightLighter::AddClassNameFormat()
{
HightLightRule rule;
QTextCharFormat classNameFormat;
classNameFormat.setFont(QFont(mFontFamily, mFontSize));
classNameFormat.setForeground(QColor(150, 20, 100));
classNameFormat.setFontWeight(99);
rule.format = classNameFormat;
// 匹配
rule.pattern = QRegExp("\\b[A-Z]+\\w*");
hightLightRules.append(rule);
}
// 方法名/函数名高亮
void MyHightLighter::AddFunctionFormat()
{
HightLightRule rule;
QTextCharFormat functionFormat;
functionFormat.setFont(QFont(mFontFamily, mFontSize));
functionFormat.setForeground(Qt::darkCyan);
rule.format = functionFormat;
// 匹配
rule.pattern = QRegExp("\\w+\\(");
hightLightRules.append(rule);
rule.pattern = QRegExp("\\)");
hightLightRules.append(rule);
}
// 按行来传入的
void MyHightLighter::highlightBlock(const QString &text)
{
foreach(const HightLightRule &rule, hightLightRules) {
QRegExp regExp(rule.pattern);
int index = regExp.indexIn(text);
while (index >= 0) {
int length = regExp.matchedLength();
setFormat(index, length, rule.format); // 匹配字符格式
index = regExp.indexIn(text, index + length);
}
}
// 多行注释
AddMultiLineCommentFormat(text);
}
#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H
#include <QWidget>
namespace Ui {
class MyTextEdit;
}
class MyTextEdit : public QWidget
{
Q_OBJECT
public:
explicit MyTextEdit(QWidget *parent = nullptr);
~MyTextEdit();
private slots:
void textEditHScrollBarChanged();
void scrollBarChanged();
void textEditVScrollBarChanged();
void textBrowerVScrollBarChanged();
void onTextChanged();
private:
Ui::MyTextEdit *ui;
void initConnect();
void initFont();
void initHightLighter();
};
#endif // MYTEXTEDIT_H
#include "myhightlighter.h"
#include "mytextedit.h"
#include "ui_mytextedit.h"
#include <QDebug>
MyTextEdit::MyTextEdit(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyTextEdit)
{
ui->setupUi(this);
initConnect();
initFont();
// 高亮初始化
initHightLighter();
}
MyTextEdit::~MyTextEdit()
{
delete ui;
}
void MyTextEdit::textEditHScrollBarChanged()
{
qDebug() << "value = " << endl;
// 将2个滑动条的长度设置成一样
ui->horizontalScrollBar->setMaximum(ui->textEdit->horizontalScrollBar()->maximum());
ui->horizontalScrollBar->setMinimum(ui->textEdit->horizontalScrollBar()->minimum());
ui->horizontalScrollBar->setPageStep(ui->textEdit->horizontalScrollBar()->pageStep());
ui->horizontalScrollBar->setValue(ui->textEdit->horizontalScrollBar()->value());
}
void MyTextEdit::scrollBarChanged()
{
ui->textEdit->horizontalScrollBar()->setValue(ui->horizontalScrollBar->value());
}
void MyTextEdit::textEditVScrollBarChanged()
{
ui->textBrowser->verticalScrollBar()->setValue(ui->textEdit->verticalScrollBar()->value());
}
void MyTextEdit::textBrowerVScrollBarChanged()
{
ui->textEdit->verticalScrollBar()->setValue(ui->textBrowser->verticalScrollBar()->value());
}
// 绑定信号和槽
void MyTextEdit::initConnect()
{
connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
// 通过信号和槽绑定文本显示框的水平滚动条和自定义水平滚动条
connect(ui->textEdit->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(textEditHScrollBarChanged()));
connect(ui->horizontalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(scrollBarChanged()));
// 绑定滚动条
connect(ui->textEdit->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(textEditVScrollBarChanged()));
connect(ui->textBrowser->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(textBrowerVScrollBarChanged()));
}
// 设置字体
void MyTextEdit::initFont()
{
QFont font("Consolas", 14);
ui->textEdit->setFont(font);
ui->textBrowser->setFont(font);
}
void MyTextEdit::initHightLighter()
{
new MyHightLighter(ui->textEdit->document(), "Times New Roman", 20);
}
void MyTextEdit::onTextChanged()
{
int lineCountOfTextEdit = ui->textEdit->document()->lineCount();
QString text = ui->textBrowser->toPlainText();
int lineCountOfTextBrowser = text.trimmed().split("\n").length();
if (lineCountOfTextBrowser > lineCountOfTextEdit) {
for (int i = lineCountOfTextBrowser; i > lineCountOfTextEdit; i--) {
text.chop((QString::number(i) + "\n").length());
}
} else if (lineCountOfTextBrowser == 1 && text.length() < 1) {
text += "1\n";
} else if (lineCountOfTextBrowser < lineCountOfTextEdit) {
for (int i = lineCountOfTextBrowser; i < lineCountOfTextEdit; ++i) {
text += QString::number(i + 1) + "\n";
}
}
ui->textBrowser->setMaximumWidth(30 + QString::number(lineCountOfTextEdit).length() * 30);
ui->textBrowser->setText(text);
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTreeWidget>
#include <QString>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_new_file_triggered();
void on_open_file_triggered();
void on_save_file_triggered();
void on_save_as_triggered();
void on_paste_triggered();
void on_cut_triggered();
void on_copy_triggered();
void on_font_triggered();
void on_info_triggered();
void on_undo_triggered();
void on_redo_triggered();
void on_exit_triggered();
void on_print_triggered();
void on_bolder_triggered(bool checked);
void on_italics_triggered(bool checked);
void on_underline_triggered(bool checked);
void on_clear_history_triggered();
private:
Ui::MainWindow *ui;
QString currentFile; // 当前文件
void initMenu();
void on_open_recent_file();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "mytextedit.h"
#include "ui_mainwindow.h"
#include <QToolBar>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QFontDialog>
#include <QSettings> // 保存配置文件
#include <QTabWidget>
// 保存打开历史记录
void saveHistory(QString path);
// 获取历史记录
QList<QString> getHistory();
// 先判断是否支持打印
// 定义全局变量
QSettings *mSettings;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// this->setCentralWidget(ui->textEdit);
this->setCentralWidget(ui->tabWidget); // 添加标签页
// 初始化保存历史记录的全局变量
if (mSettings == NULL) {
mSettings = new QSettings("settings.ini", QSettings::IniFormat);
}
initMenu();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initMenu()
{
// 获取menu
// 用于查找MainWindow中名为"recent"的子窗口,并将其存储为指向 `QMenu` 类型对象的指针 `recent`。
QMenu *recent = this->findChild<QMenu *>("recent");
// qDebug() << recent->title() << endl;
qDebug() << "最近打开" << endl;
// 获取Action
QSet<QObject *> chList = recent->children().toSet();
foreach(QObject *ch, chList) {
QAction *action = (QAction *)ch;
// 清空子菜单栏action
recent->removeAction(action);
}
QList<QString> lists = getHistory();
// 打开历史记录按时间从近到远
for (int i = lists.size() - 1; i >= 0; --i) {
// 生成子菜单
recent->addAction(lists[i], this, &MainWindow::on_open_recent_file);
}
// 添加"清除历史记录"action
if (lists.size() > 0) {
recent->addAction("清楚历史菜单", this, &MainWindow::on_clear_history_triggered, QKeySequence("Ctrl+Alt+Shift+C"));
}
}
// 获取历史记录
QList<QString> getHistory()
{
// 打开开始读入
int size = mSettings->beginReadArray("history");
// 创建返回对象
QList<QString> lists;
for (int i = 0; i < size; i++) {
mSettings->setArrayIndex(i);
QString path = mSettings->value("path").toString();
lists.append(path);
qDebug() << i << ":" << path;
}
// 关闭开始读入
mSettings->endArray();
return lists;
}
// 保存打开历史记录
void saveHistory(QString path)
{
// 获取历史
QList<QString> lists = getHistory();
lists.append(path);
foreach(QString str, lists) {
if (str == path) {
lists.removeOne(str);
}
}
lists.append(path);
// lists.toSet().toList(); // 去重
// 打开开始写入
mSettings->beginWriteArray("history");
for (int i = 0; i < lists.size(); ++i) {
mSettings->setArrayIndex(i);
// 保存字符串
mSettings->setValue("path", lists[i]);
}
// 关闭开始写入
mSettings->endArray();
}
// 新建文件
void MainWindow::on_new_file_triggered()
{
// 调用自定义组件
// 由于工程没有导入自定义组件的库,鼠标光标放在"MyTextEdit"上Alt+Enter可以导入
MyTextEdit *myTextEdit = new MyTextEdit(this);
ui->tabWidget->addTab(myTextEdit, "NewTab.txt");
return;
ui->tabWidget->addTab(new QTextEdit(), "newTab.txt");
// qDebug() << "start create new file ..." << endl;
// currentFile.clear(); // 如果之前有文件的话先进性清空
// ui->textEdit->setText(""); // 清空文件内容
}
void MainWindow::on_open_recent_file()
{
QAction *action = (QAction*)sender();
QString filename = action->text();
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "警告", "无法打开此文件"+file.errorString());
}
currentFile = filename;
setWindowTitle(filename);
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
saveHistory(currentFile);
initMenu();
}
// 打开文件
void MainWindow::on_open_file_triggered()
{
QString filename = QFileDialog::getOpenFileName(this, "打开文件");
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "警告", "无法打开此文件 : " + file.errorString());
return;
}
currentFile = filename;
setWindowTitle(filename);
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
saveHistory(currentFile);
initMenu();
}
// 保存文件
void MainWindow::on_save_file_triggered()
{
QString filename;
if (currentFile.isEmpty()) {
filename = QFileDialog::getSaveFileName(this, "保存文件");
currentFile = filename;
} else {
filename = currentFile;
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "警告", "无法保存文件:"+file.errorString());
return;
}
setWindowTitle(filename);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
saveHistory(currentFile);
initMenu();
}
// 另存为
void MainWindow::on_save_as_triggered()
{
QString filename;
filename = QFileDialog::getSaveFileName(this, "另存为");
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "警告", "无法保存文件:"+file.errorString());
return;
}
setWindowTitle(filename);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
saveHistory(currentFile);
initMenu();
}
// 复制
void MainWindow::on_copy_triggered()
{
ui->textEdit->copy();
}
// 粘贴
void MainWindow::on_paste_triggered()
{
ui->textEdit->paste();
}
// 剪切
void MainWindow::on_cut_triggered()
{
ui->textEdit->cut();
}
// 字体
void MainWindow::on_font_triggered()
{
bool fontSelected;
QFont font = QFontDialog::getFont(&fontSelected, this);
if (fontSelected) {
ui->textEdit->setFont(font);
}
}
// 撤销
void MainWindow::on_undo_triggered()
{
ui->textEdit->undo();
}
// 取消撤销
void MainWindow::on_redo_triggered()
{
ui->textEdit->redo();
}
// 退出
void MainWindow::on_exit_triggered()
{
QCoreApplication::exit();
}
// 信息
void MainWindow::on_info_triggered()
{
QMessageBox::about(this, "这是我的notepad", "欢迎学习和使用");
}
// 打印
void MainWindow::on_print_triggered()
{
}
// 加粗
void MainWindow::on_bolder_triggered(bool checked)
{
qDebug() << "on_bolder_triggered" << endl;
ui->textEdit->setFontWeight(checked);
}
// 斜体
void MainWindow::on_italics_triggered(bool checked)
{
ui->textEdit->setFontItalic(checked);
}
// 下划线
void MainWindow::on_underline_triggered(bool checked)
{
ui->textEdit->setFontUnderline(checked);
}
// 清除历史记录
void MainWindow::on_clear_history_triggered()
{
qDebug() << "on_clear_history_triggered..." << endl;
mSettings->remove("history");
initMenu();
}