保存窗口大小,保存字体设置,设置是否显示输出窗口。

This commit is contained in:
sonichy 2019-03-31 13:57:17 +08:00
parent 23cfbe8a23
commit a12c699b12
7 changed files with 122 additions and 49 deletions

BIN
HTYEdit

Binary file not shown.

View File

@ -5,18 +5,10 @@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("HTY");
app.setApplicationName("HTYEdit");
qSetMessagePattern("[ %{file}: %{line} ] %{message}");
/* Qt5以下
QTextCodec *tc = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForTr(tc);
QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
*/
MainWindow w;
w.show();
//在新标签里打开文件
//https://github.com/linuxdeepin/deepin-editor/blob/209eac26c54fe6d2562dd8e00608b8d62b328529/src/main.cpp#L69
//QDBusInterface.callWithArgumentList
return app.exec();
}

View File

@ -23,7 +23,8 @@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui(new Ui::MainWindow),
settings(QCoreApplication::organizationName(), QCoreApplication::applicationName())
{
ui->setupUi(this);
move((QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);
@ -50,6 +51,13 @@ MainWindow::MainWindow(QWidget *parent) :
ui->statusBar->addWidget(LS3);
connect(ui->action_quit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(subWindowActivate(QMdiSubWindow*)));
connect(ui->action_window_output, &QAction::toggled, this, [=](bool b){
if (b) {
ui->textBrowser->show();
} else {
ui->textBrowser->hide();
}
});
dialogFind = new DialogFind(this);
connect(dialogFind->ui->pushButton_find_next, SIGNAL(clicked(bool)), this, SLOT(find()));
@ -59,7 +67,7 @@ MainWindow::MainWindow(QWidget *parent) :
QStringList args = QApplication::arguments();
qDebug() << args;
QDateTime currentDateTime = QDateTime::currentDateTime();
QString log = currentDateTime.toString("yyyy/MM/dd HH:mm:ss") + ": " + args.at(0);
QString log = currentDateTime.toString("yyyy/MM/dd HH:mm:ss") + " " + args.at(0);
if(args.length()>1){
for(int i=1; i<args.length(); i++){
log += " " + args.at(i);
@ -80,6 +88,7 @@ MainWindow::MainWindow(QWidget *parent) :
file.close();
}
readSettings();
}
MainWindow::~MainWindow()
@ -94,7 +103,7 @@ void MainWindow::on_action_aboutQt_triggered()
void MainWindow::on_action_about_triggered()
{
QMessageBox aboutMB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.8\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 aboutMB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.9\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");
aboutMB.setIconPixmap(QPixmap(":/icon.png"));
aboutMB.setWindowIcon(QIcon(":/icon.png"));
aboutMB.exec();
@ -102,7 +111,7 @@ void MainWindow::on_action_about_triggered()
void MainWindow::on_action_changelog_triggered()
{
QString s = "1.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.9\n2019-04\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);
@ -431,14 +440,25 @@ void MainWindow::on_action_indent_triggered()
void MainWindow::on_action_font_triggered()
{
if(ui->mdiArea->currentSubWindow() != 0){
//if(ui->mdiArea->currentSubWindow() != 0){
bool ok;
qDebug() << ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->currentFont();
QFont font = QFontDialog::getFont(&ok, ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->currentFont(), this, "选择字体");
//qDebug() << ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->currentFont();
QString sfont = settings.value("Font").toString();
QFont font;
if (sfont == "") {
font = qApp->font();
} else {
QStringList SLFont = sfont.split(",");
font = QFont(SLFont.at(0),SLFont.at(1).toInt(),SLFont.at(2).toInt(),SLFont.at(3).toInt());
}
//QFont font = QFontDialog::getFont(&ok, ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->currentFont(), this, "选择字体");
font = QFontDialog::getFont(&ok, font, this, "选择字体");
if(ok){
((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->setCurrentFont(font);
QString sfont = font.family() + "," + QString::number(font.pointSize()) + "," + font.weight() + "," + font.italic();
settings.setValue("Font", sfont);
}
}
//}
}
//自己写的高亮效率低打开文件会卡死弃用引入QSyntaxHighlighter类
@ -576,9 +596,10 @@ void MainWindow::printDocument(QPrinter *printer)
void MainWindow::updateCommand()
{
if (ui->mdiArea->currentSubWindow() != 0) {
bool isShowOutput = ui->action_window_output->isChecked();
if (isShowOutput) ui->textBrowser->show();
QString suffix = QFileInfo(((MdiChild*)(ui->mdiArea->currentSubWindow()->widget()))->path).suffix().toLower();
if (suffix == "c" || suffix == "cpp") {
ui->textBrowser->show();
QString s = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->toPlainText();
QStringList SL = s.split("\n");
QStringList SLI = SL.filter(QRegExp("^#include"));;
@ -594,13 +615,10 @@ void MainWindow::updateCommand()
}
lineEdit_command->setText(command);
} else if (suffix == "py") {
ui->textBrowser->show();
lineEdit_command->setText("python ./%1");
} else if (suffix == "java") {
ui->textBrowser->show();
lineEdit_command->setText("javac %1");
} else if (suffix == "sh") {
ui->textBrowser->show();
lineEdit_command->setText("./%1");
} else {
lineEdit_command->setText("");
@ -624,3 +642,16 @@ void MainWindow::printError()
qDebug() << s;
ui->textBrowser->append(s);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
QMainWindow::closeEvent(event);
}
void MainWindow::readSettings()
{
qDebug() << "geometry" << restoreGeometry(settings.value("geometry").toByteArray());
qDebug() << "windowState" << restoreState(settings.value("windowState").toByteArray());
}

View File

@ -8,6 +8,7 @@
#include <QMdiSubWindow>
#include <QPrinter>
#include <QProcess>
#include <QSettings>
namespace Ui {
class MainWindow;
@ -24,15 +25,18 @@ public:
void SyntaxHighlight();
private:
void open(QString fileName);
QLineEdit *lineEdit_command;
QLabel *LS1, *LS2, *LS3;
QString filename, path;
DialogFind *dialogFind;
QSettings settings;
void open(QString fileName);
void readSettings();
protected:
void dragEnterEvent(QDragEnterEvent *e);
void dropEvent(QDropEvent *e);
void closeEvent(QCloseEvent *event);
private slots:
void on_action_new_triggered();

View File

@ -70,7 +70,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menu">
@ -107,7 +107,7 @@
<addaction name="action_aboutQt"/>
<addaction name="action_about"/>
</widget>
<widget class="QMenu" name="menu_4">
<widget class="QMenu" name="menu_window">
<property name="title">
<string>视窗</string>
</property>
@ -115,6 +115,7 @@
<addaction name="action_tabbedView"/>
<addaction name="action_cascade"/>
<addaction name="action_tile"/>
<addaction name="action_window_output"/>
</widget>
<widget class="QMenu" name="menu_5">
<property name="title">
@ -126,7 +127,7 @@
<addaction name="menu"/>
<addaction name="menu_2"/>
<addaction name="menu_5"/>
<addaction name="menu_4"/>
<addaction name="menu_window"/>
<addaction name="menu_3"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
@ -145,7 +146,8 @@
<widget class="QStatusBar" name="statusBar"/>
<action name="action_new">
<property name="icon">
<iconset theme="document-new"/>
<iconset theme="document-new">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>新建</string>
@ -156,7 +158,8 @@
</action>
<action name="action_open">
<property name="icon">
<iconset theme="document-open"/>
<iconset theme="document-open">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>打开</string>
@ -167,7 +170,8 @@
</action>
<action name="action_close">
<property name="icon">
<iconset theme="window-close"/>
<iconset theme="window-close">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>关闭</string>
@ -178,7 +182,8 @@
</action>
<action name="action_save">
<property name="icon">
<iconset theme="document-save"/>
<iconset theme="document-save">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>保存</string>
@ -189,7 +194,8 @@
</action>
<action name="action_saveas">
<property name="icon">
<iconset theme="document-save-as"/>
<iconset theme="document-save-as">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>另存为</string>
@ -200,7 +206,8 @@
</action>
<action name="action_quit">
<property name="icon">
<iconset theme="application-exit"/>
<iconset theme="application-exit">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>退出</string>
@ -211,7 +218,8 @@
</action>
<action name="action_undo">
<property name="icon">
<iconset theme="edit-undo"/>
<iconset theme="edit-undo">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>撤销</string>
@ -222,7 +230,8 @@
</action>
<action name="action_redo">
<property name="icon">
<iconset theme="edit-redo"/>
<iconset theme="edit-redo">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>重做</string>
@ -233,7 +242,8 @@
</action>
<action name="action_copy">
<property name="icon">
<iconset theme="edit-copy"/>
<iconset theme="edit-copy">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>复制</string>
@ -244,7 +254,8 @@
</action>
<action name="action_cut">
<property name="icon">
<iconset theme="edit-cut"/>
<iconset theme="edit-cut">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>剪切</string>
@ -255,7 +266,8 @@
</action>
<action name="action_paste">
<property name="icon">
<iconset theme="edit-paste"/>
<iconset theme="edit-paste">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>粘贴</string>
@ -274,7 +286,8 @@
</action>
<action name="action_find">
<property name="icon">
<iconset theme="edit-find"/>
<iconset theme="edit-find">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>查找</string>
@ -285,7 +298,8 @@
</action>
<action name="action_replace">
<property name="icon">
<iconset theme="edit-find-replace"/>
<iconset theme="edit-find-replace">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>替换</string>
@ -296,7 +310,8 @@
</action>
<action name="action_aboutQt">
<property name="icon">
<iconset theme="help-faq"/>
<iconset theme="help-faq">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>关于Qt</string>
@ -304,7 +319,8 @@
</action>
<action name="action_about">
<property name="icon">
<iconset theme="help-about"/>
<iconset theme="help-about">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>关于</string>
@ -335,7 +351,8 @@
</action>
<action name="action_changelog">
<property name="icon">
<iconset theme="help-contents"/>
<iconset theme="help-contents">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>更新历史</string>
@ -343,7 +360,8 @@
</action>
<action name="action_run">
<property name="icon">
<iconset theme="system-run"/>
<iconset theme="system-run">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>运行</string>
@ -357,7 +375,8 @@
</action>
<action name="action_zoomin">
<property name="icon">
<iconset theme="zoom-in"/>
<iconset theme="zoom-in">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>放大</string>
@ -368,7 +387,8 @@
</action>
<action name="action_zoomout">
<property name="icon">
<iconset theme="zoom-out"/>
<iconset theme="zoom-out">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>缩小</string>
@ -379,7 +399,8 @@
</action>
<action name="action_font">
<property name="icon">
<iconset theme="preferences-desktop-font"/>
<iconset theme="preferences-desktop-font">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>字体</string>
@ -387,7 +408,8 @@
</action>
<action name="action_print">
<property name="icon">
<iconset theme="document-print"/>
<iconset theme="document-print">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>打印</string>
@ -398,7 +420,8 @@
</action>
<action name="action_printPreview">
<property name="icon">
<iconset theme="document-print-preview"/>
<iconset theme="document-print-preview">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>打印预览</string>
@ -407,6 +430,14 @@
<string>Ctrl+Shift+P</string>
</property>
</action>
<action name="action_window_output">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>输出窗口</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>

View File

@ -11,7 +11,9 @@
#include <QProcess>
#include <QPainter>
MdiChild::MdiChild(QWidget *parent) : QPlainTextEdit(parent)
MdiChild::MdiChild(QWidget *parent) :
QPlainTextEdit(parent),
settings(QCoreApplication::organizationName(), QCoreApplication::applicationName())
{
setViewportMargins(50, 0, 0, 0);
QFontMetrics FM(font());
@ -22,6 +24,17 @@ MdiChild::MdiChild(QWidget *parent) : QPlainTextEdit(parent)
lineNumberArea = new LineNumberArea(this);
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
QString sfont = settings.value("Font").toString();
QFont font;
if (sfont == "") {
font = qApp->font();
} else {
QStringList SLFont = sfont.split(",");
font = QFont(SLFont.at(0),SLFont.at(1).toInt(),SLFont.at(2).toInt(),SLFont.at(3).toInt());
}
((QTextEdit*)(this))->setCurrentFont(font);
}
bool MdiChild::loadFile(QString filename)

View File

@ -2,6 +2,7 @@
#define MDICHILD_H
#include <QPlainTextEdit>
#include <QSettings>
class LineNumberArea;
@ -17,6 +18,7 @@ public:
int lineNumberAreaWidth();
private:
QSettings settings;
LineNumberArea *lineNumberArea;
protected: