Skip to content

Commit c59ef7b

Browse files
author
Mich
committed
Added line numbers to textview - replaced textBrowser with plainTextEdit
1 parent 71dc646 commit c59ef7b

File tree

7 files changed

+245
-66
lines changed

7 files changed

+245
-66
lines changed

src/QtSerialMonitor.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
3030
CONFIG += c++11
3131

3232
SOURCES += \
33+
codeeditor.cpp \
3334
filereader.cpp \
3435
infodialog.cpp \
3536
logger.cpp \
@@ -42,6 +43,7 @@ SOURCES += \
4243

4344
HEADERS += \
4445
../../../../../../Downloads/QCustomPlot.tar/qcustomplot/qcustomplot.h \
46+
codeeditor.h \
4547
config.h \
4648
filereader.h \
4749
infodialog.h \

src/codeeditor.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <QtWidgets>
2+
#include "codeeditor.h"
3+
4+
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
5+
{
6+
lineNumberArea = new LineNumberArea(this);
7+
8+
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
9+
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
10+
connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
11+
12+
updateLineNumberAreaWidth(0);
13+
// highlightCurrentLine();
14+
}
15+
16+
int CodeEditor::lineNumberAreaWidth()
17+
{
18+
int digits = 1;
19+
int max = qMax(1, blockCount());
20+
21+
while (max >= 10)
22+
{
23+
max /= 10;
24+
++digits;
25+
}
26+
27+
int space = 3 + fontMetrics().horizontalAdvance(" ") * digits;
28+
29+
return space;
30+
}
31+
32+
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
33+
{
34+
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
35+
}
36+
37+
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
38+
{
39+
if (dy)
40+
lineNumberArea->scroll(0, dy);
41+
else
42+
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
43+
44+
if (rect.contains(viewport()->rect()))
45+
updateLineNumberAreaWidth(0);
46+
}
47+
48+
void CodeEditor::resizeEvent(QResizeEvent *e)
49+
{
50+
QPlainTextEdit::resizeEvent(e);
51+
52+
QRect cr = contentsRect();
53+
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
54+
}
55+
56+
void CodeEditor::highlightCurrentLine()
57+
{
58+
QList<QTextEdit::ExtraSelection> extraSelections;
59+
60+
if (!isReadOnly())
61+
{
62+
QTextEdit::ExtraSelection selection;
63+
64+
QColor lineColor = QColor(Qt::yellow).lighter(160);
65+
66+
selection.format.setBackground(lineColor);
67+
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
68+
selection.cursor = textCursor();
69+
selection.cursor.clearSelection();
70+
extraSelections.append(selection);
71+
}
72+
73+
setExtraSelections(extraSelections);
74+
}
75+
76+
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
77+
{
78+
QPainter painter(lineNumberArea);
79+
painter.fillRect(event->rect(), Qt::lightGray);
80+
81+
QTextBlock block = firstVisibleBlock();
82+
int blockNumber = block.blockNumber();
83+
int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top();
84+
int bottom = top + (int)blockBoundingRect(block).height();
85+
86+
while (block.isValid() && top <= event->rect().bottom())
87+
{
88+
if (block.isVisible() && bottom >= event->rect().top())
89+
{
90+
QString number = QString::number(blockNumber + 1);
91+
painter.setPen(Qt::black);
92+
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), Qt::AlignLeft, number);
93+
}
94+
95+
block = block.next();
96+
top = bottom;
97+
bottom = top + (int)blockBoundingRect(block).height();
98+
++blockNumber;
99+
}
100+
}

src/codeeditor.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef CODEEDITOR_H
2+
#define CODEEDITOR_H
3+
4+
#include <QPlainTextEdit>
5+
6+
QT_BEGIN_NAMESPACE
7+
class QPaintEvent;
8+
class QResizeEvent;
9+
class QSize;
10+
class QWidget;
11+
QT_END_NAMESPACE
12+
13+
class LineNumberArea;
14+
15+
class CodeEditor : public QPlainTextEdit
16+
{
17+
Q_OBJECT
18+
19+
public:
20+
CodeEditor(QWidget *parent = nullptr);
21+
22+
void lineNumberAreaPaintEvent(QPaintEvent *event);
23+
int lineNumberAreaWidth();
24+
25+
protected:
26+
void resizeEvent(QResizeEvent *event) override;
27+
28+
private slots:
29+
void updateLineNumberAreaWidth(int newBlockCount);
30+
void highlightCurrentLine();
31+
void updateLineNumberArea(const QRect &, int);
32+
33+
private:
34+
QWidget *lineNumberArea;
35+
};
36+
37+
class LineNumberArea : public QWidget
38+
{
39+
public:
40+
LineNumberArea(CodeEditor *editor) : QWidget(editor)
41+
{
42+
codeEditor = editor;
43+
}
44+
45+
QSize sizeHint() const override
46+
{
47+
return QSize(codeEditor->lineNumberAreaWidth(), 0);
48+
}
49+
50+
protected:
51+
void paintEvent(QPaintEvent *event) override
52+
{
53+
codeEditor->lineNumberAreaPaintEvent(event);
54+
}
55+
56+
private:
57+
CodeEditor *codeEditor;
58+
};
59+
60+
#endif

