解决设置界面及保存配置后乱码问题
This commit is contained in:
parent
2af12b298c
commit
aa1bb42467
|
|
@ -11,6 +11,8 @@ SOURCES += main.cpp \
|
||||||
src/pages/about/aboutpage.cpp \
|
src/pages/about/aboutpage.cpp \
|
||||||
src/pages/batchconvert/batchconvertpage.cpp \
|
src/pages/batchconvert/batchconvertpage.cpp \
|
||||||
src/pages/functionsearch/functionsearchpage.cpp \
|
src/pages/functionsearch/functionsearchpage.cpp \
|
||||||
|
src/pages/functionsearch/uf20functionsearchpage.cpp \
|
||||||
|
src/pages/functionsearch/uft3functionsearchpage.cpp \
|
||||||
src/pages/help/helppage.cpp \
|
src/pages/help/helppage.cpp \
|
||||||
src/pages/settings/settingspage.cpp \
|
src/pages/settings/settingspage.cpp \
|
||||||
src/utils/datacache.cpp \
|
src/utils/datacache.cpp \
|
||||||
|
|
@ -23,6 +25,8 @@ HEADERS += src/mainwindow/mainwindow.h \
|
||||||
src/pages/about/aboutpage.h \
|
src/pages/about/aboutpage.h \
|
||||||
src/pages/batchconvert/batchconvertpage.h \
|
src/pages/batchconvert/batchconvertpage.h \
|
||||||
src/pages/functionsearch/functionsearchpage.h \
|
src/pages/functionsearch/functionsearchpage.h \
|
||||||
|
src/pages/functionsearch/uf20functionsearchpage.h \
|
||||||
|
src/pages/functionsearch/uft3functionsearchpage.h \
|
||||||
src/pages/help/helppage.h \
|
src/pages/help/helppage.h \
|
||||||
src/pages/settings/settingspage.h \
|
src/pages/settings/settingspage.h \
|
||||||
src/utils/datacache.h \
|
src/utils/datacache.h \
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,12 @@ QString SettingsPage::getConfigIniPath()
|
||||||
return QCoreApplication::applicationDirPath() + "/uf2touft3/config.ini";
|
return QCoreApplication::applicationDirPath() + "/uf2touft3/config.ini";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SettingsPage::isValidUtf8(const QByteArray &data)
|
||||||
|
{
|
||||||
|
QString test = QString::fromUtf8(data);
|
||||||
|
return test.toUtf8() == data;
|
||||||
|
}
|
||||||
|
|
||||||
void SettingsPage::loadFromConfigIni()
|
void SettingsPage::loadFromConfigIni()
|
||||||
{
|
{
|
||||||
QString iniPath = getConfigIniPath();
|
QString iniPath = getConfigIniPath();
|
||||||
|
|
@ -162,7 +168,12 @@ void SettingsPage::loadFromConfigIni()
|
||||||
QByteArray data = file.readAll();
|
QByteArray data = file.readAll();
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
QString content = QString::fromLocal8Bit(data);
|
QString content;
|
||||||
|
if (isValidUtf8(data)) {
|
||||||
|
content = QString::fromUtf8(data);
|
||||||
|
} else {
|
||||||
|
content = QString::fromLocal8Bit(data);
|
||||||
|
}
|
||||||
|
|
||||||
bool inProjectPath = false;
|
bool inProjectPath = false;
|
||||||
const QStringList lines = content.split('\n');
|
const QStringList lines = content.split('\n');
|
||||||
|
|
@ -195,12 +206,18 @@ void SettingsPage::saveToConfigIni()
|
||||||
QString iniPath = getConfigIniPath();
|
QString iniPath = getConfigIniPath();
|
||||||
|
|
||||||
QString content;
|
QString content;
|
||||||
|
bool isUtf8 = true;
|
||||||
if (QFile::exists(iniPath)) {
|
if (QFile::exists(iniPath)) {
|
||||||
QFile file(iniPath);
|
QFile file(iniPath);
|
||||||
if (file.open(QIODevice::ReadOnly)) {
|
if (file.open(QIODevice::ReadOnly)) {
|
||||||
QByteArray data = file.readAll();
|
QByteArray data = file.readAll();
|
||||||
file.close();
|
file.close();
|
||||||
content = QString::fromLocal8Bit(data);
|
isUtf8 = isValidUtf8(data);
|
||||||
|
if (isUtf8) {
|
||||||
|
content = QString::fromUtf8(data);
|
||||||
|
} else {
|
||||||
|
content = QString::fromLocal8Bit(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -213,53 +230,39 @@ void SettingsPage::saveToConfigIni()
|
||||||
QString ufAcct20Val = toWinPath(m_uf20AccountPathEdit->text());
|
QString ufAcct20Val = toWinPath(m_uf20AccountPathEdit->text());
|
||||||
QString createPathVal = toWinPath(m_outputPathEdit->text());
|
QString createPathVal = toWinPath(m_outputPathEdit->text());
|
||||||
|
|
||||||
bool foundSection = false;
|
bool inProjectPath = false;
|
||||||
QStringList lines = content.split('\n');
|
|
||||||
QStringList newLines;
|
QStringList newLines;
|
||||||
QSet<QString> updatedKeys;
|
QSet<QString> updatedKeys;
|
||||||
bool alreadyHadProjectPath = false;
|
|
||||||
|
|
||||||
for (const QString &rawLine : lines) {
|
QStringList lines = content.split('\n');
|
||||||
QString line = rawLine;
|
for (int i = 0; i < lines.size(); ++i) {
|
||||||
QString trimmed = rawLine.trimmed();
|
QString line = lines[i];
|
||||||
|
QString trimmed = line.trimmed();
|
||||||
|
|
||||||
if (trimmed.startsWith('[')) {
|
if (trimmed.startsWith('[')) {
|
||||||
if (foundSection) {
|
inProjectPath = (trimmed == "[projectPath]");
|
||||||
for (const QString &k : QStringList{"uft30", "uf20", "ufAcct20", "createPath"}) {
|
|
||||||
if (!updatedKeys.contains(k)) {
|
|
||||||
QString val;
|
|
||||||
if (k == "uft30") val = uft30Val;
|
|
||||||
else if (k == "uf20") val = uf20Val;
|
|
||||||
else if (k == "ufAcct20") val = ufAcct20Val;
|
|
||||||
else if (k == "createPath") val = createPathVal;
|
|
||||||
newLines << (k + "=" + val);
|
|
||||||
updatedKeys.insert(k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foundSection = false;
|
|
||||||
}
|
|
||||||
if (trimmed == "[projectPath]") {
|
|
||||||
if (alreadyHadProjectPath) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
alreadyHadProjectPath = true;
|
|
||||||
foundSection = true;
|
|
||||||
}
|
|
||||||
newLines << line;
|
newLines << line;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundSection && !trimmed.isEmpty() && !trimmed.startsWith(';') && !trimmed.startsWith('#')) {
|
if (inProjectPath && !trimmed.isEmpty() && !trimmed.startsWith(';') && !trimmed.startsWith('#')) {
|
||||||
int eqPos = trimmed.indexOf('=');
|
int eqPos = trimmed.indexOf('=');
|
||||||
if (eqPos > 0) {
|
if (eqPos > 0) {
|
||||||
QString key = trimmed.left(eqPos).trimmed();
|
QString key = trimmed.left(eqPos).trimmed();
|
||||||
if (key == "uft30" || key == "uf20" || key == "ufAcct20" || key == "createPath") {
|
if (key == "uft30") {
|
||||||
QString val;
|
newLines << ("uft30=" + uft30Val);
|
||||||
if (key == "uft30") val = uft30Val;
|
updatedKeys.insert(key);
|
||||||
else if (key == "uf20") val = uf20Val;
|
continue;
|
||||||
else if (key == "ufAcct20") val = ufAcct20Val;
|
} else if (key == "uf20") {
|
||||||
else if (key == "createPath") val = createPathVal;
|
newLines << ("uf20=" + uf20Val);
|
||||||
newLines << (key + "=" + val);
|
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);
|
updatedKeys.insert(key);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -269,19 +272,11 @@ void SettingsPage::saveToConfigIni()
|
||||||
newLines << line;
|
newLines << line;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundSection) {
|
bool hasProjectPath = content.contains("[projectPath]");
|
||||||
for (const QString &k : QStringList{"uft30", "uf20", "ufAcct20", "createPath"}) {
|
if (!hasProjectPath) {
|
||||||
if (!updatedKeys.contains(k)) {
|
if (!newLines.isEmpty() && !newLines.last().isEmpty()) {
|
||||||
QString val;
|
newLines << "";
|
||||||
if (k == "uft30") val = uft30Val;
|
|
||||||
else if (k == "uf20") val = uf20Val;
|
|
||||||
else if (k == "ufAcct20") val = ufAcct20Val;
|
|
||||||
else if (k == "createPath") val = createPathVal;
|
|
||||||
newLines << (k + "=" + val);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
newLines << "";
|
|
||||||
newLines << "[projectPath]";
|
newLines << "[projectPath]";
|
||||||
newLines << ("uft30=" + uft30Val);
|
newLines << ("uft30=" + uft30Val);
|
||||||
newLines << ("uf20=" + uf20Val);
|
newLines << ("uf20=" + uf20Val);
|
||||||
|
|
@ -291,7 +286,12 @@ void SettingsPage::saveToConfigIni()
|
||||||
|
|
||||||
QFile outFile(iniPath);
|
QFile outFile(iniPath);
|
||||||
if (outFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
if (outFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||||
QByteArray outData = newLines.join("\n").toLocal8Bit();
|
QByteArray outData;
|
||||||
|
if (isUtf8) {
|
||||||
|
outData = newLines.join("\n").toUtf8();
|
||||||
|
} else {
|
||||||
|
outData = newLines.join("\n").toLocal8Bit();
|
||||||
|
}
|
||||||
outFile.write(outData);
|
outFile.write(outData);
|
||||||
outFile.close();
|
outFile.close();
|
||||||
}
|
}
|
||||||
|
|
@ -361,7 +361,5 @@ void SettingsPage::onSelectOutputPath()
|
||||||
|
|
||||||
void SettingsPage::onThemeChanged(int index)
|
void SettingsPage::onThemeChanged(int index)
|
||||||
{
|
{
|
||||||
// 这里可以添加主题切换的逻辑
|
|
||||||
Q_UNUSED(index);
|
Q_UNUSED(index);
|
||||||
// 暂时只保存设置,切换效果可以在后续实现
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ private:
|
||||||
void saveToConfigIni();
|
void saveToConfigIni();
|
||||||
void loadFromConfigIni();
|
void loadFromConfigIni();
|
||||||
QString getConfigIniPath();
|
QString getConfigIniPath();
|
||||||
|
bool isValidUtf8(const QByteArray &data);
|
||||||
|
|
||||||
QLineEdit *m_uft30PathEdit;
|
QLineEdit *m_uft30PathEdit;
|
||||||
QLineEdit *m_uf20PathEdit;
|
QLineEdit *m_uf20PathEdit;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue