code_app/app/scanner/dialog_export.cpp

635 lines
20 KiB
C++

#include "dialog_export.h"
#include "ui_dialog_export.h"
#include <QButtonGroup>
#include <QMessageBox>
#include <algorithm>
#include <QRegExpValidator>
#include "dialog_writesettings.h"
#include "HGUIGlobal.h"
#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[] =
{
#if defined(OEM_LISICHENG) || defined(OEM_CANGTIAN) || defined(x86_64)
{".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}
, {".ppm", "PPM - Portable PixMap", FILE_TYPE_ATTR_NO_MORE_OPTION}
, {".pgm", "PGM - Portable GreyMap", FILE_TYPE_ATTR_NO_MORE_OPTION}
, {".pbm", "PBM - Portable BitMap", 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}
, {".gif", "GIF - Graphics Interchange Format", FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES}
#else
{".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}
, {".ppm", "PPM - Portable PixMap", FILE_TYPE_ATTR_NO_MORE_OPTION}
, {".pgm", "PGM - Portable GreyMap", FILE_TYPE_ATTR_NO_MORE_OPTION}
, {".pbm", "PBM - Portable BitMap", 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}
, {".gif", "GIF - Graphics Interchange Format", FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES}
, {".pdf", "OCR->PDF - Portable Document Format", FILE_TYPE_ATTR_THIRD_HANDLER | FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES}
, {".ofd", "OCR->OFD - Open Fixed-layout Document", FILE_TYPE_ATTR_THIRD_HANDLER | FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES}
, {".rtf", "OCR->RTF - Rich Text Format", FILE_TYPE_ATTR_THIRD_HANDLER | FILE_TYPE_ATTR_CONTAIN_MULTI_PAGES}
#endif
};
Dialog_Export::Dialog_Export(int total, const std::vector<int> &selectedIndexs, QWidget *parent) :
QDialog(parent)
, ui(new Ui::Dialog_Export)
, m_total(total)
, m_selectedIndexs(selectedIndexs)
{
ui->setupUi(this);
ui->fileDialog->setAcceptMode(QFileDialog::AcceptSave);
ui->fileDialog->setWindowFlags(ui->fileDialog->windowFlags() & ~Qt::Dialog);
ui->fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
ui->fileDialog->setOption(QFileDialog::ReadOnly, true); // disable 'Delete' menu item
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)");
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()));
connect(ui->fileDialog, SIGNAL(filterSelected(const QString&)), this, SLOT(on_filterSelected(const QString&)));
#ifdef USE_FILE_DLG_WITHOUT_PROMPT
init_custom_file_dlg(ui->fileDialog);
#endif
QButtonGroup* btn_group = new QButtonGroup(this);
btn_group->addButton(ui->radio_chosenPages, 0);
btn_group->addButton(ui->radio_allPages, 1);
btn_group->addButton(ui->radio_nominatedPages, 2);
QRegExp rx("[0-9,-]+$");
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));
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(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)
{
ui->radio_chosenPages->setChecked(true);
ui->lab_nominatePages->setEnabled(false);
ui->lineEdit_nominatePages->setEnabled(false);
ui->lab_warning->setVisible(false);
}
else if (1 == exportType)
{
ui->radio_allPages->setChecked(true);
ui->lab_nominatePages->setEnabled(false);
ui->lineEdit_nominatePages->setEnabled(false);
ui->lab_warning->setVisible(false);
}
else
{
ui->radio_nominatedPages->setChecked(true);
ui->lab_nominatePages->setEnabled(true);
ui->lineEdit_nominatePages->setEnabled(true);
ui->lab_warning->setVisible(true);
ui->lab_warning->setText(tr("Input is not valid.\nPage range will be 'Chosen Pages'."));
}
}
Dialog_Export::~Dialog_Export()
{
delete ui;
}
QString Dialog_Export::getSavePath()
{
return m_savePath;
}
QString Dialog_Export::getSaveName()
{
return m_saveName;
}
QString Dialog_Export::getSaveExt()
{
return m_saveExt;
}
int Dialog_Export::getJpegQuality()
{
return getCfgValue("saveParam", "jpegQuality", 80);
}
int Dialog_Export::getTiffCompressionBW()
{
return getCfgValue("saveParam", "tiffCompBW", 1);
}
int Dialog_Export::getTiffCompression()
{
return getCfgValue("saveParam", "tiffCompClr", 1);
}
int Dialog_Export::getTiffQuality()
{
return getCfgValue("saveParam", "tiffQuality", 80);
}
bool Dialog_Export::isOcr()
{
// 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()
{
return ui->check_saveAsMulti->isChecked();
}
void Dialog_Export::getSaveIndexs(std::vector<int> &indexs)
{
indexs = m_saveIndexs;
}
void Dialog_Export::on_dialog_accepted()
{
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)
{
QString path = selectedFile.left(pos + 1);
QString name = selectedFile.right(selectedFile.count() - pos - 1);
pos = name.lastIndexOf('.');
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;
m_saveExt = extName;
}
makeSaveIndexs();
// 同名警告
if(warning && avoid_overwriting(m_savePath + "/" + m_saveName + m_saveExt))
return;
saveCfgValue("export", "suffix", m_suffix);
int exportType = 0;
if (ui->radio_chosenPages->isChecked())
exportType = 0;
else if (ui->radio_allPages->isChecked())
exportType = 1;
else if (ui->radio_nominatedPages->isChecked())
exportType = 2;
saveCfgValue("export", "exportType", exportType);
saveCfgValue("export", "saveAsMulti", ui->check_saveAsMulti->isChecked());
accept();
}
void Dialog_Export::on_filterSelected(const QString& filterName)
{
bool enabled = false;
m_suffix = ui->fileDialog->nameFilters().indexOf(filterName);
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);
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()
{
Dialog_WriteSettings dlg(m_suffix, this);
dlg.exec();
}
void Dialog_Export::on_radio_nominatedPages_toggled(bool checked)
{
ui->lab_nominatePages->setEnabled(checked);
ui->lineEdit_nominatePages->setEnabled(checked);
if (!checked)
{
ui->lab_warning->setVisible(false);
}
else
{
bool isVaild = isNominatedPagesLegal(ui->lineEdit_nominatePages->text());
ui->lab_warning->setVisible(!isVaild);
if (!isVaild)
ui->lab_warning->setText(tr("Input is not valid.\nPage range will be 'Chosen Pages'."));
}
}
void Dialog_Export::on_lineEdit_nominatePages_textChanged(const QString& arg1)
{
(void)arg1;
bool isVaild = isNominatedPagesLegal(ui->lineEdit_nominatePages->text());
ui->lab_warning->setVisible(!isVaild);
if (!isVaild)
ui->lab_warning->setText(tr("Input is not valid.\nPage range will be 'Chosen Pages'."));
}
#ifdef USE_FILE_DLG_WITHOUT_PROMPT
void Dialog_Export::on_file_dialog_textChanged(const QString &path)
{
QLineEdit* edit = dynamic_cast<QLineEdit*>(sender());
on_file_name_changed(edit, path);
}
void Dialog_Export::on_file_dialog_returnPressed()
{
on_file_name_press_return();
}
#endif
bool Dialog_Export::isNominatedPagesLegal(const QString& page)
{
if (page.isEmpty())
{
return false;
}
QStringList commalist = page.split(',');
for (int i = 0; i < commalist.size(); ++i)
{
QString number1, number2;
bool setNumber2 = false;
for (int j = 0; j < commalist[i].size(); j++)
{
if (number1.isEmpty() && number2.isEmpty())
{
if (commalist[i][j] == '-')
{
return false;
}
else
{
number1.append(commalist[i][j]);
}
}
else if (!number1.isEmpty() && number2.isEmpty())
{
if (commalist[i][j] == '-')
{
if (setNumber2)
{
return false;
}
else
{
setNumber2 = true;
}
}
else
{
if (!setNumber2)
{
number1.append(commalist[i][j]);
}
else
{
number2.append(commalist[i][j]);
}
}
}
else
{
assert(!number1.isEmpty() && !number2.isEmpty());
assert(setNumber2);
if (commalist[i][j] == '-')
{
return false;
}
else
{
number2.append(commalist[i][j]);
}
}
}
if (number1.isEmpty())
{
return false;
}
if (setNumber2 && number2.isEmpty())
{
return false;
}
if (!number1.isEmpty() && number2.isEmpty())
{
int num1 = number1.toInt();
if (num1 < 1 || num1 > m_total)
{
return false;
}
}
else
{
assert(!number1.isEmpty() && !number2.isEmpty());
int num1 = number1.toInt();
int num2 = number2.toInt();
if (num1 < 1 || num1 > m_total || num2 < 1 || num2 > m_total || num1 > num2)
{
return false;
}
}
}
return true;
}
static bool indexSortCompare(int iter1, int iter2)
{
return iter1 < iter2;
}
static bool indexUniqueCompare(int iter1, int iter2)
{
return iter1 == iter2;
}
void Dialog_Export::makeSaveIndexs()
{
if (ui->radio_chosenPages->isChecked())
{
m_saveIndexs = m_selectedIndexs;
}
else if (ui->radio_allPages->isChecked())
{
for (int i = 0; i < m_total; ++i)
{
m_saveIndexs.push_back(i);
}
}
else
{
assert(ui->radio_nominatedPages->isChecked());
QString page = ui->lineEdit_nominatePages->text();
if (!page.isEmpty())
{
bool success = true;
QStringList commalist = page.split(',');
for (int i = 0; i < commalist.size(); ++i)
{
bool ret = true;
QString number1, number2;
bool setNumber2 = false;
for (int j = 0; j < commalist[i].size(); j++)
{
if (number1.isEmpty() && number2.isEmpty())
{
if (commalist[i][j] == '-')
{
ret = false;
break;
}
else
{
number1.append(commalist[i][j]);
}
}
else if (!number1.isEmpty() && number2.isEmpty())
{
if (commalist[i][j] == '-')
{
if (setNumber2)
{
ret = false;
break;
}
else
{
setNumber2 = true;
}
}
else
{
if (!setNumber2)
{
number1.append(commalist[i][j]);
}
else
{
number2.append(commalist[i][j]);
}
}
}
else
{
assert(!number1.isEmpty() && !number2.isEmpty());
assert(setNumber2);
if (commalist[i][j] == '-')
{
ret = false;
break;
}
else
{
number2.append(commalist[i][j]);
}
}
}
if (!ret)
{
success = false;
break;
}
if (number1.isEmpty())
{
ret = false;
}
if (setNumber2 && number2.isEmpty())
{
ret = false;
}
if (!number1.isEmpty() && number2.isEmpty())
{
int num1 = number1.toInt();
if (num1 < 1 || num1 > m_total)
{
ret = false;
}
else
{
m_saveIndexs.push_back(num1 - 1);
}
}
else
{
assert(!number1.isEmpty() && !number2.isEmpty());
int num1 = number1.toInt();
int num2 = number2.toInt();
if (num1 < 1 || num1 > m_total || num2 < 1 || num2 > m_total || num1 > num2)
{
ret = false;
}
else
{
for (int k = num1; k <= num2; ++k)
{
m_saveIndexs.push_back(k - 1);
}
}
}
if (!ret)
{
success = false;
break;
}
}
if (!success)
{
m_saveIndexs = m_selectedIndexs;
}
}
else
{
m_saveIndexs = m_selectedIndexs;
}
}
// 排序和去重
std::sort(m_saveIndexs.begin(), m_saveIndexs.end(), indexSortCompare);
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;
}