Changeset - e7af61f7cb69
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-10-07 04:54:41
hy@ozderya.net
add hexadecimal command validator
2 files changed with 57 insertions and 0 deletions:
0 comments (0 inline, 0 general)
commandwidget.cpp
Show inline comments
 
@@ -20,6 +20,44 @@
 
#include "commandwidget.h"
 
#include "ui_commandwidget.h"
 

	
 
#include <QRegExp>
 
#include <QRegExpValidator>
 

	
 
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}[ ])*");
 
    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::Invalid)
 
    {
 
        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;
 
}
 

	
 
CommandWidget::CommandWidget(QWidget *parent) :
 
    QWidget(parent),
 
    ui(new Ui::CommandWidget)
 
@@ -28,11 +66,15 @@ CommandWidget::CommandWidget(QWidget *pa
 

	
 
    connect(ui->pbDelete, &QPushButton::clicked, this, &CommandWidget::onDeleteClicked);
 
    connect(ui->pbSend, &QPushButton::clicked, this, &CommandWidget::onSendClicked);
 
    connect(ui->pbASCII, &QPushButton::toggled, this, &CommandWidget::onASCIIToggled);
 

	
 
    hexValidator = new HexCommandValidator(this);
 
}
 

	
 
CommandWidget::~CommandWidget()
 
{
 
    delete ui;
 
    delete hexValidator;
 
}
 

	
 
void CommandWidget::onDeleteClicked()
 
@@ -44,3 +86,15 @@ void CommandWidget::onSendClicked()
 
{
 
    emit sendCommand(ui->leCommand->text(), ui->pbASCII->isChecked());
 
}
 

	
 
void CommandWidget::onASCIIToggled(bool checked)
 
{
 
    if (checked)
 
    {
 
        ui->leCommand->setValidator(0);
 
    }
 
    else
 
    {
 
        ui->leCommand->setValidator(hexValidator);
 
    }
 
}
commandwidget.h
Show inline comments
 
@@ -22,6 +22,7 @@
 

	
 
#include <QWidget>
 
#include <QString>
 
#include <QValidator>
 

	
 
namespace Ui {
 
class CommandWidget;
 
@@ -41,10 +42,12 @@ signals:
 

	
 
private:
 
    Ui::CommandWidget *ui;
 
    QValidator* hexValidator;
 

	
 
private slots:
 
    void onDeleteClicked();
 
    void onSendClicked();
 
    void onASCIIToggled(bool checked);
 
};
 

	
 
#endif // COMMANDWIDGET_H
0 comments (0 inline, 0 general)