From ece4ce35eacb86922735281feb018bf0074f4c76 Mon Sep 17 00:00:00 2001 From: Srivats P Date: Wed, 31 Jan 2018 22:03:04 +0530 Subject: [PATCH] Make mac address editing more intuitive and easier Fixes #248 --- common/macedit.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/common/macedit.h b/common/macedit.h index 390c6a3..60c4d32 100644 --- a/common/macedit.h +++ b/common/macedit.h @@ -29,19 +29,32 @@ public: quint64 value(); void setValue(quint64 val); + +protected: + virtual void focusOutEvent(QFocusEvent *e); }; inline MacEdit::MacEdit(QWidget *parent) : QLineEdit(parent) { - QRegExp reMac("([0-9,a-f,A-F]{2,2}[:-]){5,5}[0-9,a-f,A-F]{2,2}"); + // Allow : or - as separator + QRegExp reMac("([0-9,a-f,A-F]{0,2}[:-]){5,5}[0-9,a-f,A-F]{0,2}"); setValidator(new QRegExpValidator(reMac, this)); } inline quint64 MacEdit::value() { - return text().remove(QChar(':')).toULongLong(NULL, 16); + QStringList bytes = text().split(QRegExp("[:-]")); + quint64 mac = 0; + + while (bytes.count() > 6) + bytes.removeLast(); + + for (int i = 0; i < bytes.count(); i++) + mac |= (bytes.at(i).toULongLong(NULL, 16) & 0xff) << (5-i)*8; + + return mac; } inline void MacEdit::setValue(quint64 val) @@ -50,5 +63,12 @@ inline void MacEdit::setValue(quint64 val) .replace(QRegExp("([0-9a-fA-F]{2}\\B)"), "\\1:").toUpper()); } +inline void MacEdit::focusOutEvent(QFocusEvent *e) +{ + // be helpful and show a well-formatted value on focus out + setValue(value()); + QLineEdit::focusOutEvent(e); +} + #endif