-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidget.cpp
More file actions
1336 lines (1146 loc) · 44.9 KB
/
widget.cpp
File metadata and controls
1336 lines (1146 loc) · 44.9 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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "widget.h"
#include "screencapture.h"
#include "debug_helper.h"
#include <QMessageBox>
#include <QScrollBar>
#include <QtConcurrent>
#include <QFutureWatcher>
#include <QDebug>
#include <chrono>
#include <QClipboard>
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QDateTime>
#include <QTextEdit>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextCharFormat>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
// Windows头文件(用于内存检查)
#ifdef Q_OS_WIN
#include <Windows.h>
#endif
// llama 头文件
#include "llama-cpp.h"
#include "ggml-backend.h"
// 模型路径将从配置文件中读取
Widget::Widget(QWidget *parent)
: QWidget(parent), isTranslating(false), shouldStopTranslation(false), isStayOnTop(false), currentWatcher(nullptr),
sourceLanguage("英语"), targetLanguage("中文")
{
DebugHelper::log("Widget构造函数开始");
// 设置窗口标题
setWindowTitle("英译汉");
DebugHelper::log("窗口标题设置完成");
// 亲自然设计 - 全局样式
setStyleSheet(R"(
/* 全局字体 */
* {
font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
}
/* 窗口背景 - 柔和渐变 + 微噪点 */
QWidget {
background-color: #f7f6f2;
}
/* 输入框 - 纸质感 + 叶脉纹理感 */
QTextEdit {
background-color: #faf9f6;
border: 1px solid #d4d0c8;
border-radius: 12px;
padding: 8px;
font-size: 14px;
color: #3d3d3d;
selection-background-color: #b8d4b8;
}
QTextEdit:focus {
border: 2px solid #7a9e7a;
background-color: #ffffff;
}
QTextEdit[readOnly="true"] {
background-color: #f0efe9;
color: #5a5a5a;
}
/* 按钮基础样式 */
QPushButton {
background-color: #7a9e7a;
color: white;
border: none;
border-radius: 8px;
padding: 6px 20px;
font-size: 14px;
font-weight: 500;
}
QPushButton:hover {
background-color: #8fb08f;
}
QPushButton:pressed {
background-color: #6a8e6a;
padding-top: 11px;
padding-bottom: 9px;
}
/* 交换按钮 */
QPushButton#swapLangButton {
background-color: transparent;
border: 2px solid #a8c4a8;
border-radius: 8px;
padding: 4px;
}
QPushButton#swapLangButton:hover {
background-color: #e8f0e8;
border-color: #7a9e7a;
}
QPushButton#swapLangButton:pressed {
background-color: #d0e0d0;
}
/* 置顶按钮 */
QPushButton#stayOnTopButton {
background-color: transparent;
border: none;
border-radius: 6px;
padding: 4px;
}
QPushButton#stayOnTopButton:hover {
background-color: #e8f0e8;
}
/* 截图按钮 */
QPushButton#screenshotButton {
background-color: transparent;
border: none;
border-radius: 6px;
padding: 4px;
}
QPushButton#screenshotButton:hover {
background-color: #e8f0e8;
}
/* 设置按钮 */
QPushButton#settingsButton {
background-color: transparent;
border: none;
border-radius: 6px;
padding: 4px;
}
QPushButton#settingsButton:hover {
background-color: #e8f0e8;
}
/* 清除按钮 */
QPushButton#clearInputButton {
background-color: transparent;
color: #9ca89c;
border: none;
font-size: 18px;
padding: 4px 8px;
border-radius: 4px;
}
QPushButton#clearInputButton:hover {
color: #d47474;
background-color: #fff0f0;
}
/* 语言标签 */
QLabel {
color: #4a5a4a;
font-size: 14px;
font-weight: 500;
}
/* 分组框 */
QGroupBox {
color: #4a5a4a;
font-weight: 500;
border: 1px solid #d4d0c8;
border-radius: 8px;
margin-top: 10px;
padding-top: 10px;
}
QGroupBox::title {
color: #4a5a4a;
}
/* 复选框 */
QCheckBox {
color: #4a5a4a;
}
QCheckBox::indicator {
width: 18px;
height: 18px;
}
QCheckBox::indicator:unchecked {
border: 2px solid #a8c4a8;
border-radius: 3px;
background-color: #faf9f6;
}
QCheckBox::indicator:checked {
background-color: #7a9e7a;
border: 2px solid #7a9e7a;
border-radius: 3px;
}
/* 组合框 */
QComboBox {
background-color: #faf9f6;
color: #3d3d3d;
border: 1px solid #d4d0c8;
border-radius: 6px;
padding: 6px 12px;
}
QComboBox:hover {
border: 2px solid #7a9e7a;
}
QComboBox::drop-down {
border: none;
}
QComboBox::down-arrow {
image: none;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 6px solid #7a9e7a;
margin-right: 8px;
}
/* 数字选择框 */
QSpinBox {
background-color: #faf9f6;
color: #3d3d3d;
border: 1px solid #d4d0c8;
border-radius: 6px;
padding: 6px;
}
QSpinBox:hover {
border: 2px solid #7a9e7a;
}
/* 标签页 */
QTabWidget::pane {
border: 1px solid #d4d0c8;
border-radius: 8px;
background-color: #faf9f6;
}
QTabBar::tab {
background-color: #e8f0e8;
color: #4a5a4a;
padding: 8px 16px;
border-top-left-radius: 6px;
border-top-right-radius: 6px;
}
QTabBar::tab:selected {
background-color: #faf9f6;
color: #3d5a3d;
font-weight: 500;
}
/* 快捷键编辑框 */
QKeySequenceEdit {
background-color: #faf9f6;
color: #3d3d3d;
border: 1px solid #d4d0c8;
border-radius: 6px;
padding: 6px;
}
QKeySequenceEdit:hover {
border: 2px solid #7a9e7a;
}
QKeySequenceEdit:focus {
border: 2px solid #7a9e7a;
background-color: #ffffff;
}
/* 单行文本框 */
QLineEdit {
background-color: #faf9f6;
color: #3d3d3d;
border: 1px solid #d4d0c8;
border-radius: 6px;
padding: 6px 10px;
}
QLineEdit:hover {
border: 2px solid #7a9e7a;
}
QLineEdit:focus {
border: 2px solid #7a9e7a;
background-color: #ffffff;
}
/* 滚动条美化 */
QScrollBar:vertical {
background-color: transparent;
width: 8px;
border: none;
margin: 0px;
}
QScrollBar::handle:vertical {
background-color: #c8d8c8;
border-radius: 4px;
min-height: 30px;
margin: 0px;
}
QScrollBar::handle:vertical:hover {
background-color: #7a9e7a;
}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
height: 0px;
}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
background: none;
}
QScrollBar:horizontal {
background-color: transparent;
height: 8px;
border: none;
margin: 0px;
}
QScrollBar::handle:horizontal {
background-color: #c8d8c8;
border-radius: 4px;
min-width: 30px;
margin: 0px;
}
QScrollBar::handle:horizontal:hover {
background-color: #7a9e7a;
}
QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {
width: 0px;
}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {
background: none;
}
)");
// 输入输出区域
inputTextEdit = new QTextEdit(this);
inputTextEdit->setPlaceholderText("请输入要翻译的文本...");
inputTextEdit->setMinimumHeight(150);
inputTextEdit->installEventFilter(this);
outputTextEdit = new QTextEdit(this);
outputTextEdit->setReadOnly(true);
outputTextEdit->setMinimumHeight(150);
// 功能按钮
sendButton = new QPushButton("翻译", this);
copyButton = new QPushButton("复制", this);
clearInputButton = new QPushButton("×", this);
clearInputButton->setObjectName("clearInputButton");
clearInputButton->setFixedWidth(30);
clearInputButton->setToolTip("清空");
// 置顶按钮使用图标
stayOnTopButton = new QPushButton(this);
stayOnTopButton->setObjectName("stayOnTopButton");
stayOnTopButton->setFixedSize(30, 30);
stayOnTopButton->setIcon(QIcon(":/resource_icon/nail_copy.svg"));
stayOnTopButton->setIconSize(QSize(20, 20));
stayOnTopButton->setToolTip("置顶");
// 截图按钮使用图标
screenshotButton = new QPushButton(this);
screenshotButton->setObjectName("screenshotButton");
screenshotButton->setFixedSize(30, 30);
screenshotButton->setIcon(QIcon(":/resource_icon/screenshot-line.svg"));
screenshotButton->setIconSize(QSize(26, 26));
screenshotButton->setToolTip("截图OCR");
settingsButton = new QPushButton(this);
settingsButton->setObjectName("settingsButton");
settingsButton->setFixedSize(40, 40);
settingsButton->setIcon(QIcon(":/resource_icon/setting.svg"));
settingsButton->setIconSize(QSize(36, 36));
settingsButton->setToolTip("设置");
// 语言标签和交换按钮
sourceLangLabel = new QLabel("英语", this);
targetLangLabel = new QLabel("中文", this);
swapLangButton = new QPushButton(this);
swapLangButton->setObjectName("swapLangButton");
swapLangButton->setFixedSize(30, 30);
swapLangButton->setIcon(QIcon(":/resource_icon/exchange.svg"));
swapLangButton->setIconSize(QSize(20, 20));
swapLangButton->setToolTip("交换语言");
// 输入定时器,用于实现实时翻译
inputTimer = new QTimer(this);
inputTimer->setSingleShot(true);
inputTimer->setInterval(ConfigManager::instance()->getAutoTranslateDelay()); // 从配置中读取延迟时间
// 主布局
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(13, 13, 13, 13);
mainLayout->setSpacing(15);
// 顶部输入区域
QVBoxLayout *inputLayout = new QVBoxLayout();
// 输入区标题和按钮
QHBoxLayout *inputHeaderLayout = new QHBoxLayout();
inputHeaderLayout->addWidget(new QLabel("原文", this));
inputHeaderLayout->addStretch();
inputHeaderLayout->addWidget(stayOnTopButton);
inputHeaderLayout->addWidget(screenshotButton);
inputHeaderLayout->addWidget(settingsButton);
inputHeaderLayout->addWidget(clearInputButton);
inputLayout->addLayout(inputHeaderLayout);
inputLayout->addWidget(inputTextEdit);
mainLayout->addLayout(inputLayout);
// 语言选择栏(源语言 -> 目标语言)
QHBoxLayout *langSelectLayout = new QHBoxLayout();
langSelectLayout->addStretch();
langSelectLayout->addWidget(sourceLangLabel);
langSelectLayout->addWidget(swapLangButton);
langSelectLayout->addWidget(targetLangLabel);
langSelectLayout->addStretch();
mainLayout->addLayout(langSelectLayout);
// 输出区域
QVBoxLayout *outputLayout = new QVBoxLayout();
outputLayout->addWidget(outputTextEdit);
mainLayout->addLayout(outputLayout);
// 功能按钮栏
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(sendButton);
buttonLayout->addWidget(copyButton);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
// 设置窗口大小
QSize defaultSize = ConfigManager::instance()->getDefaultWindowSize();
resize(defaultSize);
// 信号连接
connect(sendButton, &QPushButton::clicked, this, &Widget::onSendClicked);
connect(inputTextEdit, &QTextEdit::textChanged, this, &Widget::onInputTextChanged);
connect(copyButton, &QPushButton::clicked, this, &Widget::onCopyResult);
connect(clearInputButton, &QPushButton::clicked, inputTextEdit, &QTextEdit::clear);
connect(stayOnTopButton, &QPushButton::clicked, this, &Widget::onStayOnTopClicked);
connect(screenshotButton, &QPushButton::clicked, this, &Widget::startScreenCapture);
connect(settingsButton, &QPushButton::clicked, this, &Widget::onSettingsButtonClicked);
connect(swapLangButton, &QPushButton::clicked, this, &Widget::swapLanguages);
connect(inputTimer, &QTimer::timeout, this, &Widget::onSendClicked);
DebugHelper::log("初始化OCR...");
// 初始化 OCR
initTesseract();
DebugHelper::log("OCR初始化完成");
// 注册全局热键
DebugHelper::log("注册热键...");
ConfigManager *config = ConfigManager::instance();
if (config->getHotkeyEnabled()) {
if (!registerGlobalHotkey()) {
qDebug() << "无法注册全局热键,将使用普通快捷键作为备选";
DebugHelper::log("全局热键注册失败,使用普通快捷键");
// 如果全局热键注册失败,使用普通快捷键作为备选
ocrShortcut = new QShortcut(QKeySequence(config->getHotkey()), this);
connect(ocrShortcut, &QShortcut::activated, this, &Widget::onOCRShortcutTriggered);
} else {
qDebug() << "全局热键注册成功(" << config->getHotkey() << ")";
DebugHelper::log("全局热键注册成功");
ocrShortcut = nullptr; // 不需要普通快捷键
}
} else {
qDebug() << "快捷键已禁用";
DebugHelper::log("快捷键已禁用");
ocrShortcut = nullptr;
}
// llama 初始化
DebugHelper::log("初始化llama后端...");
ggml_backend_load_all();
DebugHelper::log("llama后端初始化完成");
llama_model_params mparams = llama_model_default_params();
QString modelPath = ConfigManager::instance()->getModelPath();
// 如果是相对路径,转换为相对于应用程序目录的绝对路径
QFileInfo modelFileInfo(modelPath);
if (modelFileInfo.isRelative()) {
QString appDir = QCoreApplication::applicationDirPath();
modelPath = QDir(appDir).absoluteFilePath(modelPath);
qDebug() << "相对路径转换为绝对路径:" << modelPath;
}
// 检查模型文件是否存在
QFile modelFile(modelPath);
if (!modelFile.exists()) {
QMessageBox::warning(this, "模型文件不存在",
"未找到模型文件,请前往以下地址下载模型文件:\n\n"
"https://modelscope.cn/models/Tencent-Hunyuan/HY-MT1.5-1.8B-GGUF/files\n\n"
"请下载文件:HY-MT1.5-1.8B-Q8_0.gguf\n\n"
"下载完成后,请点击 设置 -> 模型设置 选择您的模型文件路径。");
// 不加载模型,但保持窗口可用,允许用户进入设置界面
this->model = nullptr;
} else {
// 检查可用内存
MEMORYSTATUSEX memStatus;
memStatus.dwLength = sizeof(memStatus);
GlobalMemoryStatusEx(&memStatus);
DWORDLONG totalMem = memStatus.ullTotalPhys / (1024 * 1024); // MB
DWORDLONG availMem = memStatus.ullAvailPhys / (1024 * 1024); // MB
qDebug() << "系统总内存:" << totalMem << "MB";
qDebug() << "可用内存:" << availMem << "MB";
qDebug() << "正在加载模型:" << modelPath;
qDebug() << "模型文件大小:" << modelFile.size() << "bytes";
this->model = llama_model_load_from_file(modelPath.toUtf8().constData(), mparams);
if (model == nullptr) {
qDebug() << __func__ << ": error: unable to load model";
QMessageBox::critical(this, "模型加载失败",
QString("模型文件加载失败,可能原因:\n\n"
"1. CPU不支持AVX2指令集\n"
"2. 模型文件损坏\n\n"
"您可以前往以下地址重新下载:\n\n"
"https://modelscope.cn/models/Tencent-Hunyuan/HY-MT1.5-1.8B-GGUF/files\n\n"
"请下载文件:HY-MT1.5-1.8B-Q8_0.gguf"));
// 不加载模型,但保持窗口可用
this->model = nullptr;
} else {
qDebug() << "模型加载成功";
}
}
// 流式输出信号连接
connect(this, &Widget::streamTokenReady, this, [this, config](const QString &token){
outputTextEdit->insertPlainText(token);
if (config->getFollowTranslation()) {
outputTextEdit->moveCursor(QTextCursor::End);
outputTextEdit->verticalScrollBar()->setValue(outputTextEdit->verticalScrollBar()->maximum());
}
});
}
Widget::~Widget() {
delete inputTimer;
if (ocrShortcut) {
delete ocrShortcut;
}
unregisterGlobalHotkey(); // 注销全局热键
if (model != nullptr) {
llama_model_free(model);
}
}
bool Widget::eventFilter(QObject *obj, QEvent *event) {
if (obj == inputTextEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_V) {
QClipboard *clipboard = QApplication::clipboard();
QString text = clipboard->text();
if (!text.isEmpty()) {
event->accept();
QTextCursor cursor = inputTextEdit->textCursor();
cursor.insertText(text);
return true;
}
}
}
}
return QWidget::eventFilter(obj, event);
}
// 从资源文件提取traineddata文件
bool Widget::extractResourceFiles() {
QString appDir = QCoreApplication::applicationDirPath();
QString tessdataDir = appDir + "/tessdata";
QDir dir;
if (!dir.exists(tessdataDir)) {
dir.mkpath(tessdataDir);
}
QStringList filesToExtract = {
":/tessdata/chi_sim.traineddata",
":/tessdata/eng.traineddata"
};
for (const QString &resourcePath : filesToExtract) {
QString fileName = QFileInfo(resourcePath).fileName();
QString targetPath = tessdataDir + "/" + fileName;
QFile resourceFile(resourcePath);
if (!resourceFile.open(QIODevice::ReadOnly)) {
qDebug() << "无法打开资源文件:" << resourcePath;
return false;
}
QFile targetFile(targetPath);
if (targetFile.exists()) {
resourceFile.close();
continue;
}
if (!targetFile.open(QIODevice::WriteOnly)) {
qDebug() << "无法创建目标文件:" << targetPath;
resourceFile.close();
return false;
}
targetFile.write(resourceFile.readAll());
targetFile.close();
resourceFile.close();
qDebug() << "提取资源文件:" << fileName << "到:" << targetPath;
}
return true;
}
// 初始化 Tesseract
void Widget::initTesseract() {
// 先从资源文件提取traineddata文件
if (!extractResourceFiles()) {
qDebug() << "提取资源文件失败";
return;
}
// 设置 Tesseract 数据目录环境变量
QString appDir = QCoreApplication::applicationDirPath();
QString tessdataPath = appDir + "/tessdata";
qputenv("TESSDATA_PREFIX", tessdataPath.toUtf8());
qDebug() << "Tesseract 初始化完成,数据目录:" << tessdataPath;
}
void Widget::onInputTextChanged() {
qDebug() << "onInputTextChanged - sourceLanguage:" << sourceLanguage << "targetLanguage:" << targetLanguage;
inputTimer->start();
}
void Widget::onSendClicked() {
qDebug() << "onSendClicked - sourceLanguage:" << sourceLanguage << "targetLanguage:" << targetLanguage;
// 如果正在翻译,点击按钮将停止翻译
if (isTranslating) {
onStopTranslation();
return;
}
QString inputText = inputTextEdit->toPlainText().trimmed();
if (inputText.isEmpty()) return;
isTranslating = true;
shouldStopTranslation = false;
sendButton->setText("停止");
// 清空输出区域
outputTextEdit->clear();
// 使用成员变量中的语言设置
QString sourceLang = sourceLanguage;
QString targetLang = targetLanguage;
// 打印源语言和目标语言
qDebug() << "源语言:" << sourceLang;
qDebug() << "目标语言:" << targetLang;
// 生成翻译提示词
QString prompt = generateTranslationPrompt(inputText, sourceLang, targetLang);
// llama 推理放到后台线程(流式输出)
QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this);
connect(watcher, &QFutureWatcher<void>::finished, this, [this, watcher]() {
if (!shouldStopTranslation) {
isTranslating = false;
sendButton->setText("翻译");
}
watcher->deleteLater();
});
QFuture<void> future = QtConcurrent::run([this, prompt]() {
streamQueryLlama(prompt);
});
watcher->setFuture(future);
}
void Widget::onCopyResult() {
// 复制翻译结果到剪贴板
QString result = outputTextEdit->toPlainText();
if (!result.isEmpty()) {
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(result);
QMessageBox::information(this, "提示", "翻译结果已复制到剪贴板");
}
}
void Widget::onStopTranslation() {
// 设置停止标志
shouldStopTranslation = true;
// 恢复按钮状态
isTranslating = false;
sendButton->setText("翻译");
}
// 交换语言
void Widget::swapLanguages() {
qDebug() << "交换前 - sourceLanguage:" << sourceLanguage << "targetLanguage:" << targetLanguage;
QString temp = sourceLanguage;
sourceLanguage = targetLanguage;
targetLanguage = temp;
sourceLangLabel->setText(sourceLanguage);
targetLangLabel->setText(targetLanguage);
qDebug() << "交换后 - sourceLanguage:" << sourceLanguage << "targetLanguage:" << targetLanguage;
QString inputText = inputTextEdit->toPlainText();
QString outputText = outputTextEdit->toPlainText();
inputTextEdit->setPlainText(outputText);
outputTextEdit->setPlainText(inputText);
if (!inputTextEdit->toPlainText().isEmpty()) {
onSendClicked();
}
}
// OCR 快捷键触发事件
void Widget::onOCRShortcutTriggered() {
qDebug() << "OCR 快捷键触发";
startScreenCapture();
}
// 开始屏幕截图
void Widget::startScreenCapture() {
// 先隐藏主窗口,防止遮挡截屏
hide();
// 延迟一点时间再显示截屏界面,确保主窗口已经隐藏
QTimer::singleShot(100, this, [this]() {
// 创建屏幕截图窗口
ScreenCapture *capture = new ScreenCapture(this);
// 连接信号 - 截屏完成后调用OCR处理,完成后显示主窗口
connect(capture, &ScreenCapture::captureCompleted, this, [this](const QPixmap &screenshot) {
onScreenshotCompleted(screenshot);
show();
activateWindow();
});
connect(capture, &ScreenCapture::captureCanceled, this, [this]() {
show();
activateWindow();
});
connect(capture, &ScreenCapture::captureCompleted, capture, &ScreenCapture::deleteLater);
connect(capture, &ScreenCapture::captureCanceled, capture, &ScreenCapture::deleteLater);
// 显示截图窗口
capture->show();
});
}
// 生成翻译提示词
QString Widget::generateTranslationPrompt(const QString &sourceText, const QString &sourceLang, const QString &targetLang) {
qDebug() << "generateTranslationPrompt - sourceLang length:" << sourceLang.length();
qDebug() << "generateTranslationPrompt - sourceLang toUtf8:" << sourceLang.toUtf8().toHex();
qDebug() << "generateTranslationPrompt - targetLang length:" << targetLang.length();
QString prompt;
if (sourceLang == "中文") {
qDebug() << "条件1满足: sourceLang == 中文";
prompt = "Translate the following Chinese text into English, without additional explanation.\n\n" + sourceText;
} else if (sourceLang == "英语") {
qDebug() << "条件2满足: sourceLang == 英语";
prompt = "Translate the following English text into Chinese, without additional explanation.\n\n" + sourceText;
} else {
qDebug() << "条件都不满足,使用默认";
prompt = "Translate the following text, without additional explanation.\n\n" + sourceText;
}
qDebug() << "生成翻译提示词 - 源语言:" << sourceLang << "目标语言:" << targetLang;
qDebug() << "提示词内容:" << prompt;
return prompt;
}
// 语言代码映射
QString Widget::getLanguageCode(const QString &languageName) {
// 语言名称到代码的映射
if (languageName == "中文") return "zh";
if (languageName == "英语") return "en";
if (languageName == "日语") return "ja";
if (languageName == "韩语") return "ko";
if (languageName == "法语") return "fr";
if (languageName == "西班牙语") return "es";
if (languageName == "德语") return "de";
if (languageName == "俄语") return "ru";
return "en";
}
// 屏幕截图完成事件
void Widget::onScreenshotCompleted(const QPixmap &screenshot) {
qDebug() << "截图完成,开始 OCR 处理";
// 在后台线程中执行 OCR
QtConcurrent::run([this, screenshot]() {
QString extractedText = performOCR(screenshot);
// 回到主线程处理结果
QMetaObject::invokeMethod(this, [this, extractedText]() {
if (!extractedText.isEmpty()) {
// 将识别的文本填入输入框
inputTextEdit->setPlainText(extractedText);
// 触发自动翻译
inputTimer->start();
qDebug() << "OCR 完成,文本已自动填入输入框并开始翻译";
} else {
qDebug() << "OCR 失败,未能从截图中识别出文本";
}
}, Qt::QueuedConnection);
});
}
// OCR 文本提取
QString Widget::performOCR(const QPixmap &image) {
// 根据源语言选择 OCR 语言
QString ocrLang;
if (sourceLanguage == "中文") {
ocrLang = "chi_sim";
} else {
ocrLang = "eng";
}
qDebug() << "使用 OCR 语言:" << ocrLang;
// 将 QPixmap 转换为 QImage
QImage qImage = image.toImage().convertToFormat(QImage::Format_RGB32);
// 创建原始彩色图像的 Pix
Pix *originalPix = pixCreate(qImage.width(), qImage.height(), 32);
for (int y = 0; y < qImage.height(); y++) {
for (int x = 0; x < qImage.width(); x++) {
QRgb rgb = qImage.pixel(x, y);
l_uint32 pixel = (qRed(rgb) << 16) | (qGreen(rgb) << 8) | qBlue(rgb);
pixSetPixel(originalPix, x, y, pixel);
}
}
// 测试函数:使用指定图像进行 OCR 并返回置信度
auto testOCRConfidence = [&](Pix *testPix, const QString &testName) -> int {
tesseract::TessBaseAPI *testApi = new tesseract::TessBaseAPI();
QString appDir = QCoreApplication::applicationDirPath();
QString tessdataPath = appDir + "/tessdata";
if (testApi->Init(tessdataPath.toUtf8().constData(), ocrLang.toUtf8().constData())) {
qDebug() << testName << "Tesseract 初始化失败";
delete testApi;
return 0;
}
testApi->SetImage(testPix);
char *testText = testApi->GetUTF8Text();
int confidence = testApi->MeanTextConf();
qDebug() << testName << "置信度:" << confidence;
delete[] testText;
testApi->End();
delete testApi;
return confidence;
};
// 1. 测试原图(彩色)的置信度
qDebug() << "=== OCR 置信度测试 ===";
int originalConfidence = testOCRConfidence(originalPix, "原图");
// 2. 测试三通道分别转灰度图的置信度
Pix *redChannelPix = nullptr;
Pix *greenChannelPix = nullptr;
Pix *blueChannelPix = nullptr;
int redConfidence = 0;
int greenConfidence = 0;
int blueConfidence = 0;
// 辅助函数:提取指定颜色通道并创建 Pix 图像
auto extractChannel = [&](std::function<l_uint8(QRgb)> channelFunc) -> Pix* {
Pix *channelPix = pixCreate(qImage.width(), qImage.height(), 8);
// 使用 bits() 直接访问内存,提高性能
const uchar *imageBits = qImage.constBits();
int bytesPerLine = qImage.bytesPerLine();
for (int y = 0; y < qImage.height(); y++) {
const QRgb *scanLine = reinterpret_cast<const QRgb*>(imageBits + y * bytesPerLine);
for (int x = 0; x < qImage.width(); x++) {
l_uint8 channelValue = channelFunc(scanLine[x]);
pixSetPixel(channelPix, x, y, channelValue);
}
}
return channelPix;
};
// 提取三个颜色通道
redChannelPix = extractChannel([](QRgb rgb) { return qRed(rgb); });
redConfidence = testOCRConfidence(redChannelPix, "红色通道");
greenChannelPix = extractChannel([](QRgb rgb) { return qGreen(rgb); });
greenConfidence = testOCRConfidence(greenChannelPix, "绿色通道");
blueChannelPix = extractChannel([](QRgb rgb) { return qBlue(rgb); });
blueConfidence = testOCRConfidence(blueChannelPix, "蓝色通道");
// 3. 选择置信度最高的图像进行最终识别
Pix *bestPix = originalPix;
int bestConfidence = originalConfidence;
QString bestImageName = "原图";
if (redConfidence > bestConfidence) {
bestPix = redChannelPix;
bestConfidence = redConfidence;
bestImageName = "红色通道";
}
if (greenConfidence > bestConfidence) {
// 如果之前的最佳图像不是原图,需要释放
if (bestPix != originalPix) {
pixDestroy(&bestPix);
}
bestPix = greenChannelPix;
bestConfidence = greenConfidence;
bestImageName = "绿色通道";
}
if (blueConfidence > bestConfidence) {
// 如果之前的最佳图像不是原图,需要释放
if (bestPix != originalPix) {
pixDestroy(&bestPix);
}
bestPix = blueChannelPix;
bestConfidence = blueConfidence;
bestImageName = "蓝色通道";
}
qDebug() << "=== 测试完成 ===";
qDebug() << "选择置信度最高的图像进行识别:" << bestImageName << "(置信度:" << bestConfidence << ")";
// 4. 使用置信度最高的图像进行最终识别
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
QString appDir = QCoreApplication::applicationDirPath();
QString tessdataPath = appDir + "/tessdata";
if (api->Init(tessdataPath.toUtf8().constData(), ocrLang.toUtf8().constData())) {
qDebug() << "Tesseract 初始化失败";
delete api;
// 清理所有资源
pixDestroy(&originalPix);
if (redChannelPix) pixDestroy(&redChannelPix);
if (greenChannelPix) pixDestroy(&greenChannelPix);
if (blueChannelPix) pixDestroy(&blueChannelPix);
return "";
}
// 设置图像(使用置信度最高的图像)
api->SetImage(bestPix);
// 识别文本
char *outText = api->GetUTF8Text();
QString result = QString::fromUtf8(outText);
// 清理资源
delete[] outText;
api->End();
delete api;
// 释放所有图像资源
pixDestroy(&originalPix);
if (redChannelPix && redChannelPix != bestPix) pixDestroy(&redChannelPix);
if (greenChannelPix && greenChannelPix != bestPix) pixDestroy(&greenChannelPix);
if (blueChannelPix && blueChannelPix != bestPix) pixDestroy(&blueChannelPix);
if (bestPix != originalPix && bestPix != redChannelPix && bestPix != greenChannelPix && bestPix != blueChannelPix) {
pixDestroy(&bestPix);
}
qDebug() << "OCR 识别结果:" << result;
return result;
}
// 流式输出实现
void Widget::streamQueryLlama(const QString &prompt) {
qDebug() << "streamQueryLlama 收到 prompt:" << prompt;
// 检查模型是否已加载
if (model == nullptr) {
QMessageBox::warning(nullptr, "模型未加载",
"模型文件未加载,请先下载模型文件并在设置中配置模型路径。\n\n"
"下载地址:https://modelscope.cn/models/Tencent-Hunyuan/HY-MT1.5-1.8B-GGUF/files");
return;
}
// 使用llama_chat_apply_template处理消息
struct llama_chat_message messages[] = {
{"user", prompt.toUtf8().constData()}
};
// 分配缓冲区