code_app/app/scanner2/dialog_saveas.cpp

366 lines
9.0 KiB
C++

#include "dialog_saveas.h"
#include "ui_dialog_saveas.h"
#include "dialog_writesettings.h"
#include "base/HGBase.h"
#include "HGUIGlobal.h"
#include "app_cfg.h"
#include "lang/app_language.h"
#include <assert.h>
#include <qabstractproxymodel.h>
#include <QDateTime>
#include <QLineEdit>
#include <QMessageBox>
MyFileDialog::MyFileDialog(QWidget *parent)
: QFileDialog(parent)
{
}
MyFileDialog::~MyFileDialog()
{
}
void MyFileDialog::accept()
{
emit accepted();
}
Dialog_SaveAs::Dialog_SaveAs(bool isSaveAs, QWidget *parent) :
QDialog(parent)
, ui(new Ui::Dialog_SaveAs)
, m_isSaveAs(isSaveAs)
{
ui->setupUi(this);
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
if (!m_isSaveAs)
setWindowTitle(tr("Save"));
ui->fileDialog->setAcceptMode(QFileDialog::AcceptSave);
ui->fileDialog->setWindowFlags(ui->fileDialog->windowFlags() & ~Qt::Dialog);
ui->fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
ui->fileDialog->setOption(QFileDialog::ReadOnly, false); // disable 'Delete' menu item
ui->fileDialog->setSizeGripEnabled(false);
QObjectList objs(ui->fileDialog->children());
for(auto& o : objs)
{
if ("fileNameEdit" == o->objectName())
{
QLineEdit *edit = (QLineEdit *)o;
QRegExp rx("[^\\\\/:*?\"<>|]+$");
QRegExpValidator *pReg = new QRegExpValidator(rx, this);
edit->setValidator(pReg);
connect(edit, SIGNAL(editingFinished()), this, SLOT(on_editingFinished()));
}
}
bool MicroteckOcr = false;
#if defined (OEM_ZHONGJING) && defined (HG_CMP_MSC)
if (1258 == lang_get_cur_code_page() || 950 == lang_get_cur_code_page())
{
MicroteckOcr = true;
}
#endif
QStringList filters = { "JPG - JPEG / JFIF(*.jpg)",
"BMP - Windows Bitmap(*.bmp)",
"PNG - Portable Network Graphics(*.png)",
"PPM - Portable PixMap(*.ppm)",
"PGM - Portable GreyMap(*.pgm)",
"PBM - Portable BitMap(*.pbm)",
"TIF - TIFF Revision 6(*.tif)",
"PDF - Portable Document Format(*.pdf)",
"OFD - Open Fixed-layout Document(*.ofd)",
"GIF - Graphics Interchange Format(*.gif)" };
#if !defined (x86_64) && !defined (loongarch64)
filters.append("OCR->PDF - Portable Document Format(*.pdf)");
if (!MicroteckOcr)
{
filters.append("OCR->OFD - Open Fixed-layout Document(*.ofd)");
if (m_isSaveAs)
filters.append("OCR->RTF - Rich Text Format(*.rtf)");
}
#endif
#if defined (HG_CMP_MSC)
filters.append("TGA - Tagged Graphics(*.tga)");
filters.append("PCX - PC Paintbrush Exchange(*.pcx)");
filters.append("RAS - Sun Raster files(*.ras)");
#endif
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&)));
connect(ui->fileDialog, SIGNAL(currentChanged(const QString&)), this, SLOT(on_currentChanged(const QString&)));
m_suffix = getCfgValue("saveAs", "suffix", 0);
if (m_suffix > filters.count())
m_suffix = 0;
else if (m_suffix < 0)
m_suffix = 0;
ui->fileDialog->selectNameFilter(ui->fileDialog->nameFilters().at(m_suffix));
ui->btn_option->setEnabled(0 == m_suffix || 6 == m_suffix || 7 == m_suffix || 8 == m_suffix);
ui->cbox_subFolder->setChecked(false);
ui->cbox_subFolder->setChecked(getCfgValue("saveAs", "subFolderByTime", false));
ui->label_ocrType->setVisible(false);
ui->comboBox_ocrType->setVisible(false);
}
Dialog_SaveAs::~Dialog_SaveAs()
{
delete ui;
}
void Dialog_SaveAs::set_current_directory(const QString& dir)
{
ui->fileDialog->setDirectory(dir);
}
QString Dialog_SaveAs::getSavePath()
{
return m_savePath;
}
int Dialog_SaveAs::getJpegQuality()
{
return getCfgValue("saveParam", "jpegQuality", 80);
}
int Dialog_SaveAs::getTiffCompressionBW()
{
return getCfgValue("saveParam", "tiffCompBW", 1);
}
int Dialog_SaveAs::getTiffCompression()
{
return getCfgValue("saveParam", "tiffCompClr", 1);
}
int Dialog_SaveAs::getTiffQuality()
{
return getCfgValue("saveParam", "tiffQuality", 80);
}
bool Dialog_SaveAs::isOcr()
{
return (m_suffix >= 10 && m_suffix <= 12);
}
void Dialog_SaveAs::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 = ".ppm";
break;
case 4:
extName = ".pgm";
break;
case 5:
extName = ".pbm";
break;
case 6:
extName = ".tif";
break;
case 7:
extName = ".pdf";
break;
case 8:
extName = ".ofd";
break;
case 9:
extName = ".gif";
break;
case 10:
extName = ".pdf";
break;
case 11:
extName = ".ofd";
break;
case 12:
extName = ".rtf";
break;
case 13:
extName = ".tga";
break;
case 14:
extName = ".pcx";
break;
case 15:
extName = ".ras";
break;
}
assert(!extName.isEmpty());
QString selectedFile = ui->fileDialog->selectedFiles()[0];
QFileInfo selectFile(selectedFile);
if (selectFile.isDir())
{
ui->fileDialog->setDirectory(selectedFile);
return;
}
int pos = selectedFile.lastIndexOf('/');
if (-1 != pos)
{
QString path = selectedFile.left(pos + 1);
QString name = selectedFile.right(selectedFile.count() - pos - 1);
bool checked = ui->cbox_subFolder->isChecked();
if(checked)
{
QString dirName = NULL;
dirName = path + QDateTime::currentDateTime().toString("yy-MM-dd hh.mm.ss") + "/";
QDir dir(dirName);
if(!dir.exists())
{
dir.mkdir(dirName);
}
path = dirName;
}
#if defined (HG_CMP_MSC)
if (name.endsWith(extName, Qt::CaseInsensitive))
#else
if (name.endsWith(extName, Qt::CaseSensitive))
#endif
m_savePath = getStdFileName(path + name);
else
m_savePath = getStdFileName(path + name + extName);
}
QFile file(m_savePath);
if (file.exists())
{
QMessageBox msg(QMessageBox::Question, tr("Question"),
tr("The file already exists. Do you want to overwrite it?"),
QMessageBox::Yes | QMessageBox::No, this);
msg.exec();
if (msg.clickedButton() != msg.button(QMessageBox::Yes))
{
return;
}
}
saveCfgValue("saveAs", "suffix", m_suffix);
saveCfgValue("saveAs", "subFolderByTime", ui->cbox_subFolder->isChecked());
accept();
}
void Dialog_SaveAs::on_filterSelected(const QString& filterName)
{
m_suffix = ui->fileDialog->nameFilters().indexOf(filterName);
ui->btn_option->setEnabled(0 == m_suffix || 6 == m_suffix || 7 == m_suffix || 8 == m_suffix);
}
void Dialog_SaveAs::on_currentChanged(const QString& filePath)
{
QFileInfo file(filePath);
if (file.isDir())
{
ui->fileDialog->setAcceptMode(QFileDialog::AcceptOpen);
ui->fileDialog->setLabelText(QFileDialog::Accept, QFileDialog::tr("&Open"));
QObjectList objs(ui->fileDialog->children());
for (auto& o : objs)
{
if ("fileNameEdit" == o->objectName())
{
QLineEdit* edit = (QLineEdit*)o;
QString hhhh = edit->text();
edit->setText("");
}
}
}
else
{
ui->fileDialog->setAcceptMode(QFileDialog::AcceptSave);
ui->fileDialog->setLabelText(QFileDialog::Accept, QFileDialog::tr("&Save"));
}
}
void Dialog_SaveAs::on_btn_option_clicked()
{
Dialog_WriteSettings dlg(m_suffix, this);
dlg.exec();
}
void Dialog_SaveAs::on_editingFinished()
{
QString extName;
switch (m_suffix)
{
case 0:
extName = ".jpg";
break;
case 1:
extName = ".bmp";
break;
case 2:
extName = ".png";
break;
case 3:
extName = ".ppm";
break;
case 4:
extName = ".pgm";
break;
case 5:
extName = ".pbm";
break;
case 6:
extName = ".tif";
break;
case 7:
extName = ".pdf";
break;
case 8:
extName = ".ofd";
break;
case 9:
extName = ".gif";
break;
case 10:
extName = ".pdf";
break;
case 11:
extName = ".ofd";
break;
case 12:
extName = ".rtf";
break;
}
QLineEdit* lineEdit = qobject_cast<QLineEdit*>(sender());
QString fileName = lineEdit->text();
if (!fileName.endsWith(extName))
{
lineEdit->setText(fileName + extName);
}
}