修改: HTYEdit

修改:     HTYEdit.pro
	修改:     README.md
	新文件:   highlighter.cpp
	新文件:   highlighter.h
	修改:     mainwindow.cpp
	修改:     mdichild.cpp
This commit is contained in:
sonichy 2017-06-23 16:22:16 +08:00
parent e93c96958d
commit 591cc45c17
7 changed files with 137 additions and 6 deletions

BIN
HTYEdit

Binary file not shown.

View File

@ -15,11 +15,14 @@ TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mdichild.cpp \
dialogfind.cpp
dialogfind.cpp \
highlighter.cpp
HEADERS += mainwindow.h \
mdichild.h \
dialogfind.h
dialogfind.h \
highlighter.h
FORMS += mainwindow.ui \
dialogfind.ui

View File

@ -1,4 +1,6 @@
# Qt 海天鹰编辑器
![alt](preview.png)
Linux 平台基于 Qt 的文字编辑程序。
已编译好的 HTYEdit 程序适用64位Linux系统Qt5环境双击运行其他版本请自行编译。
已编译好的 HTYEdit 程序适用 64 位 Linux 系统 Qt5 环境,双击运行,其他版本请自行编译。
###1.1 [新增语法高亮](http://www.cnblogs.com/lenxvp/p/5475931.html)
高亮类具体工作过程没看懂,因为很好用先用着。

83
highlighter.cpp Normal file
View File

@ -0,0 +1,83 @@
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(QColor("#D33D6D"));
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bbool\\b";
foreach (const QString &pattern, keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
classFormat.setForeground(QColor("#FFFFFF"));
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(QColor("#86897C"));
rule.pattern = QRegularExpression("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(QColor("#5C5D55"));
quotationFormat.setForeground(QColor("#DCB98D"));
rule.pattern = QRegularExpression("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
//functionFormat.setFontItalic(true);
functionFormat.setForeground(QColor("#F385F3"));
rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()");
rule.format = functionFormat;
highlightingRules.append(rule);
commentStartExpression = QRegularExpression("/\\*");
commentEndExpression = QRegularExpression("\\*/");
}
void Highlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression);
while (startIndex >= 0) {
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
int endIndex = match.capturedStart();
int commentLength = 0;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ match.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
}
}

39
highlighter.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>
class QTextDocument;
class Highlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
Highlighter(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text) override;
private:
struct HighlightingRule
{
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegularExpression commentStartExpression;
QRegularExpression commentEndExpression;
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;
};
#endif // HIGHLIGHTER_H

View File

