Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
229 lines (201 loc) · 7.92 KB
/
Copy pathmainwindow.cpp
File metadata and controls
229 lines (201 loc) · 7.92 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qdebug.h"
#include <qprocess.h>
#include <qtextcodec.h>
#include <time.h>
#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle(tr("哈夫曼压缩工具"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_BT_Select_clicked()
{
Init();
memset(filename,NULL,256);
memset(htfname,NULL,256);
hfm=nullptr;
QString fileName = QFileDialog::getOpenFileName(this, tr("打开文件"),
NULL,
tr("anyFile (*)"));
current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+"\t选择文件 "+fileName+"\n");
ui->lineEdit->setText(fileName);
QByteArray cdata = fileName.toLocal8Bit();
string str=string(cdata);
const char *pStr=str.data();
strcpy(filename,pStr);
statusBar()->showMessage(tr("打开文件中..."));
double time_Start=(double)clock();
if(OpenFile(filename,hfm)==false)
{
ui->textEdit->append(current_date+"\t打开文件失败!\n");
statusBar()->showMessage(tr("打开文件失败"),3000);
return;
}
double time_End=(double)clock();
ui->textEdit->append(current_date+tr("\t成功打开 ")+fileName+"\n");
QString t=QString::number((time_End-time_Start)/1000,10,3);
statusBar()->showMessage(tr("打开文件完成,用时")+t+tr("s"),3000);
}
void MainWindow::on_BT_Compress_clicked()
{
saveHfmFile();
if(path=="")
{
QMessageBox::warning(this, tr("警告"),
tr("请选择一个huffman文件!"));
statusBar()->showMessage(tr("没有选择压缩文件"),3000);
}
else
{
ui->ED_CmpName->setText(path);
QByteArray cdata =path.toLocal8Bit();
string str=string(cdata);
const char *pStr=str.data();
strcpy(htfname,pStr);
statusBar()->showMessage(tr("文件压缩中..."));
double time_Start=(double)clock();
Compress(hfm,filename,htfname);
double time_End=(double)clock();
QString t=QString::number((time_End-time_Start)/1000,10,3);
QMessageBox::information(this,tr("Information"),tr("文件压缩成功!"));
current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+tr("\t成功压缩文件 ")+path+"\n");
statusBar()->showMessage(tr("文件压缩完成,用时")+t+tr("s"),3000);
}
}
void MainWindow::saveHfmFile()
{
path = QFileDialog::getSaveFileName(this,
tr("保存文件"),
".",
tr("Huffman Files(*.hfm)"));
if(!path.isEmpty()) {
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+"\t不能打开 "+path+"\n");
QMessageBox::warning(this, tr("Write File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream out(&file);
out << ui->textEdit->toPlainText();
file.close();
} else {
QMessageBox::warning(this, tr("警告"),
tr("你还没有选择任何文件"));
}
}
void MainWindow::on_BT_UnCompress_clicked()
{
//选择hfm文件
memset(htfname,NULL,256);
QString fileName = QFileDialog::getOpenFileName(this, tr("选择文件"),
NULL,
tr("HuffmanFile(*.hfm)"));
current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+"\t打开文件 "+fileName+"\n");
QByteArray cdata =fileName.toLocal8Bit();
string str=string(cdata);
const char *pStr=str.data();
strcpy(htfname,pStr);
//选择解压路径
saveExtraceFile();
if(path=="")
{
QMessageBox::warning(this, tr("警告"),
tr("请选择一个解压文件"));
statusBar()->showMessage(tr("没有选择解压文件"),3000);
return;
}
cdata=path.toLocal8Bit();
str=string(cdata);
ui->lineEdit_2->setText(tr(str.data()));
current_date_time = QDateTime::currentDateTime();
statusBar()->showMessage(tr("解压文件中..."));
double time_Start=(double)clock();
if(Extract(htfname,str)==false)
{
current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+"\t文件解压失败!\n");
QMessageBox::warning(this, tr("Warning"),
tr("文件解压失败!"));
current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
//ui->textEdit->append(current_date+"\tFaild to extrace huffman file\n");
statusBar()->showMessage(tr("解压失败"),3000);
}
else{
double time_End=(double)clock();
QString t=QString::number((time_End-time_Start)/1000,10,3);
current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+"\t文件解压成功,保存路径为 "+path+"\n");
QMessageBox::information(this,tr("提示"),tr("文件解压成功!"));
statusBar()->showMessage(tr("解压文件完成,用时")+t+tr("s"),3000);
}
}
void MainWindow::saveExtraceFile()
{
path = QFileDialog::getSaveFileName(this,
tr("保存文件"),
".",
"");
if(!path.isEmpty()) {
QFile file(path);
current_date_time = QDateTime::currentDateTime();
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+"\tCannot open file "+path+"\n");
QMessageBox::warning(this, tr("Write File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream out(&file);
out << ui->textEdit->toPlainText();
file.close();
} else {
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
ui->textEdit->append(current_date+"\t没有选择任何文件\n");
QMessageBox::warning(this, tr("警告"),
tr("没有选择任何文件!"));
}
}
void MainWindow::on_pushButton_clicked()
{
QFile logfile("log.txt");
current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
QString log=ui->textEdit->toPlainText();
if(!logfile.open(QIODevice::WriteOnly|QIODevice::Append))
{
ui->textEdit->append(current_date+"\t打开log.txt失败\n");
QMessageBox::warning(this, tr("Log"),
tr("打开log.txt失败"));
}
else
{
logfile.write(log.toUtf8());
logfile.close();
ui->textEdit->append(current_date+"\t成功保存信息到log.txt\n");
QMessageBox::warning(this, tr("Log"),
tr("成功保存log!"));
}
}
void MainWindow::on_pushButton_2_clicked()
{
QProcess* process = new QProcess();
QString notepadPath = "notepad.exe log.txt";
process->start(notepadPath);
}