Skip to content

Commit

Permalink
更新VT100控制字解析
Browse files Browse the repository at this point in the history
  • Loading branch information
SKiars committed Jul 23, 2019
1 parent 564ff18 commit ac359f8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 94 deletions.
45 changes: 0 additions & 45 deletions SerialTool/config/highlight/berry.json

This file was deleted.

2 changes: 1 addition & 1 deletion SerialTool/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define _STR_(s) #s
#define __STR(s) _STR_(s)

#define BUILD_VERSION _STR_(74638M)
#define BUILD_VERSION _STR_(87564M)
#define SOFTWARE_VERSION __STR(MAIN_VERSION)

#if QT_NO_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion SerialTool/language/zh_cn/settings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Auto Indent 自动缩进
Indentation Guides 缩进指南
Plot 绘图
Graphics Options 图形选项
Update Interv (ms) 刷新速率 (ms)
Update Interval (ms) 刷新速率 (ms)
Use OpenGL 使用OpenGL
Use Antialias 抗锯齿
2 changes: 1 addition & 1 deletion SerialTool/src/updatedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <QProcess>
#include <QFile>

#define LASTRELEASE_URL "https://api.github.com/repos/gztss/SerialTool/releases/latest"
#define LASTRELEASE_URL "https://api.github.com/repos/skiars/SerialTool/releases/latest"

UpdateDialog::UpdateDialog(QWidget *parent) :
QDialog(parent),
Expand Down
103 changes: 60 additions & 43 deletions SerialTool/src/views/terminal/qvterminal/qvterminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ QVTerminal::QVTerminal(QWidget *parent)
_echo = false;
_crlf = false;
_state = QVTerminal::Text;

setFormat(QVTCharFormat());

_layout = new QVTLayout();

_pasteAction = new QAction("Paste", this);
_pasteAction->setShortcut(QKeySequence("Ctrl+V"));
connect(_pasteAction, &QAction::triggered, this, &QVTerminal::paste);
Expand All @@ -36,14 +33,12 @@ QVTerminal::QVTerminal(QWidget *parent)

QVTerminal::~QVTerminal()
{

}

void QVTerminal::setIODevice(QIODevice *device)
{
_device = device;
if (_device)
{
if (_device) {
connect(_device, &QIODevice::readyRead, this, &QVTerminal::read);
read();
}
Expand All @@ -55,11 +50,9 @@ void QVTerminal::appendData(const QByteArray &data)

setUpdatesEnabled(false);
QByteArray::const_iterator it = data.cbegin();
while (it != data.cend())
{
while (it != data.cend()) {
QChar c = *it;
switch (_state)
{
switch (_state) {
case QVTerminal::Text:
switch (c.unicode()) {
case '\033':
Expand All @@ -75,13 +68,12 @@ void QVTerminal::appendData(const QByteArray &data)
case '\n':
appendString(text);
text.clear();
_layout->appendLine();
_cursorPos.setY(_cursorPos.y() + 1);
moveCursor(0, 1);
break;
case '\b':
appendString(text);
text.clear();
_cursorPos.setX(_cursorPos.x() > 0 ? _cursorPos.x() - 1 : 0);
moveCursor(-1, 0);
break;
default:
if (c.isPrint()) {
Expand Down Expand Up @@ -135,10 +127,8 @@ void QVTerminal::appendData(const QByteArray &data)
it++;
}
appendString(text);

verticalScrollBar()->setRange(0, _ch * (_layout->lineCount() + 1) - viewport()->size().height());
verticalScrollBar()->setValue(verticalScrollBar()->maximum());

setUpdatesEnabled(true);
update();
}
Expand All @@ -149,11 +139,37 @@ void QVTerminal::formatChar(const QChar &c)
case 'K':
clearToEnd();
break;
case 'A':
moveCursor(0, -_formatValue);
break;
case 'B':
moveCursor(0, _formatValue);
break;
case 'C':
moveCursor(_formatValue, 0);
break;
case 'D':
moveCursor(-_formatValue, 0);
break;
default:
break;
}
}

void QVTerminal::moveCursor(int xpos, int ypos)
{
_cursorPos += QPoint(xpos, ypos);
if (_cursorPos.x() < 0) {
_cursorPos.setX(0);
}
if (_cursorPos.y() < 0) {
_cursorPos.setY(0);
}
if (_cursorPos.y() >= _layout->lineCount()) {
_layout->appendLine(_cursorPos.y() - _layout->lineCount() + 1);
}
}

void QVTerminal::paste()
{
QByteArray data;
Expand All @@ -163,24 +179,15 @@ void QVTerminal::paste()

QColor QVTerminal::vt100color(char c)
{
switch (c)
{
case '1':
return QColor(Qt::red);
case '2':
return QColor(Qt::green);
case '3':
return QColor(Qt::yellow);
case '4':
return QColor(Qt::blue);
case '5':
return QColor(Qt::magenta);
case '6':
return QColor(Qt::cyan);
case '7':
return QColor(Qt::white);
default:
return QColor(Qt::black);
switch (c) {
case '1': return QColor(Qt::red);
case '2': return QColor(Qt::green);
case '3': return QColor(Qt::yellow);
case '4': return QColor(Qt::blue);
case '5': return QColor(Qt::magenta);
case '6': return QColor(Qt::cyan);
case '7': return QColor(Qt::white);
default: return QColor(Qt::black);
}
}

Expand All @@ -194,8 +201,7 @@ void QVTerminal::read()

void QVTerminal::appendString(QString str)
{
foreach (QChar c, str)
{
foreach (QChar c, str) {
QVTChar termChar(c, _curentFormat);
_layout->lineAt(_cursorPos.y()).append(termChar, _cursorPos.x());
_cursorPos.setX(_cursorPos.x() + 1);
Expand Down Expand Up @@ -226,8 +232,9 @@ void QVTerminal::setCrlf(bool crlf)
void QVTerminal::writeData(QByteArray data)
{
_device->write(data);
if (_echo)
if (_echo) {
appendData(data);
}
}

bool QVTerminal::echo() const
Expand All @@ -254,6 +261,15 @@ void QVTerminal::setFormat(const QVTCharFormat &format)
_ch = fm.height();
}

bool QVTerminal::event(QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
keyPressEvent(static_cast<QKeyEvent*>(event));
return true;
}
return QAbstractScrollArea::event(event);
}

void QVTerminal::keyPressEvent(QKeyEvent *event)
{
QByteArray data;
Expand All @@ -271,10 +287,13 @@ void QVTerminal::keyPressEvent(QKeyEvent *event)
data.append("\033[D");
break;
case Qt::Key_Home:
data.append(0x01);
data.append('\x01');
break;
case Qt::Key_End:
data.append(0x05);
data.append('\x05');
break;
case Qt::Key_Tab:
data.append('\t');
break;
case Qt::Key_Backspace:
data.append('\b');
Expand All @@ -295,7 +314,6 @@ void QVTerminal::paintEvent(QPaintEvent */* paintEvent */)
p.setPen(QColor(187, 187, 187));
p.setBrush(QColor(0x23, 0x26, 0x29));
p.setFont(*_format.font());
p.setRenderHint(QPainter::Antialiasing);

p.fillRect(viewport()->rect(), QColor(0x23, 0x26, 0x29));

Expand All @@ -320,14 +338,13 @@ void QVTerminal::paintEvent(QPaintEvent */* paintEvent */)
pos.setX(0);
for (auto vtc : _layout->lineAt(l).chars()) {
p.setPen(pos == curPos ? vtc.background() : vtc.foreground());
p.drawText(QRect(pos, QSize(_cw, _ch)).normalized(), Qt::AlignCenter, QString(vtc.c()));
//p.setBrush(QBrush());
//p.drawRect(QRect(pos, QSize(_cw, -_ch)));
p.drawText(QRect(pos, QSize(_cw, _ch)), Qt::AlignCenter, vtc.c());
p.setBrush(QBrush());
//p.drawRect(QRect(pos, QSize(_cw, _ch)));
pos.setX(pos.x() + _cw);
}
pos.setY(pos.y() + _ch);
}

}

void QVTerminal::resizeEvent(QResizeEvent */* event */)
Expand Down
2 changes: 2 additions & 0 deletions SerialTool/src/views/terminal/qvterminal/qvterminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected slots:

private:
void formatChar(const QChar &c);
void moveCursor(int xpos, int ypos);

private:
QIODevice *_device;
Expand Down Expand Up @@ -76,6 +77,7 @@ protected slots:

// QWidget interface
protected:
virtual bool event(QEvent *event);
virtual void keyPressEvent(QKeyEvent *event);
virtual void paintEvent(QPaintEvent *event);
virtual void resizeEvent(QResizeEvent *event);
Expand Down
6 changes: 4 additions & 2 deletions SerialTool/src/views/terminal/qvterminal/qvtlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ QVTLine &QVTLayout::lineAt(int i)
return _lines[i];
}

void QVTLayout::appendLine()
void QVTLayout::appendLine(int count)
{
_lines.append(QVTLine());
while (count--) {
_lines.append(QVTLine());
}
}
2 changes: 1 addition & 1 deletion SerialTool/src/views/terminal/qvterminal/qvtlayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class QVTLayout
int lineCount() const;
QVTLine &lineAt(int i);

void appendLine();
void appendLine(int count = 1);

protected:
QList< QVTLine > _lines;
Expand Down

0 comments on commit ac359f8

Please sign in to comment.