Verify and fix MacAddress type field edit

Use QRegularExpression instead of QRegExp as the latter is deprecated in
Qt6
This commit is contained in:
Srivats P 2021-12-05 12:09:02 +05:30
parent d3400f0897
commit 9385b31bc5
4 changed files with 52 additions and 12 deletions

View File

@ -55,3 +55,19 @@ void FieldEdit::setRange(quint64 min, quint64 max)
{
uint64Validator_.setRange(min, max);
}
QString FieldEdit::text() const
{
QString str = QLineEdit::text();
switch (type_) {
case kMacAddress:
str.remove(QRegularExpression("[:-]"));
str.prepend("0x");
break;
default:
break;
}
return str;
}

View File

@ -42,6 +42,8 @@ public:
void setType(FieldType type);
void setRange(quint64 min, quint64 max);
QString text() const;
private:
FieldType type_{kUInt64};

View File

@ -92,7 +92,13 @@ void FindReplaceDialog::on_field_currentIndexChanged(int index)
FieldAttrib fieldAttrib = fieldAttrib_.at(index);
qDebug("XXXXXX %s bitSize %d max %llx", qPrintable(field->currentText()),
// Use heuristics to determine field type
if (fieldAttrib.bitSize == 48) {
findValue->setType(FieldEdit::kMacAddress);
replaceValue->setType(FieldEdit::kMacAddress);
} else {
qDebug("XXXXXX %s bitSize %d max %llx",
qPrintable(field->currentText()),
fieldAttrib.bitSize, fieldAttrib.max);
findValue->setType(FieldEdit::kUInt64);
@ -101,6 +107,7 @@ void FindReplaceDialog::on_field_currentIndexChanged(int index)
replaceValue->setType(FieldEdit::kUInt64);
replaceValue->setRange(0, fieldAttrib.max);
}
}
void FindReplaceDialog::on_buttonBox_accepted()
{

View File

@ -20,18 +20,33 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#ifndef _MAC_ADDRESS_VALIDATOR_H
#define _MAC_ADDRESS_VALIDATOR_H
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
class MacAddressValidator : public QRegExpValidator
// Allow : or - as separator
class MacAddressValidator : public QRegularExpressionValidator
{
public:
MacAddressValidator(QObject *parent = 0)
: QRegExpValidator(parent)
: QRegularExpressionValidator(
QRegularExpression(
"([0-9,a-f,A-F]{2,2}[:-]){5,5}[0-9,a-f,A-F]{2,2}"),
parent)
{
// Allow : or - as separator
setRegExp(QRegExp("([0-9,a-f,A-F]{2,2}[:-]){5,5}[0-9,a-f,A-F]{2,2}"));
}
~MacAddressValidator() {}
virtual void fixup(QString &input) const
{
QStringList bytes = input.split(QRegularExpression("[:-]"),
QString::SkipEmptyParts);
if (!bytes.isEmpty() && bytes.constLast().size() == 1)
bytes.last().prepend("0");
while (bytes.count() < 6)
bytes.append("00");
input = bytes.join(":");
}
};
#endif