-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
129 lines (99 loc) · 3.38 KB
/
widget.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
#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QFileInfo>
#include <QTextCodec>
#include <QTextStream>
#include <QApplication>
#include <QDir>
#include <QDirIterator>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
// 添加编码
QList<QByteArray> codecs = QTextCodec::availableCodecs();
QStringList items;
items << "UTF-8";
for (int i = 9; i < codecs.size(); i++) {
items << codecs.at(i);
}
ui->comboBoxCodecSource->clear();
ui->comboBoxCodecDest->clear();
ui->comboBoxCodecSource->addItems(items);
ui->comboBoxCodecSource->setCurrentText("GB18030");
ui->comboBoxCodecDest->addItems(items);
ui->comboBoxCodecDest->setCurrentText("UTF8");
m_strTrPath = "";
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btnChange_clicked()
{
QDir dir (m_strTrPath);
QDirIterator dirIterator(m_strTrPath,
QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot,
QDirIterator::Subdirectories);
ui->textBrowser->clear();
while (dirIterator.hasNext()) {
QStringList filters;
filters << "*.cpp" << "*.h";
dir.setPath(m_strTrPath);
ui->textBrowser->append(QString("<span style=\" color:#FF0000;\">进入目录:</span> [ %1 ] <br>").arg(m_strTrPath));
// 转换文件
foreach (QFileInfo fileInfo, dir.entryInfoList(filters)) {
// 转换编码
changeTextcodec(fileInfo.absoluteFilePath());
}
m_strTrPath = dirIterator.next();
}
}
void Widget::changeTextcodec(QString fileName)
{
QFile fileRead(fileName);
fileRead.open(QIODevice::ReadOnly);
QTextStream out(&fileRead);
out.setCodec(QTextCodec::codecForName(ui->comboBoxCodecSource->currentText().toLatin1()));
out.setAutoDetectUnicode(true);
QString strTemp;
QStringList strContents;
while (!out.atEnd()) {
strTemp = out.readLine();
strContents << strTemp;
}
fileRead.close();
QFile fielWrite(fileName);
if (!fielWrite.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
ui->labelStatus->setText("文件打开失败!");
return;
}
QTextStream in(&fielWrite);
in.setCodec(QTextCodec::codecForName(ui->comboBoxCodecDest->currentText().toLatin1()));
in.setAutoDetectUnicode(true);
for (int i = 0; i < strContents.size(); i++)
in << strContents.at(i) << endl;
fielWrite.close();
ui->textBrowser->append(QString(" <span style=\" color:#0000FF;\">转换完成:</span> [ %1 ] <br>").arg(fileName));
}
void Widget::on_btnProject_clicked()
{
static QString strDir = "d:/";
strDir = QFileDialog::getExistingDirectory(this, tr("选择工程"),
strDir,
QFileDialog::ShowDirsOnly |
QFileDialog::DontResolveSymlinks);
// 选择工程目录
if (!strDir.isEmpty()) {
m_strTrPath = strDir;
ui->labelStatus->setText(QString("选择目录[%1]").arg(strDir));
}
else {
strDir = "d:/";
}
}