@ -67,14 +67,14 @@ void MainWindow::on_action_aboutQt_triggered()
void MainWindow::on_action_about_triggered()
{
QMessageBox aboutMB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.0\n一款基于Qt的文本编辑程序。\n作者:黄颖\nE-mail: sonichy@163.com\n主页sonichy.96.lt\n参考文献:\nQMdiArea基本用法http://www.mamicode.com/info-detail-1607476.html\nhttp://www.qter.org/?page_id=161,多文档编辑器\n保存http://blog.csdn.net/neicole/article/details/7330234");
QMessageBox aboutMB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.1\n一款基于 Qt 的文本编辑程序。\n作者:黄颖\nE-mail: sonichy@163.com\n主页sonichy.96.lt\n参考文献:\nQMdiArea基本用法http://www.mamicode.com/info-detail-1607476.html\n多文档编辑器:http://www.qter.org/?page_id=161\n保存文本http://blog.csdn.net/neicole/article/details/7330234\n语法高亮http://www.cnblogs.com/lenxvp/p/5475931.html");
aboutMB.setIconPixmap(QPixmap(":/icon.png"));
aboutMB.exec();
}
void MainWindow::on_action_changelog_triggered()
{
QMessageBox aboutMB(QMessageBox::NoIcon, "更新历史", "1.0\n2017-06\n提取打开文件的相对路径使Markdown预览能够载入相对路径图片。\n2017-03\n支持命令行打开文件和打开方式打开文件。\n查找窗口填入选中文本。\n2017-02\n根据文件扩展名选择语法高亮方案。\nJS语法高亮实验成功\nHTML语法高亮实验成功\n增加设置字体。\n设置状态栏左右边距。\n2017-01\n实现全部替换。\n设置循环查找。\n增加查找替换窗体和功能。\n根据文件扩展名决定是否使用默认程序打开如htm。\n优化保存、另存为和文本修动标题标记逻辑。\n增加撤销,重做,子窗标题文本改动标识。\n增加子窗体类实现Ctrl+滚轮缩放和保存打开文件的路径。\n增加使用默认程序预览文件。\n把上一个打开或保存的路径设置为打开或保存对话框的默认路径和文件名。\n增加放大、缩小。\n增加文本光标变化信号,光标所在行列显示在状态栏第二栏。\n状态栏分为2栏\n修复没有子窗口时预览引起的崩溃。\n增加预览功能。\n保存成功。\n修改字体颜色,背景色成功。\n新建文件成功,打开文件载入成功。\n选用QMdiArea作为主控件增加窗口标签、平铺、层叠菜单。 \n制作主要菜单。");
QMessageBox aboutMB(QMessageBox::NoIcon, "更新历史", "1.1\n2017-06\n增加语法高亮。\n提取打开文件的相对路径使Markdown预览能够载入相对路径图片。\n\n1.0\n2017-03\n支持命令行打开文件和打开方式打开文件。\n查找窗口填入选中文本。\n2017-02\n根据文件扩展名选择语法高亮方案。\nJS语法高亮实验成功\nHTML语法高亮实验成功\n增加设置字体。\n设置状态栏左右边距。\n2017-01\n实现全部替换。\n设置循环查找。\n增加查找替换窗体和功能。\n根据文件扩展名决定是否使用默认程序打开如htm。\n优化保存、另存为和文本修动标题标记逻辑。\n增加撤销,重做,子窗标题文本改动标识。\n增加子窗体类实现Ctrl+滚轮缩放和保存打开文件的路径。\n增加使用默认程序预览文件。\n把上一个打开或保存的路径设置为打开或保存对话框的默认路径和文件名。\n增加放大、缩小。\n增加文本光标变化信号,光标所在行列显示在状态栏第二栏。\n状态栏分为2栏\n修复没有子窗口时预览引起的崩溃。\n增加预览功能。\n保存成功。\n修改字体颜色,背景色成功。\n新建文件成功,打开文件载入成功。\n选用QMdiArea作为主控件增加窗口标签、平铺、层叠菜单。 \n制作主要菜单。");
aboutMB.exec();
}
@ -118,7 +118,7 @@ void MainWindow::on_action_new_triggered()
}
void MainWindow::on_action_open_triggered()
{//,"文本文件(.txt .htm .c .cpp .md .desktop)"
{
if(filename==""){
filename = QFileDialog::getOpenFileName(this, "打开文本", ".");
}else{
@ -309,6 +309,7 @@ void MainWindow::on_action_font_triggered()
}
void MainWindow::SyntaxHighlight(){
//自己写的高亮效率低打开文件会卡死弃用引入QSyntaxHighlighter类。
QTextCursor cursor;
cursor=((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor();
QString suffix=QFileInfo(((MdiChild*)(ui->mdiArea->currentSubWindow()->widget()))->path).suffix().toLower();

View File

@ -1,4 +1,5 @@
#include "mdichild.h"
#include "highlighter.h"
#include <QWheelEvent>
#include <QTextStream>
#include <QApplication>
@ -6,6 +7,7 @@
#include <QMessageBox>
#include <QFileInfo>
#include <QDebug>
MdiChild::MdiChild()
{
connect(document(),SIGNAL(contentsChanged()),this,SLOT(onContentsChanged()));
@ -46,6 +48,7 @@ bool MdiChild::loadFile(QString filename)
file->close();
delete file;
setPlainText(s);
Highlighter *highlighter = new Highlighter(document());
showMaximized();
return true;
}else{