-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigmanager.cpp
More file actions
217 lines (177 loc) · 4.93 KB
/
configmanager.cpp
File metadata and controls
217 lines (177 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "configmanager.h"
#include <QDir>
#include <QDebug>
#include <QCoreApplication>
ConfigManager* ConfigManager::m_instance = nullptr;
ConfigManager::ConfigManager(QObject *parent) : QObject(parent)
{
// 获取配置文件路径 - 使用应用程序所在目录
QString appDir = QCoreApplication::applicationDirPath();
m_configPath = QDir(appDir).absoluteFilePath("config.json");
qDebug() << "应用程序目录:" << appDir;
qDebug() << "配置文件路径:" << m_configPath;
// 初始化默认配置
initDefaultConfig();
// 尝试加载配置文件
loadConfig();
}
ConfigManager::~ConfigManager()
{
// 保存配置
saveConfig();
// 注意:不要在析构函数中修改静态成员变量m_instance
// 因为当析构函数被调用时,对象已经在被销毁的过程中
}
ConfigManager* ConfigManager::instance()
{
if (!m_instance) {
m_instance = new ConfigManager();
}
return m_instance;
}
void ConfigManager::initDefaultConfig()
{
// 基本设置
m_config["hotkey"] = "Ctrl+Shift+O";
m_config["hotkeyEnabled"] = true;
// 模型设置
m_config["modelPath"] = "model/model_1.8B.gguf";
// 界面设置
m_config["autoTranslateEnabled"] = true;
m_config["autoTranslateDelay"] = 500;
m_config["followTranslation"] = false;
m_config["stayOnTopByDefault"] = false;
m_config["defaultWindowSize"] = QJsonObject({
{"width", 350},
{"height", 500}
});
// OCR设置
m_config["ocrLanguage"] = "eng";
}
bool ConfigManager::loadConfig()
{
QFile file(m_configPath);
if (!file.exists()) {
qDebug() << "配置文件不存在,使用默认配置";
return false;
}
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "无法打开配置文件";
return false;
}
QByteArray jsonData = file.readAll();
file.close();
QJsonParseError parseError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError);
if (parseError.error != QJsonParseError::NoError) {
qDebug() << "配置文件解析错误:" << parseError.errorString();
return false;
}
m_config = jsonDoc.object();
qDebug() << "配置文件加载成功";
return true;
}
bool ConfigManager::saveConfig()
{
QFile file(m_configPath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "无法打开配置文件进行写入";
return false;
}
QJsonDocument jsonDoc(m_config);
QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Indented);
if (file.write(jsonData) == -1) {
qDebug() << "配置文件写入失败";
file.close();
return false;
}
file.close();
qDebug() << "配置文件保存成功";
return true;
}
void ConfigManager::resetToDefault()
{
initDefaultConfig();
saveConfig();
qDebug() << "配置已重置为默认值";
}
// 获取配置项
QString ConfigManager::getHotkey()
{
return m_config["hotkey"].toString();
}
bool ConfigManager::getHotkeyEnabled()
{
return m_config["hotkeyEnabled"].toBool();
}
QString ConfigManager::getModelPath()
{
return m_config["modelPath"].toString();
}
bool ConfigManager::getAutoTranslateEnabled()
{
return m_config["autoTranslateEnabled"].toBool();
}
int ConfigManager::getAutoTranslateDelay()
{
return m_config["autoTranslateDelay"].toInt();
}
bool ConfigManager::getFollowTranslation()
{
return m_config["followTranslation"].toBool(false);
}
bool ConfigManager::getStayOnTopByDefault()
{
return m_config["stayOnTopByDefault"].toBool();
}
QSize ConfigManager::getDefaultWindowSize()
{
QJsonObject sizeObj = m_config["defaultWindowSize"].toObject();
int width = sizeObj["width"].toInt(350);
int height = sizeObj["height"].toInt(500);
return QSize(width, height);
}
QString ConfigManager::getOCRLanguage()
{
return m_config["ocrLanguage"].toString();
}
// 设置配置项
void ConfigManager::setHotkey(const QString& hotkey)
{
m_config["hotkey"] = hotkey;
}
void ConfigManager::setHotkeyEnabled(bool enabled)
{
m_config["hotkeyEnabled"] = enabled;
}
void ConfigManager::setModelPath(const QString& path)
{
m_config["modelPath"] = path;
}
void ConfigManager::setAutoTranslateEnabled(bool enabled)
{
m_config["autoTranslateEnabled"] = enabled;
}
void ConfigManager::setAutoTranslateDelay(int delay)
{
m_config["autoTranslateDelay"] = delay;
}
void ConfigManager::setFollowTranslation(bool follow)
{
m_config["followTranslation"] = follow;
}
void ConfigManager::setStayOnTopByDefault(bool stayOnTop)
{
m_config["stayOnTopByDefault"] = stayOnTop;
}
void ConfigManager::setDefaultWindowSize(const QSize& size)
{
QJsonObject sizeObj;
sizeObj["width"] = size.width();
sizeObj["height"] = size.height();
m_config["defaultWindowSize"] = sizeObj;
}
void ConfigManager::setOCRLanguage(const QString& language)
{
m_config["ocrLanguage"] = language;
}