Files
handleaiuinew/HanddleAI/HandleAiUi-new/front/pages/configSettingWidget.h
T
2024-08-14 11:15:16 +08:00

431 lines
12 KiB
C++

#include <qwidget.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <unordered_map>
#include <QStackedWidget>
#include <vector>
#include <qlineedit.h>
#include <uiModule/components/fancyButton.h>
#include <qscrollarea.h>
#include <qcheckbox.h>
#include <unordered_map>
#include <qobject.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qregularexpression.h>
#include <qtimer.h>
#include <appConfig/json/generalJsonParsing.hpp>
#include <qspinbox.h>
/**************格式****************/
//...
//{
// "showName":"Name", //仅显示
// "valueName":"Name2",//检索
// "value":"value" //值
// "policy":"formatPolicy"//可选 格式检查
// }
// ...
/***********************************/
static bool isNumeric(const QString& str) {
QRegExp re("^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$");
return re.exactMatch(str);
}
class configSettingWidget :public QWidget
{
Q_OBJECT
public:
enum formatPolicy
{
none,
filePath,
bhdPath,
IP,
number,
boolType,
path
};
struct aLineData
{
int acessLevel;
std::string showName;
std::string value;
std::string formatPolicyStr;
formatPolicy _formatPolicy;
QWidget* userInputWidget;
std::string getValueFromQwidget()
{
switch (_formatPolicy)
{
case boolType:
if (((QCheckBox*)userInputWidget)->isChecked() == true)
return "true";
else
return "flase";
break;
default:
return ((QLineEdit*)userInputWidget)->text().toStdString();
}
return "error";
}
};
QVBoxLayout* Layout;
QHBoxLayout* btnLayout;
std::vector<gradientButton*>btns;
QLabel* icon, * Title;
QStackedWidget* stackedWidgets;
int currentPageIndex = 0;
configSettingWidget()
{
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::white);
QFont font("微软雅黑", 24, QFont::Bold);
stackedWidgets = new QStackedWidget();
stackedWidgets->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
stackedWidgets->setMaximumSize(9999, 9999);
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->setMaximumSize(9999, 9999);
this->setObjectName("configSettingWidget");
//this->setStyleSheet(QString("QWidget {background-color: rgb(%1)}").arg(sColor_Gold));
//this->showMaximized();
Layout = new QVBoxLayout(this);
Layout->setContentsMargins(0, 0, 0, 0);
Layout->setAlignment(Qt::AlignLeft|Qt::AlignTop);
Title = new QLabel(u8"配置文件");
Title->setFont(font);
Title->setPalette(palette);
Title->setAlignment(Qt::AlignLeft);
Layout->addWidget(Title);
btnLayout = new QHBoxLayout();
btnLayout->setContentsMargins(0, 0, 0, 0);
btnLayout->setAlignment(Qt::AlignLeft);
Layout->addLayout(btnLayout);
addConfigWidget(u8"通用设置",".//config//basicSetting.json");
addConfigWidget(u8"检测设置","");
addConfigWidget(u8"通讯设置",".//config//communicateConfig");
addConfigWidget(u8"算法设置","");
addConfigWidget(u8"运动设置","");
addConfigWidget(u8"模型设置","");
btns[0]->pressed();
Layout->addWidget(stackedWidgets);
}
//格式检查
bool formatPolicyCompare(formatPolicy _formatPolicy,QString compareStr,QString& textOut,QString &colorOut)
{
switch (_formatPolicy)
{
case path:
if (!isNumeric(compareStr))
{
textOut = u8"格式错误,非数字";
colorOut = "red";
return true;
}
break;
case number:
if (!isNumeric(compareStr))
{
textOut = u8"格式错误,非数字";
colorOut = "red";
return true;
}
break;
case filePath:
if (QFile(compareStr).exists() == false)
{
textOut = u8"路径错误!";
colorOut = "red";
return true;
}
break;
case bhdPath:
if (QFile(compareStr).exists() == false)
{
textOut = u8"路径错误!";
colorOut = "red";
return true;
}
if (compareStr.contains(".bhd") == false)
{
textOut = u8"不是一个模型文件!";
colorOut = "red";
return true;
}
break;
case IP:
if (QRegularExpression("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$").match(compareStr).hasMatch() == false)
{
textOut = u8"IP格式错误!";
colorOut = "red";
return true;
}
break;
}
return false;
}
void addConfigWidget(QString btnText,QString jsonPath)
{
int thistPageIndex = privateJsondatas.size();
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::white);
QFont font("微软雅黑", 13, QFont::Bold);
QFont font2("微软雅黑", 8);
QFont font3("微软雅黑", 16, QFont::Bold);
//切换配置页面
gradientButton* btn = new gradientButton(160,50, btnText, "");
btnLayout->addWidget(btn);
btns.push_back(btn);
connect(btn, &QPushButton::clicked, [=]()
{
for (auto btnPtr : btns)
btnPtr->toDefault();
btn->pressed();
currentPageIndex = thistPageIndex;
stackedWidgets->setCurrentIndex(thistPageIndex);
});
//页面widget
QWidget* widget = new QWidget();
stackedWidgets->addWidget(widget);
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QVBoxLayout* widgetLayout = new QVBoxLayout(widget);
widgetLayout->setContentsMargins(0, 0, 0, 0);
widgetLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
QScrollArea* scrollArea = new QScrollArea();
widgetLayout->addWidget(scrollArea);
scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
scrollArea->setWidgetResizable(true);
scrollArea->setStyleSheet("QScrollArea { background-color: transparent; "
"border: 0px solid red;"
"border-radius: 0px;}");
QWidget* jsonWidget = new QWidget();
jsonWidget->setObjectName("jsonWidget");
jsonWidget->setStyleSheet("QWidget#jsonWidget { background-color: transparent; "
"border: 0px solid red;"
"border-radius: 0px;}");
jsonWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
jsonWidget->setMaximumSize(9999 , 9999);
QGridLayout* jsonLayout = new QGridLayout(jsonWidget);
jsonLayout->setAlignment(Qt::AlignTop);
scrollArea->setWidget(jsonWidget);
jsonLayouts.push_back(jsonLayout);
//参数列表读取以及初始化
std::vector<QWidget*> widgets;
std::vector< aLineData> jsonData=loadJson(jsonPath);
for (auto& data : jsonData)
{
QWidget* tempWidget = new QWidget();
QVBoxLayout* tempVLayout = new QVBoxLayout(tempWidget);
tempVLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
tempWidget->setContentsMargins(0, 0, 0, 0);
tempVLayout->setContentsMargins(0, 0, 0, 0);
QLabel* showLabel = new QLabel(QString::fromStdString(data.showName));
showLabel->setFont(font);
showLabel->setPalette(palette);
showLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
QLabel* warningLabel = new QLabel("");
warningLabel->setFixedHeight(30);
warningLabel->setFont(font2);
if (data._formatPolicy == boolType)
{
QCheckBox* checkBox = new QCheckBox();
checkBox->setFont(font);
checkBox->setPalette(palette);
//checkBox->setFixedSize(QSize(50,50));
QHBoxLayout* tempLayout = new QHBoxLayout();
tempLayout->addWidget(showLabel);
tempLayout->addWidget(checkBox);
tempVLayout->addLayout(tempLayout);
tempVLayout->addWidget(warningLabel);
if (!data.value.compare("true"))
checkBox->setChecked(true);
data.userInputWidget = checkBox;
//修改时警告
connect(checkBox, &QCheckBox::stateChanged, [=]()
{
if (checkBox->isChecked()==true && data.value =="false")//修改后的值不匹配
{
warningLabel->setText("此项被修改!");
warningLabel->setStyleSheet("color:yellow");
checkBox->setStyleSheet("border: 2px solid yellow;");
}
else if (checkBox->isChecked() == false && data.value == "true")//修改后的值不匹配
{
warningLabel->setText("此项被修改!");
warningLabel->setStyleSheet("color:yellow");
checkBox->setStyleSheet("border: 2px solid yellow;");
}
else
{
warningLabel->setText("");
checkBox->setStyleSheet("");
warningLabel->setText("");
}
});
}
else
{
QLineEdit* lineEdit = new QLineEdit(QString::fromStdString(data.value));
lineEdit->setFont(font);
lineEdit->setPalette(palette);
lineEdit->setFixedWidth(500);
tempVLayout->addWidget(showLabel);
tempVLayout->addWidget(lineEdit);
tempVLayout->addWidget(warningLabel);
data.userInputWidget = lineEdit;
//修改时警告
connect(lineEdit, &QLineEdit::textChanged, [=]()
{
if (lineEdit->text().toStdString().compare(data.value))//修改后的值不匹配
{
QString text, color;
if (formatPolicyCompare(data._formatPolicy, lineEdit->text(), text, color))
{
warningLabel->setText(text);
warningLabel->setStyleSheet(QString("color:%1;").arg(color));
lineEdit->setStyleSheet(QString("border: 2px solid %1;").arg(color));
}
else
{
warningLabel->setText("此项被修改!");
warningLabel->setStyleSheet("color:yellow");
lineEdit->setStyleSheet("border: 2px solid yellow;");
}
}
else
{
warningLabel->setText("");
lineEdit->setStyleSheet("");
warningLabel->setText("");
}
});
}
tempWidget->setFixedHeight(100);
widgets.push_back(tempWidget);
jsonLayout->addWidget(tempWidget);
}
privateWidgets.push_back(widgets);
privateJsondatas.push_back(jsonData);
//保存按钮
fancyButtonText* saveBtn = new fancyButtonText(160, 50, u8"保存", u8"保存当前配置文件");
connect(saveBtn, &QPushButton::clicked, this, [=]() {saveJson(jsonPath); });
widgetLayout->addWidget(saveBtn);
}
void setMinimumSizePolicy(QWidget* widget) {
widget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
for (QObject* child : widget->children()) {
QWidget* childWidget = qobject_cast<QWidget*>(child);
if (childWidget) {
setMinimumSizePolicy(childWidget);
}
}
}
//读取json
std::vector< aLineData> loadJson(const QString& fileName) {
std::vector< aLineData> returnData;
nlohmann::json json = readJsonFromPath(fileName.toStdString());
try
{
for (auto data : json)
{
aLineData lineData;
lineData.acessLevel = data["level"];
lineData.showName = data["showName"];
lineData.formatPolicyStr = data["policy"];
lineData.value = data["value"];
if (!lineData.formatPolicyStr.compare("filepath"))
lineData._formatPolicy = filePath;
else if (!lineData.formatPolicyStr.compare("bhdpath"))
lineData._formatPolicy = bhdPath;
else if (!lineData.formatPolicyStr.compare("ip"))
lineData._formatPolicy = IP;
else if (!lineData.formatPolicyStr.compare("number"))
lineData._formatPolicy = number;
else if (!lineData.formatPolicyStr.compare("bool"))
lineData._formatPolicy = boolType;
returnData.push_back(lineData);
}
}
catch (...)
{
qDebug() << "read json failure" << fileName;
}
return returnData;
}
void updateLayout()
{
QMainWindow* mainWindow = qobject_cast<QMainWindow*>(this->window());
QSize stackedWidgetSize = mainWindow->size();
int xCount = stackedWidgetSize.width() / 600;
if (xCount == 0)
return;
int eachWidth = stackedWidgetSize.width() / xCount;
std::vector<int>eachHeightCount(xCount, 0);
std::vector<int>eachHeightIndexCount(xCount, 0);
if (xCount == 1)
{
for (int i = 0; i < privateWidgets[currentPageIndex].size(); i++)
jsonLayouts[currentPageIndex]->addWidget(privateWidgets[currentPageIndex][i], i, 0);
}
for (int i = 0; i < privateWidgets[currentPageIndex].size(); i++)
{
int xIndex = 0;
for (xIndex = eachHeightCount.size() - 1; xIndex > 0; xIndex--)
if (eachHeightCount[xIndex - 1] > eachHeightCount[xIndex])
{
break;
}
eachHeightCount[xIndex] += privateWidgets[currentPageIndex][i]->size().height();
jsonLayouts[currentPageIndex]->addWidget(privateWidgets[currentPageIndex][i], eachHeightIndexCount[xIndex], xIndex);
eachHeightIndexCount[xIndex]++;
}
//jsonLayout->addLayout(classVLayout, jsonDataIndex, 0);
}
void saveJson(QString path)
{
//
nlohmann::json json;
for (auto& data : privateJsondatas[currentPageIndex])
{
nlohmann::json eachJson;
eachJson["level"] = data.acessLevel;
eachJson["showName"] = data.showName;
eachJson["value"] = data.getValueFromQwidget();
eachJson["policy"] = data.formatPolicyStr;
json.push_back(eachJson);
}
std::string sss = path.toStdString();
saveJsonFromPath(sss, json);
}
protected:
void resizeEvent(QResizeEvent* event) override {
Q_UNUSED(event);
QTimer::singleShot(0, this, &configSettingWidget::updateLayout);
}
private:
std::vector<std::vector< aLineData>> privateJsondatas;//路径与数据
std::vector<std::vector<QWidget*>> privateWidgets;//路径与数据
std::vector<QGridLayout*>jsonLayouts;
};
#pragma once