按照Qt官方CodeEditor范例,增加行号
This commit is contained in:
parent
d4ff946220
commit
34e3525fd5
|
@ -1,9 +1,8 @@
|
||||||
# Qt 海天鹰编辑器
|
# Qt 海天鹰编辑器
|
||||||
Linux 平台基于 Qt 的文字编辑程序。
|
Linux 平台基于 Qt 的文字编辑程序。
|
||||||
已编译好的 HTYEdit 程序适用 64 位 Linux 系统 Qt5 环境,双击运行,其他版本请自行编译。
|
已编译好的 HTYEdit 程序适用 64 位 Linux 系统 Qt5 环境,双击运行,其他版本请自行编译。
|
||||||
|
|
||||||

|

|
||||||

|
### 1.3 新增行号
|
||||||
### 1.3 新增调试窗口
|
### 1.3 新增调试窗口
|
||||||
### 1.1 [新增语法高亮](http://www.cnblogs.com/lenxvp/p/5475931.html)
|
### 1.1 [新增语法高亮](http://www.cnblogs.com/lenxvp/p/5475931.html)
|
||||||
高亮类具体工作过程没看懂,因为很好用先用着。
|
高亮类具体工作过程没看懂,因为很好用先用着。
|
||||||
|
@ -11,3 +10,4 @@ Linux 平台基于 Qt 的文字编辑程序。
|
||||||
### 参考:
|
### 参考:
|
||||||
多文档编辑器:http://www.qter.org/portal.php?mod=view&aid=10
|
多文档编辑器:http://www.qter.org/portal.php?mod=view&aid=10
|
||||||
编译、运行输出:https://github.com/m-iDev-0792/HJ-Editor
|
编译、运行输出:https://github.com/m-iDev-0792/HJ-Editor
|
||||||
|
行号:http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
|
|
@ -13,93 +13,69 @@
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
<string>查找</string>
|
<string>查找</string>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QLabel" name="label">
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
<property name="geometry">
|
<item>
|
||||||
<rect>
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
<x>30</x>
|
<item>
|
||||||
<y>20</y>
|
<widget class="QLabel" name="label_find">
|
||||||
<width>48</width>
|
|
||||||
<height>25</height>
|
|
||||||
</rect>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>查找:</string>
|
<string>查找:</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QLabel" name="label_2">
|
</item>
|
||||||
<property name="geometry">
|
<item>
|
||||||
<rect>
|
<widget class="QLineEdit" name="lineEdit_find"/>
|
||||||
<x>30</x>
|
</item>
|
||||||
<y>60</y>
|
</layout>
|
||||||
<width>48</width>
|
</item>
|
||||||
<height>25</height>
|
<item>
|
||||||
</rect>
|
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||||
</property>
|
<item>
|
||||||
|
<widget class="QLabel" name="label_replace">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>替换:</string>
|
<string>替换:</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QLineEdit" name="lineEditFind">
|
</item>
|
||||||
<property name="geometry">
|
<item>
|
||||||
<rect>
|
<widget class="QLineEdit" name="lineEdit_replace"/>
|
||||||
<x>80</x>
|
</item>
|
||||||
<y>20</y>
|
</layout>
|
||||||
<width>300</width>
|
</item>
|
||||||
<height>30</height>
|
<item>
|
||||||
</rect>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
<widget class="QLineEdit" name="lineEditReplace">
|
|
||||||
<property name="geometry">
|
|
||||||
<rect>
|
|
||||||
<x>80</x>
|
|
||||||
<y>60</y>
|
|
||||||
<width>300</width>
|
|
||||||
<height>30</height>
|
|
||||||
</rect>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
<widget class="QWidget" name="layoutWidget">
|
|
||||||
<property name="geometry">
|
|
||||||
<rect>
|
|
||||||
<x>10</x>
|
|
||||||
<y>120</y>
|
|
||||||
<width>381</width>
|
|
||||||
<height>41</height>
|
|
||||||
</rect>
|
|
||||||
</property>
|
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="btnFind">
|
<widget class="QPushButton" name="pushButton_find_last">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>查找</string>
|
<string>上一个</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="btnReplace">
|
<widget class="QPushButton" name="pushButton_find_next">
|
||||||
|
<property name="text">
|
||||||
|
<string>下一个</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_replace">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>替换</string>
|
<string>替换</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="btnReplaceAll">
|
<widget class="QPushButton" name="pushButton_replaceAll">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>全部替换</string>
|
<string>全部替换</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
</layout>
|
||||||
<widget class="QPushButton" name="btnCancel">
|
|
||||||
<property name="text">
|
|
||||||
<string>取消</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</widget>
|
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
</ui>
|
</ui>
|
||||||
|
|
|
@ -51,10 +51,9 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||||
connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(subWindowActivate(QMdiSubWindow*)));
|
connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(subWindowActivate(QMdiSubWindow*)));
|
||||||
|
|
||||||
dialogFind = new DialogFind(this);
|
dialogFind = new DialogFind(this);
|
||||||
connect(dialogFind->ui->btnCancel,SIGNAL(clicked(bool)),dialogFind,SLOT(close()));
|
connect(dialogFind->ui->pushButton_find_next,SIGNAL(clicked(bool)),this,SLOT(find()));
|
||||||
connect(dialogFind->ui->btnFind,SIGNAL(clicked(bool)),this,SLOT(find()));
|
connect(dialogFind->ui->pushButton_replace,SIGNAL(clicked(bool)),this,SLOT(replace()));
|
||||||
connect(dialogFind->ui->btnReplace,SIGNAL(clicked(bool)),this,SLOT(replace()));
|
connect(dialogFind->ui->pushButton_replaceAll,SIGNAL(clicked(bool)),this,SLOT(replaceAll()));
|
||||||
connect(dialogFind->ui->btnReplaceAll,SIGNAL(clicked(bool)),this,SLOT(replaceAll()));
|
|
||||||
|
|
||||||
QStringList Largs = QApplication::arguments();
|
QStringList Largs = QApplication::arguments();
|
||||||
qDebug() << Largs;
|
qDebug() << Largs;
|
||||||
|
@ -76,7 +75,7 @@ void MainWindow::on_action_aboutQt_triggered()
|
||||||
|
|
||||||
void MainWindow::on_action_about_triggered()
|
void MainWindow::on_action_about_triggered()
|
||||||
{
|
{
|
||||||
QMessageBox aboutMB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.4\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");
|
QMessageBox aboutMB(QMessageBox::NoIcon, "关于", "海天鹰编辑器 1.5\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.setIconPixmap(QPixmap(":/icon.png"));
|
||||||
aboutMB.setWindowIcon(QIcon(":/icon.png"));
|
aboutMB.setWindowIcon(QIcon(":/icon.png"));
|
||||||
aboutMB.exec();
|
aboutMB.exec();
|
||||||
|
@ -84,7 +83,7 @@ void MainWindow::on_action_about_triggered()
|
||||||
|
|
||||||
void MainWindow::on_action_changelog_triggered()
|
void MainWindow::on_action_changelog_triggered()
|
||||||
{
|
{
|
||||||
QString s = "1.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.5\n2018-08\n增加行号\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;
|
QDialog *dialog = new QDialog;
|
||||||
dialog->setWindowTitle("更新历史");
|
dialog->setWindowTitle("更新历史");
|
||||||
dialog->setFixedSize(400,300);
|
dialog->setFixedSize(400,300);
|
||||||
|
@ -131,7 +130,7 @@ void MainWindow::on_action_tile_triggered()
|
||||||
|
|
||||||
void MainWindow::on_action_new_triggered()
|
void MainWindow::on_action_new_triggered()
|
||||||
{
|
{
|
||||||
MdiChild *child = new MdiChild;
|
MdiChild *child = new MdiChild(this);
|
||||||
ui->mdiArea->addSubWindow(child);
|
ui->mdiArea->addSubWindow(child);
|
||||||
QPalette plt = palette();
|
QPalette plt = palette();
|
||||||
plt.setColor(QPalette::Text,QColor(Qt::white));
|
plt.setColor(QPalette::Text,QColor(Qt::white));
|
||||||
|
@ -160,7 +159,7 @@ void MainWindow::on_action_open_triggered()
|
||||||
|
|
||||||
void MainWindow::open(QString fileName)
|
void MainWindow::open(QString fileName)
|
||||||
{
|
{
|
||||||
MdiChild *child = new MdiChild;
|
MdiChild *child = new MdiChild(this);
|
||||||
connect(child, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChange()));
|
connect(child, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChange()));
|
||||||
ui->mdiArea->addSubWindow(child);
|
ui->mdiArea->addSubWindow(child);
|
||||||
if (child->loadFile(fileName)) {
|
if (child->loadFile(fileName)) {
|
||||||
|
@ -225,11 +224,12 @@ void MainWindow::on_action_run_triggered()
|
||||||
s.replace("](", "<img src=" + filepath);
|
s.replace("](", "<img src=" + filepath);
|
||||||
s.replace(")", ">");
|
s.replace(")", ">");
|
||||||
qDebug() << s;
|
qDebug() << s;
|
||||||
MdiChild *child = new MdiChild;
|
MdiChild *child = new MdiChild(this);
|
||||||
ui->mdiArea->addSubWindow(child);
|
ui->mdiArea->addSubWindow(child);
|
||||||
child->show();
|
child->show();
|
||||||
child->setWindowTitle("预览 " + filename1);
|
child->setWindowTitle("预览 " + filename1);
|
||||||
child->setHtml(s);
|
//child->setHtml(s);
|
||||||
|
child->setPlainText(s);
|
||||||
connect(child, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChange()));
|
connect(child, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChange()));
|
||||||
LS2->setText("行,列:1,0 ");
|
LS2->setText("行,列:1,0 ");
|
||||||
} else if (suffix == "htm" || suffix == "html") {
|
} else if (suffix == "htm" || suffix == "html") {
|
||||||
|
@ -316,13 +316,15 @@ void MainWindow::on_action_find_triggered()
|
||||||
{
|
{
|
||||||
if(ui->mdiArea->currentSubWindow() != 0){
|
if(ui->mdiArea->currentSubWindow() != 0){
|
||||||
dialogFind->show();
|
dialogFind->show();
|
||||||
dialogFind->ui->lineEditFind->setText(((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor().selectedText());
|
dialogFind->ui->lineEdit_find->setFocus();
|
||||||
|
dialogFind->ui->lineEdit_find->setText(((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor().selectedText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::find(){
|
void MainWindow::find()
|
||||||
|
{
|
||||||
if(ui->mdiArea->currentSubWindow() != 0){
|
if(ui->mdiArea->currentSubWindow() != 0){
|
||||||
QString sfind = dialogFind->ui->lineEditFind->text();
|
QString sfind = dialogFind->ui->lineEdit_find->text();
|
||||||
if(!((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->find(sfind)){
|
if(!((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->find(sfind)){
|
||||||
QMessageBox MB(QMessageBox::Question, "提示", QString("找不到\"%1\",是否从头查起。").arg(sfind));
|
QMessageBox MB(QMessageBox::Question, "提示", QString("找不到\"%1\",是否从头查起。").arg(sfind));
|
||||||
MB.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
MB.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||||
|
@ -340,8 +342,8 @@ void MainWindow::find(){
|
||||||
|
|
||||||
void MainWindow::replace()
|
void MainWindow::replace()
|
||||||
{
|
{
|
||||||
QString sfind = dialogFind->ui->lineEditFind->text();
|
QString sfind = dialogFind->ui->lineEdit_find->text();
|
||||||
QString sreplace = dialogFind->ui->lineEditReplace->text();
|
QString sreplace = dialogFind->ui->lineEdit_replace->text();
|
||||||
QTextCursor cursor = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor();
|
QTextCursor cursor = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor();
|
||||||
if(cursor.selectedText() == sfind){
|
if(cursor.selectedText() == sfind){
|
||||||
cursor.insertText(sreplace);
|
cursor.insertText(sreplace);
|
||||||
|
@ -351,8 +353,8 @@ void MainWindow::replace()
|
||||||
|
|
||||||
void MainWindow::replaceAll()
|
void MainWindow::replaceAll()
|
||||||
{
|
{
|
||||||
QString sfind = dialogFind->ui->lineEditFind->text();
|
QString sfind = dialogFind->ui->lineEdit_find->text();
|
||||||
QString sreplace = dialogFind->ui->lineEditReplace->text();
|
QString sreplace = dialogFind->ui->lineEdit_replace->text();
|
||||||
QTextCursor cursor = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor();
|
QTextCursor cursor = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor();
|
||||||
cursor.setPosition(0,QTextCursor::MoveAnchor);
|
cursor.setPosition(0,QTextCursor::MoveAnchor);
|
||||||
((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->setTextCursor(cursor);
|
((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->setTextCursor(cursor);
|
||||||
|
@ -530,14 +532,9 @@ void MainWindow::updateCommand()
|
||||||
QString s = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->toPlainText();
|
QString s = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->toPlainText();
|
||||||
QStringList SL = s.split("\n");
|
QStringList SL = s.split("\n");
|
||||||
QStringList SLI = SL.filter(QRegExp("^#include"));;
|
QStringList SLI = SL.filter(QRegExp("^#include"));;
|
||||||
// QStringList SLI;
|
|
||||||
// foreach (const QString &str, SL) {
|
|
||||||
// if (str.startsWith("#include"))
|
|
||||||
// SLI += str;
|
|
||||||
// }
|
|
||||||
QString command = "";
|
QString command = "";
|
||||||
for(int i=0; i<SLI.length(); i++){
|
for(int i=0; i<SLI.length(); i++){
|
||||||
qDebug() << SLI.at(i);
|
//qDebug() << SLI.at(i);
|
||||||
if (SLI.at(i).contains("#include <GL/")) {
|
if (SLI.at(i).contains("#include <GL/")) {
|
||||||
command = "g++ %1 -o %2 -l GL -l GLU -l glut";
|
command = "g++ %1 -o %2 -l GL -l GLU -l glut";
|
||||||
break;
|
break;
|
||||||
|
@ -559,13 +556,15 @@ void MainWindow::updateCommand()
|
||||||
void MainWindow::printOutput()
|
void MainWindow::printOutput()
|
||||||
{
|
{
|
||||||
QProcess *process = qobject_cast<QProcess*>(sender());
|
QProcess *process = qobject_cast<QProcess*>(sender());
|
||||||
//qDebug() << process->readAllStandardOutput();
|
QString s = QString(process->readAllStandardOutput());
|
||||||
ui->textBrowser->append(process->readAllStandardOutput());
|
qDebug() << s;
|
||||||
|
ui->textBrowser->append(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::printError()
|
void MainWindow::printError()
|
||||||
{
|
{
|
||||||
QProcess *process = qobject_cast<QProcess*>(sender());
|
QProcess *process = qobject_cast<QProcess*>(sender());
|
||||||
//qDebug() << process->readAllStandardError();
|
QString s = QString(process->readAllStandardError());
|
||||||
ui->textBrowser->append(process->readAllStandardError());
|
qDebug() << s;
|
||||||
|
ui->textBrowser->append(s);
|
||||||
}
|
}
|
100
mdichild.cpp
100
mdichild.cpp
|
@ -9,30 +9,19 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QTextCodec>
|
#include <QTextCodec>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
MdiChild::MdiChild()
|
MdiChild::MdiChild(QWidget *parent) : QPlainTextEdit(parent)
|
||||||
{
|
{
|
||||||
|
setViewportMargins(50, 0, 0, 0);
|
||||||
QFontMetrics FM(font());
|
QFontMetrics FM(font());
|
||||||
int ts = 4;
|
int ts = 4;
|
||||||
setTabStopWidth(ts * FM.boundingRect("*").width());
|
setTabStopWidth(ts * FM.boundingRect("*").width());
|
||||||
connect(document(),SIGNAL(contentsChanged()),this,SLOT(onContentsChanged()));
|
connect(document(), SIGNAL(contentsChanged()), this, SLOT(contentsChange()));
|
||||||
}
|
|
||||||
|
|
||||||
void MdiChild::wheelEvent(QWheelEvent *e)
|
lineNumberArea = new LineNumberArea(this);
|
||||||
{
|
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
|
||||||
if(QApplication::keyboardModifiers() == Qt::ControlModifier){
|
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
||||||
if(e->delta() > 0){
|
|
||||||
zoomIn();
|
|
||||||
}else{
|
|
||||||
zoomOut();
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
if(e->delta() > 0){
|
|
||||||
verticalScrollBar()->setValue(verticalScrollBar()->value()-30);
|
|
||||||
}else{
|
|
||||||
verticalScrollBar()->setValue(verticalScrollBar()->value()+30);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MdiChild::loadFile(QString filename)
|
bool MdiChild::loadFile(QString filename)
|
||||||
|
@ -93,11 +82,24 @@ bool MdiChild::save()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MdiChild::onContentsChanged()
|
void MdiChild::contentsChange()
|
||||||
{
|
{
|
||||||
setWindowModified(document()->isModified());
|
setWindowModified(document()->isModified());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MdiChild::wheelEvent(QWheelEvent *e)
|
||||||
|
{
|
||||||
|
if(QApplication::keyboardModifiers() == Qt::ControlModifier){
|
||||||
|
if(e->delta() > 0){
|
||||||
|
zoomIn();
|
||||||
|
}else{
|
||||||
|
zoomOut();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
QPlainTextEdit::wheelEvent(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MdiChild::keyPressEvent(QKeyEvent *e)
|
void MdiChild::keyPressEvent(QKeyEvent *e)
|
||||||
{
|
{
|
||||||
if(e->key() == Qt::Key_ParenLeft){
|
if(e->key() == Qt::Key_ParenLeft){
|
||||||
|
@ -115,5 +117,63 @@ void MdiChild::keyPressEvent(QKeyEvent *e)
|
||||||
QTextCursor c = textCursor();
|
QTextCursor c = textCursor();
|
||||||
c.movePosition(QTextCursor::Left,QTextCursor::MoveAnchor,2);
|
c.movePosition(QTextCursor::Left,QTextCursor::MoveAnchor,2);
|
||||||
}
|
}
|
||||||
return QTextEdit::keyPressEvent(e);
|
return QPlainTextEdit::keyPressEvent(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MdiChild::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||||
|
{
|
||||||
|
QPainter painter(lineNumberArea);
|
||||||
|
painter.fillRect(event->rect(), QBrush(QColor(33,33,33)));
|
||||||
|
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);
|
||||||
|
painter.setPen(Qt::white);
|
||||||
|
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number);
|
||||||
|
}
|
||||||
|
block = block.next();
|
||||||
|
top = bottom;
|
||||||
|
bottom = top + (int) blockBoundingRect(block).height();
|
||||||
|
++blockNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int MdiChild::lineNumberAreaWidth()
|
||||||
|
{
|
||||||
|
int digits = 1;
|
||||||
|
int max = qMax(1, blockCount());
|
||||||
|
while (max >= 10) {
|
||||||
|
max /= 10;
|
||||||
|
++digits;
|
||||||
|
}
|
||||||
|
//int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
|
||||||
|
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
|
||||||
|
return space;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MdiChild::resizeEvent(QResizeEvent *e)
|
||||||
|
{
|
||||||
|
QPlainTextEdit::resizeEvent(e);
|
||||||
|
QRect cr = contentsRect();
|
||||||
|
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MdiChild::updateLineNumberArea(const QRect &rect, int dy)
|
||||||
|
{
|
||||||
|
if (dy)
|
||||||
|
lineNumberArea->scroll(0, dy);
|
||||||
|
else
|
||||||
|
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
|
||||||
|
|
||||||
|
if (rect.contains(viewport()->rect()))
|
||||||
|
updateLineNumberAreaWidth(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MdiChild::updateLineNumberAreaWidth(int /* newBlockCount */)
|
||||||
|
{
|
||||||
|
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
|
||||||
}
|
}
|
38
mdichild.h
38
mdichild.h
|
@ -1,24 +1,54 @@
|
||||||
#ifndef MDICHILD_H
|
#ifndef MDICHILD_H
|
||||||
#define MDICHILD_H
|
#define MDICHILD_H
|
||||||
|
|
||||||
#include <QTextEdit>
|
#include <QPlainTextEdit>
|
||||||
|
|
||||||
class MdiChild : public QTextEdit
|
class LineNumberArea;
|
||||||
|
|
||||||
|
class MdiChild : public QPlainTextEdit
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
MdiChild();
|
MdiChild(QWidget *parent);
|
||||||
bool loadFile(QString);
|
bool loadFile(QString);
|
||||||
bool save();
|
bool save();
|
||||||
QString path, scodec;
|
QString path, scodec;
|
||||||
|
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
||||||
|
int lineNumberAreaWidth();
|
||||||
|
|
||||||
|
private:
|
||||||
|
LineNumberArea *lineNumberArea;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void wheelEvent(QWheelEvent*);
|
void wheelEvent(QWheelEvent*);
|
||||||
void keyPressEvent(QKeyEvent *e);
|
void keyPressEvent(QKeyEvent *e);
|
||||||
|
void resizeEvent(QResizeEvent* e);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onContentsChanged();
|
void contentsChange();
|
||||||
|
void updateLineNumberAreaWidth(int newBlockCount);
|
||||||
|
void updateLineNumberArea(const QRect &, int);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class LineNumberArea : public QWidget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LineNumberArea(MdiChild *editor) : QWidget(editor) {
|
||||||
|
mdiChild = editor;
|
||||||
|
}
|
||||||
|
|
||||||
|
QSize sizeHint() const override {
|
||||||
|
return QSize(mdiChild->lineNumberAreaWidth(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void paintEvent(QPaintEvent *event) override {
|
||||||
|
mdiChild->lineNumberAreaPaintEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
MdiChild *mdiChild;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MDICHILD_H
|
#endif // MDICHILD_H
|
||||||
|
|
BIN
preview.png
BIN
preview.png
Binary file not shown.
Before Width: | Height: | Size: 242 KiB After Width: | Height: | Size: 95 KiB |
Loading…
Reference in New Issue