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

|
||||

|
||||
### 1.3 新增行号
|
||||
### 1.3 新增调试窗口
|
||||
### 1.1 [新增语法高亮](http://www.cnblogs.com/lenxvp/p/5475931.html)
|
||||
高亮类具体工作过程没看懂,因为很好用先用着。
|
||||
|
||||
### 参考:
|
||||
多文档编辑器:http://www.qter.org/portal.php?mod=view&aid=10
|
||||
编译、运行输出:https://github.com/m-iDev-0792/HJ-Editor
|
||||
多文档编辑器:http://www.qter.org/portal.php?mod=view&aid=10
|
||||
编译、运行输出:https://github.com/m-iDev-0792/HJ-Editor
|
||||
行号:http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
|
148
dialogfind.ui
148
dialogfind.ui
|
@ -13,92 +13,68 @@
|
|||
<property name="windowTitle">
|
||||
<string>查找</string>
|
||||
</property>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>30</x>
|
||||
<y>20</y>
|
||||
<width>48</width>
|
||||
<height>25</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>查找:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>30</x>
|
||||
<y>60</y>
|
||||
<width>48</width>
|
||||
<height>25</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>替换:</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLineEdit" name="lineEditFind">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>80</x>
|
||||
<y>20</y>
|
||||
<width>300</width>
|
||||
<height>30</height>
|
||||
</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">
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnFind">
|
||||
<property name="text">
|
||||
<string>查找</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnReplace">
|
||||
<property name="text">
|
||||
<string>替换</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnReplaceAll">
|
||||
<property name="text">
|
||||
<string>全部替换</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnCancel">
|
||||
<property name="text">
|
||||
<string>取消</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_find">
|
||||
<property name="text">
|
||||
<string>查找:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit_find"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_replace">
|
||||
<property name="text">
|
||||
<string>替换:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit_replace"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_find_last">
|
||||
<property name="text">
|
||||
<string>上一个</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<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">
|
||||
<string>替换</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_replaceAll">
|
||||
<property name="text">
|
||||
<string>全部替换</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
|
|
@ -51,10 +51,9 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||
connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(subWindowActivate(QMdiSubWindow*)));
|
||||
|
||||
dialogFind = new DialogFind(this);
|
||||
connect(dialogFind->ui->btnCancel,SIGNAL(clicked(bool)),dialogFind,SLOT(close()));
|
||||
connect(dialogFind->ui->btnFind,SIGNAL(clicked(bool)),this,SLOT(find()));
|
||||
connect(dialogFind->ui->btnReplace,SIGNAL(clicked(bool)),this,SLOT(replace()));
|
||||
connect(dialogFind->ui->btnReplaceAll,SIGNAL(clicked(bool)),this,SLOT(replaceAll()));
|
||||
connect(dialogFind->ui->pushButton_find_next,SIGNAL(clicked(bool)),this,SLOT(find()));
|
||||
connect(dialogFind->ui->pushButton_replace,SIGNAL(clicked(bool)),this,SLOT(replace()));
|
||||
connect(dialogFind->ui->pushButton_replaceAll,SIGNAL(clicked(bool)),this,SLOT(replaceAll()));
|
||||
|
||||
QStringList Largs = QApplication::arguments();
|
||||
qDebug() << Largs;
|
||||
|
@ -76,7 +75,7 @@ void MainWindow::on_action_aboutQt_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.setWindowIcon(QIcon(":/icon.png"));
|
||||
aboutMB.exec();
|
||||
|
@ -84,7 +83,7 @@ void MainWindow::on_action_about_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;
|
||||
dialog->setWindowTitle("更新历史");
|
||||
dialog->setFixedSize(400,300);
|
||||
|
@ -131,7 +130,7 @@ void MainWindow::on_action_tile_triggered()
|
|||
|
||||
void MainWindow::on_action_new_triggered()
|
||||
{
|
||||
MdiChild *child = new MdiChild;
|
||||
MdiChild *child = new MdiChild(this);
|
||||
ui->mdiArea->addSubWindow(child);
|
||||
QPalette plt = palette();
|
||||
plt.setColor(QPalette::Text,QColor(Qt::white));
|
||||
|
@ -160,7 +159,7 @@ void MainWindow::on_action_open_triggered()
|
|||
|
||||
void MainWindow::open(QString fileName)
|
||||
{
|
||||
MdiChild *child = new MdiChild;
|
||||
MdiChild *child = new MdiChild(this);
|
||||
connect(child, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChange()));
|
||||
ui->mdiArea->addSubWindow(child);
|
||||
if (child->loadFile(fileName)) {
|
||||
|
@ -225,11 +224,12 @@ void MainWindow::on_action_run_triggered()
|
|||
s.replace("](", "<img src=" + filepath);
|
||||
s.replace(")", ">");
|
||||
qDebug() << s;
|
||||
MdiChild *child = new MdiChild;
|
||||
MdiChild *child = new MdiChild(this);
|
||||
ui->mdiArea->addSubWindow(child);
|
||||
child->show();
|
||||
child->setWindowTitle("预览 " + filename1);
|
||||
child->setHtml(s);
|
||||
//child->setHtml(s);
|
||||
child->setPlainText(s);
|
||||
connect(child, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChange()));
|
||||
LS2->setText("行,列:1,0 ");
|
||||
} else if (suffix == "htm" || suffix == "html") {
|
||||
|
@ -316,13 +316,15 @@ void MainWindow::on_action_find_triggered()
|
|||
{
|
||||
if(ui->mdiArea->currentSubWindow() != 0){
|
||||
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){
|
||||
QString sfind = dialogFind->ui->lineEditFind->text();
|
||||
QString sfind = dialogFind->ui->lineEdit_find->text();
|
||||
if(!((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->find(sfind)){
|
||||
QMessageBox MB(QMessageBox::Question, "提示", QString("找不到\"%1\",是否从头查起。").arg(sfind));
|
||||
MB.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
|
@ -340,8 +342,8 @@ void MainWindow::find(){
|
|||
|
||||
void MainWindow::replace()
|
||||
{
|
||||
QString sfind = dialogFind->ui->lineEditFind->text();
|
||||
QString sreplace = dialogFind->ui->lineEditReplace->text();
|
||||
QString sfind = dialogFind->ui->lineEdit_find->text();
|
||||
QString sreplace = dialogFind->ui->lineEdit_replace->text();
|
||||
QTextCursor cursor = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor();
|
||||
if(cursor.selectedText() == sfind){
|
||||
cursor.insertText(sreplace);
|
||||
|
@ -351,8 +353,8 @@ void MainWindow::replace()
|
|||
|
||||
void MainWindow::replaceAll()
|
||||
{
|
||||
QString sfind = dialogFind->ui->lineEditFind->text();
|
||||
QString sreplace = dialogFind->ui->lineEditReplace->text();
|
||||
QString sfind = dialogFind->ui->lineEdit_find->text();
|
||||
QString sreplace = dialogFind->ui->lineEdit_replace->text();
|
||||
QTextCursor cursor = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->textCursor();
|
||||
cursor.setPosition(0,QTextCursor::MoveAnchor);
|
||||
((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->setTextCursor(cursor);
|
||||
|
@ -530,14 +532,9 @@ void MainWindow::updateCommand()
|
|||
QString s = ((QTextEdit*)(ui->mdiArea->currentSubWindow()->widget()))->toPlainText();
|
||||
QStringList SL = s.split("\n");
|
||||
QStringList SLI = SL.filter(QRegExp("^#include"));;
|
||||
// QStringList SLI;
|
||||
// foreach (const QString &str, SL) {
|
||||
// if (str.startsWith("#include"))
|
||||
// SLI += str;
|
||||
// }
|
||||
QString command = "";
|
||||
for(int i=0; i<SLI.length(); i++){
|
||||
qDebug() << SLI.at(i);
|
||||
//qDebug() << SLI.at(i);
|
||||
if (SLI.at(i).contains("#include <GL/")) {
|
||||
command = "g++ %1 -o %2 -l GL -l GLU -l glut";
|
||||
break;
|
||||
|
@ -559,13 +556,15 @@ void MainWindow::updateCommand()
|
|||
void MainWindow::printOutput()
|
||||
{
|
||||
QProcess *process = qobject_cast<QProcess*>(sender());
|
||||
//qDebug() << process->readAllStandardOutput();
|
||||
ui->textBrowser->append(process->readAllStandardOutput());
|
||||
QString s = QString(process->readAllStandardOutput());
|
||||
qDebug() << s;
|
||||
ui->textBrowser->append(s);
|
||||
}
|
||||
|
||||
void MainWindow::printError()
|
||||
{
|
||||
QProcess *process = qobject_cast<QProcess*>(sender());
|
||||
//qDebug() << process->readAllStandardError();
|
||||
ui->textBrowser->append(process->readAllStandardError());
|
||||
QString s = QString(process->readAllStandardError());
|
||||
qDebug() << s;
|
||||
ui->textBrowser->append(s);
|
||||
}
|
100
mdichild.cpp
100
mdichild.cpp
|
@ -9,30 +9,19 @@
|
|||
#include <QDebug>
|
||||
#include <QTextCodec>
|
||||
#include <QProcess>
|
||||
#include <QPainter>
|
||||
|
||||
MdiChild::MdiChild()
|
||||
MdiChild::MdiChild(QWidget *parent) : QPlainTextEdit(parent)
|
||||
{
|
||||
setViewportMargins(50, 0, 0, 0);
|
||||
QFontMetrics FM(font());
|
||||
int ts = 4;
|
||||
setTabStopWidth(ts * FM.boundingRect("*").width());
|
||||
connect(document(),SIGNAL(contentsChanged()),this,SLOT(onContentsChanged()));
|
||||
}
|
||||
connect(document(), SIGNAL(contentsChanged()), this, SLOT(contentsChange()));
|
||||
|
||||
void MdiChild::wheelEvent(QWheelEvent *e)
|
||||
{
|
||||
if(QApplication::keyboardModifiers() == Qt::ControlModifier){
|
||||
if(e->delta() > 0){
|
||||
zoomIn();
|
||||
}else{
|
||||
zoomOut();
|
||||
}
|
||||
}else{
|
||||
if(e->delta() > 0){
|
||||
verticalScrollBar()->setValue(verticalScrollBar()->value()-30);
|
||||
}else{
|
||||
verticalScrollBar()->setValue(verticalScrollBar()->value()+30);
|
||||
}
|
||||
}
|
||||
lineNumberArea = new LineNumberArea(this);
|
||||
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
|
||||
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
||||
}
|
||||
|
||||
bool MdiChild::loadFile(QString filename)
|
||||
|
@ -93,11 +82,24 @@ bool MdiChild::save()
|
|||
}
|
||||
}
|
||||
|
||||
void MdiChild::onContentsChanged()
|
||||
void MdiChild::contentsChange()
|
||||
{
|
||||
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)
|
||||
{
|
||||
if(e->key() == Qt::Key_ParenLeft){
|
||||
|
@ -115,5 +117,63 @@ void MdiChild::keyPressEvent(QKeyEvent *e)
|
|||
QTextCursor c = textCursor();
|
||||
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
|
||||
#define MDICHILD_H
|
||||
|
||||
#include <QTextEdit>
|
||||
#include <QPlainTextEdit>
|
||||
|
||||
class MdiChild : public QTextEdit
|
||||
class LineNumberArea;
|
||||
|
||||
class MdiChild : public QPlainTextEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MdiChild();
|
||||
MdiChild(QWidget *parent);
|
||||
bool loadFile(QString);
|
||||
bool save();
|
||||
QString path, scodec;
|
||||
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
||||
int lineNumberAreaWidth();
|
||||
|
||||
private:
|
||||
LineNumberArea *lineNumberArea;
|
||||
|
||||
protected:
|
||||
void wheelEvent(QWheelEvent*);
|
||||
void keyPressEvent(QKeyEvent *e);
|
||||
void resizeEvent(QResizeEvent* e);
|
||||
|
||||
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
|
||||
|
|
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