You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.4 KiB
C++
136 lines
3.4 KiB
C++
#include "CUILineEdit.h"
|
|
|
|
#include <QHBoxLayout>
|
|
#include <CUIConfig.h>
|
|
#include <QLabel>
|
|
#include <CUI.h>
|
|
#include <QDebug>
|
|
#include <QDoubleValidator>
|
|
#include <QIntValidator>
|
|
|
|
/**
|
|
* @brief CUILineEdit::CUILineEdit 构造函数
|
|
* @param conf 配置信息
|
|
* @param parent
|
|
*/
|
|
CUILineEdit::CUILineEdit(CUIConfig* conf, QVector<CUI*> &subCUI, QWidget *parent) : CUIComponentBase(parent)
|
|
{
|
|
this->m_conf = conf;
|
|
this->iniUI(subCUI);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief CUILineEdit::getLabelWidth 获取当前组件的label的最小宽度
|
|
* @return label的最小宽度
|
|
*/
|
|
qint32 CUILineEdit::getLabelWidth()
|
|
{
|
|
return m_label->minimumSizeHint().width();
|
|
}
|
|
|
|
/**
|
|
* @brief CUILineEdit::setLabelWidth 设置label的宽度
|
|
* @param width 宽度
|
|
*/
|
|
void CUILineEdit::setLabelWidth(qint32 width)
|
|
{
|
|
m_label->setMinimumWidth(width);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief CUILineEdit::iniUI 根据配置信息初始化
|
|
*/
|
|
void CUILineEdit::iniUI(QVector<CUI*> &subCUI)
|
|
{
|
|
m_layout = new QHBoxLayout();
|
|
m_label = new QLabel(m_conf->getPropertyValue("name"));
|
|
m_lineEdit = new QLineEdit(this->getValueString());
|
|
m_layout->addWidget(m_label);
|
|
m_layout->addWidget(m_lineEdit);
|
|
this->setLayout(m_layout);
|
|
// 设置格式校验
|
|
this->setValidator();
|
|
this->setInputTips();
|
|
connect(m_lineEdit, &QLineEdit::textEdited, this, &CUILineEdit::onTextChanged);
|
|
}
|
|
|
|
void CUILineEdit::setValidator()
|
|
{
|
|
switch (m_dataType) {
|
|
case CUI_DATA_TYPE_DOUBLE: {
|
|
m_lineEdit->setValidator(new QDoubleValidator(m_rangeMin.toDouble(), m_rangeMax.toDouble(), 10));
|
|
break;
|
|
}
|
|
case CUI_DATA_TYPE_INT: {
|
|
m_lineEdit->setValidator(new QIntValidator(m_rangeMin.toInt(), m_rangeMax.toInt()));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief CUILineEdit::showMsg 显示提示信息
|
|
*/
|
|
void CUILineEdit::setInputTips()
|
|
{
|
|
QString msg = "";
|
|
if(m_checkRange) {
|
|
msg += m_inclusiveMin ? "[" : "(";
|
|
msg += this->getRangeMinString() + "," + this->getRangeMaxString();
|
|
msg += m_inclusiveMax ? "]" : ")";
|
|
}
|
|
if(m_required) {
|
|
msg += "\t(必填)";
|
|
}
|
|
m_lineEdit->setPlaceholderText(msg);
|
|
}
|
|
|
|
/**
|
|
* @brief CUILineEdit::checkRange 检查范围,并给出提示
|
|
*/
|
|
void CUILineEdit::showRangeError()
|
|
{
|
|
if(m_checkRange) {
|
|
QPalette palette = m_lineEdit->palette();
|
|
if(this->inRange()) {
|
|
palette.setColor(QPalette::Text, Qt::black);
|
|
} else {
|
|
palette.setColor(QPalette::Text, Qt::red);
|
|
}
|
|
m_lineEdit->setPalette(palette);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief CUILineEdit::onTextChanged 槽函数
|
|
*
|
|
* 当文本改变时,根据设置的类型修改LineEdit的内容
|
|
* 当文本改变时,如果有范围设置,不在范围内则标红
|
|
*
|
|
* @param text 当前输入后的文本内容
|
|
*/
|
|
void CUILineEdit::onTextChanged(const QString &text)
|
|
{
|
|
this->showRangeError();
|
|
if (this->inRange()) {
|
|
switch (m_dataType) {
|
|
case CUI_DATA_TYPE_DOUBLE: {
|
|
m_value = qvariant_cast<double>(m_lineEdit->text().toDouble());
|
|
break;
|
|
}
|
|
case CUI_DATA_TYPE_INT: {
|
|
m_value = qvariant_cast<int>(m_lineEdit->text().toInt());
|
|
break;
|
|
}
|
|
case CUI_DATA_TYPE_STRING: {
|
|
m_value = m_lineEdit->text();
|
|
break;
|
|
}
|
|
}
|
|
this->setValueToOrigin();
|
|
}
|
|
}
|