diff --git a/commandedit.cpp b/commandedit.cpp --- a/commandedit.cpp +++ b/commandedit.cpp @@ -72,6 +72,9 @@ CommandEdit::~CommandEdit() delete hexValidator; } +QString unEscape(QString str); +QString escape(QString str); + void CommandEdit::setMode(bool ascii) { ascii_mode = ascii; @@ -87,12 +90,13 @@ void CommandEdit::setMode(bool ascii) hexText.replace(hexText.size()-1, 1, "3F"); // 0x3F = '?' qWarning() << "Broken byte in hex command is replaced. Check your command!"; } - setText(QByteArray::fromHex(hexText.toLatin1())); + + setText(escape(QByteArray::fromHex(hexText.toLatin1()))); } else { setValidator(hexValidator); - setText(text().toLatin1().toHex()); + setText(unEscape(text()).toLatin1().toHex()); } } @@ -115,3 +119,24 @@ void CommandEdit::keyPressEvent(QKeyEven QLineEdit::keyPressEvent(event); } + +QString CommandEdit::unEscapedText() +{ + return unEscape(text()); +} + +QString unEscape(QString str) +{ + str.replace("\\n", "\n"); + str.replace("\\r", "\r"); + str.replace("\\t", "\t"); + return str; +} + +QString escape(QString str) +{ + str.replace("\n", "\\n"); + str.replace("\r", "\\r"); + str.replace("\t", "\\t"); + return str; +} diff --git a/commandedit.h b/commandedit.h --- a/commandedit.h +++ b/commandedit.h @@ -22,6 +22,7 @@ #include #include +#include class CommandEdit : public QLineEdit { @@ -31,6 +32,7 @@ public: explicit CommandEdit(QWidget *parent = 0); ~CommandEdit(); void setMode(bool ascii); // true = ascii, false = hex + QString unEscapedText(); // return unescaped text(), used in ascii_mode only private: bool ascii_mode; diff --git a/commandwidget.cpp b/commandwidget.cpp --- a/commandwidget.cpp +++ b/commandwidget.cpp @@ -60,7 +60,7 @@ void CommandWidget::onSendClicked() if (isASCIIMode()) { qDebug() << "Sending:" << command; - emit sendCommand(command.toLatin1()); + emit sendCommand(ui->leCommand->unEscapedText().toLatin1()); } else // hex mode {