Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 1

郭志杰/serialplot

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (14)
master
v0.10.0
v0.9.1
v0.9.0
v0.8.1
v0.8.0
v0.7.1
v0.7
v0.6
v0.5.1
v0.5
v0.4
v0.3
v0.2
v0.1
master
Branches (1)
Tags (14)
master
v0.10.0
v0.9.1
v0.9.0
v0.8.1
v0.8.0
v0.7.1
v0.7
v0.6
v0.5.1
v0.5
v0.4
v0.3
v0.2
v0.1
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (14)
master
v0.10.0
v0.9.1
v0.9.0
v0.8.1
v0.8.0
v0.7.1
v0.7
v0.6
v0.5.1
v0.5
v0.4
v0.3
v0.2
v0.1
serialplot
/
src
/
commandedit.cpp
serialplot
/
src
/
commandedit.cpp
commandedit.cpp 4.28 KB
Copy Edit Raw Blame History
/*
Copyright © 2017 Hasan Yavuz Özderya
This file is part of serialplot.
serialplot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
serialplot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with serialplot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QKeyEvent>
#include <QtDebug>
#include "commandedit.h"
class HexCommandValidator : public QRegExpValidator
{
public:
explicit HexCommandValidator(QObject* parent = 0);
QValidator::State validate(QString & input, int & pos) const;
};
HexCommandValidator::HexCommandValidator(QObject* parent) :
QRegExpValidator(parent)
{
QRegExp regExp("^(?:(?:[0-9A-F]{2}[ ])+(?:[0-9A-F]{2}))|(?:[0-9A-F]{2})$");
setRegExp(regExp);
}
QValidator::State HexCommandValidator::validate(QString & input, int & pos) const
{
input = input.toUpper();
// don't let pos to be altered at this stage
int orgPos = pos;
auto r = QRegExpValidator::validate(input, pos);
pos = orgPos;
// try fixing up spaces
if (r != QValidator::Acceptable)
{
input = input.replace(" ", "");
input.replace(QRegExp("([0-9A-F]{2}(?!$))"), "\\1 ");
if (pos == input.size()-1) pos = input.size();
r = QRegExpValidator::validate(input, pos);
}
return r;
}
CommandEdit::CommandEdit(QWidget *parent) :
QLineEdit(parent)
{
hexValidator = new HexCommandValidator(this);
asciiValidator = new QRegExpValidator(QRegExp("[\\x0000-\\x007F]+"), this);
ascii_mode = true;
setValidator(asciiValidator);
}
CommandEdit::~CommandEdit()
{
delete hexValidator;
}
static QString unEscape(QString str);
static QString escape(QString str);
void CommandEdit::setMode(bool ascii)
{
ascii_mode = ascii;
if (ascii)
{
setValidator(asciiValidator);
auto hexText = text().remove(" ");
// try patching HEX string in case of missing nibble so that
// input doesn't turn into gibberish
if (hexText.size() % 2 == 1)
{
hexText.replace(hexText.size()-1, 1, "3F"); // 0x3F = '?'
qWarning() << "Broken byte in hex command is replaced. Check your command!";
}
setText(escape(QByteArray::fromHex(hexText.toLatin1())));
}
else
{
setValidator(hexValidator);
setText(unEscape(text()).toLatin1().toHex());
}
}
void CommandEdit::keyPressEvent(QKeyEvent * event)
{
if (ascii_mode)
{
QLineEdit::keyPressEvent(event);
return;
}
if (event->key() == Qt::Key_Backspace && !hasSelectedText())
{
int cursor = cursorPosition();
if (cursor != 0 && text()[cursor-1] == ' ')
{
setCursorPosition(cursor-1);
}
}
QLineEdit::keyPressEvent(event);
}
QString CommandEdit::unEscapedText()
{
return unEscape(text());
}
static QString unEscape(QString str)
{
const QMap<QString, QString> replacements({
{"\\\\", "\\"},
{"\\n", "\n"},
{"\\r", "\r"},
{"\\t", "\t"}
});
QString result;
int i = 0;
while (i < str.size())
{
bool found = false;
for (auto k : replacements.keys())
{
// has enough text left?
if (str.size() - i < 1) continue;
// try matching the key at current position
if (k == str.midRef(i, k.size()))
{
// append replacement
result += replacements[k];
i += k.size();
found = true;
break; // skip other keys
}
}
if (!found)
{
// append unmatched character
result += str[i];
i++;
}
}
return result;
}
static QString escape(QString str)
{
str.replace("\\", "\\\\");
str.replace("\n", "\\n");
str.replace("\r", "\\r");
str.replace("\t", "\\t");
return str;
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

THIS IS A MIRROR. Small and simple software for plotting data from serial port in realtime.
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kbytes/serialplot.git
git@gitee.com:kbytes/serialplot.git
kbytes
serialplot
serialplot
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /