#include "PictureConvert.h"
#include <QMessageBox>
PictureConvert::PictureConvert(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
filter = ".jpg";
jpgRadioButton->setChecked(true);
setWindowTitle(tr("图片格式批量转换器"));
infoLabel->setText(tr("\tReady..."));
initConnect();
}
void PictureConvert::initConnect()
{
connect(openPushButton, SIGNAL(clicked()), this, SLOT(desPath()));
connect(browsePushButton, SIGNAL(clicked()), this, SLOT(srcPath()));
connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
connect(startConvertPushButton, SIGNAL(clicked()),
this, SLOT(startConvert()));
connect(desComboBox, SIGNAL(editTextChanged(const QString&)),
this, SLOT(comboBoxChanged(const QString&)));
connect(bmpRadioButton, SIGNAL(clicked()), this, SLOT(doSelect()));
connect(pngRadioButton, SIGNAL(clicked()), this, SLOT(doSelect()));
connect(jpgRadioButton, SIGNAL(clicked()), this, SLOT(doSelect()));
connect(jpegRadioButton, SIGNAL(clicked()), this, SLOT(doSelect()));
connect(ppmRadioButton, SIGNAL(clicked()), this, SLOT(doSelect()));
connect(xpmRadioButton, SIGNAL(clicked()), this, SLOT(doSelect()));
connect(xbmRadioButton, SIGNAL(clicked()), this, SLOT(doSelect()));
}
void PictureConvert::desPath()
{
QString tempPath;
tempPath = srcComboBox->currentText();
if (tempPath.isEmpty())
{
outDir = QFileDialog::getExistingDirectory(this,
tr("保存的文件夹"), QDir::currentPath());
}
else
{
QDir dir(tempPath);
outDir = QFileDialog::getExistingDirectory(this,
tr("保存的文件夹"), dir.absolutePath());
}
desComboBox->setEditText(outDir);
}
void PictureConvert::srcPath()
{
QString dirName;
QString info;
inFiles = QFileDialog::getOpenFileNames(this, tr("选择要转换的文件"),
QDir::currentPath(), tr("*.bmp *.png *.jpg *.ico"));
if (!inFiles.isEmpty())
{
QDir dir(inFiles.at(0));
dirName = dir.path();
int i = dirName.lastIndexOf("/");
dirName.remove(i+1, dirName.length() - i -1);
srcComboBox->insertItem(0, dirName);
srcComboBox->setCurrentIndex(0);
}
info = tr("共有需要转换的文件 %1 个").arg(inFiles.size());
infoLabel->setText(info);
infoLabel->update();
}
void PictureConvert::doSelect()
{
QRadioButton *radioBtn = (QRadioButton *)sender();
if (radioBtn == bmpRadioButton)
{
filter = ".bmp";
bmpRadioButton->setChecked(true);
}
else if (radioBtn == jpegRadioButton)
{
filter = ".jpeg";
jpegRadioButton->setChecked(true);
}
else if (radioBtn == jpgRadioButton)
{
filter = ".jpg";
jpgRadioButton->setChecked(true);
}
else if (radioBtn == pngRadioButton)
{
filter = ".png";
pngRadioButton->setChecked(true);
}
else if (radioBtn == ppmRadioButton)
{
filter = ".ppm";
ppmRadioButton->setChecked(true);
}
else if (radioBtn == xbmRadioButton)
{
filter = ".xbm";
xbmRadioButton->setChecked(true);
}
else if (radioBtn == xpmRadioButton)
{
filter = ".xpm";
xpmRadioButton->setChecked(true);
}
}
void PictureConvert::comboBoxChanged(const QString &outDir)
{
this->outDir = outDir;
}
void PictureConvert::startConvert()
{
QString newFilePath;
int count = 0;
infoLabel->setText(tr("正在转换,请耐心等待..."));
foreach (QString file, inFiles)
{
QString tempFileName;
QPixmap newPicture;
bool ok = false;
QDir dir(file);
int i;
newPicture.load(file);
tempFileName = dir.dirName();
i = tempFileName.lastIndexOf(".");
tempFileName.remove(i, tempFileName.length() - i);
newFilePath += outDir + "/" + tempFileName + filter;
if (QFile::exists(newFilePath))
{
QMessageBox box(QMessageBox::Warning, tr("警告"),
tr("存在同名文件%1\n是否覆盖?").arg(newFilePath),
QMessageBox::Yes | QMessageBox::No);
if (box.exec() == QMessageBox::No)
{
continue;
}
}
ok = newPicture.save(newFilePath);
if (ok)
{
count++;
}
newFilePath.clear();
qApp->processEvents();
}
infoLabel->setText(tr("\tReady..."));
QMessageBox box(QMessageBox::Information,
tr("转换完成"), tr("共完成%1个文件的转换").arg(count));
box.exec();
}