-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
156 lines (139 loc) · 5.37 KB
/
Copy pathmainwindow.cpp
File metadata and controls
156 lines (139 loc) · 5.37 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>
#include<QWidget>
#include<QScrollBar>
#include<QSpinBox>
#include<QLabel>
#include<QSettings>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// qDebug() << "Button geometry:" << ui->clearButton->geometry();
// qDebug() << "Button visible:" << ui->clearButton->isVisible();
// qDebug() << "Button size:" << ui->clearButton->size();
auto handwriting = ui->widget;
handwriting->setFocusPolicy(Qt::StrongFocus);
handwriting->loadBackground(handwriting->getBgPath());
label = new QLabel(this);
label->setText(tr("Set Label"));
recLabel = new QLabel(tr("Pending..."),this);
recLabel->show();
toolBar = addToolBar(tr("Tool"));
// recLabel->setBaseSize(100,100);
spinBox = new QSpinBox(this);
spinBox->setRange(0,9);
spinBox->setSingleStep(1);
spinBox->setValue(0);
//spinBox for label setting
lang = new QComboBox(this);
lang->setToolTip(tr("Language"));
lang->addItem("English","en");
lang->addItem("简体中文(中国)","zh_CN");
lang->addItem("繁體中文(台灣)","zh_TW");
lang->addItem("繁體中文(香港)","zh_HK");
lang->addItem("Svenska","sv");
connect(lang,&QComboBox::currentIndexChanged,this,&MainWindow::switchLanguage);
saveSound = new QSoundEffect(this);
saveSound->setSource(QUrl("qrc:/media/yoshino_save-new.wav"));
saveSound->setVolume(1.0);
exitSound = new QSoundEffect(this);
exitSound->setSource(QUrl("qrc:/media/yoshino_goodbye-new.wav"));
showSound = new QSoundEffect(this);
showSound->setSource(QUrl("qrc:/media/yoshino_yuzu-new.wav"));
clearSound = new QSoundEffect(this);
clearSound->setSource(QUrl("qrc:/media/yoshino_reset-new.wav"));
recAction = new QAction(tr("Recognize(Ctrl+R)"),this);
recMultAction =new QAction(tr("Multi-Recognize"),this);
connect(handwriting, &HandWriting::recFinished,
recLabel, &QLabel::setText);
connect(recAction,&QAction::triggered,this,
[this,handwriting]()
{handwriting->recognize();
recLabel->setText(handwriting->getOutput());
recLabel->show();});
connect(recMultAction,&QAction::triggered,this,
[this,handwriting](){
handwriting->multiRecognize();
recLabel->setText(handwriting->getOutput());
recLabel->show();
});
//update recLabel w/ output so that you can see
toolBar->addWidget(lang);
clearAction = new QAction(tr("Clear(Ctrl+C)"),this);
connect(clearAction,&QAction::triggered,this,[this](){clearSound->play();});
connect(clearAction,&QAction::triggered,handwriting,&HandWriting::clear);
saveAction = new QAction(tr("Save(Ctrl+S)"),this);
connect(saveAction, &QAction::triggered, this,
[this]() {
saveSound->play();
});
connect(saveAction,&QAction::triggered,handwriting,&HandWriting::saveToImage);
toolBar->addAction(clearAction);
toolBar->addAction(saveAction);
toolBar->addAction(recAction);
toolBar->addAction(recMultAction);
toolBar->addWidget(label);
toolBar->addWidget(spinBox);
toolBar->addWidget(recLabel);
connect(spinBox,QOverload<int>::of(&QSpinBox::valueChanged),handwriting,&HandWriting::setLabel);
// setCentralWidget(handwriting);
handwriting->loadModel();
QString lastLang = QSettings().value("language", "en").toString();
int idx = lang->findData(lastLang);
if (idx != -1) lang->setCurrentIndex(idx);
else lang->setCurrentIndex(0);
qDebug() << handwriting->size();
toolBar->show();
qDebug() << "Toolbar visible:" << toolBar->isVisible();
qDebug() << "Toolbar actions count:" << toolBar->actions().size();
}
void MainWindow::switchLanguage(int index) {
QString langCode = "lang_"+lang->itemData(index).toString();
// 加载对应的 .qm 文件(从文件系统)
QTranslator *newTranslator = new QTranslator;
if (newTranslator->load(":/translations/" + langCode + ".qm")) {
// 移除旧的翻译器
if (current_tr) {
qApp->removeTranslator(current_tr);
delete current_tr; // 可选,不删除也可以
}
current_tr = newTranslator;
qApp->installTranslator(current_tr);
// 刷新界面
ui->retranslateUi(this);
retranslateUi();
QSettings().setValue("language", langCode);
} else {
delete newTranslator;
qDebug() << "Failed to load translation:" << langCode;
}
}
void MainWindow::retranslateUi()
{
ui->retranslateUi(this);
label->setText(tr("Set Label"));
recLabel->setText(tr("Pending..."));
toolBar->setToolTip(tr("Tool"));
lang->setToolTip(tr("Language"));
recAction->setText(tr("Recognize(Ctrl+R)"));
recMultAction->setText(tr("Multi-Recognize"));
clearAction->setText(tr("Clear(Ctrl+C)"));
saveAction->setText(tr("Save(Ctrl+S)"));
lang->blockSignals(true);
int idx = lang->currentIndex();
lang->setItemText(0, "English");
lang->setItemText(1, "简体中文(中国)");
lang->setItemText(2, "繁體中文(台灣)");
lang->setItemText(3, "繁體中文(香港)");
lang->setItemText(4,"Svenska");
lang->setCurrentIndex(idx);
lang->setToolTip(tr("Language"));
lang->blockSignals(false);
}
MainWindow::~MainWindow()
{
delete ui;
}