修复文件名最后一个.后面的字符被当作扩展名的问题

This commit is contained in:
gb 2022-05-26 14:33:48 +08:00
parent f526122c2b
commit 684e8c8276
3 changed files with 151 additions and 45 deletions

View File

@ -230,9 +230,19 @@ protected:
new_->setText(text);
else if(edit_)
{
QString ext(text), sel_ext(((T*)this)->getSaveExt());
int pos = ext.lastIndexOf('.');
list_->clearSelection();
edit_->setText(text);
edit_->setSelection(0, text.length());
if(pos == -1)
edit_->setText(text + sel_ext);
else {
ext = ext.right(ext.length() - pos);
if(ext.compare(sel_ext, Qt::CaseInsensitive) == 0)
edit_->setText(text);
else
edit_->setText(text + sel_ext);
}
}
if(save_)

View File

@ -9,6 +9,32 @@
#include "app_cfg.h"
#include <assert.h>
#define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0])
enum file_type_attr
{
FILE_TYPE_ATTR_NO_MORE_OPTION = 0x0,
FILE_TYPE_ATTR_MORE_OPTIONS = 0x01 << 0, // this type supports more-parameters
FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES = 0x01 << 1, // this type supports multi-images to be saved into one file
FILE_TYPE_ATTR_THIRD_HANDLER = 0x01 << 2, // this type supports third-handling methods
};
static struct
{
QString ext;
QString title;
int attr;
}g_support_type[] =
{
{".jpg", "JPG - JPEG / JFIF", FILE_TYPE_ATTR_MORE_OPTIONS}
, {".bmp", "BMP - Windows Bitmap", FILE_TYPE_ATTR_NO_MORE_OPTION}
, {".png", "PNG - Portable Network Graphics", FILE_TYPE_ATTR_NO_MORE_OPTION}
, {".tif", "TIF - TIFF Revision 6", FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES | FILE_TYPE_ATTR_MORE_OPTIONS}
, {".pdf", "PDF - Portable Document Format", FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES}
, {".ofd", "OFD - Open Fixed-layout Document", FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES}
, {".pdf", "OCR->PDF - Portable Document Format", FILE_TYPE_ATTR_THIRD_HANDLER}
, {".ofd", "OCR->OFD - Open Fixed-layout Document", FILE_TYPE_ATTR_THIRD_HANDLER}
};
Dialog_Export::Dialog_Export(int total, const std::vector<int> &selectedIndexs, QWidget *parent) :
QDialog(parent)
, ui(new Ui::Dialog_Export)
@ -22,14 +48,18 @@ Dialog_Export::Dialog_Export(int total, const std::vector<int> &selectedIndexs,
ui->fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
ui->fileDialog->setSizeGripEnabled(false);
ui->fileDialog->setNameFilter("JPG - JPEG / JFIF(*.jpg);;"
"BMP - Windows Bitmap(*.bmp);;"
"PNG - Portable Network Graphics(*.png);;"
"TIF - TIFF Revision 6(*.tif);;"
"PDF - Portable Document Format(*.pdf);;"
"OFD - Open Fixed-layout Document(*.ofd);;"
"OCR->PDF - Portable Document Format(*.pdf);;"
"OCR->OFD - Open Fixed-layout Document(*.ofd)");
// ui->fileDialog->setNameFilter("JPG - JPEG / JFIF(*.jpg);;"
// "BMP - Windows Bitmap(*.bmp);;"
// "PNG - Portable Network Graphics(*.png);;"
// "TIF - TIFF Revision 6(*.tif);;"
// "PDF - Portable Document Format(*.pdf);;"
// "OFD - Open Fixed-layout Document(*.ofd);;"
// "OCR->PDF - Portable Document Format(*.pdf);;"
// "OCR->OFD - Open Fixed-layout Document(*.ofd)");
QStringList filters;
for(int i = 0; i < ARRAY_SIZE(g_support_type); ++i)
filters.push_back(g_support_type[i].title + "(*" + g_support_type[i].ext + ")");
ui->fileDialog->setNameFilters(filters);
connect(ui->fileDialog, SIGNAL(accepted()), this, SLOT(on_dialog_accepted()));
connect(ui->fileDialog, SIGNAL(rejected()), this, SLOT(close()));
@ -48,10 +78,15 @@ Dialog_Export::Dialog_Export(int total, const std::vector<int> &selectedIndexs,
ui->lineEdit_nominatePages->setValidator(new QRegExpValidator(rx, this));
m_suffix = getCfgValue("export", "suffix", 0);
if(m_suffix < 0 || m_suffix >= ARRAY_SIZE(g_support_type))
m_suffix = 0;
ui->fileDialog->selectNameFilter(ui->fileDialog->nameFilters().at(m_suffix));
ui->check_saveAsMulti->setEnabled(m_suffix > 2 && m_suffix < 6);
m_saveExt = g_support_type[m_suffix].ext;
// ui->check_saveAsMulti->setEnabled(m_suffix > 2 && m_suffix < 6);
ui->check_saveAsMulti->setEnabled((g_support_type[m_suffix].attr & FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES) == FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES);
ui->check_saveAsMulti->setChecked(getCfgValue("export", "saveAsMulti", false));
ui->btn_option->setEnabled(0 == m_suffix || 3 == m_suffix);
// ui->btn_option->setEnabled(m_suffix == 0 || m_suffix == 3);
ui->btn_option->setEnabled((g_support_type[m_suffix].attr & FILE_TYPE_ATTR_MORE_OPTIONS) == FILE_TYPE_ATTR_MORE_OPTIONS);
int exportType = getCfgValue("export", "exportType", 0);
if (0 == exportType)
@ -120,7 +155,8 @@ int Dialog_Export::getTiffQuality()
bool Dialog_Export::isOcr()
{
return (m_suffix >= 6);
// return (m_suffix >= 6);
return (g_support_type[m_suffix].attr & FILE_TYPE_ATTR_THIRD_HANDLER) == FILE_TYPE_ATTR_THIRD_HANDLER;
}
bool Dialog_Export::isSaveAsMultiPage()
@ -135,37 +171,38 @@ void Dialog_Export::getSaveIndexs(std::vector<int> &indexs)
void Dialog_Export::on_dialog_accepted()
{
QString extName;
switch (m_suffix)
{
case 0:
extName = ".jpg";
break;
case 1:
extName = ".bmp";
break;
case 2:
extName = ".png";
break;
case 3:
extName = ".tif";
break;
case 4:
extName = ".pdf";
break;
case 5:
extName = ".ofd";
break;
case 6:
extName = ".pdf";
break;
case 7:
extName = ".ofd";
break;
}
QString extName(g_support_type[m_suffix].ext);
// switch (m_suffix)
// {
// case 0:
// extName = ".jpg";
// break;
// case 1:
// extName = ".bmp";
// break;
// case 2:
// extName = ".png";
// break;
// case 3:
// extName = ".tif";
// break;
// case 4:
// extName = ".pdf";
// break;
// case 5:
// extName = ".ofd";
// break;
// case 6:
// extName = ".pdf";
// break;
// case 7:
// extName = ".ofd";
// break;
// }
assert(!extName.isEmpty());
bool warning = true;
QString selectedFile = ui->fileDialog->selectedFiles()[0];
int pos = selectedFile.lastIndexOf('/');
if (-1 != pos)
@ -173,8 +210,12 @@ void Dialog_Export::on_dialog_accepted()
QString path = selectedFile.left(pos + 1);
QString name = selectedFile.right(selectedFile.count() - pos - 1);
pos = name.lastIndexOf('.');
if (-1 != pos)
if (-1 != pos
&& is_support_file_type(name.right(name.length() - pos)))
{
warning = false; // 带扩展名,系统已经警告同名文件
name = name.left(pos);
}
m_savePath = getStdFileName(path);
m_saveName = name;
@ -183,6 +224,10 @@ void Dialog_Export::on_dialog_accepted()
makeSaveIndexs();
// 同名警告
if(warning && avoid_overwriting(m_savePath + "/" + m_saveName + m_saveExt))
return;
saveCfgValue("export", "suffix", m_suffix);
int exportType = 0;
@ -200,12 +245,20 @@ void Dialog_Export::on_dialog_accepted()
void Dialog_Export::on_filterSelected(const QString& filterName)
{
bool enabled = false;
m_suffix = ui->fileDialog->nameFilters().indexOf(filterName);
ui->check_saveAsMulti->setEnabled(m_suffix > 2 && m_suffix < 6);
if (m_suffix <= 2 || m_suffix >= 6)
enabled = (g_support_type[m_suffix].attr & FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES) == FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES;
ui->check_saveAsMulti->setEnabled(enabled);
// ui->check_saveAsMulti->setEnabled(m_suffix > 2 && m_suffix < 6);
// if (m_suffix <= 2 || m_suffix >= 6)
if(!enabled)
ui->check_saveAsMulti->setChecked(false);
ui->btn_option->setEnabled(0 == m_suffix || 3 == m_suffix);
enabled = (g_support_type[m_suffix].attr & FILE_TYPE_ATTR_MORE_OPTIONS) == FILE_TYPE_ATTR_MORE_OPTIONS;
// ui->btn_option->setEnabled(0 == m_suffix || 3 == m_suffix);
ui->btn_option->setEnabled(enabled);
m_saveExt = g_support_type[m_suffix].ext;
}
void Dialog_Export::on_btn_option_clicked()
@ -519,3 +572,44 @@ void Dialog_Export::makeSaveIndexs()
std::vector<int>::iterator iter = std::unique(m_saveIndexs.begin(), m_saveIndexs.end(), indexUniqueCompare);
m_saveIndexs.erase(iter, m_saveIndexs.end());
}
bool Dialog_Export::is_support_file_type(const QString& ext)
{
return ext.compare(g_support_type[m_suffix].ext, Qt::CaseInsensitive) == 0;
for(int i = 0; i < ARRAY_SIZE(g_support_type); ++i)
{
if(ext.compare(g_support_type[i].ext, Qt::CaseInsensitive) == 0)
return true;
}
return false;
}
bool Dialog_Export::avoid_overwriting(const QString& path_file)
{
return false; // 不能阻止文件对话框关闭,暂时不做处理
bool ret = false;
QFileInfo fi(path_file);
if(fi.exists())
{
// 单个文件才判断
if((ui->radio_allPages->isChecked() && m_total == 1) ||
(ui->radio_chosenPages->isChecked() && m_saveIndexs.size() == 1) ||
(ui->radio_nominatedPages->isChecked() && m_saveIndexs.size() == 1))
{
QString title(QString::fromStdString("\345\257\274\345\207\272\344\270\272")), info("");
int pos = path_file.lastIndexOf('/');
if(pos++ == -1)
info = path_file;
else
info = path_file.right(path_file.length() - pos);
info += QString::fromStdString("\345\267\262\347\273\217\345\255\230\345\234\250\357\274\214\n\346\202\250\346\203\263\350\246\201\346\233\277\346\215\242\345\256\203\345\220\227\357\274\237");
ret = QMessageBox::question(this, title, info) == QMessageBox::No;
}
}
return ret;
}

View File

@ -49,6 +49,8 @@ private slots:
private:
bool isNominatedPagesLegal(const QString& page);
void makeSaveIndexs();
bool is_support_file_type(const QString& ext); // ext is like as '.bmp' '.png' ...
bool avoid_overwriting(const QString& path_file); // 判断文件是否存在若存在则提示用户是否覆盖。返回true表示用户希望重新选择
private:
Ui::Dialog_Export *ui;