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.
AppFlow/CFDStruct/CUIProperty/CUIComponentILineEdit.cpp

141 lines
3.7 KiB
C++

#include "CUIComponentILineEdit.h"
#include <CUIConfig.h>
#include <CUIPropertyWidget.h>
#include <QDebug>
#include <QDoubleValidator>
#include <QHBoxLayout>
#include <QIntValidator>
#include <QLabel>
/**
* @brief CUIComponentLineEdit::CUIComponentLineEdit 构造函数
* @param conf 配置信息
* @param parent
*/
CUIComponentLineEdit::CUIComponentLineEdit(CUIConfig *conf,
QVector<CUIPropertyWidget *> &subCUI,
QWidget *parent)
: CUIComponentBaseWidget(parent)
{
this->m_conf = conf;
this->initUI(subCUI);
}
/**
* @brief CUIComponentLineEdit::getLabelWidth 获取当前组件的label的最小宽度
* @return label的最小宽度
*/
qint32 CUIComponentLineEdit::getLabelWidth()
{
return m_label->minimumSizeHint().width();
}
/**
* @brief CUIComponentLineEdit::setLabelWidth 设置label的宽度
* @param width 宽度
*/
void CUIComponentLineEdit::setLabelWidth(qint32 width)
{
m_label->setMinimumWidth(width);
}
/**
* @brief CUIComponentLineEdit::initUI 根据配置信息初始化
*/
void CUIComponentLineEdit::initUI(QVector<CUIPropertyWidget *> &subCUI)
{
m_layout = new QHBoxLayout();
m_label = new QLabel(m_conf->getPropertyValue("name"));
m_label->setFixedWidth(m_labelLength);
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,
&CUIComponentLineEdit::onTextChanged);
}
void CUIComponentLineEdit::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 CUIComponentLineEdit::showMsg 显示提示信息
*/
void CUIComponentLineEdit::setInputTips()
{
QString msg = "";
if (m_checkRange) {
msg += m_inclusiveMin ? "[" : "(";
msg += this->getRangeMinString() + "," + this->getRangeMaxString();
msg += m_inclusiveMax ? "]" : ")";
}
if (m_required) {
msg += "\t" + tr("(required)");
}
m_lineEdit->setPlaceholderText(msg);
}
/**
* @brief CUIComponentLineEdit::checkRange 检查范围,并给出提示
*/
void CUIComponentLineEdit::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 CUIComponentLineEdit::onTextChanged 槽函数
*
* 当文本改变时,根据设置的类型修改LineEdit的内容
* 当文本改变时,如果有范围设置,不在范围内则标红
*
* @param text 当前输入后的文本内容
*/
void CUIComponentLineEdit::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();
}
}