增加导出HTML

This commit is contained in:
sonichy 2025-06-24 17:01:19 +08:00
parent 7b73991d11
commit e9d4149e46
6 changed files with 62 additions and 9 deletions

BIN
HTYEdit

Binary file not shown.

View File

@ -181,7 +181,7 @@ void MainWindow::on_action_aboutQt_triggered()
void MainWindow::on_action_about_triggered()
{
QMessageBox MB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.13\n\n一款基于 Qt 的文本编辑程序。\n作者:海天鹰\nE-mail: sonichy@163.com\n主页https://github.com/sonichy\n参考文献:\n多文档编辑器http://www.qter.org/?page_id=161\nQMdiArea基本用法http://www.mamicode.com/info-detail-1607476.html\n保存文本http://blog.csdn.net/neicole/article/details/7330234\n语法高亮http://www.cnblogs.com/lenxvp/p/5475931.html\n拖放打开文件http://blog.csdn.net/rl529014/article/details/53057577\n行号http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html");
QMessageBox MB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.14\n\n一款基于 Qt 的文本编辑程序。\n作者:海天鹰\nE-mail: sonichy@163.com\n主页https://github.com/sonichy\n参考文献:\n多文档编辑器http://www.qter.org/?page_id=161\nQMdiArea基本用法http://www.mamicode.com/info-detail-1607476.html\n保存文本http://blog.csdn.net/neicole/article/details/7330234\n语法高亮http://www.cnblogs.com/lenxvp/p/5475931.html\n拖放打开文件http://blog.csdn.net/rl529014/article/details/53057577\n行号http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html");
MB.setIconPixmap(QPixmap(":/HTYEdit.png"));
MB.setWindowIcon(QIcon(":/HTYEdit.png"));
MB.exec();
@ -189,7 +189,7 @@ void MainWindow::on_action_about_triggered()
void MainWindow::on_action_changelog_triggered()
{
QString s = "1.13\n2025-03\n修复file:\\协议打不开问题。\n修改log路径。\n\n1.12\n2021-01\n修复img去属性增加删除标签、删除换行。\n\n1.11\n2019-12\n优化排版增加选中多行Tab缩进。\n\n1.10\n2019-08\n增加:字符数统计。\nHTML排版增加选中文本分行插入td、tr、p、h1~h6的工具。\n\n1.9\n2019-04\n便签右键菜单增加打开文件路径。\n换行自动缩进。\n快捷键 Ctrl+/ 注释、取消注释选中行。\n保存窗口大小,保存字体设置,设置是否显示输出窗口。\n\n1.8\n2018-11\n使用系统主题图标代替Qt内置图标。\n\n1.7\n2018-10\n支持拖放打开多个文件。\n增加打开文件log。\n打开方式文件路径兼容深度文管和其他文管。\n\n1.6\n2018-09\n标签右键增加只读菜单。\n优化括号补全。\n\n1.5\n2018-08\n增加行号\n增加java文件编译命令\n\n1.4\n2018-07\n设置QTextEdit的Tab跳过的空格数为4个空格\n\n1.3\n2018-06\n增加调试窗口。\n\n1.2\n2018-05\n增加c、cpp的OpenGL编译命令。\n2018-05\n解决右键打开方式无法打开文件问题。\n增加运行python。\n2018-04\n增加打印功能。\n\n1.1\n2017-10\n增加获取文本编码(使用 file --mime-encoding 命令返回),但是没有解决乱码问题。\n排版实验。\n用文本框代替消息框显示更新日志。\n2017-07\n增加拖放打开文件。\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制作主要菜单。";
QString s = "1.14\n2025-06\n增加导出HTML。\n\n1.13\n2025-03\n修复 file:\\\\ 协议打不开问题。\n修改log路径。\n\n1.12\n2021-01\n修复img去属性增加删除标签、删除换行。\n\n1.11\n2019-12\n优化排版增加选中多行Tab缩进。\n\n1.10\n2019-08\n增加:字符数统计。\nHTML排版增加选中文本分行插入td、tr、p、h1~h6的工具。\n\n1.9\n2019-04\n便签右键菜单增加打开文件路径。\n换行自动缩进。\n快捷键 Ctrl+/ 注释、取消注释选中行。\n保存窗口大小,保存字体设置,设置是否显示输出窗口。\n\n1.8\n2018-11\n使用系统主题图标代替Qt内置图标。\n\n1.7\n2018-10\n支持拖放打开多个文件。\n增加打开文件log。\n打开方式文件路径兼容深度文管和其他文管。\n\n1.6\n2018-09\n标签右键增加只读菜单。\n优化括号补全。\n\n1.5\n2018-08\n增加行号\n增加java文件编译命令\n\n1.4\n2018-07\n设置QTextEdit的Tab跳过的空格数为4个空格\n\n1.3\n2018-06\n增加调试窗口。\n\n1.2\n2018-05\n增加c、cpp的OpenGL编译命令。\n2018-05\n解决右键打开方式无法打开文件问题。\n增加运行python。\n2018-04\n增加打印功能。\n\n1.1\n2017-10\n增加获取文本编码(使用 file --mime-encoding 命令返回),但是没有解决乱码问题。\n排版实验。\n用文本框代替消息框显示更新日志。\n2017-07\n增加拖放打开文件。\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制作主要菜单。";
QDialog *dialog = new QDialog;
dialog->setWindowTitle("更新历史");
dialog->setFixedSize(400, 300);
@ -307,11 +307,11 @@ void MainWindow::on_action_save_triggered()
{
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
if (window != nullptr) {
path = ((MdiChild*)(window->widget()))->path;
MdiChild *child = (MdiChild*)(window->widget());
path = child->path;
if (path == "") {
on_action_saveas_triggered();
} else {
MdiChild *child = (MdiChild*)(window->widget());
if (child->save()) {
LS1->setText("保存 " + child->path);
}
@ -326,7 +326,7 @@ void MainWindow::on_action_saveas_triggered()
MdiChild *child = (MdiChild*)(window->widget());
path = child->path;
if (path == "") {
filename = QFileDialog::getSaveFileName(this, "保存文本", "./未命名");
filename = QFileDialog::getSaveFileName(this, "保存文本", "./未命名.txt");
} else {
filename = QFileDialog::getSaveFileName(this, "保存文本", path);
}
@ -337,6 +337,25 @@ void MainWindow::on_action_saveas_triggered()
}
}
void MainWindow::on_action_exportHTML_triggered()
{
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
if (window != nullptr) {
MdiChild *child = (MdiChild*)(window->widget());
path = child->path;
if (path == "") {
filename = QFileDialog::getSaveFileName(this, "保存文本", "./未命名.htm");
} else {
filename = QFileDialog::getSaveFileName(this, "保存文本", QFileInfo(path).absolutePath() + "/" + QFileInfo(path).baseName() + ".htm");
}
if (!filename.isEmpty()) {
if (child->saveHTML(filename)) {
LS1->setText("导出:" + QFileInfo(filename).baseName() + ".htm");
}
}
}
}
void MainWindow::on_action_run_triggered()
{
QMdiSubWindow *window = ui->mdiArea->currentSubWindow();
@ -631,13 +650,13 @@ void MainWindow::SyntaxHighlight()
((QTextEdit*)(window->widget()))->setTextColor(QColor(255,255,0));
}
}
} else if (suffix=="js") {
} else if (suffix == "js") {
//JS
QString str = "window,event,var,new,Array(),Image(),push,document,getElementById,createElement,appendChild,console,.log,.style,backgroundColor,for,if,textContent,innerHTML,function,.src,.load,.complete,.onload,.width,.height,.value,beginPath(),lineTo,moveTo,stroke(),strokeStyle,getContext,eval,translate,textAlign,Math,.cos,.sin,.pow,.random,fillText,addEventListener,length,drawImage,.top,.bottom,.left,.right,onmousemove,onmouseup,offsetLeft,offsetRight,offsetTop,offsetBottom,offsetWidth,offsetHeight,this,options,selectedIndex,.text,setInterval,clearInterval";
QStringList list = str.split(",");
for(int i=0; i<list.size(); i++){
//cursor=((QTextEdit*)(window->widget()))->textCursor();
cursor.setPosition(0,QTextCursor::MoveAnchor);
cursor.setPosition(0, QTextCursor::MoveAnchor);
((QTextEdit*)(window->widget()))->setTextCursor(cursor);
while(((QTextEdit*)(window->widget()))->find(list[i],QTextDocument::FindCaseSensitively)){
((QTextEdit*)(window->widget()))->setTextColor(QColor(0,255,0));

View File

@ -45,6 +45,7 @@ private slots:
void on_action_close_triggered();
void on_action_save_triggered();
void on_action_saveas_triggered();
void on_action_exportHTML_triggered();
void on_action_print_triggered();
void on_action_printPreview_triggered();
void on_action_theme_triggered();

View File

@ -86,6 +86,7 @@
<addaction name="action_close"/>
<addaction name="action_save"/>
<addaction name="action_saveas"/>
<addaction name="action_exportHTML"/>
<addaction name="action_print"/>
<addaction name="action_printPreview"/>
<addaction name="action_quit"/>
@ -562,6 +563,14 @@
<string>主题</string>
</property>
</action>
<action name="action_exportHTML">
<property name="icon">
<iconset theme="text-html"/>
</property>
<property name="text">
<string>导出HTML</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>

View File

@ -92,7 +92,30 @@ bool MdiChild::save()
setWindowModified(false);
return true;
} else {
QMessageBox::warning(this,"错误", QString(" %1:\n%2").arg(path).arg(file.errorString()));
QMessageBox::warning(this, "错误", QString(" %1:\n%2").arg(path).arg(file.errorString()));
return false;
}
}
bool MdiChild::saveHTML(QString filepath)
{
QFile file(filepath);
if (file.open(QFile::WriteOnly)) {
QTextStream TS(&file);
QString s = toPlainText();
QStringList SL = s.split("\n");
s = "<!DOCTYPE html>\n<head>\n<meta name='viewport' content='width=device-width, initial-scale=1.0'>\n<title>" + QFileInfo(filepath).baseName() + "</title>\n<style>\nh1, h2 { text-align:center; }\np { text-indent:2em; font-size:1.2em; }\n</style>\n</head>\n<body>";
for (int i=0; i<SL.length(); i++) {
if (i == 0)
s = s + "\n<h1>" + SL.at(i) + "</h1>";
else
s = s + "\n<p>" + SL.at(i) + "</p>";
}
s = s + "\n</body>\n</html>";
TS << s;
return true;
} else {
QMessageBox::warning(this, "错误", QString(" %1:\n%2").arg(filepath).arg(file.errorString()));
return false;
}
}

View File

@ -12,9 +12,10 @@ class MdiChild : public QPlainTextEdit
public:
MdiChild(QWidget *parent);
QString path, scodec;
bool loadFile(QString);
bool save();
QString path, scodec;
bool saveHTML(QString);
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
void insertH(QString h);