391 lines
13 KiB
C++
391 lines
13 KiB
C++
#include "settingspage.h"
|
|
#include "utils/Constants.h"
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QGroupBox>
|
|
#include <QFileDialog>
|
|
#include <QMessageBox>
|
|
#include <QSettings>
|
|
#include <QCoreApplication>
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QSet>
|
|
#include "ElaLineEdit.h"
|
|
#include "ElaComboBox.h"
|
|
#include "ElaPushButton.h"
|
|
#include "src/utils/configmanager.h"
|
|
|
|
SettingsPage::SettingsPage(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
initUI();
|
|
loadSettings();
|
|
}
|
|
|
|
SettingsPage::~SettingsPage()
|
|
{
|
|
}
|
|
|
|
void SettingsPage::initUI()
|
|
{
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
layout->setContentsMargins(30, 30, 30, 30);
|
|
layout->setSpacing(20);
|
|
|
|
// ==========================================
|
|
// 第一部分:环境配置
|
|
// ==========================================
|
|
QGroupBox *envBox = new QGroupBox("环境配置");
|
|
QVBoxLayout *envLayout = new QVBoxLayout(envBox);
|
|
envLayout->setSpacing(15);
|
|
|
|
// UFT30项目环境路径
|
|
QHBoxLayout *uft30Layout = new QHBoxLayout;
|
|
QLabel *uft30Label = new QLabel("UFT30项目环境路径:");
|
|
uft30Label->setMinimumWidth(160);
|
|
m_uft30PathEdit = new ElaLineEdit;
|
|
ElaPushButton *uft30Btn = new ElaPushButton("浏览...");
|
|
connect(uft30Btn, &QPushButton::clicked, this, &SettingsPage::onSelectUFT30Path);
|
|
uft30Layout->addWidget(uft30Label);
|
|
uft30Layout->addWidget(m_uft30PathEdit);
|
|
uft30Layout->addWidget(uft30Btn);
|
|
envLayout->addLayout(uft30Layout);
|
|
|
|
// UF20项目环境路径
|
|
QHBoxLayout *uf20Layout = new QHBoxLayout;
|
|
QLabel *uf20Label = new QLabel("UF20项目环境路径:");
|
|
uf20Label->setMinimumWidth(160);
|
|
m_uf20PathEdit = new ElaLineEdit;
|
|
ElaPushButton *uf20Btn = new ElaPushButton("浏览...");
|
|
connect(uf20Btn, &QPushButton::clicked, this, &SettingsPage::onSelectUF20Path);
|
|
uf20Layout->addWidget(uf20Label);
|
|
uf20Layout->addWidget(m_uf20PathEdit);
|
|
uf20Layout->addWidget(uf20Btn);
|
|
envLayout->addLayout(uf20Layout);
|
|
|
|
// UF20账户项目环境路径
|
|
QHBoxLayout *uf20AccountLayout = new QHBoxLayout;
|
|
QLabel *uf20AccountLabel = new QLabel("UF20账户项目环境路径:");
|
|
uf20AccountLabel->setMinimumWidth(160);
|
|
m_uf20AccountPathEdit = new ElaLineEdit;
|
|
ElaPushButton *uf20AccountBtn = new ElaPushButton("浏览...");
|
|
connect(uf20AccountBtn, &QPushButton::clicked, this, &SettingsPage::onSelectUF20AccountPath);
|
|
uf20AccountLayout->addWidget(uf20AccountLabel);
|
|
uf20AccountLayout->addWidget(m_uf20AccountPathEdit);
|
|
uf20AccountLayout->addWidget(uf20AccountBtn);
|
|
envLayout->addLayout(uf20AccountLayout);
|
|
|
|
// 转码生成路径
|
|
QHBoxLayout *outputLayout = new QHBoxLayout;
|
|
QLabel *outputLabel = new QLabel("转码生成路径:");
|
|
outputLabel->setMinimumWidth(160);
|
|
m_outputPathEdit = new ElaLineEdit;
|
|
ElaPushButton *outputBtn = new ElaPushButton("浏览...");
|
|
connect(outputBtn, &QPushButton::clicked, this, &SettingsPage::onSelectOutputPath);
|
|
outputLayout->addWidget(outputLabel);
|
|
outputLayout->addWidget(m_outputPathEdit);
|
|
outputLayout->addWidget(outputBtn);
|
|
envLayout->addLayout(outputLayout);
|
|
|
|
layout->addWidget(envBox);
|
|
|
|
// ==========================================
|
|
// 第二部分:系统配置
|
|
// ==========================================
|
|
QGroupBox *systemBox = new QGroupBox("系统配置");
|
|
QVBoxLayout *systemLayout = new QVBoxLayout(systemBox);
|
|
systemLayout->setSpacing(15);
|
|
|
|
// 主题切换
|
|
QHBoxLayout *themeLayout = new QHBoxLayout;
|
|
QLabel *themeLabel = new QLabel("主题模式:");
|
|
themeLabel->setMinimumWidth(160);
|
|
m_themeCombo = new ElaComboBox;
|
|
m_themeCombo->addItems({"日间模式", "夜间模式"});
|
|
m_themeCombo->setMinimumWidth(150);
|
|
connect(m_themeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsPage::onThemeChanged);
|
|
themeLayout->addWidget(themeLabel);
|
|
themeLayout->addWidget(m_themeCombo);
|
|
themeLayout->addStretch();
|
|
systemLayout->addLayout(themeLayout);
|
|
|
|
layout->addWidget(systemBox);
|
|
|
|
// ==========================================
|
|
// 底部按钮
|
|
// ==========================================
|
|
QHBoxLayout *btnLayout = new QHBoxLayout;
|
|
ElaPushButton *saveBtn = new ElaPushButton("保存设置");
|
|
connect(saveBtn, &QPushButton::clicked, this, &SettingsPage::onSaveSettings);
|
|
ElaPushButton *resetBtn = new ElaPushButton("恢复默认");
|
|
connect(resetBtn, &QPushButton::clicked, this, &SettingsPage::onResetSettings);
|
|
btnLayout->addStretch();
|
|
btnLayout->addWidget(saveBtn);
|
|
btnLayout->addWidget(resetBtn);
|
|
layout->addLayout(btnLayout);
|
|
|
|
// 添加弹性空间
|
|
layout->addStretch();
|
|
}
|
|
|
|
void SettingsPage::loadSettings()
|
|
{
|
|
loadFromConfigIni();
|
|
|
|
QSettings settings("UFT30", "ChangeCode");
|
|
QString theme = settings.value("Theme", "日间模式").toString();
|
|
int index = m_themeCombo->findText(theme);
|
|
if (index >= 0) {
|
|
m_themeCombo->setCurrentIndex(index);
|
|
}
|
|
}
|
|
|
|
QString SettingsPage::getConfigIniPath()
|
|
{
|
|
return Constants::configIni();
|
|
}
|
|
|
|
bool SettingsPage::isValidUtf8(const QByteArray &data)
|
|
{
|
|
QString test = QString::fromUtf8(data);
|
|
return test.toUtf8() == data;
|
|
}
|
|
|
|
void SettingsPage::loadFromConfigIni()
|
|
{
|
|
QString iniPath = getConfigIniPath();
|
|
if (!QFile::exists(iniPath)) {
|
|
return;
|
|
}
|
|
|
|
QFile file(iniPath);
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
return;
|
|
}
|
|
|
|
QByteArray data = file.readAll();
|
|
file.close();
|
|
|
|
QString content;
|
|
if (isValidUtf8(data)) {
|
|
content = QString::fromUtf8(data);
|
|
} else {
|
|
content = QString::fromLocal8Bit(data);
|
|
}
|
|
|
|
bool inProjectPath = false;
|
|
const QStringList lines = content.split('\n');
|
|
for (const QString &rawLine : lines) {
|
|
QString line = rawLine.trimmed();
|
|
if (line.startsWith('[')) {
|
|
inProjectPath = (line == "[projectPath]");
|
|
continue;
|
|
}
|
|
if (!inProjectPath || line.isEmpty() || line.startsWith(';') || line.startsWith('#')) {
|
|
continue;
|
|
}
|
|
|
|
int eqPos = line.indexOf('=');
|
|
if (eqPos < 0) continue;
|
|
|
|
QString key = line.left(eqPos).trimmed();
|
|
QString value = line.mid(eqPos + 1).trimmed();
|
|
value.replace("/", "\\");
|
|
|
|
if (key == "uft30") m_uft30PathEdit->setText(value);
|
|
else if (key == "uf20") m_uf20PathEdit->setText(value);
|
|
else if (key == "ufAcct20") m_uf20AccountPathEdit->setText(value);
|
|
else if (key == "createPath") m_outputPathEdit->setText(value);
|
|
}
|
|
}
|
|
|
|
void SettingsPage::saveToConfigIni()
|
|
{
|
|
QString iniPath = getConfigIniPath();
|
|
|
|
QString content;
|
|
bool isUtf8 = true;
|
|
if (QFile::exists(iniPath)) {
|
|
QFile file(iniPath);
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
QByteArray data = file.readAll();
|
|
file.close();
|
|
isUtf8 = isValidUtf8(data);
|
|
if (isUtf8) {
|
|
content = QString::fromUtf8(data);
|
|
} else {
|
|
content = QString::fromLocal8Bit(data);
|
|
}
|
|
}
|
|
}
|
|
|
|
auto toWinPath = [](const QString &p) -> QString {
|
|
return p.trimmed().replace("/", "\\");
|
|
};
|
|
|
|
QString uft30Val = toWinPath(m_uft30PathEdit->text());
|
|
QString uf20Val = toWinPath(m_uf20PathEdit->text());
|
|
QString ufAcct20Val = toWinPath(m_uf20AccountPathEdit->text());
|
|
QString createPathVal = toWinPath(m_outputPathEdit->text());
|
|
|
|
bool inProjectPath = false;
|
|
QStringList newLines;
|
|
QSet<QString> updatedKeys;
|
|
|
|
QStringList lines = content.split('\n');
|
|
for (int i = 0; i < lines.size(); ++i) {
|
|
QString line = lines[i];
|
|
QString trimmed = line.trimmed();
|
|
|
|
if (trimmed.startsWith('[')) {
|
|
inProjectPath = (trimmed == "[projectPath]");
|
|
newLines << line;
|
|
continue;
|
|
}
|
|
|
|
if (trimmed.startsWith("uft30pub=")) {
|
|
int eqPos = trimmed.indexOf('=');
|
|
QString pathValue = trimmed.mid(eqPos + 1);
|
|
int pubIndex = pathValue.indexOf("\\upub");
|
|
if (pubIndex >= 0) {
|
|
QString pubSuffix = pathValue.mid(pubIndex);
|
|
QString newPath = uft30Val + pubSuffix;
|
|
newLines << ("uft30pub=" + newPath);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (inProjectPath && !trimmed.isEmpty() && !trimmed.startsWith(';') && !trimmed.startsWith('#')) {
|
|
int eqPos = trimmed.indexOf('=');
|
|
if (eqPos > 0) {
|
|
QString key = trimmed.left(eqPos).trimmed();
|
|
if (key == "uft30") {
|
|
newLines << ("uft30=" + uft30Val);
|
|
updatedKeys.insert(key);
|
|
continue;
|
|
} else if (key == "uf20") {
|
|
newLines << ("uf20=" + uf20Val);
|
|
updatedKeys.insert(key);
|
|
continue;
|
|
} else if (key == "ufAcct20") {
|
|
newLines << ("ufAcct20=" + ufAcct20Val);
|
|
updatedKeys.insert(key);
|
|
continue;
|
|
} else if (key == "createPath") {
|
|
newLines << ("createPath=" + createPathVal);
|
|
updatedKeys.insert(key);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
newLines << line;
|
|
}
|
|
|
|
bool hasProjectPath = content.contains("[projectPath]");
|
|
if (!hasProjectPath) {
|
|
if (!newLines.isEmpty() && !newLines.last().isEmpty()) {
|
|
newLines << "";
|
|
}
|
|
newLines << "[projectPath]";
|
|
newLines << ("uft30=" + uft30Val);
|
|
newLines << ("uf20=" + uf20Val);
|
|
newLines << ("ufAcct20=" + ufAcct20Val);
|
|
newLines << ("createPath=" + createPathVal);
|
|
}
|
|
|
|
QFile outFile(iniPath);
|
|
if (outFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
QByteArray outData;
|
|
if (isUtf8) {
|
|
outData = newLines.join("\n").toUtf8();
|
|
} else {
|
|
outData = newLines.join("\n").toLocal8Bit();
|
|
}
|
|
outFile.write(outData);
|
|
outFile.close();
|
|
}
|
|
}
|
|
|
|
void SettingsPage::onSaveSettings()
|
|
{
|
|
QSettings settings("UFT30", "ChangeCode");
|
|
settings.setValue("Theme", m_themeCombo->currentText());
|
|
|
|
ConfigManager* configManager = ConfigManager::instance();
|
|
|
|
QString uft30Val = m_uft30PathEdit->text().trimmed().replace("/", "\\");
|
|
QString uf20Val = m_uf20PathEdit->text().trimmed().replace("/", "\\");
|
|
QString ufAcct20Val = m_uf20AccountPathEdit->text().trimmed().replace("/", "\\");
|
|
QString createPathVal = m_outputPathEdit->text().trimmed().replace("/", "\\");
|
|
|
|
configManager->setValue("projectPath", "uft30", uft30Val);
|
|
configManager->setValue("projectPath", "uf20", uf20Val);
|
|
configManager->setValue("projectPath", "ufAcct20", ufAcct20Val);
|
|
configManager->setValue("projectPath", "createPath", createPathVal);
|
|
|
|
QString uft30PubVal = uft30Val + "\\upub\\dev_codes";
|
|
configManager->setValue("projectPath", "uft30pub", uft30PubVal);
|
|
|
|
configManager->saveConfig();
|
|
|
|
QMessageBox::information(this, "保存成功", "配置已保存!");
|
|
}
|
|
|
|
void SettingsPage::onResetSettings()
|
|
{
|
|
QMessageBox::StandardButton reply;
|
|
reply = QMessageBox::question(this, "确认重置", "确定要恢复所有默认设置吗?",
|
|
QMessageBox::Yes | QMessageBox::No);
|
|
if (reply == QMessageBox::Yes) {
|
|
m_uft30PathEdit->clear();
|
|
m_uf20PathEdit->clear();
|
|
m_uf20AccountPathEdit->clear();
|
|
m_outputPathEdit->clear();
|
|
m_themeCombo->setCurrentIndex(0);
|
|
|
|
saveToConfigIni();
|
|
|
|
QSettings settings("UFT30", "ChangeCode");
|
|
settings.clear();
|
|
QMessageBox::information(this, "重置成功", "已恢复默认设置!");
|
|
}
|
|
}
|
|
|
|
void SettingsPage::onSelectUFT30Path()
|
|
{
|
|
QString dir = QFileDialog::getExistingDirectory(this, "选择UFT30项目环境路径", m_uft30PathEdit->text());
|
|
if (!dir.isEmpty()) {
|
|
m_uft30PathEdit->setText(dir);
|
|
}
|
|
}
|
|
|
|
void SettingsPage::onSelectUF20Path()
|
|
{
|
|
QString dir = QFileDialog::getExistingDirectory(this, "选择UF20项目环境路径", m_uf20PathEdit->text());
|
|
if (!dir.isEmpty()) {
|
|
m_uf20PathEdit->setText(dir);
|
|
}
|
|
}
|
|
|
|
void SettingsPage::onSelectUF20AccountPath()
|
|
{
|
|
QString dir = QFileDialog::getExistingDirectory(this, "选择UF20账户项目环境路径", m_uf20AccountPathEdit->text());
|
|
if (!dir.isEmpty()) {
|
|
m_uf20AccountPathEdit->setText(dir);
|
|
}
|
|
}
|
|
|
|
void SettingsPage::onSelectOutputPath()
|
|
{
|
|
QString dir = QFileDialog::getExistingDirectory(this, "选择转码生成路径", m_outputPathEdit->text());
|
|
if (!dir.isEmpty()) {
|
|
m_outputPathEdit->setText(dir);
|
|
}
|
|
}
|
|
|
|
void SettingsPage::onThemeChanged()
|
|
{
|
|
}
|