#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 #include #include #include #include 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, true); // 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())); } } 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 !defined (x86_64) && !defined (loongarch64) 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)"; #endif ui->fileDialog->setNameFilter(filter); 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 > 10) m_suffix = 10; 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); } 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; } 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(sender()); QString fileName = lineEdit->text(); if (!fileName.endsWith(extName)) { lineEdit->setText(fileName + extName); } }