Changeset - b1ac644087e5
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-10-10 14:48:42
hy@ozderya.net
when switching from hex to ascii, patch hex string in case of missing nibble
1 file changed with 12 insertions and 1 deletions:
0 comments (0 inline, 0 general)
commandedit.cpp
Show inline comments
 
@@ -16,12 +16,14 @@
 
  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);
 
@@ -73,13 +75,22 @@ CommandEdit::~CommandEdit()
 
void CommandEdit::setMode(bool ascii)
 
{
 
    ascii_mode = ascii;
 
    if (ascii)
 
    {
 
        setValidator(asciiValidator);
 
        setText(QByteArray::fromHex(text().remove(" ").toLatin1()));
 

	
 
        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(QByteArray::fromHex(hexText.toLatin1()));
 
    }
 
    else
 
    {
 
        setValidator(hexValidator);
 
        setText(text().toLatin1().toHex());
 
    }
0 comments (0 inline, 0 general)