-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
197 lines (171 loc) · 4.77 KB
/
mainwindow.cpp
File metadata and controls
197 lines (171 loc) · 4.77 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QCloseEvent>
#include <QTimer>
#include <QDebug>
#include <QTranslator>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setFixedSize(480,360);
// slider & spinbox
ui->slider_1->setRange(0,100);
ui->slider_1->setSingleStep(1);
ui->slider_1->setValue(100);
ui->spinbox_1->setRange(0,100);
ui->spinbox_1->setSingleStep(1);
ui->spinbox_1->setValue(100);
ui->spinbox_1->setWrapping(true);
ui->spinbox_1->setFrame(true);
// timer spinbox
ui->time_value->setRange(0,99999999);
ui->time_value->setSingleStep(1);
ui->time_value->setSuffix(" Ms");
ui->time_value->setValue(500);
timer = new QTimer(this);
direction_group->addButton(ui->direction_up,1);
direction_group->addButton(ui->direction_down,2);
QActionGroup *language_group = new QActionGroup(this);
language_group->setExclusive(true);
language_group->addAction(ui->L_en);
language_group->addAction(ui->L_zh);
ui->button_pause->setEnabled(false);
ui->button_stop->setEnabled(false);
connect(direction_group,SIGNAL(buttonClicked(int)),this,SLOT(set_direction()));
connect(timer,SIGNAL(timeout()),this,SLOT(timer_start()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_menu_exit_triggered()
{
this->close();
}
void MainWindow::on_help_about_triggered()
{
// 设置about_dialog为主窗口的子对象
about_info* about_dialog = new about_info(this);
about_dialog->show();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
// 重写closeevent确认关闭窗口
switch( QMessageBox::information( this, tr("确认?"),
tr("关闭?"),
tr("upup"), tr("124"),0, 1 ) )
{
case 0:
event->accept();
break;
case 1:
default:
event->ignore();
break;
}
}
void MainWindow::on_button_up_clicked()
{
ui->spinbox_1->setValue(ui->spinbox_1->value()+1);
}
void MainWindow::on_button_donw_clicked()
{
ui->spinbox_1->setValue(ui->spinbox_1->value()-1);
}
void MainWindow::set_direction()
{
switch (direction_group->checkedId()) {
case 1:
direction = 1;
break;
default:
direction = 2;
break;
}
}
void MainWindow::timer_start()
{
if(direction == 1)
ui->spinbox_1->setValue(ui->spinbox_1->value()+1);
else
ui->spinbox_1->setValue(ui->spinbox_1->value()-1);
}
void MainWindow::on_button_start_clicked()
{
ui->button_pause->setEnabled(true);
ui->button_stop->setEnabled(true);
ui->button_start->setEnabled(false);
ui->time_value->setEnabled(false);
timer->setInterval(ui->time_value->value());
timer->start();
}
void MainWindow::on_button_pause_clicked()
{
if(pause_continue_flag)
{
timer->stop();
ui->button_pause->setText(tr("继续"));
pause_continue_flag = !pause_continue_flag;
}
else
{
timer->start();
ui->button_pause->setText(tr("暂停"));
pause_continue_flag = !pause_continue_flag;
}
}
void MainWindow::on_button_stop_clicked()
{
ui->button_pause->setEnabled(false);
ui->button_pause->setText(tr("暂停"));
ui->button_start->setEnabled(true);
ui->button_stop->setEnabled(false);
ui->time_value->setEnabled(true);
timer->stop();
}
void MainWindow::change_language()
{
// qDebug() << language_id;
QString strLanguageFile;
if (language_id == 2)
{
strLanguageFile = qApp->applicationDirPath() + QString("/languages/EN.qm");
// qDebug() << strLanguageFile;
load_language_file(strLanguageFile);
}
else if (language_id == 1)
{
qApp->removeTranslator(&translator);
ui->retranslateUi(this);
}
}
void MainWindow::on_L_zh_triggered()
{
language_id = ZH;
change_language();
}
void MainWindow::on_L_en_triggered()
{
language_id = EN;
change_language();
}
void MainWindow::load_language_file(QString &file_dir)
{
if(QFile(file_dir).exists())
{
// qApp为application全局变量
// 在translator加载翻译文件后,app安装翻译后,需要重新翻译
translator.load(file_dir);
qApp->installTranslator(&translator);
ui->retranslateUi(this);
// qDebug() << "load";
}
else
{
QMessageBox::about(NULL, "错误!(Error!)", " 语言文件不存在! \n\rLanguage files not exist!");
}
}