logplus/Workflow/WFCrystal/Crystal/include/PaiValidator.h

396 lines
11 KiB
C
Raw Normal View History

2026-01-16 17:18:41 +08:00
/**
* @file PaiValidator.h
* @brief IDEmail,
* @date 2014-12-05
*/
#ifndef PAI_FRAME_CRYSTAL_PAIVALIDATOR_H
#define PAI_FRAME_CRYSTAL_PAIVALIDATOR_H
#include <limits>
#include <QRegExpValidator>
#include <QToolTip>
namespace pai
{
namespace gui
{
/**
* @class PaiNameValidator
* @brief 使 Validator
*/
class PaiNameValidator : public QRegExpValidator
{
public:
/**
* @brief FailureMessage一致,15930(28931)(31250)(32741)
* @param[in] pParent
*/
PaiNameValidator(QObject *pParent) :
QRegExpValidator(QRegExp("[\u4e00-\u9fa50-9a-zA-Z_\\-\\.]+$"), pParent)
{
}
/**
* @brief Validator失败后要提示的信息
* @return
*/
QString GetFailureMessage()
{
return QObject::tr("Please Use a-z, A-Z, 0-9,_,.,- and Chinese only!");
}
};
/**
* @class PaiIntValidator
* @brief Validator
*/
class PaiIntValidator : public QIntValidator
{
public:
/**
* @brief
* @param[in] pParent
*/
PaiIntValidator(QObject *pParent) :
QIntValidator(pParent)
{
m_bSupportNullString = false;
}
/**
* @brief
* @param[in] min
* @param[in] max
* @param[in] pParent
*/
PaiIntValidator(int min, int max, QObject *pParent) :
QIntValidator(min, max, pParent)
{
m_bSupportNullString = false;
}
/**
* @brief Validator是否支持空字符
* @param[in] support true支持false不支持
*/
void SetSupportNullString(bool support)
{
m_bSupportNullString = support;
}
/**
* @brief
* @param[in] input
* @param[in] pos
*/
virtual QValidator::State validate(QString & input, int & pos) const
{
// 多位数字首位不能为0
if((pos == 2) && (input.count() > 1) && (input.left(1) == "0"))
{
return QValidator::Invalid;
}
// 不能出现逗号
if(input.contains(","))
{
return QValidator::Invalid;
}
if(m_bSupportNullString == true && input.isEmpty())
{
return QValidator::Acceptable;
}
return QIntValidator::validate(input, pos);
}
private:
bool m_bSupportNullString; ///< 该变量表示是否支持空字符,默认不支持
};
/**
* @class PaiIdValidator
* @brief id
*/
class PaiIdValidator : public QRegExpValidator
{
public:
/**
* @brief ID只能输入a-z A-Z 1-19
* @param[in] pParent
*/
PaiIdValidator(QObject *pParent) :
QRegExpValidator(QRegExp("[a-zA-Z0-9]{1,19}"), pParent)
{
}
};
/**
* @class PaiEmailValidator
* @brief email的时候对一些字符的限定
*/
class PaiEmailValidator : public QRegExpValidator
{
public:
/**
* @brief "@" "."
* @param[in] pParent
*/
PaiEmailValidator(QObject *pParent) :
QRegExpValidator(QRegExp("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"), pParent)
{
}
};
/**
* @class PaiTimeValidator
* @brief TIME的 Validator
*/
class PaiTimeValidator : public QValidator
{
public:
/**
* @brief
* @param[in] pParent
*/
PaiTimeValidator(QObject *pParent = NULL) :
QValidator(pParent)
{
}
/**
* @brief
* @param[in] input
*/
virtual QValidator::State validate(QString & input, int & /*pos*/) const
{
QRegExp regx = QRegExp("[0-9.dhms ]+$");
QString copyText = input;
QWidget *pWidget = qobject_cast< QWidget * > (parent());
QWidget *pParentWidget = NULL;
if(pWidget)
{
pParentWidget = pWidget->parentWidget();
}
//输入为空给出输入提示
if(copyText == "")
{
if(pParentWidget)
{
QToolTip::showText(pParentWidget->mapToGlobal(pWidget->pos()) + QPoint(pWidget->width() / 3,
pWidget->height() / 2),
"valid format00d 00h 00m 00s ",
pWidget);
}
return QValidator::Acceptable;
}
//输入非法字符
else if(!regx.exactMatch(copyText))
{
if(pParentWidget)
{
QToolTip::showText(pParentWidget->mapToGlobal(pWidget->pos()) + QPoint(pWidget->width() / 3,
pWidget->height() / 2),
"valid format00d 00h 00m 00s ",
pWidget);
}
return QValidator::Invalid;
}
else
{
//dhms顺序正确
int d = copyText.indexOf('d');
int h = copyText.indexOf('h');
int m = copyText.indexOf('m');
int s = copyText.indexOf('s');
if(!((d <= h || m == -1) && (h <= m || m == -1) && (m <= s || s == -1)))
{
if(pParentWidget)
{
QToolTip::showText(pParentWidget->mapToGlobal(pWidget->pos()) + QPoint(pWidget->width() / 3,
pWidget->height() / 2),
"valid format00d 00h 00m 00s ",
pWidget);
}
return QValidator::Invalid;
}
//dhms不允许重复
d = copyText.count('d');
h = copyText.count('h');
m = copyText.count('m');
s = copyText.count('s');
if(!(d <= 1 && h <= 1 && m <= 1 && s <= 1))
{
if(pParentWidget)
{
QToolTip::showText(pParentWidget->mapToGlobal(pWidget->pos()) + QPoint(pWidget->width() / 3,
pWidget->height() / 2),
"valid format00d 00h 00m 00s ",
pWidget);
}
return QValidator::Invalid;
}
// 输入s后不允许在输入
copyText.remove(" ");
int size = copyText.size();
if(size - 2 >= 0)
{
if(copyText.at(size - 2) == 's')
{
if(pParentWidget)
{
QToolTip::showText(pParentWidget->mapToGlobal(pWidget->pos()) + QPoint(pWidget->width() / 3,
pWidget->height() / 2),
"valid format00d 00h 00m 00s ",
pWidget);
}
return QValidator::Invalid;
}
}
}
return QValidator::Acceptable;
}
};
/**
* @class PaiPasswordValidator
* @brief
*/
class PaiPasswordValidator : public QRegExpValidator
{
public:
/**
* @brief ID只能输入a-z A-Z
* @param[in] pParent
*/
PaiPasswordValidator(QObject *pParent) :
QRegExpValidator(QRegExp(
"[a-zA-Z0-9\
\\`\\!\\@\\#\\$\\%\\^\\&\
\\*\\(\\)\\_\\+\\-\\=\\{\
\\}\\|\\[\\]\\\\\\:\"\\;\\'\
\\<\\>\\?\\,\\.\\/]{0,}"),
pParent)
{
}
};
/**
* @class PaiPhoneNumberValidator
* @brief
* + + + + -"分机号码"
*/
class PaiPhoneNumberValidator : public QRegExpValidator
{
public:
/**
* @brief ++16+(1~3)16+ +
* @param[in] pParent
*/
PaiPhoneNumberValidator(QObject *pParent) :
QRegExpValidator(QRegExp("^(\\+\\d{1,6}-)?((\\(\\d{1,3}\\))?(\\d{1,6}-))?(\\d{1,15})(-\\d{1,5})?$"),
pParent)
{
}
/**
* @brief
* @param[in] input
* @param[in] pos
*/
virtual QRegExpValidator::State validate(QString & input, int & pos) const
{
if(QRegExpValidator::validate(input, pos) == QRegExpValidator::Invalid)
{
return QRegExpValidator::Invalid;
}
else // 在合法的基础上判断是否为全'0'与'-'的情况
{
QString strInput = input;
if(strInput.remove('0').remove('-').isEmpty())
{
return QRegExpValidator::Intermediate;
}
else
{
return QRegExpValidator::validate(input, pos);
}
}
}
};
/**
* @class PaiFloatValidator
* @brief float类型数据填写进行限定
*/
class PaiFloatValidator : public QDoubleValidator
{
public:
/**
* @brief
* @param[in] pParent
* @param[in] decimal
*/
PaiFloatValidator(QObject *pParent, int decimal = 2) :
QDoubleValidator(pParent)
{
this->setNotation(QDoubleValidator::StandardNotation); // 设置不接受科学计数法输入
this->setTop(std::numeric_limits< float >::max()); // 防止输入数字值越界
this->setDecimals(decimal); // 设置小数点后位数
}
/**
* @brief
* @param[in] top
* @param[in] bottom
* @param[in] pParent
* @param[in] decimal
*/
PaiFloatValidator(double top, double bottom, QObject *pParent, int decimal = 2) :
QDoubleValidator(top, bottom, decimal, pParent)
{
this->setNotation(QDoubleValidator::StandardNotation); // 设置不接受科学计数法输入
}
/**
* @brief
* @param[in] input
* @param[in] pos
*/
virtual QDoubleValidator::State validate(QString & input, int & pos) const
{
// 正数不能连续输入0
if((pos == 2) && (input.count() >= 2) && (input.left(2) == "00"))
{
return QDoubleValidator::Invalid;
}
// 负数不能连续输入0
if((pos == 3) && (input.count() >= 3) && (input.left(3) == "-00"))
{
return QDoubleValidator::Invalid;
}
if(input.isEmpty() || (input == "-"))//允许全选删除以及输入小数点“0.” “-0.”
{
return QDoubleValidator::validate(input, pos);
}
bool ok = false;
input.toFloat(&ok);
if(ok)
{
return QDoubleValidator::validate(input, pos);
}
else
{
return QDoubleValidator::Invalid;
}
}
};
}
}
#endif ///< PAI_FRAME_CRYSTAL_PAIVALIDATOR_H