feat: 添加ElaWidgetTool库

This commit is contained in:
sleepwithoutbz
2025-09-19 22:40:52 +08:00
parent 5f93e8caf6
commit 4eef5c7fd5
407 changed files with 36325 additions and 7 deletions

View File

@@ -0,0 +1,86 @@
#include "ElaIntValidator.h"
ElaIntValidator::ElaIntValidator(QObject* parent)
: QIntValidator{parent}
{
_pIsHexMode = false;
}
ElaIntValidator::ElaIntValidator(int bottom, int top, QObject* parent)
: QIntValidator{bottom, top, parent}
{
_pIsHexMode = false;
}
ElaIntValidator::~ElaIntValidator()
{
}
QValidator::State ElaIntValidator::validate(QString& input, int& pos) const
{
QString inputCopy = input;
if (_pIsHexMode)
{
inputCopy.remove("#");
if (!inputCopy.isEmpty())
{
bool isInt = false;
int value = inputCopy.toInt(&isInt, 16);
if (!isInt)
{
return QValidator::Invalid;
}
if (value < bottom() || value > top())
{
return QValidator::Invalid;
}
int topLength = QString::number(top(), 16).length();
if (inputCopy.length() > topLength)
{
return QValidator::Invalid;
}
}
inputCopy.prepend("#");
}
else
{
if (input.isEmpty())
{
return QValidator::Intermediate;
}
bool isInt = false;
int value = inputCopy.toInt(&isInt);
if (!isInt)
{
return QValidator::Invalid;
}
if (value < bottom() || value > top())
{
return QValidator::Invalid;
}
}
input = inputCopy;
return QValidator::Acceptable;
}
void ElaIntValidator::fixup(QString& input) const
{
if (_pIsHexMode)
{
QString inputComplete = _completeInput(input, QString::number(top(), 16).length());
input = QString("#") + inputComplete;
}
else
{
input = QString::number(bottom());
}
}
QString ElaIntValidator::_completeInput(QString input, int length) const
{
while (input.length() < length)
{
input.prepend("0");
}
return input;
}