#include "GBK2UTF8.h"
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtCore/QTextStream>
#include <QTextCodec>
#include <QKeyEvent>
#include <QtGlobal>
GBK2UTF8::GBK2UTF8(QWidget *parent) : QDialog(parent)
{
setObjectName(QString::fromUtf8("GBK2UTF8"));
resize(580, 480);
setMinimumSize(QSize(580, 480));
setMaximumSize(QSize(580, 480));
setBaseSize(QSize(580, 480));
setFocusPolicy(Qt::StrongFocus);
setWindowTitle(QString::fromUtf8("GBK2UTF8"));
textLabel = new QLabel(this);
textLabel->setObjectName(QString::fromUtf8("textLabel"));
textLabel->setGeometry(QRect(30, 10, 143, 20));
textLabel->setText(QObject::trUtf8("文件列表(注意备份)"));
FileList = new QListWidget(this);
FileList->setObjectName(QString::fromUtf8("FileList"));
FileList->setGeometry(QRect(29, 39, 391, 361));
FileList->setEditTriggers(QAbstractItemView::NoEditTriggers);
AddFile = new QPushButton(this);
AddFile->setObjectName(QString::fromUtf8("AddFile"));
AddFile->setGeometry(QRect(450, 60, 102, 24));
AddFile->setText(QObject::trUtf8("添加文件"));
AddDir = new QPushButton(this);
AddDir->setObjectName(QString::fromUtf8("AddDir"));
AddDir->setGeometry(QRect(450, 120, 102, 24));
AddDir->setText(QObject::trUtf8("添加目录"));
DelFile = new QPushButton(this);
DelFile->setObjectName(QString::fromUtf8("DelFile"));
DelFile->setGeometry(QRect(450, 180, 102, 24));
DelFile->setText(QObject::trUtf8("移除文件"));
DelAll = new QPushButton(this);
DelAll->setObjectName(QString::fromUtf8("DelAll"));
DelAll->setGeometry(QRect(450, 240, 102, 24));
DelAll->setText(QObject::trUtf8("全部移除"));
Start = new QPushButton(this);
Start->setObjectName(QString::fromUtf8("Start"));
Start->setGeometry(QRect(450, 300, 102, 24));
Start->setText(QObject::trUtf8("开始处理"));
Quit = new QPushButton(this);
Quit->setObjectName(QString::fromUtf8("Quit"));
Quit->setGeometry(QRect(450, 360, 102, 24));
Quit->setText(QObject::trUtf8("退出"));
this->setStyleSheet("QGroupBox { color: red; } ");
groupBox = new QGroupBox(this);
groupBox->setObjectName(QString::fromUtf8("groupBox"));
groupBox->setGeometry(QRect(70, 420, 310, 50));
groupBox->setFlat(false);
groupBox->setCheckable(false);
groupBox->setTitle(QObject::trUtf8("请务必确认文件原始编码方式,以免转换后出现乱码!"));
gbk_2_utf8 = new QRadioButton(groupBox);
gbk_2_utf8->setObjectName(QString::fromUtf8("gbk_2_utf8"));
gbk_2_utf8->setGeometry(QRect(20, 20, 135, 20));
gbk_2_utf8->setChecked(true);
gbk_2_utf8->setText(QObject::trUtf8("GBK转换为UTF-8"));
utf8_2_gbk = new QRadioButton(groupBox);
utf8_2_gbk->setObjectName(QString::fromUtf8("utf8_2_gbk"));
utf8_2_gbk->setGeometry(QRect(180, 20, 135, 20));
utf8_2_gbk->setText(QObject::trUtf8("UTF-8转换为GBK"));
Backup = new QCheckBox(this);
Backup->setObjectName(QString::fromUtf8("Backup"));
Backup->setGeometry(QRect(430, 440, 102, 24));
Backup->setText(QObject::trUtf8("保留文件备份"));
QObject::connect(AddFile, SIGNAL(clicked()), this, SLOT(addFile()));
QObject::connect(AddDir, SIGNAL(clicked()), this, SLOT(addDir()));
QObject::connect(DelFile, SIGNAL(clicked()), this, SLOT(deleteOne()));
QObject::connect(DelAll, SIGNAL(clicked()), this, SLOT(deleteAll()));
QObject::connect(Start, SIGNAL(clicked()), this, SLOT(startConvert()));
QObject::connect(Quit, SIGNAL(clicked()), this, SLOT(close()));
}
GBK2UTF8::~GBK2UTF8()
{
if(textLabel != NULL)
{
delete textLabel;
textLabel = NULL;
}
if(FileList != NULL)
{
delete FileList;
FileList = NULL;
}
if(AddFile != NULL)
{
delete AddFile;
AddFile = NULL;
}
if(AddDir != NULL)
{
delete AddDir;
AddDir = NULL;
}
if(DelFile != NULL)
{
delete DelFile;
DelFile = NULL;
}
if(DelAll != NULL)
{
delete DelAll;
DelAll = NULL;
}
if (Start != NULL)
{
delete Start;
Start = NULL;
}
if(Quit != NULL)
{
delete Quit;
Quit = NULL;
}
if(gbk_2_utf8 != NULL)
{
delete gbk_2_utf8;
gbk_2_utf8 = NULL;
}
if(utf8_2_gbk != NULL)
{
delete utf8_2_gbk;
utf8_2_gbk = NULL;
}
if(groupBox != NULL)
{
delete groupBox;
groupBox = NULL;
}
if(Backup != NULL)
{
delete Backup;
Backup = NULL;
}
}
void GBK2UTF8::addFile()
{
QStringList fileNames;
QString fileName;
QList<QListWidgetItem*> fileItem;
QString filters = "Java file(*.java *.jsp);;C file(*.c *.cpp *.h);;All files(*)";
QFileDialog *fileDialog = new QFileDialog(this, QObject::trUtf8("选择文件"), "/home", filters);
fileDialog->setFileMode(QFileDialog::ExistingFile);
fileDialog->setViewMode(QFileDialog::Detail);
fileDialog->setFilter(filters);
if(fileDialog->exec())
{
fileNames = fileDialog->selectedFiles();
for(QStringList::Iterator it = fileNames.begin();it != fileNames.end();it++)
{
fileName = *it;
fileItem = FileList->findItems(fileName,Qt::MatchExactly);
if(!fileItem.isEmpty()){
QMessageBox::warning(this, QObject::trUtf8("警告"),
QObject::trUtf8("该文件已经存在于列表中!\n"));
}
else
FileList->addItem(fileName);
}
}
delete fileDialog;
fileDialog = NULL;
}
void GBK2UTF8::addDir()
{
QString path;
QStringList pathList;
QList<QListWidgetItem*> pathItem;
QString filters = "Java file(*.java *.jsp);;C file(*.c *.cpp *.h);;All files(*)";
QFileDialog *fileDialog = new QFileDialog(this, QObject::trUtf8("选择目录"), "/home", filters);
fileDialog->setFileMode(QFileDialog::DirectoryOnly);
fileDialog->setViewMode(QFileDialog::Detail);
fileDialog->setFilter(filters);
if(fileDialog->exec())
{
pathList = fileDialog->selectedFiles();
for(QStringList::Iterator it = pathList.begin();it != pathList.end();it++)
{
path = *it;
pathItem = FileList->findItems(path,Qt::MatchContains);
if(!pathItem.isEmpty()){
QMessageBox::warning(this, QObject::trUtf8("警告"),
QObject::trUtf8("该目录中文件已经存在于列表中!\n"));
}
else{
if(path.right(1) == "/")
path.remove(path.length()-1,1);
traversalFolder(path);
}
}
}
delete fileDialog;
fileDialog = NULL;
}
void GBK2UTF8::deleteOne()
{
int row = FileList->currentRow();
if(row == -1){
QMessageBox::warning(this, QObject::trUtf8("警告"),
QObject::trUtf8("请选择一个要移去的文件!\n"));
}
else{
FileList->removeItemWidget(FileList->item(row));
}
}
void GBK2UTF8::deleteAll()
{
if(FileList->count() == 0){
QMessageBox::warning(this, QObject::trUtf8("警告"),
QObject::trUtf8("文件列表是空的!\n"));
}
else{
FileList->clear();
}
}
void GBK2UTF8::startConvert()
{
int i;
QListWidgetItem *item = NULL;
if(FileList->count() == 0){
QMessageBox::warning(this, QObject::trUtf8("警告"),
QObject::trUtf8("文件列表是空的,请先选择文件!\n"));
return;
}
if(gbk_2_utf8->isChecked())
{
for(i = 0;i < FileList->count();i++)
{
item = FileList->item(i);
changeCode(item->text(),true);
}
}
if(utf8_2_gbk->isChecked())
{
for(i = 0;i < FileList->count();i