-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwidget_funcs.cpp
1898 lines (1686 loc) · 85.2 KB
/
widget_funcs.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
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 "ui_widget.h"
#include "widget.h"
//-------------------------------------------------------------------------
//----------------------------------装载相关--------------------------------
//-------------------------------------------------------------------------
//动画播放逻辑
// 1.先展示背景load_play()
// 2.启动load_begin_pTimer动画,向中滑动
// 2.启动load_pTimer动画,连接动画
// 3.启动load_over_pTimer动画,向下滑动
// 4.启动force_unlockload_pTimer,强制解锁
// 5.真正装载完的处理unlockLoad()
// 初始化动画,主要是背景和12条连线
void Widget::init_movie() {
movie_line << " ## "; // 2
movie_line << " # # "; // 3
movie_line << " ## "; // 4
movie_line << " ## ## "; // 5
movie_line << " # # # # "; // 6
movie_line << " ## ## "; // 7
movie_line << " ## "; // 8
movie_line << " # # "; // 9
movie_line << " ## "; // 10
movie_line << " ## ## "; // 11
movie_line << " # # # # "; // 12
movie_line << " ## ## "; // 13
movie_line << " ## "; // 14
movie_line << " # # "; // 15
movie_line << " ## "; // 16
movie_dot << QPointF(5, 12) << QPointF(4, 15) << QPointF(3, 19) << QPointF(2, 22); // load_action 1
movie_dot << QPointF(5, 12) << QPointF(6, 15) << QPointF(7, 19) << QPointF(8, 22); // load_action 2
movie_dot << QPointF(5, 12) << QPointF(6, 13) << QPointF(7, 14) << QPointF(8, 15) << QPointF(9, 16) << QPointF(10, 17) << QPointF(11, 18) << QPointF(12, 19) << QPointF(13, 20) << QPointF(14, 22); // load_action 3
movie_dot << QPointF(11, 12) << QPointF(10, 13) << QPointF(9, 14) << QPointF(8, 15) << QPointF(7, 16) << QPointF(6, 17) << QPointF(5, 18) << QPointF(4, 19) << QPointF(3, 20) << QPointF(2, 22); // load_action 4
movie_dot << QPointF(11, 12) << QPointF(10, 15) << QPointF(9, 19) << QPointF(8, 22); // load_action 5
movie_dot << QPointF(11, 12) << QPointF(12, 15) << QPointF(13, 19) << QPointF(14, 22); // load_action 6
movie_dot << QPointF(2, 31) << QPointF(3, 34) << QPointF(4, 38) << QPointF(5, 41); // load_action 7
movie_dot << QPointF(2, 31) << QPointF(3, 33) << QPointF(4, 34) << QPointF(5, 35) << QPointF(6, 36) << QPointF(7, 37) << QPointF(8, 38) << QPointF(9, 39) << QPointF(10, 40) << QPointF(11, 41); // load_action 8
movie_dot << QPointF(8, 31) << QPointF(7, 34) << QPointF(6, 38) << QPointF(5, 41); // load_action 9
movie_dot << QPointF(8, 31) << QPointF(9, 34) << QPointF(10, 38) << QPointF(11, 41); // load_action 10
movie_dot << QPointF(14, 31) << QPointF(13, 33) << QPointF(12, 34) << QPointF(11, 35) << QPointF(10, 36) << QPointF(9, 37) << QPointF(8, 38) << QPointF(7, 39) << QPointF(6, 40) << QPointF(5, 41); // load_action 11
movie_dot << QPointF(14, 31) << QPointF(13, 34) << QPointF(12, 38) << QPointF(11, 41); // load_action 12
//添加颜色
for (int i = 0; i < 12; ++i) {
//彩色
// movie_color <<QColor(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256));
//黑色
movie_color << NORMAL_BLACK;
}
//设置动画内容字体格式
movie_format.setFontWeight(QFont::Bold); // 设置粗体
movie_font.setPointSize(6);
// movie_font.setFamily(DEFAULT_FONT);
movie_format.setFont(movie_font);
load_pTimer = new QTimer(this); //连接接动画
load_begin_pTimer = new QTimer(this); //向中滑动
load_over_pTimer = new QTimer(this); //向下滑动
force_unlockload_pTimer = new QTimer(this); //强制解锁
connect(load_pTimer, SIGNAL(timeout()), this, SLOT(load_handleTimeout())); //设置终止信号触发的槽函数
connect(load_begin_pTimer, SIGNAL(timeout()), this, SLOT(load_begin_handleTimeout())); //设置终止信号触发的槽函数
connect(load_over_pTimer, SIGNAL(timeout()), this, SLOT(load_over_handleTimeout())); //设置终止信号触发的槽函数
connect(force_unlockload_pTimer, SIGNAL(timeout()), this, SLOT(unlockLoad())); //新开一个线程
decode_pTimer = new QTimer(this); //启动后,达到规定时间将发射终止信号
connect(decode_pTimer, SIGNAL(timeout()), this, SLOT(decode_handleTimeout())); //设置终止信号触发的槽函数
}
//设置72个点的字体前景色颜色
void Widget::set_dotcolor(QTextCharFormat *format, int load_action) {
if (load_action < 4) {
format->setForeground(movie_color.at(0));
} else if (load_action < 8) {
format->setForeground(movie_color.at(1));
} else if (load_action < 18) {
format->setForeground(movie_color.at(2));
} else if (load_action < 28) {
format->setForeground(movie_color.at(3));
} else if (load_action < 32) {
format->setForeground(movie_color.at(4));
} else if (load_action < 36) {
format->setForeground(movie_color.at(5));
} else if (load_action < 40) {
format->setForeground(movie_color.at(6));
} else if (load_action < 50) {
format->setForeground(movie_color.at(7));
} else if (load_action < 54) {
format->setForeground(movie_color.at(8));
} else if (load_action < 58) {
format->setForeground(movie_color.at(9));
} else if (load_action < 68) {
format->setForeground(movie_color.at(10));
} else if (load_action < 72) {
format->setForeground(movie_color.at(11));
}
}
//连接动画的下一帧
void Widget::load_move() {
QTextCursor cursor = ui->state->textCursor();
if (load_action % 2 == 0) {
cursor.movePosition(QTextCursor::Start); //移到文本开头
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, movie_dot.at(load_action / 2).x() - 2 + playlineNumber); //向下移动到指定行
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, movie_dot.at(load_action / 2).y() - 1); //向右移动到指定列
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); //选中当前字符
cursor.removeSelectedText(); //删除选中字符
set_dotcolor(&movie_format, load_action / 2); //设置字体颜色
cursor.setCharFormat(movie_format); //设置字体
cursor.insertText("*"); //插入字符
} else {
cursor.movePosition(QTextCursor::Start); //移到文本开头
cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, movie_dot.at(load_action / 2 + 1).x() - 2 + playlineNumber); //向下移动到指定行
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, movie_dot.at(load_action / 2 + 1).y() - 1); //向右移动到指定列
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); //选中当前字符
cursor.removeSelectedText(); //删除选中字符
cursor.setCharFormat(movie_format); //设置字体
cursor.insertText(" "); //插入字符
}
load_action++;
}
//开始播放
void Widget::load_play() {
QTextCursor cursor = ui->state->textCursor();
cursor.movePosition(QTextCursor::End);
cursor.insertText("\n"); //插个回车
load_action = 0;
//获取当前行数
playlineNumber = 0;
QTextDocument *document = ui->state->document();
for (QTextBlock block = document->begin(); block != document->end(); block = block.next()) {
++playlineNumber;
}
// qDebug() << "lineNumber: " << playlineNumber;
//展示背景
for (int i = 0; i < movie_line.size(); ++i) {
cursor.movePosition(QTextCursor::End); //移到文本开头
cursor.setCharFormat(movie_format); //设置字体
cursor.insertText(movie_line.at(i) + "\n"); //插入字符
}
//向下滑
load_begin_pTimer->start(100);
//先自动播放动画
load_pTimer->start(800);
}
//连接动画
void Widget::load_handleTimeout() {
if (load_pTimer->isActive()) {
load_pTimer->stop();
} //控制超时处理函数只会处理一次
if (load_action < all_fps) {
load_move(); //下一帧
}
//循环播放
if (load_action < all_fps) {
if (is_load) {
load_pTimer->start(10); //延时多少ms后发出timeout()信号
} else {
load_pTimer->start(1100); //延时多少ms后发出timeout()信号
}
} else if (is_load) {
load_action = 0; //重置动作计数
all_fps--; //减去补上的最后一帧
load_over_pTimer->start(100);
}
}
//滑动到最佳动画位置
void Widget::load_begin_handleTimeout() {
if (load_begin_pTimer->isActive()) {
load_begin_pTimer->stop();
} //控制超时处理函数只会处理一次
int currentValue = ui->state->verticalScrollBar()->value(); //当前滑动条位置
ui->state->verticalScrollBar()->setValue(currentValue + 1);
// qDebug() << currentValue <<playlineNumber;
if (currentValue < playlineNumber - 2) {
load_begin_pTimer->start(100);
} else {
load_begin_pTimer->stop();
}
}
//模型装载完毕动画,并解锁按钮
void Widget::load_over_handleTimeout() {
if (load_over_pTimer->isActive()) {
load_over_pTimer->stop();
} //控制超时处理函数只会处理一次
int currentValue = ui->state->verticalScrollBar()->value(); //当前滑动条位置
ui->state->verticalScrollBar()->setValue(currentValue + 1);
currentValue++;
//展示完就停止
if (currentValue <= ui->state->verticalScrollBar()->maximum()) {
load_over_pTimer->start(100);
}
//滚到最下面才解锁按钮,真正装载完毕
else {
force_unlockload_pTimer->start(0); //强制解锁
}
}
// 装载完毕强制预处理
void Widget::unlockLoad() {
if (ui_SETTINGS.ngl < ui_maxngl) {
reflash_state("ui:" + jtr("ngl tips"), USUAL_SIGNAL);
}
reflash_state("ui:" + jtr("load model") + jtr("over") + " " + QString::number(load_time, 'f', 2) + " s " + jtr("right click and check model log"), SUCCESS_SIGNAL);
if (ui_SETTINGS.ngl > 0) {
QApplication::setWindowIcon(QIcon(":/logo/green_logo.png"));
} // 设置应用程序图标
else {
QApplication::setWindowIcon(QIcon(":/logo/blue_logo.png"));
} // 设置应用程序图标
this->setWindowTitle(jtr("current model") + " " + ui_SETTINGS.modelpath.split("/").last());
ui->kv_bar->show_text = jtr("brain");
ui->cpu_bar->setToolTip(jtr("nthread/maxthread") + " " + QString::number(ui_SETTINGS.nthread) + "/" + QString::number(max_thread));
auto_save_user(); //保存ui配置
force_unlockload_pTimer->stop();
is_load_play_over = true; //标记模型动画已经完成
ui_state_normal(); //解锁界面
reflash_output(bot_predecode_content, 0, SYSTEM_BLUE);
; //显示预解码内容
}
// 按日志显示装载进度
void Widget::load_log_play() {
int load_count = load_percent * all_fps / 100;
// qDebug() << load_count;
while (load_count > load_action && ui_SETTINGS.ngl != 0) {
load_move();
}
}
//-------------------------------------------------------------------------
//------------------------------文字输出相关--------------------------------
//-------------------------------------------------------------------------
// output和state采用verticalScrollBar()控制滑动条,如果其在底部,有新内容加入将自动下滑,用户上滑后下滑效果取消
//更新输出区,is_while表示从流式输出的token
void Widget::reflash_output(const QString result, bool is_while, QColor color) {
if (is_test && is_while) //现在要知道是模型输出的答案还是预编码完成的结果,要将预编码完成的结果排除
{
test_count++; //已经加一了
QString result_ = result;
//答对,remove(' ')移除答案中的空格
if (result_.remove(' ') == test_list_answer.at(test_question_index.at(0))) {
test_score++;
output_scroll(result + "\n", Qt::green);
ui_state_info = "ui:" + QString::number(test_count) + " " + jtr("answer right") + " " + jtr("right answer") + test_list_answer.at(test_question_index.at(0));
reflash_state(ui_state_info, SUCCESS_SIGNAL);
}
//答错
else {
output_scroll(result + "\n", Qt::red);
ui_state_info = "ui:" + QString::number(test_count) + " " + jtr("answer error") + " " + jtr("right answer") + test_list_answer.at(test_question_index.at(0));
reflash_state(ui_state_info, WRONG_SIGNAL);
}
float acc = test_score / test_count * 100.0; //回答准确率
test_question_index.removeAt(0); //回答完毕删除开头的第一个问题
if (ui_mode == LINK_MODE) {
this->setWindowTitle(jtr("test") + QString::number(test_count) + "/" + QString::number(test_list_question.size()) + " " + jtr("accurate") + QString::number(acc, 'f', 1) + "% " + " " + jtr("current api") + " " + current_api);
} else {
this->setWindowTitle(jtr("test") + QString::number(test_count) + "/" + QString::number(test_list_question.size()) + " " + jtr("accurate") + QString::number(acc, 'f', 1) + "% " + " " + ui_SETTINGS.modelpath.split("/").last());
}
//每20次题加一次引导题
if (int(test_count) % 20 == 0) {
help_input = true;
ui_state_info = "ui:" + jtr("add help question");
reflash_state(ui_state_info, SIGNAL_SIGNAL);
}
} else {
//正常输出
output_scroll(result, color);
}
if (is_while) {
temp_assistant_history += result;
}
}
//输出区滚动条事件响应
void Widget::output_scrollBarValueChanged(int value) {
//如果滑动条在最下面则自动滚动
int maximumValue = output_scrollBar->maximum();
if (value == maximumValue) {
is_stop_output_scroll = 0;
} else {
is_stop_output_scroll = 1;
}
}
//向output末尾添加文本并滚动
void Widget::output_scroll(QString output, QColor color) {
QTextCursor cursor = ui->output->textCursor();
QTextCharFormat textFormat;
textFormat.setForeground(QBrush(color)); // 设置文本颜色
cursor.movePosition(QTextCursor::End); //光标移动到末尾
cursor.mergeCharFormat(textFormat); // 应用文本格式
cursor.insertText(output); //输出
QTextCharFormat textFormat0; // 清空文本格式
cursor.movePosition(QTextCursor::End); //光标移动到末尾
cursor.mergeCharFormat(textFormat0); // 应用文本格式
if (!is_stop_output_scroll) //如果停止标签没有启用,则每次输出完自动滚动到最下面
{
ui->output->verticalScrollBar()->setValue(ui->output->verticalScrollBar()->maximum()); //滚动条滚动到最下面
}
}
//更新状态区
void Widget::reflash_state(QString state_string, SIGNAL_STATE state) {
QTextCharFormat format; //设置特殊文本颜色
// QFont font;//字体 设置了字体就不能缩放了
// font.setPointSize(9);
// format.setFont(font);
//过滤回车和换行符
if (state != MATRIX_SIGNAL) {
state_string.replace("\n", "\\n");
state_string.replace("\r", "\\r");
}
if (state == USUAL_SIGNAL || state == MATRIX_SIGNAL) //一般黑色
{
format.clearForeground(); //清除前景颜色
format.setForeground(NORMAL_BLACK); //还是黑色吧
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(state_string);
} else if (state == SUCCESS_SIGNAL) //正常绿色
{
format.setForeground(QColor(0, 200, 0)); // 设置前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(state_string);
format.clearForeground(); //清除前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
} else if (state == WRONG_SIGNAL) //不正常红色
{
format.setForeground(QColor(200, 0, 0)); // 设置前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(state_string);
format.clearForeground(); //清除前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
} else if (state == SIGNAL_SIGNAL) //信号蓝色
{
format.setForeground(QColor(0, 0, 200)); // 蓝色设置前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(state_string);
format.clearForeground(); //清除前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
} else if (state == EVA_SIGNAL) //行为警告
{
QFont font = format.font();
// font.setFamily(DEFAULT_FONT);
// font.setLetterSpacing(QFont::AbsoluteSpacing, 0); // 设置字母间的绝对间距
font.setPixelSize(14);
format.setFont(font);
format.setFontItalic(true); // 设置斜体
// format.setForeground(QColor(128,0,128)); // 紫色设置前景颜色
format.setForeground(NORMAL_BLACK); //还是黑色吧
ui->state->setCurrentCharFormat(format); //设置光标格式
//■■■■■■■■■■■■■■
ui->state->appendPlainText(jtr("cubes")); //显示
//中间内容
format.setFontItalic(false); // 取消斜体
format.setFontWeight(QFont::Black); // 设置粗体
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(" " + state_string); //显示
//■■■■■■■■■■■■■■
format.setFontItalic(true); // 设置斜体
format.setFontWeight(QFont::Normal); // 取消粗体
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(jtr("cubes")); //显示
format.setFontWeight(QFont::Normal); // 取消粗体
format.setFontItalic(false); // 取消斜体
format.clearForeground(); //清除前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
} else if (state == TOOL_SIGNAL) //工具天蓝色
{
format.setForeground(TOOL_BLUE); //天蓝色设置前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(state_string);
format.clearForeground(); //清除前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
} else if (state == SYNC_SIGNAL) //同步橘黄色
{
format.setForeground(LCL_ORANGE); //天蓝色设置前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
ui->state->appendPlainText(state_string);
format.clearForeground(); //清除前景颜色
ui->state->setCurrentCharFormat(format); //设置光标格式
}
}
//-------------------------------------------------------------------------
//-------------------------------响应槽相关---------------------------------
//-------------------------------------------------------------------------
//温度滑块响应
void Widget::temp_change() { settings_ui->temp_label->setText(jtr("temperature") + " " + QString::number(settings_ui->temp_slider->value() / 100.0)); }
// ngl滑块响应
void Widget::ngl_change() { settings_ui->ngl_label->setText("gpu " + jtr("offload") + " " + QString::number(settings_ui->ngl_slider->value())); }
// nctx滑块响应
void Widget::nctx_change() { settings_ui->nctx_label->setText(jtr("brain size") + " " + QString::number(settings_ui->nctx_slider->value())); }
// repeat滑块响应
void Widget::repeat_change() { settings_ui->repeat_label->setText(jtr("repeat") + " " + QString::number(settings_ui->repeat_slider->value() / 100.0)); }
void Widget::nthread_change() { settings_ui->nthread_label->setText("cpu " + jtr("thread") + " " + QString::number(settings_ui->nthread_slider->value())); }
//补完状态按钮响应
void Widget::complete_change() {
//选中则禁止约定输入
if (settings_ui->complete_btn->isChecked()) {
settings_ui->sample_box->setEnabled(1);
settings_ui->nthread_slider->setEnabled(1);
settings_ui->nctx_slider->setEnabled(1);
settings_ui->port_lineEdit->setEnabled(0);
}
}
//对话状态按钮响应
void Widget::chat_change() {
if (settings_ui->chat_btn->isChecked()) {
settings_ui->sample_box->setEnabled(1);
settings_ui->nctx_slider->setEnabled(1);
settings_ui->nthread_slider->setEnabled(1);
settings_ui->port_lineEdit->setEnabled(0);
}
}
//服务状态按钮响应
void Widget::web_change() {
if (settings_ui->web_btn->isChecked()) {
settings_ui->sample_box->setEnabled(0);
settings_ui->port_lineEdit->setEnabled(1);
}
}
//提示词模板下拉框响应
void Widget::prompt_template_change() {
if (date_ui->chattemplate_comboBox->currentText() == jtr("custom set1")) {
date_ui->date_prompt_TextEdit->setEnabled(1);
date_ui->user_name_LineEdit->setEnabled(1);
date_ui->model_name_LineEdit->setEnabled(1);
date_ui->date_prompt_TextEdit->setPlainText(custom1_date_system);
date_ui->user_name_LineEdit->setText(custom1_user_name);
date_ui->model_name_LineEdit->setText(custom1_model_name);
} else if (date_ui->chattemplate_comboBox->currentText() == jtr("custom set2")) {
date_ui->date_prompt_TextEdit->setEnabled(1);
date_ui->user_name_LineEdit->setEnabled(1);
date_ui->model_name_LineEdit->setEnabled(1);
date_ui->date_prompt_TextEdit->setPlainText(custom2_date_system);
date_ui->user_name_LineEdit->setText(custom2_user_name);
date_ui->model_name_LineEdit->setText(custom2_model_name);
} else {
date_ui->date_prompt_TextEdit->setPlainText(date_map[date_ui->chattemplate_comboBox->currentText()].date_prompt);
date_ui->date_prompt_TextEdit->setEnabled(0);
date_ui->user_name_LineEdit->setText(date_map[date_ui->chattemplate_comboBox->currentText()].user_name);
date_ui->user_name_LineEdit->setEnabled(0);
date_ui->model_name_LineEdit->setText(date_map[date_ui->chattemplate_comboBox->currentText()].model_name);
date_ui->model_name_LineEdit->setEnabled(0);
}
}
void Widget::chooseLorapath() {
//用户选择模型位置
currentpath = customOpenfile(currentpath, jtr("choose lora model"), "(*.bin *.gguf)");
settings_ui->lora_LineEdit->setText(currentpath);
}
void Widget::chooseMmprojpath() {
//用户选择模型位置
currentpath = customOpenfile(currentpath, jtr("choose mmproj model"), "(*.bin *.gguf)");
settings_ui->mmproj_LineEdit->setText(currentpath);
}
//响应工具选择
void Widget::tool_change() {
// 判断是否挂载了工具
if (date_ui->calculator_checkbox->isChecked() || date_ui->engineer_checkbox->isChecked() || date_ui->webengine_checkbox->isChecked() || date_ui->knowledge_checkbox->isChecked() || date_ui->controller_checkbox->isChecked() || date_ui->stablediffusion_checkbox->isChecked()) {
if (is_load_tool == false) {
reflash_state("ui:" + jtr("enable output parser"), SIGNAL_SIGNAL);
}
is_load_tool = true;
} else {
if (is_load_tool == true) {
reflash_state("ui:" + jtr("disable output parser"), SIGNAL_SIGNAL);
}
is_load_tool = false;
}
ui_extra_prompt = create_extra_prompt();
}
//-------------------------------------------------------------------------
//--------------------------------设置选项相关------------------------------
//-------------------------------------------------------------------------
void Widget::set_SetDialog() {
settings_dialog = new QDialog(this);
settings_dialog->setWindowFlags(settings_dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint); //隐藏?按钮
settings_dialog->setWindowFlags(settings_dialog->windowFlags() & ~Qt::WindowCloseButtonHint);// 隐藏关闭按钮
settings_ui = new Ui::Settings_Dialog_Ui;
settings_ui->setupUi(settings_dialog);
//性能测试
settings_ui->bench_plaintextedit->setVisible(0);//性能测试结果暂不显示
connect(settings_ui->bench_btn,&QPushButton::clicked,this,&Widget::bench_btn_clicked);
//温度控制
settings_ui->temp_slider->setRange(0, 100); // 设置范围为1到99
settings_ui->temp_slider->setValue(ui_SETTINGS.temp * 100.0);
connect(settings_ui->temp_slider, &QSlider::valueChanged, this, &Widget::temp_change);
//重复惩罚控制
settings_ui->repeat_slider->setRange(0, 200); // 设置范围
settings_ui->repeat_slider->setValue(ui_SETTINGS.repeat * 100.0);
connect(settings_ui->repeat_slider, &QSlider::valueChanged, this, &Widget::repeat_change);
//加速支持
settings_ui->ngl_slider->setRange(0, 99);
settings_ui->ngl_slider->setValue(ui_SETTINGS.ngl);
connect(settings_ui->ngl_slider, &QSlider::valueChanged, this, &Widget::ngl_change);
// cpu线程数设置
settings_ui->nthread_slider->setValue(ui_SETTINGS.nthread);
connect(settings_ui->nthread_slider, &QSlider::valueChanged, this, &Widget::nthread_change);
// ctx length 记忆容量
settings_ui->nctx_slider->setRange(128, 32768);
settings_ui->nctx_slider->setValue(ui_SETTINGS.nctx);
connect(settings_ui->nctx_slider, &QSlider::valueChanged, this, &Widget::nctx_change);
// load lora
settings_ui->lora_LineEdit->setContextMenuPolicy(Qt::NoContextMenu); //取消右键菜单
settings_ui->lora_LineEdit->installEventFilter(this);
// load mmproj
settings_ui->mmproj_LineEdit->setContextMenuPolicy(Qt::NoContextMenu); //取消右键菜单
settings_ui->mmproj_LineEdit->installEventFilter(this);
//补完控制
connect(settings_ui->complete_btn, &QRadioButton::clicked, this, &Widget::complete_change);
//多轮对话
settings_ui->chat_btn->setChecked(1);
connect(settings_ui->chat_btn, &QRadioButton::clicked, this, &Widget::chat_change);
//网页服务控制
QHBoxLayout *layout_H10 = new QHBoxLayout(); //水平布局器
settings_ui->port_lineEdit->setText(ui_port);
QIntValidator *validator = new QIntValidator(0, 65535); //限制端口输入
settings_ui->port_lineEdit->setValidator(validator);
connect(settings_ui->web_btn, &QRadioButton::clicked, this, &Widget::web_change);
connect(settings_ui->confirm, &QPushButton::clicked, this, &Widget::settings_ui_confirm_button_clicked);
connect(settings_ui->cancel, &QPushButton::clicked, this, &Widget::settings_ui_cancel_button_clicked);
settings_dialog->setWindowTitle(jtr("set"));
}
// 设置选项卡确认按钮响应
void Widget::settings_ui_confirm_button_clicked() {
settings_dialog->close();
set_set();
}
// 设置选项卡取消按钮响应
void Widget::settings_ui_cancel_button_clicked() {
settings_dialog->close();
if(!is_load)//如果没有装载模型则装载
{
set_set();
}
}
// 设置用户设置内容
void Widget::set_set() {
EVA_STATE current_ui_state = ui_state; //上一次机体的状态
get_set(); //获取设置中的纸面值
//如果不是对话模式则禁用约定
if (ui_state != CHAT_STATE) {
date_ui->prompt_box->setEnabled(0);
date_ui->tool_box->setEnabled(0);
} else {
date_ui->prompt_box->setEnabled(1);
date_ui->tool_box->setEnabled(1);
}
//从补完模式回来强行预解码
if(current_ui_state == COMPLETE_STATE && ui_state == CHAT_STATE)
{
emit ui2bot_preDecode();
}
//发送设置参数给模型
if(ui_state != SERVER_STATE && ui_mode != LINK_MODE){emit ui2bot_set(ui_SETTINGS, 1);}
// llama-server接管,不需要告知bot约定
if (ui_state == SERVER_STATE) {
serverControl();
} else {
if (ui_mode == LINK_MODE) //链接模式不发信号
{
on_reset_clicked();
}
}
}
//-------------------------------------------------------------------------
//--------------------------------约定选项相关------------------------------
//-------------------------------------------------------------------------
void Widget::set_DateDialog() {
//初始化约定窗口
date_dialog = new QDialog(this);
date_dialog->setWindowTitle(jtr("date"));
date_dialog->setWindowFlags(date_dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint); //隐藏?按钮
date_ui = new Ui::Date_Dialog_Ui;
date_ui->setupUi(date_dialog);
for (const QString &key : date_map.keys()) {
date_ui->chattemplate_comboBox->addItem(key);
}
date_ui->chattemplate_comboBox->addItem(jtr("custom set1")); //添加自定义模板
date_ui->chattemplate_comboBox->addItem(jtr("custom set2")); //添加自定义模板
date_ui->chattemplate_comboBox->setCurrentText(ui_template); //默认使用default的提示词模板
connect(date_ui->chattemplate_comboBox, &QComboBox::currentTextChanged, this, &Widget::prompt_template_change);
connect(date_ui->confirm_button, &QPushButton::clicked, this, &Widget::date_ui_confirm_button_clicked);
connect(date_ui->cancel_button, &QPushButton::clicked, this, &Widget::date_ui_cancel_button_clicked);
connect(date_ui->switch_lan_button, &QPushButton::clicked, this, &Widget::switch_lan_change);
connect(date_ui->knowledge_checkbox, &QCheckBox::stateChanged, this, &Widget::tool_change); //点击工具响应
connect(date_ui->stablediffusion_checkbox, &QCheckBox::stateChanged, this, &Widget::tool_change); //点击工具响应
connect(date_ui->calculator_checkbox, &QCheckBox::stateChanged, this, &Widget::tool_change); //点击工具响应
connect(date_ui->controller_checkbox, &QCheckBox::stateChanged, this, &Widget::tool_change); //点击工具响应
connect(date_ui->webengine_checkbox, &QCheckBox::stateChanged, this, &Widget::tool_change); //点击工具响应
connect(date_ui->engineer_checkbox, &QCheckBox::stateChanged, this, &Widget::tool_change); //点击工具响应
if (language_flag == 0) {
ui_extra_lan = "zh";
}
if (language_flag == 1) {
ui_extra_lan = "en";
}
prompt_template_change(); //先应用提示词模板
}
// 约定选项卡确认按钮响应
void Widget::date_ui_confirm_button_clicked() {
date_dialog->close();
set_date();
}
// 约定选项卡取消按钮响应
void Widget::date_ui_cancel_button_clicked() {
date_dialog->close();
cancel_date();
}
//-------------------------------------------------------------------------
//--------------------------------api选项相关------------------------------
//-------------------------------------------------------------------------
void Widget::setApiDialog() {
api_dialog = new QDialog();
api_dialog->setWindowTitle(jtr("link") + jtr("set"));
api_dialog->setWindowFlags(api_dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint); //隐藏?按钮
// api_dialog->setWindowFlags(api_dialog->windowFlags() & ~Qt::WindowCloseButtonHint); //隐藏关闭按钮
api_dialog->resize(400, 100); // 设置宽度,高度
QVBoxLayout *layout = new QVBoxLayout(api_dialog); //垂直布局器
// api_endpoint
QHBoxLayout *layout_H1 = new QHBoxLayout(); //水平布局器
api_endpoint_label = new QLabel(jtr("api endpoint"),this);
api_endpoint_label->setFixedWidth(80);
layout_H1->addWidget(api_endpoint_label);
api_endpoint_LineEdit = new QLineEdit(this);
api_endpoint_LineEdit->setPlaceholderText(jtr("input api endpoint"));
api_endpoint_LineEdit->setToolTip(jtr("api endpoint tool tip"));
api_endpoint_LineEdit->setText(apis.api_endpoint);
layout_H1->addWidget(api_endpoint_LineEdit);
layout->addLayout(layout_H1); //将布局添加到总布局
// api_key
QHBoxLayout *layout_H2 = new QHBoxLayout(); //水平布局器
api_key_label = new QLabel(jtr("api key"),this);
api_key_label->setFixedWidth(80);
layout_H2->addWidget(api_key_label);
api_key_LineEdit = new QLineEdit(this);
api_key_LineEdit->setEchoMode(QLineEdit::Password);
api_key_LineEdit->setPlaceholderText(jtr("sd_vaepath_lineEdit_placeholder"));
api_key_LineEdit->setToolTip(jtr("input api key"));
api_key_LineEdit->setText(apis.api_key);
layout_H2->addWidget(api_key_LineEdit);
layout->addLayout(layout_H2); //将布局添加到总布局
// api_model
QHBoxLayout *layout_H3 = new QHBoxLayout(); //水平布局器
api_model_label = new QLabel(jtr("api model"),this);
api_model_label->setFixedWidth(80);
layout_H3->addWidget(api_model_label);
api_model_LineEdit = new QLineEdit(this);
api_model_LineEdit->setPlaceholderText(jtr("sd_vaepath_lineEdit_placeholder"));
api_model_LineEdit->setToolTip(jtr("input api model"));
api_model_LineEdit->setText(apis.api_model);
layout_H3->addWidget(api_model_LineEdit);
layout->addLayout(layout_H3); //将布局添加到总布局
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, api_dialog); // 创建 QDialogButtonBox 用于确定和取消按钮
layout->addWidget(buttonBox);
connect(buttonBox, &QDialogButtonBox::accepted, this, &Widget::set_api);
connect(buttonBox, &QDialogButtonBox::accepted, api_dialog, &QDialog::reject);// 点击确定后直接退出
connect(buttonBox, &QDialogButtonBox::rejected, api_dialog, &QDialog::reject);
}
//-------------------------------------------------------------------------
//----------------------------------编码动画--------------------------------
//-------------------------------------------------------------------------
void Widget::decode_play() {
decode_pTimer->start(100); //延时多少ms后发出timeout()信号
}
void Widget::decode_move() {
int decode_LineNumber = ui->state->document()->blockCount() - 1; // 获取最后一行的行数
//如果在新的行数上播放动画,则先删除上一次解码动画的残余部分
if (currnet_LineNumber != decode_LineNumber) {
QTextBlock currnet_block = ui->state->document()->findBlockByLineNumber(currnet_LineNumber); //取上次最后一行
QTextCursor currnet_cursor(currnet_block); //取游标
currnet_cursor.movePosition(QTextCursor::EndOfBlock); //游标移动到末尾
currnet_cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); //选中当前字符
if (currnet_cursor.selectedText() == "\\" || currnet_cursor.selectedText() == "/" || currnet_cursor.selectedText() == "-" || currnet_cursor.selectedText() == "|") {
currnet_cursor.removeSelectedText(); //删除选中字符
}
currnet_LineNumber = decode_LineNumber;
}
QTextBlock block = ui->state->document()->findBlockByLineNumber(decode_LineNumber); //取最后一行
QTextCursor cursor(block); //取游标
cursor.movePosition(QTextCursor::EndOfBlock); //游标移动到末尾
//四帧动画
if (decode_action % 8 == 0) {
cursor.insertText("|"); //插入字符
} else if (decode_action % 8 == 2) {
cursor.insertText("/"); //插入字符
} else if (decode_action % 8 == 4) {
cursor.insertText("-"); //插入字符
} else if (decode_action % 8 == 6) {
cursor.insertText("\\"); //插入字符
} else {
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); //选中当前字符
if (cursor.selectedText() == "|" || cursor.selectedText() == "\\" || cursor.selectedText() == "/" || cursor.selectedText() == "-") {
cursor.removeSelectedText(); //删除选中字符
}
}
decode_action++;
}
void Widget::decode_handleTimeout() {
if (decode_pTimer->isActive()) {
decode_pTimer->stop();
} //控制超时处理函数只会处理一次
decode_move();
decode_pTimer->start(100); //延时多少ms后发出timeout()信号
}
//-------------------------------------------------------------------------
//----------------------------------链接相关--------------------------------
//-------------------------------------------------------------------------
//应用api设置
void Widget::set_api() {
emit ui2bot_free(0); //释放原来的模型
is_load = false;// 重置
historypath = "";// 重置
//重置评级
MODELINFO model_info_;
modelinfo = model_info_;
//获取设置值
apis.api_endpoint = api_endpoint_LineEdit->text();
apis.api_key = api_key_LineEdit->text();
apis.api_model = api_model_LineEdit->text();
ui_mode = LINK_MODE; //按照链接模式的行为来
reflash_state("ui:" + jtr("eva link"), EVA_SIGNAL);
if (ui_state == CHAT_STATE) {
current_api = apis.api_endpoint + apis.api_chat_endpoint;
} else {
current_api = apis.api_endpoint + apis.api_completion_endpoint;
}
reflash_state("ui:" + jtr("current api") + " " + current_api, USUAL_SIGNAL);
this->setWindowTitle(jtr("current api") + " " + current_api);
QApplication::setWindowIcon(QIcon(":/logo/dark_logo.png")); //设置应用程序图标
ui->kv_bar->setToolTip("");
emit ui2net_apis(apis);
ui->output->clear();
reflash_output(ui_DATES.date_prompt, 0, SYSTEM_BLUE);
ui_state_normal();
auto_save_user();
}
//链接模式下工具返回结果时延迟发送
void Widget::tool_testhandleTimeout() {
ENDPOINT_DATA data;
data.date_prompt = ui_DATES.date_prompt;
data.input_pfx = ui_DATES.user_name;
data.input_sfx = ui_DATES.model_name;
data.stopwords = ui_DATES.extra_stop_words;
if (ui_state == COMPLETE_STATE) {
data.complete_state = true;
} else {
data.complete_state = false;
}
data.temp = ui_SETTINGS.temp;
data.repeat = ui_SETTINGS.repeat;
data.n_predict = ui_SETTINGS.hid_npredict;
data.insert_history = ui_insert_history;
emit ui2net_data(data);
emit ui2net_push();
}
void Widget::send_testhandleTimeout() { on_send_clicked(); }
//链接模式切换时某些控件可见状态
void Widget::change_api_dialog(bool enable) {
settings_ui->repeat_label->setVisible(enable);
settings_ui->repeat_slider->setVisible(enable);
settings_ui->nctx_label->setVisible(enable);
settings_ui->nctx_slider->setVisible(enable);
settings_ui->nthread_label->setVisible(enable);
settings_ui->nthread_slider->setVisible(enable);
settings_ui->mmproj_label->setVisible(enable);
settings_ui->mmproj_LineEdit->setVisible(enable);
settings_ui->ngl_label->setVisible(enable);
settings_ui->ngl_slider->setVisible(enable);
settings_ui->lora_label->setVisible(enable);
settings_ui->lora_LineEdit->setVisible(enable);
settings_ui->port_label->setVisible(enable);
settings_ui->port_lineEdit->setVisible(enable);
settings_ui->web_btn->setVisible(enable);
settings_ui->bench_btn->setVisible(enable);
}
//-------------------------------------------------------------------------
//----------------------------------界面状态--------------------------------
//-------------------------------------------------------------------------
//按钮的可用和可视状态控制
//最终要归到下面的几种状态来
//初始界面状态
void Widget::ui_state_init() {
ui->load->setEnabled(1); //装载按钮
ui->date->setEnabled(0); //约定按钮
ui->set->setEnabled(0); //设置按钮
ui->reset->setEnabled(0); //重置按钮
ui->send->setEnabled(0); //发送按钮
ui->output->setReadOnly(1);
}
// 装载中界面状态
void Widget::ui_state_loading() {
ui->send->setEnabled(0); //发送按钮
ui->reset->setEnabled(0); //重置按钮
ui->date->setEnabled(0); //约定按钮
ui->set->setEnabled(0); //设置按钮
ui->load->setEnabled(0); //装载按钮
ui->input->setFocus(); //设置输入区为焦点
}
//推理中界面状态
void Widget::ui_state_pushing() {
decode_play(); //开启推理动画
ui->load->setEnabled(0);
ui->date->setEnabled(0);
ui->set->setEnabled(0);
if (ui_syncrate_manager.is_sync) {
ui->reset->setEnabled(0);
} else {
ui->reset->setEnabled(1);
}
ui->send->setEnabled(0);
}
//服务中界面状态
void Widget::ui_state_servering() {
ui->load->setEnabled(0);
ui->date->setEnabled(0);
ui->set->setEnabled(1);
ui->reset->setEnabled(0);
ui->input->setVisible(0);
ui->send->setVisible(0);
}
//待机界面状态
void Widget::ui_state_normal() {
if (is_run) //如果是模型正在运行的状态的话
{
ui->reset->setEnabled(1);
ui->input->setEnabled(1);
ui->input->setReadOnly(0);
ui->send->setEnabled(0);
ui->input->setPlaceholderText(jtr("chat or right click to choose question"));
ui->input->setStyleSheet("background-color: white;");
return;
}
decode_pTimer->stop(); //停止解码动画
if (ui_state == CHAT_STATE) {
ui->input->setVisible(1);
ui->send->setVisible(1);
ui->load->setEnabled(1);
if (is_load || ui_mode == LINK_MODE) {
ui->reset->setEnabled(1);
ui->send->setEnabled(1);
}
if(is_load)
{
ui->date->setEnabled(1);
ui->set->setEnabled(1);
}
ui->input->setVisible(1);
ui->send->setVisible(1);
ui->input->setPlaceholderText(jtr("chat or right click to choose question"));
ui->input->setStyleSheet("background-color: white;");
ui->input->setReadOnly(0);
ui->input->setFocus(); //设置输入区为焦点
ui->send->setText(jtr("send"));
ui->output->setReadOnly(1);
} else if (ui_state == COMPLETE_STATE) {
ui->load->setEnabled(1);
if (is_load || ui_mode == LINK_MODE) {
ui->reset->setEnabled(1);
ui->send->setEnabled(1);
}
ui->date->setEnabled(1);
ui->set->setEnabled(1);
ui->input->setVisible(1);
ui->send->setVisible(1);
ui->input->clear();
ui->input->setPlaceholderText(jtr("Please modify any text above"));
ui->input->setStyleSheet("background-color: rgba(255, 165, 0, 127);"); //设置背景为橘黄色
ui->input->setReadOnly(1);
ui->send->setText(jtr("complete"));
ui->output->setReadOnly(0);
ui->output->setFocus(); //设置输出区为焦点
} else if (ui_state == SERVER_STATE) {
ui->set->setEnabled(1);
}
if (ui_mode == LINK_MODE) {
change_api_dialog(0);
} //链接模式不要解码设置
else {
change_api_dialog(1);
}
}
//录音界面状态
void Widget::ui_state_recoding() {
if (audio_time == 0) {
ui->load->setEnabled(0);
ui->date->setEnabled(0);
ui->set->setEnabled(0);
ui->reset->setEnabled(0);
ui->send->setEnabled(0);
ui->input->setFocus();
ui->input->clear();
ui->input->setStyleSheet("background-color: rgba(144, 238, 144, 127);"); //透明绿色
ui->input->setReadOnly(1);
ui->input->setFocus(); //设置输入区为焦点