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

224 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->property["name"]);
lineedit = new QLineEdit(initial_value_);
layout->addWidget(label);
layout->addWidget(lineedit);
this->setLayout(layout);
// qDebug()<<"new lineedit";
connect(lineedit, &QLineEdit::textEdited, this, &CUILineEdit::onTextChanged);
}
/**
* @brief CUILineEdit::appSetting ,
*/
void CUILineEdit::appSetting()
{
data_type = conf->property["data_type"];
initial_value_ = conf->property["initial_value_"];
qDebug()<< initial_value_;
if(data_type == "string"){
check_range_ = 0;
}
if(data_type == "int"){
if(conf->property["check_range_"] == "true")
{
check_range_ = true;
range_min_ = conf->property["range_min_"].toInt();
range_max_ = conf->property["range_max_"].toInt();
qDebug()<<conf->property["name"]<<' '<<range_min_<<' '<<range_max_;
if(conf->property["inclusive_"] == "true")
inclusive_ = true;
else
inclusive_ = false;
}else{
check_range_ = false;
}
}
if(data_type == "double"){
if(conf->property["check_range_"] == "true"){
check_range_ = true;
range_min_ = conf->property["range_min_"].toDouble();
range_max_ = conf->property["range_max_"].toDouble();
if(conf->property["inclusive_"] == "true")
inclusive_ = true;
else
inclusive_ = false;
}else{
check_range_ = false;
}
}
if(conf->property["required_"] == "false")
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();
}