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/CUILineEdit.cpp

136 lines
3.4 KiB
C++

3 weeks ago
#include "CUILineEdit.h"
#include <QHBoxLayout>
#include <CUIConfig.h>
#include <QLabel>
#include <CUI.h>
#include <QDebug>
#include <QDoubleValidator>
#include <QIntValidator>
3 weeks ago
/**
* @brief CUILineEdit::CUILineEdit
* @param conf
* @param parent
*/
CUILineEdit::CUILineEdit(CUIConfig* conf, QVector<CUI*> &subCUI, QWidget *parent) : CUIComponentBase(parent)
3 weeks ago
{
this->m_conf = conf;
this->iniUI(subCUI);
3 weeks ago
}
/**
* @brief CUILineEdit::getLabelWidth label
* @return label
*/
qint32 CUILineEdit::getLabelWidth()
{
return m_label->minimumSizeHint().width();
3 weeks ago
}
/**
* @brief CUILineEdit::setLabelWidth label
* @param width
*/
void CUILineEdit::setLabelWidth(qint32 width)
{
m_label->setMinimumWidth(width);
3 weeks ago
}
/**
* @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);
3 weeks ago
}
void CUILineEdit::setValidator()
3 weeks ago
{
switch (m_dataType) {
case CUI_DATA_TYPE_DOUBLE: {
m_lineEdit->setValidator(new QDoubleValidator(m_rangeMin.toDouble(), m_rangeMax.toDouble(), 10));
break;
3 weeks ago
}
case CUI_DATA_TYPE_INT: {
m_lineEdit->setValidator(new QIntValidator(m_rangeMin.toInt(), m_rangeMax.toInt()));
break;
3 weeks ago
}
}
}
/**
* @brief CUILineEdit::showMsg
*/
void CUILineEdit::setInputTips()
3 weeks ago
{
QString msg = "";
if(m_checkRange) {
msg += m_inclusiveMin ? "[" : "(";
msg += this->getRangeMinString() + "," + this->getRangeMaxString();
msg += m_inclusiveMax ? "]" : ")";
3 weeks ago
}
if(m_required) {
3 weeks ago
msg += "\t(必填)";
}
m_lineEdit->setPlaceholderText(msg);
3 weeks ago
}
/**
* @brief CUILineEdit::checkRange
*/
void CUILineEdit::showRangeError()
3 weeks ago
{
if(m_checkRange) {
QPalette palette = m_lineEdit->palette();
if(this->inRange()) {
3 weeks ago
palette.setColor(QPalette::Text, Qt::black);
} else {
3 weeks ago
palette.setColor(QPalette::Text, Qt::red);
}
m_lineEdit->setPalette(palette);
3 weeks ago
}
}
/**
* @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();
}
3 weeks ago
}