src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CONFIG_H
22
#define CONFIG_H
33

4-
#define VERSION "1.2"
4+
#define VERSION "1.3"
55

66
#define CHANGELOG_TEXT "" //"Changelog - version " VERSION ": \n"
77

src/mainwindow.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ void MainWindow::processTable(QStringList labels, QList<double> values)
484484
void MainWindow::on_printIntroChangelog() // TODO
485485
{
486486
ui->pushButtonTextLogToggle->setChecked(false);
487-
ui->textBrowserLogs->append(INTRO_TEXT);
488-
ui->textBrowserLogs->append("\n" CHANGELOG_TEXT);
487+
ui->textBrowserLogs->appendPlainText(INTRO_TEXT);
488+
ui->textBrowserLogs->appendPlainText("\n" CHANGELOG_TEXT);
489489

490490
// ui->textBrowserLogs->horizontalScrollBar()->setValue(0);
491491
}
@@ -839,7 +839,7 @@ void MainWindow::addLog(QString text)
839839
if (ui->checkBoxShowTime->isChecked())
840840
text = currentDateTime + text;
841841

842-
ui->textBrowserLogs->append(text);
842+
ui->textBrowserLogs->appendPlainText(text);
843843

844844
// ui->textBrowserLogs->insertPlainText(text);
845845
// ui->textBrowserLogs->moveCursor(QTextCursor::MoveOperation::End, QTextCursor::MoveMode::MoveAnchor);
@@ -869,7 +869,7 @@ void MainWindow::addLogBytes(QString prefix, QByteArray bytes, bool hexToBinary)
869869
if (ui->checkBoxShowTime->isChecked())
870870
bytesText = currentDateTime + bytesText;
871871

872-
ui->textBrowserLogs->append(bytesText);
872+
ui->textBrowserLogs->appendPlainText(bytesText);
873873
}
874874
}
875875

@@ -1330,9 +1330,9 @@ void MainWindow::on_highlighLog(QString searchString)
13301330
void MainWindow::on_checkBoxWrapText_toggled(bool checked)
13311331
{
13321332
if (checked)
1333-
ui->textBrowserLogs->setLineWrapMode(QTextBrowser::LineWrapMode::WidgetWidth);
1333+
ui->textBrowserLogs->setLineWrapMode(QPlainTextEdit::LineWrapMode::WidgetWidth);
13341334
else
1335-
ui->textBrowserLogs->setLineWrapMode(QTextBrowser::LineWrapMode::NoWrap);
1335+
ui->textBrowserLogs->setLineWrapMode(QPlainTextEdit::LineWrapMode::NoWrap);
13361336
}
13371337

13381338
void MainWindow::on_checkBoxEnableTracer_toggled(bool checked)
@@ -2172,3 +2172,11 @@ void MainWindow::closeEvent(QCloseEvent *event)
21722172
event->accept();
21732173
}
21742174
}
2175+
2176+
void MainWindow::on_comboBoxFormat_currentIndexChanged(int index)
2177+
{
2178+
if (index == 0)
2179+
ui->comboBoxTextProcessing->setEnabled(true);
2180+
else
2181+
ui->comboBoxTextProcessing->setEnabled(false);
2182+
}

src/mainwindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private slots:
8585
void on_checkBoxWrapText_toggled(bool checked);
8686
void on_clearGraphSelection();
8787
void on_comboBoxClockSource_currentIndexChanged(int index);
88+
void on_comboBoxFormat_currentIndexChanged(int index);
8889
void on_comboBoxGraphDisplayMode_currentIndexChanged(const QString &arg1);
8990
void on_comboBoxLoggingMode_currentIndexChanged(int index);
9091
void on_comboBoxSendReturnPressedSlot();

0 commit comments

Comments
 (0)