修改部分参数修改界面

This commit is contained in:
2024-06-25 02:06:25 +08:00
parent e929cbfa1d
commit 5307c06c46
17 changed files with 178 additions and 245 deletions
@@ -13,11 +13,13 @@
#include <qobject.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qregularexpression.h>
#include <qtimer.h>
#include <appConfig/json/generalJsonParsing.hpp>
/**************格式****************/
//...
//{
// "className":"className",//大类名
// "showName":"Name", //仅显示
// "valueName":"Name2",//检索
// "value":"value" //值
@@ -25,35 +27,10 @@
// }
// ...
/***********************************/
class simpleJsonSearch : public std::unordered_map < QString, std::unordered_map<QString, QVariant>>
{
//对于此界面来说,第一层QString为按钮名,如通用设置
//第二层为其子项,QString为valuename,QString为值
public:
void insertData(QString btnName, QString valueName,QVariant data)
{
if (this->find(btnName) == this->end())
this->insert({ btnName,std::unordered_map<QString, QVariant>() });
auto mapData = this->find(btnName);
mapData->second.insert(std::make_pair(valueName, data));
}
//第一层QString为按钮名,第二层为valuename
QVariant searchData(QString btnName, QString valueName)
{
auto it1=this->find(btnName);
if (it1 != this->end())
{
auto it2 = it1->second.find(valueName);
if (it2 != it1->second.end())
{
return it2->second;
}
}
return "notFind";
}
};
static bool isNumeric(const QString& str) {
QRegExp re("^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$");
return re.exactMatch(str);
}
class configSettingWidget :public QWidget
{
Q_OBJECT
@@ -69,7 +46,6 @@ public:
};
struct aLineData
{
QString className;
QString showName;
QString valueName;
QVariant value;
@@ -81,8 +57,7 @@ public:
std::vector<gradientButton*>btns;
QLabel* icon, * Title;
QStackedWidget* stackedWidgets;
simpleJsonSearch configJsonDatas;
int currentPageIndex = 0;
configSettingWidget()
{
QPalette palette;
@@ -129,6 +104,14 @@ public:
{
switch (_formatPolicy)
{
case number:
if (!isNumeric(compareStr))
{
textOut = u8"格式错误,非数字";
colorOut = "red";
return true;
}
break;
case filePath:
if (QFile(compareStr).exists() == false)
{
@@ -164,7 +147,7 @@ public:
}
void addConfigWidget(QString btnText,QString jsonPath)
{
int currentPageIndex = privateJsondatas.size();
int thistPageIndex = privateJsondatas.size();
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::white);
QFont font("微软雅黑", 13, QFont::Bold);
@@ -180,9 +163,9 @@ public:
for (auto btnPtr : btns)
btnPtr->toDefault();
btn->pressed();
stackedWidgets->setCurrentIndex(currentPageIndex);
currentPageIndex = thistPageIndex;
stackedWidgets->setCurrentIndex(thistPageIndex);
});
privateWidgets.push_back(std::vector<QWidget*>());
//页面widget
QWidget* widget = new QWidget();
@@ -215,111 +198,81 @@ public:
jsonLayouts.push_back(jsonLayout);
//参数列表读取以及初始化
std::unordered_map<QString, std::vector< aLineData>> jsonData=loadJson(jsonPath);
privateJsondatas.push_back(jsonData);
int index = 0;
for (auto& classData : jsonData)
std::vector<QWidget*> widgets;
std::vector< aLineData> jsonData=loadJson(jsonPath);
for (auto& data : jsonData)
{
QString className = classData.first;
std::vector< aLineData> classDatas = classData.second;
QWidget* tempWidget = new QWidget();
QVBoxLayout* tempVLayout = new QVBoxLayout(tempWidget);
tempVLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
tempVLayout->setContentsMargins(0, 0, 0, 0);
QWidget* classWidget = new QWidget();
classWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
QVBoxLayout* classVLayout = new QVBoxLayout(classWidget);
classVLayout->setSpacing(0);
classVLayout->setContentsMargins(0, 0, 0, 0);
QLabel* showLabel = new QLabel(data.showName);
showLabel->setFont(font);
showLabel->setPalette(palette);
showLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
QLabel* label = new QLabel(className);
label->setFont(font3);
label->setPalette(palette);
label->setFixedHeight(50);
classVLayout->addWidget(label);
QLabel* warningLabel = new QLabel("");
warningLabel->setFixedHeight(30);
warningLabel->setFont(font2);
for (auto& data : classDatas)
if (data._formatPolicy == boolType)
{
QWidget* tempWidget = new QWidget();
QVBoxLayout* tempVLayout = new QVBoxLayout(tempWidget);
tempVLayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
tempVLayout->setContentsMargins(0, 0, 0, 0);
QCheckBox* checkBox = new QCheckBox();
checkBox->setFont(font);
checkBox->setPalette(palette);
checkBox->setFixedWidth(500);
QLabel* showLabel = new QLabel(data.showName);
showLabel->setFont(font);
showLabel->setPalette(palette);
showLabel->setAlignment(Qt::AlignLeft|Qt::AlignTop);
QHBoxLayout* tempLayout = new QHBoxLayout();
QLabel* warningLabel = new QLabel("");
warningLabel->setFixedHeight(30);
warningLabel->setFont(font2);
tempLayout->addWidget(showLabel);
tempLayout->addWidget(checkBox);
tempVLayout->addLayout(tempLayout);
if (data._formatPolicy == boolType)
{
QCheckBox* checkBox = new QCheckBox();
checkBox->setFont(font);
checkBox->setPalette(palette);
checkBox->setFixedWidth(500);
QHBoxLayout* tempLayout = new QHBoxLayout();
tempLayout->addWidget(showLabel);
tempLayout->addWidget(checkBox);
tempVLayout->addLayout(tempLayout);
tempVLayout->addWidget(warningLabel);
}
else
{
QLineEdit* lineEdit = new QLineEdit(data.value.toString());
lineEdit->setFont(font);
lineEdit->setPalette(palette);
lineEdit->setFixedWidth(500);
tempVLayout->addWidget(warningLabel);
}
else
{
QLineEdit* lineEdit = new QLineEdit(data.value.toString());
lineEdit->setFont(font);
lineEdit->setPalette(palette);
lineEdit->setFixedWidth(500);
tempVLayout->addWidget(showLabel);
tempVLayout->addWidget(lineEdit);
tempVLayout->addWidget(warningLabel);
//修改时警告
connect(lineEdit, &QLineEdit::textChanged, [=]()
tempVLayout->addWidget(showLabel);
tempVLayout->addWidget(lineEdit);
tempVLayout->addWidget(warningLabel);
//修改时警告
connect(lineEdit, &QLineEdit::textChanged, [=]()
{
if (lineEdit->text().compare(data.value.toString()))//修改后的值不匹配
{
if (lineEdit->text().compare(data.value.toString()))//修改后的值不匹配
QString text, color;
if (formatPolicyCompare(data._formatPolicy, lineEdit->text(), text, color))
{
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;");
}
warningLabel->setText(text);
warningLabel->setStyleSheet(QString("color:%1;").arg(color));
lineEdit->setStyleSheet(QString("border: 2px solid %1;").arg(color));
}
else
{
warningLabel->setText("");
lineEdit->setStyleSheet("");
warningLabel->setText("");
warningLabel->setText("此项被修改!");
warningLabel->setStyleSheet("color:yellow");
lineEdit->setStyleSheet("border: 2px solid yellow;");
}
});
}
tempWidget->setFixedHeight(100);
classVLayout->addWidget(tempWidget);
}
else
{
warningLabel->setText("");
lineEdit->setStyleSheet("");
warningLabel->setText("");
}
});
}
// json数据
for (auto& classData : jsonData)
{
std::vector< aLineData> classDatas = classData.second;
for (auto& data : classDatas)
configJsonDatas.insertData(btnText,data.valueName,data.value);
}
setMinimumSizePolicy(classWidget);
classWidget->adjustSize();
classWidget->setFixedSize(classWidget->size());
privateWidgets[currentPageIndex].push_back(classWidget);
//jsonLayout->addWidget(classWidget,index,0);
index++;
tempWidget->setFixedHeight(100);
widgets.push_back(tempWidget);
jsonLayout->addWidget(tempWidget);
}
privateWidgets.push_back(widgets);
//保存按钮
fancyButtonText* saveBtn = new fancyButtonText(160, 50, u8"保存", u8"保存当前配置文件");
connect(saveBtn, &QPushButton::clicked, this, [=]() {saveJson(jsonPath); });
@@ -335,138 +288,118 @@ public:
}
}
//读取json
std::unordered_map<QString, std::vector< aLineData>> loadJson(const QString& fileName) {
QFile file(fileName);
std::unordered_map<QString, std::vector < aLineData>> returnDatas;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
//QMessageBox::warning(this, tr(u8"路径失效!!!"), tr(fileName.toStdString().c_str()));
return returnDatas;
std::vector< aLineData> loadJson(const QString& fileName) {
std::vector< aLineData> returnData;
nlohmann::json json = readJsonFromPath(fileName.toStdString());
for (auto data : json)
{
aLineData lineData;
std::string policyStr = data["policy"];
std::string showName = data["showName"];
std::string value = data["value"];
std::string valueName = data["valueName"];
formatPolicy _formatPolicy = formatPolicy::none;
if (!policyStr.compare("filepath"))
_formatPolicy = filePath;
else if (!policyStr.compare("bhdpath"))
_formatPolicy = bhdPath;
else if (!policyStr.compare("ip"))
_formatPolicy = IP;
else if (!policyStr.compare("number"))
_formatPolicy = number;
else if (!policyStr.compare("bool"))
_formatPolicy = boolType;
lineData.showName = QString::fromStdString(showName);
lineData._formatPolicy = _formatPolicy;
lineData.value = QString::fromStdString(value);
lineData.valueName = QString::fromStdString(valueName);
returnData.push_back(lineData);
}
QByteArray jsonData = file.readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
QJsonArray jsonArray = jsonDoc.array();
for (const QJsonValue& value : jsonArray) {
if (value.isObject()) {
QJsonObject obj = value.toObject();
QString className = obj.value("className").toString();
QString showName = obj.value("showName").toString();
QString valueName = obj.value("valueName").toString();
QString valueStr = obj.value("value").toString();
QString policyStr = obj.value("policy").toString().toLower();
formatPolicy _formatPolicy = formatPolicy::none;
if (policyStr.contains("filepath"))
_formatPolicy = filePath;
else if (policyStr.contains("bhdpath"))
_formatPolicy = bhdPath;
else if (policyStr.contains("ip"))
_formatPolicy = IP;
else if (policyStr.contains("number"))
_formatPolicy = number;
else if (policyStr.contains("bool"))
_formatPolicy = boolType;
//无分类
if (className == "")
className = "none";
if (returnDatas.find(className) == returnDatas.end())
returnDatas.insert({ className ,std::vector< aLineData>() });
auto classVec = returnDatas.find(className);
classVec->second.push_back({ className, showName ,valueName ,valueStr ,policyStr ,_formatPolicy });
}
}
file.close();
return returnDatas;
return returnData;
}
void updateLayout()
{
for (int widgetIndex = 0; widgetIndex < privateWidgets.size(); widgetIndex++)
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)
{
QMainWindow* mainWindow = qobject_cast<QMainWindow*>(this->window());
QSize stackedWidgetSize = mainWindow->size();
//QSize stackedWidgetSize = stackedWidgets->size();
std::vector<QWidget*> widgets = privateWidgets[widgetIndex];
int xCount = stackedWidgetSize.width() / 600;
if (xCount == 0)
continue;
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 < widgets.size(); i++)
jsonLayouts[widgetIndex]->addWidget(widgets[i], i, 0);
continue;
}
for (int i = 0; i < widgets.size(); i++)
{
int xIndex = 0;
for (xIndex = eachHeightCount.size() - 1; xIndex > 0; xIndex--)
if (eachHeightCount[xIndex - 1] > eachHeightCount[xIndex])
{
break;
}
eachHeightCount[xIndex] += widgets[i]->size().height();
jsonLayouts[widgetIndex]->addWidget(widgets[i], eachHeightIndexCount[xIndex], xIndex);
eachHeightIndexCount[xIndex]++;
}
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)
{
// 构建 JSON 对象
QJsonArray jsonArray;
for (auto& classData : privateJsondatas)
{
for(auto& jsonData: classData)
for (auto& data : jsonData.second)
{
QJsonObject obj1;
obj1["className"] = data.className;
obj1["showName"] = data.showName;
obj1["valueName"] = data.valueName;
//QJsonArray jsonArray;
//for (auto& classData : privateJsondatas)
//{
// for(auto& jsonData: classData)
// for (auto& data : jsonData.second)
// {
// QJsonObject obj1;
// obj1["className"] = data.className;
// obj1["showName"] = data.showName;
// obj1["valueName"] = data.valueName;
switch (data.value.type()) {
case QVariant::Bool:
obj1["value"] = data.value.toBool();
break;
case QVariant::Int:
obj1["value"] = data.value.toInt();
break;
case QVariant::Double:
obj1["value"] = data.value.toDouble();
break;
case QVariant::String:
obj1["value"] = data.value.toString();
break;
default:
obj1["value"] = "error";
break;
}
// switch (data.value.type()) {
// case QVariant::Bool:
// obj1["value"] = data.value.toBool();
// break;
// case QVariant::Int:
// obj1["value"] = data.value.toInt();
// break;
// case QVariant::Double:
// obj1["value"] = data.value.toDouble();
// break;
// case QVariant::String:
// obj1["value"] = data.value.toString();
// break;
// default:
// obj1["value"] = "error";
// break;
// }
obj1["policy"] = data.formatPolicyStr;
jsonArray.append(obj1);
}
}
std::string sss = path.toStdString();
// obj1["policy"] = data.formatPolicyStr;
// jsonArray.append(obj1);
// }
//}
//std::string sss = path.toStdString();
QJsonDocument jsonDocument(jsonArray);
//QJsonDocument jsonDocument(jsonArray);
// 保存 JSON 文档到文件
QFile file(path);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out.setCodec("UTF-8");
out << jsonDocument.toJson();
file.close();
QMessageBox::information(this, "成功", "");
qDebug() << "JSON 文件保存成功";
}
else {
qDebug() << "无法保存 JSON 文件";
}
//// 保存 JSON 文档到文件
//QFile file(path);
//if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
// QTextStream out(&file);
// out.setCodec("UTF-8");
// out << jsonDocument.toJson();
// file.close();
// QMessageBox::information(this, "成功", "");
// qDebug() << "JSON 文件保存成功";
//}
//else {
// qDebug() << "无法保存 JSON 文件";
//}
}
protected:
void resizeEvent(QResizeEvent* event) override {
@@ -475,7 +408,7 @@ protected:
}
private:
std::vector<std::unordered_map<QString, std::vector< aLineData>>> privateJsondatas;//路径与数据
std::vector<std::vector< aLineData>> privateJsondatas;//路径与数据
std::vector<std::vector<QWidget*>> privateWidgets;//路径与数据
std::vector<QGridLayout*>jsonLayouts;
};
+2 -2
View File
@@ -17,8 +17,8 @@
#define sColor_Black "22, 22, 22"
#define sColor_BlackWithWhite "45, 45, 45"
#define enableHandleAiModel 1
#define disableBackEndCheck 0
#define enableHandleAiModel 0
#define disableBackEndCheck 1
#define disableeAlgo 0
//窗口设置
@@ -13,7 +13,7 @@ public:
{
Warning,
tip
};
};
QPushButton* closeBtn = new QPushButton(this);
fancyButtonText* continueBtn = new fancyButtonText(120,60,"继续","继续");
fancyWarnWindow(type typeIn,QString title,QString detail)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.