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

137 lines
3.6 KiB
C++

#include "CUIComponentILineEdit.h"
3 weeks ago
#include <QHBoxLayout>
#include <CUIConfig.h>
#include <QLabel>
#include <CUI.h>
#include <QDebug>
#include <QDoubleValidator>
#include <QIntValidator>
3 weeks ago
/**
* @brief CUIComponentLineEdit::CUIComponentLineEdit
3 weeks ago
* @param conf
* @param parent
*/
CUIComponentLineEdit::CUIComponentLineEdit(CUIConfig* conf, QVector<CUI*> &subCUI, QWidget *parent) : CUIComponentBaseWidget(parent)
3 weeks ago
{
this->m_conf = conf;
this->initUI(subCUI);
3 weeks ago
}
/**
* @brief CUIComponentLineEdit::getLabelWidth label
3 weeks ago
* @return label
*/
qint32 CUIComponentLineEdit::getLabelWidth()
3 weeks ago
{
return m_label->minimumSizeHint().width();
3 weeks ago
}
/**
* @brief CUIComponentLineEdit::setLabelWidth label
3 weeks ago
* @param width
*/
void CUIComponentLineEdit::setLabelWidth(qint32 width)
3 weeks ago
{
m_label->setMinimumWidth(width);
3 weeks ago
}
/**
* @brief CUIComponentLineEdit::initUI
3 weeks ago
*/
void CUIComponentLineEdit::initUI(QVector<CUI*> &subCUI)
3 weeks ago
{
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);
3 weeks ago
}
void CUIComponentLineEdit::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 CUIComponentLineEdit::showMsg
3 weeks ago
*/
void CUIComponentLineEdit::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) {
msg += "\t" + tr("(required)");
3 weeks ago
}
m_lineEdit->setPlaceholderText(msg);
3 weeks ago
}
/**
* @brief CUIComponentLineEdit::checkRange
3 weeks ago
*/
void CUIComponentLineEdit::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 CUIComponentLineEdit::onTextChanged
3 weeks ago
*
* ,LineEdit
*
*
* @param text
*/
void CUIComponentLineEdit::onTextChanged(const QString &text)
3 weeks ago
{
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
}