code_app/app/scanner/dialog_saveas.cpp

207 lines
5.7 KiB
C++

#include "dialog_saveas.h"
#include "ui_dialog_saveas.h"
#include "dialog_writesettings.h"
#include "HGUIGlobal.h"
#include "app_cfg.h"
#include <assert.h>
#include <qabstractproxymodel.h>
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);
}
};
Dialog_SaveAs::Dialog_SaveAs(QWidget *parent) :
QDialog(parent)
, ui(new Ui::Dialog_SaveAs)
{
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);
#if defined(OEM_LISICHENG) || defined(x86_64)
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);;"
"GIF - Graphics Interchange Format(*.gif)");
#else
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);;"
"GIF - Graphics Interchange Format(*.gif);;"
"OCR->PDF - Portable Document Format(*.pdf);;"
"OCR->OFD - Open Fixed-layout Document(*.ofd)");
#endif
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);
if (m_suffix > 7)
m_suffix = 7;
else if (m_suffix < 0)
m_suffix = 0;
ui->fileDialog->selectNameFilter(ui->fileDialog->nameFilters().at(m_suffix));
ui->btn_option->setEnabled(0 == m_suffix || 3 == m_suffix);
}
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 >= 7);
}
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 = ".tif";
break;
case 4:
extName = ".pdf";
break;
case 5:
extName = ".ofd";
break;
case 6:
extName = ".gif";
break;
case 7:
extName = ".pdf";
break;
case 8:
extName = ".ofd";
break;
}
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);
if(name.endsWith(extName))
m_savePath = getStdFileName(path + name);
else
m_savePath = getStdFileName(path + name + extName);
}
saveCfgValue("saveAs", "suffix", m_suffix);
accept();
}
void Dialog_SaveAs::on_filterSelected(const QString& filterName)
{
m_suffix = ui->fileDialog->nameFilters().indexOf(filterName);
ui->btn_option->setEnabled(0 == m_suffix || 3 == m_suffix);
}
void Dialog_SaveAs::on_btn_option_clicked()
{
Dialog_WriteSettings dlg(m_suffix, this);
dlg.exec();
}