-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettingsdialog.cpp
507 lines (422 loc) · 16.2 KB
/
settingsdialog.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "customdir.h"
#include "configsaver.h"
#include "llminterface.h"
#define ZOOM_ENABLE_REQ "(关闭鼠标缩放后可用)"
SettingsDialog::SettingsDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
this->setFixedSize(this->size());
p = (VPetInterface *) this->parentWidget();
orgSize = p->size();
// 窗体设置
this->move(150, 100); // 设置窗体默认位置
this->setWindowTitle("虚拟桌宠设置");
this->setWindowIcon(QIcon(":/img/resources/icons/settings.png"));
this->setWindowModality(Qt::WindowModal);
this->setAttribute(Qt::WA_DeleteOnClose, true);
ButtonBoxInit();
GeneralSettingsInit();
ModelSettingsInit();
VoiceSettingsInit();
LLMSettingsInit();
AboutSettingsInit();
}
SettingsDialog::~SettingsDialog()
{
ConfigSaver::writeConfig(p);
qDebug() << QT_BACKGROUND_LOG << "Settings released";
delete ui;
}
void SettingsDialog::ButtonBoxInit()
{
// 设置文字
ui->configureBox->button(QDialogButtonBox::Cancel)->setText("取消");
ui->configureBox->button(QDialogButtonBox::Ok)->setText("确定");
// 设置大小
auto buttonList = ui->configureBox->buttons();
for(auto button: buttonList)
button->setFixedSize(72, 26);
}
void SettingsDialog::GeneralSettingsInit()
{
isSliderZoomAvailable = !p->wheelZoomState();
ui->checkBoxWindowTop->setChecked(p->windowOnTopState());
ui->checkBoxWheelZoom->setChecked(p->wheelZoomState());
ui->sliderZoom->setEnabled(isSliderZoomAvailable);
ui->sliderZoom->setMaximum(MAX_MODEL_WIDTH); // in width
ui->sliderZoom->setMinimum(MIN_MODEL_WIDTH); // in width
ui->sliderZoom->setPageStep(5); // in width
// 设置添加自启动按钮状态
if(p->isRegAutoRun())
{
ui->btnSetAutoRun->setDisabled(true);
ui->labelAutoRunInfo->setText("开机自启动项已存在,请在系统启动设置中设置");
} else {
ui->labelAutoRunInfo->setText("将虚拟桌宠添加进开机自启动项");
connect(ui->btnSetAutoRun, &QPushButton::clicked,
this, &SettingsDialog::onAutoRunBtnClicked);
}
// 设置拖动条状态
if(isSliderZoomAvailable)
{
auto size = p->size();
QString sizeStr = QString("(%1×%2)").arg(size.width()).arg(size.height());
ui->labelSize->setText(QString("缩放").append(sizeStr));
ui->sliderZoom->setValue(size.width());
// 缩放条时间槽连接:实现拖动缩放
connect(ui->sliderZoom, &QSlider::sliderMoved, this, &SettingsDialog::onSliderChanged);
connect(ui->sliderZoom, &QSlider::sliderReleased, this, &SettingsDialog::onSliderChanged);
} else
ui->labelSize->setText(QString("缩放").append(ZOOM_ENABLE_REQ));
QStringList fpsValues;
fpsValues << "30" << "60";
ui->comboFPSSetting->addItems(fpsValues);
ui->comboFPSSetting->setCurrentText(QString::number(p->fps()));
connect(ui->checkBoxWheelZoom, &QCheckBox::stateChanged,
this, &SettingsDialog::onZoomBoxChanged);
}
void SettingsDialog::ModelSettingsInit()
{
fileDir = FileHandler::getModelDirList();
ui->comboModel->addItems(fileDir);
modelIndex = p->modelIndex();
ui->comboModel->setCurrentIndex(modelIndex);
connect(ui->comboModel, &QComboBox::textActivated,
this, &SettingsDialog::onComboBoxChanged);
connect(ui->importButton, &QPushButton::clicked,
this, &SettingsDialog::onImportClicked);
connect(ui->deleteButton, &QPushButton::clicked,
this, &SettingsDialog::onDeleteModelClicked);
}
void SettingsDialog::VoiceSettingsInit()
{
auto voiceList = *LlmInterface::getVoiceList(); // 取值
ui->comboVoice->addItems(voiceList);
// 初始化音量条、语速条及相关标签
ui->volumeSlider->setMaximum(100);
ui->volumeSlider->setMinimum(0);
ui->volumeSlider->setPageStep(1);
ui->volumeSlider->setValue(p->volume());
ui->volume->setText(QString::number(p->volume()) + "%");
ui->comboVoice->setCurrentIndex(p->voice());
ui->paceSlider->setMaximum(100);
ui->paceSlider->setMinimum(0);
ui->volumeSlider->setPageStep(1);
ui->paceSlider->setValue(p->pace());
ui->pace->setText(QString::number(p->pace()) + "%");
// 音量条语速条信号和槽
connect(ui->volumeSlider, &OptimizedSlider::sliderMoved,
this, &SettingsDialog::onVolumeChanged);
connect(ui->volumeSlider, &OptimizedSlider::sliderReleased,
this, &SettingsDialog::onVolumeChanged);
connect(ui->paceSlider, &OptimizedSlider::sliderMoved,
this, &SettingsDialog::onPaceChanged);
connect(ui->paceSlider, &OptimizedSlider::sliderReleased,
this, &SettingsDialog::onPaceChanged);
connect(ui->previewButton, &QPushButton::clicked,
this, &SettingsDialog::testVoice);
connect(ui->voiceBindButton, &QPushButton::clicked,
this, &SettingsDialog::onSaveToModelClicked);
}
void SettingsDialog::LLMSettingsInit()
{
QStringList api = p->api();
if(!api[0].isEmpty() && !api[1].isEmpty())
{
ui->textEditAPIKey->setText(api[0]);
ui->textEditSecretKey->setText(api[1]);
ui->checkBoxTTS->setDisabled(!p->LLMEnable());
ui->checkBoxLLM->setChecked(p->LLMEnable());
ui->checkBoxTTS->setChecked(p->TTSEnable());
} else {
ui->checkBoxTTS->setDisabled(true);
ui->checkBoxLLM->setDisabled(true);
}
connect(ui->checkBoxLLM, &QCheckBox::stateChanged,
this, &SettingsDialog::onLLMBoxChanged);
connect(ui->checkBoxTTS, &QCheckBox::stateChanged,
this, &SettingsDialog::onTTSBoxChanged);
connect(ui->btnAPISave, &QPushButton::pressed,
this, &SettingsDialog::onAPISaveClicked);
}
void SettingsDialog::AboutSettingsInit()
{
connect(ui->btnWebRaw, &QPushButton::clicked, this, [=]() {
QString url = "https://github.com/Positron114514/GunShipVPet";
QDesktopServices::openUrl(QUrl(url.toLatin1()));
});
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &SettingsDialog::onChangedAboutPage);
// if(p->isRegAutoRun())
// ui->btnRegAutoRun->setDisabled(true);
// connect(ui->btnRegAutoRun, &QPushButton::clicked,
// this, &SettingsDialog::onRegBtnClicked);
}
void SettingsDialog::accept()
{
p->setWindowOnTopState(ui->checkBoxWindowTop->isChecked());
p->setWheelZoomState(ui->checkBoxWheelZoom->isChecked());
p->setModelIndex(ui->comboModel->currentIndex());
p->setFps(ui->comboFPSSetting->currentText().toInt());
p->setLLMEnable(ui->checkBoxLLM->isChecked());
p->setTTSEnable(ui->checkBoxTTS->isChecked());
p->setVoice(ui->comboVoice->currentIndex());
int voiceIndex = FileHandler::getModelVoiceIndex();
if(voiceIndex != -1)
{
qDebug() << "Find voice from model. index: " << voiceIndex;
p->setVoice(voiceIndex);
}
qDebug() << QT_BACKGROUND_LOG << "settings carded";
done(Accepted);
}
void SettingsDialog::reject()
{
p->resizeWindow(orgSize);
qDebug() << QT_BACKGROUND_LOG << "action rejected";
done(Rejected);
}
void SettingsDialog::onZoomBoxChanged()
{
bool state = ui->checkBoxWheelZoom->isChecked();
if(!state)
{
auto size = p->size();
QString sizeStr = QString("(%1×%2)").arg(size.width()).arg(size.height());
ui->labelSize->setText(QString("缩放").append(sizeStr));
ui->sliderZoom->setValue(p->size().width());
// 缩放条时间槽连接:实现拖动缩放
connect(ui->sliderZoom, &QSlider::sliderMoved, this, &SettingsDialog::onSliderChanged);
connect(ui->sliderZoom, &QSlider::sliderReleased, this, &SettingsDialog::onSliderChanged);
isSliderZoomAvailable = true;
} else {
ui->labelSize->setText(QString("缩放").append(ZOOM_ENABLE_REQ));
isSliderZoomAvailable = false;
}
ui->sliderZoom->setDisabled(!isSliderZoomAvailable);
}
void SettingsDialog::onSliderChanged()
{
int value = ui->sliderZoom->value();
p->resizeWindow(value, value * MODEL_PROPORTION);
auto size = p->size();
QString sizeStr = QString("(%1×%2)").arg(size.width()).arg(size.height());
ui->labelSize->setText(QString("缩放").append(sizeStr));
}
void SettingsDialog::onComboBoxChanged()
{
modelIndex = ui->comboModel->currentIndex();
qDebug() << QT_DEBUG_OUTPUT << "current index:" << modelIndex;
}
void SettingsDialog::onImportClicked()
{
QString modelDir = QFileDialog::getOpenFileName(this,
"选择模型",
"/",
tr("模型文件(*.model3.json)"));
QFileInfo modelInfo(modelDir);
// qDebug() << QT_DEBUG_OUTPUT << info.path();
FileHandler::addModel(modelInfo.path());
ui->comboModel->clear();
fileDir = FileHandler::getModelDirList();
ui->comboModel->addItems(fileDir);
}
void SettingsDialog::onLLMBoxChanged()
{
p->setLLMEnable(ui->checkBoxLLM->isChecked());
if(!ui->checkBoxLLM->isChecked())
{
ui->checkBoxTTS->setDisabled(true);
ui->checkBoxTTS->setChecked(false);
p->setTTSEnable(false);
} else {
ui->checkBoxTTS->setDisabled(false);
}
}
void SettingsDialog::onTTSBoxChanged()
{
p->setTTSEnable(ui->checkBoxTTS->isChecked());
}
void SettingsDialog::onAutoRunBtnClicked()
{
p->regAutoRun();
if(p->isRegAutoRun())
{
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle("操作成功");
msgBox.setText("开机自启动项添加成功!\n如果需要关闭请在系统启动设置中手动关闭");
// msgBox.button(QMessageBox::Ok)->setText("确定");
msgBox.exec();
ui->btnSetAutoRun->setDisabled(true);
ui->labelAutoRunInfo->setText("开机自启动项已存在,请在系统启动设置中设置");
} else {
qDebug() << QT_BACKGROUND_LOG << "AUTORUN regedit creation failed! Time"
<< autoRunErrorTime + 1;
if(autoRunErrorTime <= 3)
{
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle("操作失败");
msgBox.setText("开机自启动项添加失败!\n请重试...");
// msgBox.button(QMessageBox::Ok)->setText("确定");
msgBox.exec();
qDebug() << QT_INTERFACE_LOG << "Error window exec and notified";
autoRunErrorTime++;
} else {
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Critical);
msgBox.setWindowTitle("操作多次失败...");
msgBox.setText("开机自启动项多次添加失败!\n请向我们提交log与Issues,谢谢!");
// msgBox.button(QMessageBox::Ok)->setText("确定");
connect(msgBox.button(QMessageBox::Ok), &QPushButton::clicked, this, [=]() {
QString url = "https://github.com/Positron114514/GunShipVPet/issues";
QDesktopServices::openUrl(url);
});
msgBox.exec();
qDebug() << QT_INTERFACE_LOG << "Multi-error window exec";
ui->btnSetAutoRun->setDisabled(true);
ui->labelAutoRunInfo->setText("看起来功能出现了问题,暂时不可用...");
}
}
}
void SettingsDialog::onChangedAboutPage()
{
QWidget *tab = ui->tabWidget->currentWidget();
if(tab == ui->aboutTab)
{
QFont font;
font.setFamily("MiSans");
font.setPointSize(16);
ui->ourName->setFont(font);
}
}
void SettingsDialog::onAPISaveClicked()
{
QString apiKey = ui->textEditAPIKey->text();
QString secretKey = ui->textEditSecretKey->text();
if(!apiKey.isEmpty() && !secretKey.isEmpty())
{
p->setAPI(apiKey, secretKey);
QMessageBox msg(this);
msg.setWindowTitle("提示");
if(!LlmInterface::getAccessToken(p->api()[0], p->api()[1]))
{
qDebug() << QT_DEBUG_OUTPUT << "failed to load access token";
msg.setText("错误:获取accessToken失败(api不可用)");
p->setTokenState(false);
}else
{
msg.setText("API设置成功");
}
msg.exec();
ui->checkBoxLLM->setDisabled(false);
} else {
QMessageBox msg(this);
msg.setWindowTitle("失败");
msg.setText("请完整填写文心一言API并重试");
msg.exec();
QStringList cur = p->api();
if(!cur[0].isEmpty() && !cur[1].isEmpty())
{
ui->textEditAPIKey->setText(cur[0]);
ui->textEditSecretKey->setText(cur[1]);
}
}
}
void SettingsDialog::onDeleteModelClicked()
{
// 弹窗初始化
QMessageBox msgBox(this);
msgBox.setWindowTitle("删除");
QString modelName = ui->comboModel->currentText();
msgBox.setText("确定要删除模型" + modelName + "?");
msgBox.setIcon(QMessageBox::Question);
QPushButton *btnAccept = new QPushButton("确认");
QPushButton *btnReject = new QPushButton("取消");
msgBox.addButton(btnAccept, QMessageBox::AcceptRole);
msgBox.addButton(btnReject, QMessageBox::RejectRole);
// qDebug() << QT_DEBUG_OUTPUT << msgBox.exec();
if(!msgBox.exec())
{
int activatedIndex = p->modelIndex();
int deletedIndex = ui->comboModel->currentIndex();
qDebug() << QT_DEBUG_OUTPUT << "activated" << activatedIndex << " deleted" << deletedIndex;
if(activatedIndex == deletedIndex)
{
QMessageBox warning(this);
warning.setWindowTitle("错误");
warning.setIcon(QMessageBox::Warning);
warning.setText("请先选择其他模型再尝试删除");
warning.exec();
return;
}
QString path = CustomDir::resourcesDir() + modelName;
qDebug() << QT_BACKGROUND_LOG << "start deleting" << path;
if(!FileHandler::deleteFolder(path))
{
QMessageBox warning(this);
warning.setWindowTitle("错误");
warning.setIcon(QMessageBox::Warning);
warning.setText("删除文件夹失败,请阅读log并反馈");
warning.exec();
return;
}
FileHandler::resetModel();
p->setModelIndex((activatedIndex < deletedIndex) ?
activatedIndex : activatedIndex - 1);
// 重新建立模型列表
ui->comboModel->clear();
fileDir = FileHandler::getModelDirList();
ui->comboModel->addItems(fileDir);
ui->comboModel->setCurrentIndex(p->modelIndex());
}
}
void SettingsDialog::onVolumeChanged()
{
int volume = ui->volumeSlider->value();
int returnValue;
if(volume > HIGH_VOLUME)
{
if(!isHighVolumeNotified)
{
QMessageBox notify(this);
notify.setWindowTitle("高音量警告");
notify.setText("音量高于" + QString::number(HIGH_VOLUME) + "%,可能会损伤听力,是否继续?");
notify.setIcon(QMessageBox::Warning);
notify.addButton("确定", QMessageBox::AcceptRole);
notify.addButton("取消", QMessageBox::RejectRole);
returnValue = notify.exec();
if(returnValue)
isHighVolumeNotified = true;
else
return;
}
}
p->setVolume(volume);
ui->volume->setText(QString::number(p->volume()) + "%");
}
void SettingsDialog::onPaceChanged()
{
int pace = ui->paceSlider->value();
p->setPace(pace);
ui->pace->setText(QString::number(p->pace()) + "%");
}
void SettingsDialog::testVoice()
{
qDebug() << QT_DEBUG_OUTPUT << "test triggered";
ui->previewButton->setEnabled(false);
thread = new VoiceThread(this, VoiceThread::Test, ui->comboVoice->currentIndex());
thread->start();
connect(thread, &VoiceThread::done, this, [=]() {
ui->previewButton->setEnabled(true);
});
}
void SettingsDialog::onSaveToModelClicked()
{
qDebug() << QT_DEBUG_OUTPUT << "Save voice to model triggered";
FileHandler::saveVoiceToModel(p->voice());
}