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
 
@@ -10,24 +10,26 @@
 

	
 
  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)
 
{
 
@@ -67,25 +69,34 @@ CommandEdit::CommandEdit(QWidget *parent
 

	
 
CommandEdit::~CommandEdit()
 
{
 
    delete hexValidator;
 
}
 

	
 
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());
 
    }
 
}
 

	
 
void CommandEdit::keyPressEvent(QKeyEvent * event)
 
{
 
    if (ascii_mode)
 
    {
0 comments (0 inline, 0 general)