#include "CUILineEdit.h" #include #include #include #include #include /** * @brief CUILineEdit::CUILineEdit 构造函数 * @param conf 配置信息 * @param parent */ CUILineEdit::CUILineEdit(CUIConfig* conf, QVector &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 &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()<property["name"]<<' '<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(); }