code_app/app/scanner2/dialog_saveas.cpp

344 lines
9.1 KiB
C++
Raw Normal View History

#include "dialog_saveas.h"
2022-05-03 10:25:52 +00:00
#include "ui_dialog_saveas.h"
#include "dialog_writesettings.h"
2023-02-17 01:20:12 +00:00
#include "base/HGBase.h"
2022-05-03 10:25:52 +00:00
#include "HGUIGlobal.h"
#include "app_cfg.h"
#include <assert.h>
#include <qabstractproxymodel.h>
#include <QDateTime>
#include <QLineEdit>
class del_delegate : public QAbstractProxyModel
{
QAbstractProxyModel* prev_ = nullptr;
QFileDialog* dlg_ = nullptr;
public:
del_delegate(QFileDialog* fd) : dlg_(fd)
{
prev_ = dlg_->proxyModel();
dlg_->setProxyModel(this);
dlg_->setOption(QFileDialog::Option::ReadOnly, true);
}
~del_delegate()
{
dlg_->setProxyModel(prev_);
prev_ = nullptr;
}
public:
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
{
return prev_->removeRows(row, count, parent);
}
Q_INVOKABLE virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
{
return prev_->mapToSource(proxyIndex);
}
Q_INVOKABLE virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
{
return prev_->mapFromSource(sourceIndex);
}
Q_INVOKABLE virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
return prev_->rowCount(parent);
}
Q_INVOKABLE virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override
{
return prev_->columnCount(parent);
}
Q_INVOKABLE virtual QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override
{
return prev_->index(row, column, parent);
}
Q_INVOKABLE virtual QModelIndex parent(const QModelIndex &child) const override
{
return prev_->parent(child);
}
};
2022-05-03 10:25:52 +00:00
Dialog_SaveAs::Dialog_SaveAs(bool isSaveAs, QWidget *parent) :
2022-05-03 10:25:52 +00:00
QDialog(parent)
, ui(new Ui::Dialog_SaveAs)
, m_isSaveAs(isSaveAs)
2022-05-03 10:25:52 +00:00
{
ui->setupUi(this);
2023-03-08 06:32:31 +00:00
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
2022-05-03 10:25:52 +00:00
if (!m_isSaveAs)
setWindowTitle(tr("Save"));
2022-05-03 10:25:52 +00:00
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
2022-05-03 10:25:52 +00:00
ui->fileDialog->setSizeGripEnabled(false);
2023-02-17 01:20:12 +00:00
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()));
}
}
2023-02-17 01:20:12 +00:00
QString filter = "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 0
2023-02-17 01:20:12 +00:00
if (!getOcrPath().empty())
filter += ";;OCR->PDF - Portable Document Format(*.pdf)";
#elif !defined (x86_64) && !defined (loongarch64)
2023-02-17 01:20:12 +00:00
filter += ";;OCR->PDF - Portable Document Format(*.pdf)";
filter += ";;OCR->OFD - Open Fixed-layout Document(*.ofd)";
if (m_isSaveAs)
filter += ";;OCR->RTF - Rich Text Format(*.rtf)";
2022-07-18 11:17:26 +00:00
#endif
2022-05-03 10:25:52 +00:00
2023-02-17 01:20:12 +00:00
ui->fileDialog->setNameFilter(filter);
2022-05-03 10:25:52 +00:00
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&)));
m_suffix = getCfgValue("saveAs", "suffix", 0);
2022-11-06 10:13:08 +00:00
if (m_suffix > 10)
m_suffix = 10;
2022-05-16 09:00:08 +00:00
else if (m_suffix < 0)
m_suffix = 0;
2022-05-03 10:25:52 +00:00
ui->fileDialog->selectNameFilter(ui->fileDialog->nameFilters().at(m_suffix));
2023-08-01 06:24:42 +00:00
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);
#if 0
if (!getOcrPath().empty())
{
ui->label_ocrType->setVisible(isOcr());
ui->comboBox_ocrType->setVisible(isOcr());
QStringList ocrType = { tr("English"), tr("Simplified Chinese"), tr("Traditional Chinese"), tr("Japanese"), tr("Korean") };
ui->comboBox_ocrType->addItems(ocrType);
ui->comboBox_ocrType->setCurrentIndex(getCfgValue("saveAs", "ocrLanguage", 0));
}
#endif
2022-05-03 10:25:52 +00:00
}
Dialog_SaveAs::~Dialog_SaveAs()
{
delete ui;
}
2022-06-02 10:27:14 +00:00
void Dialog_SaveAs::set_current_directory(const QString& dir)
{
ui->fileDialog->setDirectory(dir);
}
2022-05-03 10:25:52 +00:00
QString Dialog_SaveAs::getSavePath()
{
return m_savePath;
}
int Dialog_SaveAs::getJpegQuality()
2022-05-03 10:25:52 +00:00
{
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);
2022-05-03 10:25:52 +00:00
}
2022-05-16 09:00:08 +00:00
bool Dialog_SaveAs::isOcr()
{
2022-11-06 10:13:08 +00:00
return (m_suffix >= 10);
2022-05-16 09:00:08 +00:00
}
2022-05-03 10:25:52 +00:00
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:
2022-11-06 10:13:08 +00:00
extName = ".ppm";
2022-05-03 10:25:52 +00:00
break;
case 4:
2022-11-06 10:13:08 +00:00
extName = ".pgm";
2022-05-03 10:25:52 +00:00
break;
case 5:
2022-11-06 10:13:08 +00:00
extName = ".pbm";
2022-05-03 10:25:52 +00:00
break;
2022-05-16 09:00:08 +00:00
case 6:
2022-11-06 10:13:08 +00:00
extName = ".tif";
2022-05-16 09:00:08 +00:00
break;
case 7:
extName = ".pdf";
break;
case 8:
2022-05-16 09:00:08 +00:00
extName = ".ofd";
break;
2022-09-02 05:08:51 +00:00
case 9:
2022-11-06 10:13:08 +00:00
extName = ".gif";
break;
case 10:
extName = ".pdf";
break;
case 11:
extName = ".ofd";
break;
case 12:
2022-09-02 05:08:51 +00:00
extName = ".rtf";
break;
2022-05-03 10:25:52 +00:00
}
assert(!extName.isEmpty());
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);
bool checked = ui->cbox_subFolder->isChecked();
2022-08-24 03:03:41 +00:00
if(checked)
{
2022-08-24 03:03:41 +00:00
QString dirName = NULL;
dirName = path + QDateTime::currentDateTime().toString("yy-MM-dd hh.mm.ss");
QDir dir(dirName);
if(!dir.exists())
{
dir.mkdir(dirName);
}
2022-08-24 03:03:41 +00:00
path = dirName;
}
2022-08-24 03:03:41 +00:00
2022-08-12 01:28:30 +00:00
if(name.endsWith(extName))
m_savePath = getStdFileName(path + "/" + name);
2022-08-12 01:47:03 +00:00
else
m_savePath = getStdFileName(path + "/" + name + extName);
2022-05-03 10:25:52 +00:00
}
#if 0
int ocrLanguage = ui->comboBox_ocrType->currentIndex();
saveCfgValue("saveAs", "ocrLanguage", ocrLanguage);
#endif
2022-05-03 10:25:52 +00:00
saveCfgValue("saveAs", "suffix", m_suffix);
saveCfgValue("saveAs", "subFolderByTime", ui->cbox_subFolder->isChecked());
2022-05-03 10:25:52 +00:00
accept();
}
void Dialog_SaveAs::on_filterSelected(const QString& filterName)
{
m_suffix = ui->fileDialog->nameFilters().indexOf(filterName);
2023-03-03 09:57:31 +00:00
ui->btn_option->setEnabled(0 == m_suffix || 6 == m_suffix || 7 == m_suffix || 8 == m_suffix);
#if 0
ui->label_ocrType->setVisible(isOcr());
ui->comboBox_ocrType->setVisible(isOcr());
#endif
2022-05-03 10:25:52 +00:00
}
void Dialog_SaveAs::on_btn_option_clicked()
{
Dialog_WriteSettings dlg(m_suffix, this);
dlg.exec();
2022-05-03 10:25:52 +00:00
}
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);
}
}