logplus/Workflow/WFCrystal/Crystal/include/PaiValidator.h
2026-01-16 17:18:41 +08:00

396 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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