Files
@ 51e636c4e69b
Branch filter:
Location: tempo-plotter/commandedit.cpp - annotation
51e636c4e69b
2.6 KiB
text/x-c++hdr
implemented sending hex command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 2d43eb4fe15c fc3cbb071069 2d43eb4fe15c fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 2d43eb4fe15c fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 fc3cbb071069 | /*
Copyright © 2015 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 "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]+"));
ascii_mode = true;
setValidator(asciiValidator);
}
CommandEdit::~CommandEdit()
{
delete hexValidator;
}
void CommandEdit::setMode(bool ascii)
{
ascii_mode = ascii;
if (ascii)
{
setValidator(asciiValidator);
}
else
{
setValidator(hexValidator);
}
}
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);
}
|