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

167 lines
4.8 KiB
C++

#include "CUIComponentBase.h"
CUIComponentBase::CUIComponentBase()
{
this->initSetting();
}
void CUIComponentBase::initSetting()
{
if (!m_conf) {
return;
}
m_dataType = (CUI_DATA_TYPE)m_conf->getPropertyOriginValue("value_type").toInt();
qDebug()<<"------";
qDebug()<<m_dataType;
qDebug()<<static_cast<CUI_DATA_TYPE>(m_conf->getPropertyOriginValue("value_type").toInt());
qDebug()<<"++++++";
if(m_conf->getPropertyOriginValue("initial_value") != QVariant::Invalid) {
m_value = m_conf->getPropertyOriginValue("initial_value");
}
// 初始化是否必填
if(m_conf->getPropertyOriginValue("required") != QVariant::Invalid) {
m_required = m_conf->getPropertyOriginValue("required").toBool();
}
if(m_dataType == CUI_DATA_TYPE_STRING) {
m_checkRange = false;
return;
}
// 是否检查范围
if(m_conf->getPropertyOriginValue("check_range") != QVariant::Invalid) {
m_checkRange = m_conf->getPropertyOriginValue("check_range").toBool();
}
// 左值和右值是否包含
if(m_conf->getPropertyOriginValue("inclusive_min") != QVariant::Invalid) {
m_inclusiveMin = m_conf->getPropertyOriginValue("inclusive_min").toBool();
}
if(m_conf->getPropertyOriginValue("inclusive_max") != QVariant::Invalid) {
m_inclusiveMax = m_conf->getPropertyOriginValue("inclusive_max").toBool();
}
// 初始化左值和右值
if (m_checkRange) {
if(m_conf->getPropertyOriginValue("range_min") != QVariant::Invalid) {
m_rangeMin = m_conf->getPropertyOriginValue("range_min");
} else {
if (m_dataType == CUI_DATA_TYPE_INT) {
m_rangeMin = INT_MIN;
} else if (m_dataType == CUI_DATA_TYPE_DOUBLE) {
m_rangeMin = -1*DBL_MAX;
}
}
if(m_conf->getPropertyOriginValue("range_max") != QVariant::Invalid) {
m_rangeMax = m_conf->getPropertyOriginValue("range_max");
} else {
qDebug()<<"Base init min";
if (m_dataType == CUI_DATA_TYPE_INT) {
m_rangeMax = INT_MAX;
} else if (m_dataType == CUI_DATA_TYPE_DOUBLE) {
m_rangeMax = DBL_MAX;
}
}
}
}
bool CUIComponentBase::inRange(QVariant v)
{
if(m_checkRange) {
// 如果包含最小值,但是修改后的值小于最小值,则不在范围内
if (m_inclusiveMin && v < m_rangeMin ) {
return false;
}
// 如果不包含最小值,但是修改后的值小于等于最小值,则不在范围内
if (!m_inclusiveMin && v <= m_rangeMin ) {
return false;
}
// 如果包含最大值,但是修改后的值大于最大值,则不在范围内
if (m_inclusiveMax && v > m_rangeMax ) {
return false;
}
// 如果不包含最大值,但是修改后的值大于等于最大值,则不在范围内
if (!m_inclusiveMax && v >= m_rangeMax ) {
return false;
}
}
return true;
}
QVariant CUIComponentBase::getQVFrom(QString str)
{
QVariant v;
switch (m_dataType) {
case CUI_DATA_TYPE_DOUBLE: {
v = qvariant_cast<double>(str.toDouble());
break;
}
case CUI_DATA_TYPE_INT: {
// qDebug()<< qvariant_cast<int>(m_lineEdit->text().toInt());
v = qvariant_cast<int>(str.toInt());
break;
}
case CUI_DATA_TYPE_STRING: {
v = str;
break;
}
}
return v;
}
void CUIComponentBase::initUI()
{
}
void CUIComponentBase::initValidator()
{
}
QString CUIComponentBase::getValueString()
{
if(m_dataType == CUI_DATA_TYPE_STRING) {
return m_value.toString();
}
if(m_dataType == CUI_DATA_TYPE_INT) {
return QString::number(m_value.toInt());
}
if(m_dataType == CUI_DATA_TYPE_DOUBLE) {
return QString::number(m_value.toDouble());
}
return QString("");
}
QString CUIComponentBase::getRangeMinString()
{
if(m_dataType == CUI_DATA_TYPE_STRING) {
return m_rangeMin.toString();
}
if(m_dataType == CUI_DATA_TYPE_INT) {
return QString::number(m_rangeMin.toInt());
}
if(m_dataType == CUI_DATA_TYPE_DOUBLE) {
return QString::number(m_rangeMin.toDouble());
}
return QString("");
}
QString CUIComponentBase::getRangeMaxString()
{
if(m_dataType == CUI_DATA_TYPE_STRING) {
return m_rangeMax.toString();
}
if(m_dataType == CUI_DATA_TYPE_INT) {
return QString::number(m_rangeMax.toInt());
}
if(m_dataType == CUI_DATA_TYPE_DOUBLE) {
return QString::number(m_rangeMax.toDouble());
}
return QString("");
}
void CUIComponentBase::setValueToOrigin()
{
if (m_conf) {
m_conf->setValue(m_value);
}
}