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