-
-
Save cmaughan/88b73b1c0d7727f786c64e50b875ce92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "QNumberEdit.h" | |
QString QNumberEdit::PREFIX = "0x"; | |
QNumberEdit::QNumberEdit(QWidget *parent) | |
: QLineEdit(parent) | |
{ | |
QRegExp re("("+PREFIX+")?[0-9A-Fa-f]+"); | |
validator = new QRegExpValidator(re, this); | |
QRegExpValidator *v = new QRegExpValidator(re); | |
setValidator( v ); | |
} | |
bool QNumberEdit::hasPrefix(const QString &text) const | |
{ | |
if (text.startsWith(PREFIX, Qt::CaseInsensitive)) { | |
return true; | |
} | |
return false; | |
} | |
int QNumberEdit::getBase(const QString &text) const | |
{ | |
int defaultbase = 10; | |
if (text.length() == 0) return defaultbase; | |
if (hasPrefix(text)) return 16; | |
if (text.contains(QRegExp("[A-Fa-f]+"))) return 16; | |
return defaultbase; | |
} | |
void QNumberEdit::leaveEvent ( QEvent * event ) | |
{ | |
QString text = this->text(); | |
if (!hasPrefix(text) && getBase(text) == 16) { | |
this->setText(PREFIX + text); | |
} | |
} | |
QString QNumberEdit::textFromValue(long long value, int base) const | |
{ | |
return QString::number(value, base).toUpper(); | |
} | |
long long QNumberEdit::valueFromText(const QString &text, bool *isValid) const | |
{ | |
bool isOk; | |
int base = this->getBase(text); | |
printf("Base = %d\n", base); | |
int val = text.toLongLong(&isOk, base); | |
if (isValid != NULL) (*isValid) = isOk; | |
if (isOk) return val; | |
return 0; //DEFAULT | |
} | |
long long QNumberEdit::value(bool *isValid) | |
{ | |
QString text = this->text(); | |
return valueFromText(text, isValid); | |
} | |
void QNumberEdit::setValue(long long value, int base) | |
{ | |
QString text = textFromValue(value, base); | |
return setText(text); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <QtCore> | |
#include <QtGui> | |
class QNumberEdit : public QLineEdit | |
{ | |
Q_OBJECT | |
Q_PROPERTY(long long value READ value WRITE setValue) | |
public: | |
QNumberEdit(QWidget *parent); | |
~QNumberEdit() {} | |
long long value(bool *isValid = NULL); | |
void setValue(long long value, int base = 10); | |
protected: | |
long long valueFromText(const QString &text, bool *isValid) const; | |
QString textFromValue(long long value, int base) const; | |
protected: | |
void leaveEvent ( QEvent * event ); | |
int getBase(const QString &text) const; | |
bool hasPrefix(const QString &text) const; | |
private: | |
static QString PREFIX; | |
QRegExpValidator *validator; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment