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

149 lines
4.3 KiB
C++

#include "CUIComponentBase.h"
CUIComponentBase::CUIComponentBase(QWidget *parent) : QWidget(parent)
{
this->initSetting();
}
void CUIComponentBase::initSetting()
{
if (!m_conf) {
return;
}
m_dataType = (CUI_DATA_TYPE)m_conf->getPropertyOriginValue("m_dataType").toInt();
if(m_conf->getPropertyValue("initial_value") != QVariant::Invalid) {
m_value = m_conf->getPropertyOriginValue("initial_value");
}
// 初始化是否必填
if(m_conf->getPropertyValue("required") != QVariant::Invalid) {
m_required = m_conf->getPropertyOriginValue("required").toBool();
}
if(m_dataType == CUI_DATA_TYPE_STRING) {
m_checkRange = false;
return;
}
// 是否检查范围
if(m_conf->getPropertyValue("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->getPropertyValue("range_min");
} else {
if (m_dataType == CUI_DATA_TYPE_INT) {
m_rangeMin = INT_MIN;
} else if (m_dataType == CUI_DATA_TYPE_DOUBLE) {
m_rangeMin = DBL_MIN;
}
}
if(m_conf->getPropertyOriginValue("range_max") != QVariant::Invalid) {
m_rangeMax = m_conf->getPropertyValue("range_max");
} else {
if (m_dataType == CUI_DATA_TYPE_INT) {
m_rangeMin = INT_MAX;
} else if (m_dataType == CUI_DATA_TYPE_DOUBLE) {
m_rangeMin = DBL_MAX;
}
}
}
}
bool CUIComponentBase::inRange()
{
if(m_checkRange) {
// 如果包含最小值,但是修改后的值小于最小值,则不在范围内
if (m_inclusiveMin && m_value < m_rangeMin ) {
return false;
}
// 如果不包含最小值,但是修改后的值小于等于最小值,则不在范围内
if (!m_inclusiveMin && m_value <= m_rangeMin ) {
return false;
}
// 如果包含最大值,但是修改后的值大于最大值,则不在范围内
if (m_inclusiveMax && m_value > m_rangeMax ) {
return false;
}
// 如果不包含最大值,但是修改后的值大于等于最大值,则不在范围内
if (!m_inclusiveMax && m_value >= m_rangeMax ) {
return false;
}
}
return true;
}
void CUIComponentBase::initUI()
{
}
void CUIComponentBase::initValidator()
{
}
QString CUIComponentBase::getValueString()
{
if (m_value == QVariant::Invalid) {
return QString("");
}
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_STRING) {
return QString::number(m_value.toDouble());
}
return QString("");
}
QString CUIComponentBase::getRangeMinString()
{
if (m_rangeMin == QVariant::Invalid) {
return QString("");
}
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_STRING) {
return QString::number(m_rangeMin.toDouble());
}
return QString("");
}
QString CUIComponentBase::getRangeMaxString()
{
if (m_rangeMax == QVariant::Invalid) {
return QString("");
}
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_STRING) {
return QString::number(m_rangeMax.toDouble());
}
return QString("");
}
void CUIComponentBase::setValueToOrigin()
{
if (m_conf) {
m_conf->setValue(m_value);
}
}