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

222 lines
5.6 KiB
C++

3 weeks ago
#include "CUILineEdit.h"
#include <QHBoxLayout>
#include<CUIConfig.h>
#include<QLabel>
#include<CUI.h>
#include<QDebug>
/**
* @brief CUILineEdit::CUILineEdit
* @param conf
* @param parent
*/
CUILineEdit::CUILineEdit(CUIConfig* conf, QVector<CUI*> &subCUI,QWidget *parent) : QWidget(parent)
{
this->conf = conf;
appSetting();
iniUI(subCUI);
showMsg();
checkRange();
}
/**
* @brief CUILineEdit::isInRange ,
* @return true\false
*/
bool CUILineEdit::isInRange() const
{
bool res = false;
if(check_range_){
if(data_type == "int")
{
if(inclusive_){
res = range_min_.toInt() <= lineedit->text().toInt() &&
lineedit->text().toInt() <= range_max_.toInt();
}else{
res = range_min_.toInt() < lineedit->text().toInt() &&
lineedit->text().toInt() < range_max_.toInt();
}
}
if(data_type == "double")
{
if(inclusive_){
res = range_min_.toDouble() <= lineedit->text().toDouble() &&
lineedit->text().toDouble() <= range_max_.toDouble();
}else{
res = range_min_.toDouble() < lineedit->text().toDouble() &&
lineedit->text().toDouble() < range_max_.toDouble();
}
}
}
return res;
}
/**
* @brief CUILineEdit::getLabelWidth label
* @return label
*/
qint32 CUILineEdit::getLabelWidth()
{
return label->minimumSizeHint().width();
}
/**
* @brief CUILineEdit::setLabelWidth label
* @param width
*/
void CUILineEdit::setLabelWidth(qint32 width)
{
label->setMinimumWidth(width);
}
/**
* @brief CUILineEdit::iniUI
*/
void CUILineEdit::iniUI(QVector<CUI*> &subCUI)
{
layout = new QHBoxLayout();
label = new QLabel(conf->getPropertyValue("name"));
3 weeks ago
lineedit = new QLineEdit(initial_value_);
layout->addWidget(label);
layout->addWidget(lineedit);
this->setLayout(layout);
connect(lineedit, &QLineEdit::textEdited, this, &CUILineEdit::onTextChanged);
}
/**
* @brief CUILineEdit::appSetting ,
*/
void CUILineEdit::appSetting()
{
data_type = conf->getPropertyValue("data_type");
initial_value_ = conf->getPropertyValue("initial_value_");
3 weeks ago
qDebug()<< initial_value_;
if(data_type == "string"){
check_range_ = 0;
}
if(data_type == "int"){
if(conf->getPropertyValue("check_range_") == "true")
3 weeks ago
{
check_range_ = true;
range_min_ = conf->getPropertyValue("range_min_").toInt();
range_max_ = conf->getPropertyValue("range_max_").toInt();
3 weeks ago
if(conf->getPropertyValue("inclusive_") == "true")
3 weeks ago
inclusive_ = true;
else
inclusive_ = false;
}else{
check_range_ = false;
}
}
if(data_type == "double"){
if(conf->getPropertyValue("check_range_") == "true"){
3 weeks ago
check_range_ = true;
range_min_ = conf->getPropertyValue("range_min_").toDouble();
range_max_ = conf->getPropertyValue("range_max_").toDouble();
if(conf->getPropertyValue("inclusive_") == "true")
inclusive_ = true;
else
inclusive_ = false;
3 weeks ago
}else{
check_range_ = false;
}
}
if(conf->getPropertyValue("required_") == "false")
3 weeks ago
required_ = false;
else
required_ = true;
}
/**
* @brief CUILineEdit::checkType
*/
void CUILineEdit::checkType(const QString &text)
{
qint32 cursorPosition = lineedit->cursorPosition();
if(data_type == "int"){
bool ok;
text.toInt(&ok);
if(ok || text == "" || text == "-")
lastEdited = text;
else
{
lineedit->setText(lastEdited);
lineedit->setCursorPosition(cursorPosition-1);
}
}
if(data_type == "double"){
bool ok;
text.toDouble(&ok);
if(ok || text == "" || text == "-")
lastEdited = text;
else
{
lineedit->setText(lastEdited);
lineedit->setCursorPosition(cursorPosition-1);
}
}
}
/**
* @brief CUILineEdit::showMsg
*/
void CUILineEdit::showMsg()
{
QString msg = "";
if(check_range_){
if(inclusive_){
msg += "["+range_min_.toString()+","+range_max_.toString()+"] ";
}else{
msg += "("+range_min_.toString()+","+range_max_.toString()+") ";
}
}
if(required_){
msg += "\t(必填)";
}
lineedit->setPlaceholderText(msg);
}
/**
* @brief CUILineEdit::checkRange
*/
bool CUILineEdit::checkRange()
{
bool res = true;
if(check_range_){
QPalette palette = lineedit->palette();
res = isInRange();
if(res){
palette.setColor(QPalette::Text, Qt::black);
}else{
palette.setColor(QPalette::Text, Qt::red);
}
lineedit->setPalette(palette);
}
return res;
}
/**
* @brief CUILineEdit::onTextChanged
*
* ,LineEdit
*
*
* @param text
*/
void CUILineEdit::onTextChanged(const QString &text)
{
checkType(text);
checkRange();
}