解决插入扫描时不能正确重命名后续文件的问题

This commit is contained in:
luoliangyi 2023-08-16 11:02:21 +08:00
parent 8697ff65fd
commit 23f5da6065
2 changed files with 86 additions and 56 deletions

View File

@ -67,7 +67,6 @@ MainWindow::MainWindow(const QString& appLang, QWidget *parent)
, m_scanCurIndex(-1) , m_scanCurIndex(-1)
, m_scanFileName("") , m_scanFileName("")
, m_scanImgFmtWriter(nullptr) , m_scanImgFmtWriter(nullptr)
, m_fileNameStartIndex(-1)
, m_isScanning(false) , m_isScanning(false)
, auto_save_changes_(false) , auto_save_changes_(false)
, save_from_changed_(false) , save_from_changed_(false)
@ -86,6 +85,8 @@ MainWindow::MainWindow(const QString& appLang, QWidget *parent)
{ {
ui->setupUi(this); ui->setupUi(this);
m_aquireIntoSaveParam.m_fileNameStartIndex = -1;
m_versionDll = new VersionDll; m_versionDll = new VersionDll;
HGBase_RegisterCrashFunc(CrashFunc, this); HGBase_RegisterCrashFunc(CrashFunc, this);
@ -637,9 +638,8 @@ void MainWindow::on_AcquireInto2()
m_scanInsertPos = -1; m_scanInsertPos = -1;
m_scanCurIndex = -1; m_scanCurIndex = -1;
Dialog_AquireInto dlg(m_fileNameStartIndex, this); Dialog_AquireInto dlg(-1, this);
m_aquireIntoSaveParam = dlg.getSaveParam(); m_aquireIntoSaveParam = dlg.getSaveParam();
m_fileNameStartIndex = m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoInBlank = true; m_aquireIntoInBlank = true;
m_aquireIntoBatchStartIndex = 0; m_aquireIntoBatchStartIndex = 0;
m_aquireIntoPageIndex = 0; m_aquireIntoPageIndex = 0;
@ -693,7 +693,7 @@ void MainWindow::on_reloadAcquireIntoCfg()
void MainWindow::on_m_acquireIntoCfg_changed(QString schemeName) void MainWindow::on_m_acquireIntoCfg_changed(QString schemeName)
{ {
QString selectedCfgName = m_cbtn_acquireIntoCfg->currentText(); QString selectedCfgName = m_cbtn_acquireIntoCfg->currentText();
Dialog_AquireInto dlg(m_fileNameStartIndex, this); Dialog_AquireInto dlg(-1, this);
dlg.setConfig(selectedCfgName); dlg.setConfig(selectedCfgName);
} }
@ -1080,21 +1080,30 @@ void MainWindow::on_newImage(void *image)
HGBase_CreateDir(getStdString(m_aquireIntoSaveParam.m_savePath).c_str()); HGBase_CreateDir(getStdString(m_aquireIntoSaveParam.m_savePath).c_str());
QString scanFileName; QString scanFileName;
if (-1 == m_scanInsertPos)
{
while (1) while (1)
{ {
scanFileName = m_aquireIntoSaveParam.m_savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2") scanFileName = m_aquireIntoSaveParam.m_savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2")
.arg(m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0')) .arg(m_aquireIntoSaveParam.m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0'))
.arg(m_aquireIntoSaveParam.m_fileNameExt); .arg(m_aquireIntoSaveParam.m_fileNameExt);
QFileInfo fileInfo(scanFileName); QFileInfo fileInfo(scanFileName);
if (fileInfo.isFile()) if (fileInfo.isFile())
{ {
++m_fileNameStartIndex; ++m_aquireIntoSaveParam.m_fileNameStartIndex;
} }
else else
{ {
break; break;
} }
} }
}
else
{
HGChar uuid[256];
HGBase_GetUuid(uuid, 256);
scanFileName = m_aquireIntoSaveParam.m_savePath + QString(uuid) + "." + m_aquireIntoSaveParam.m_fileNameExt;
}
HGUInt fmtType = 0; HGUInt fmtType = 0;
if (m_aquireIntoSaveParam.m_isOcr) if (m_aquireIntoSaveParam.m_isOcr)
@ -1146,7 +1155,8 @@ void MainWindow::on_newImage(void *image)
m_scanImgFmtWriter = nullptr; m_scanImgFmtWriter = nullptr;
emit post_new_image(m_scanFileName); emit post_new_image(m_scanFileName);
m_scanFileName.clear(); m_scanFileName.clear();
++m_fileNameStartIndex; if (-1 == m_scanInsertPos)
++m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoMultiPageCount = 0; m_aquireIntoMultiPageCount = 0;
} }
} }
@ -1197,21 +1207,30 @@ void MainWindow::on_newImage(void *image)
HGBase_CreateDir(getStdString(savePath).c_str()); HGBase_CreateDir(getStdString(savePath).c_str());
if (-1 == m_scanInsertPos)
{
while (1) while (1)
{ {
m_scanFileName = savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2") m_scanFileName = savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2")
.arg(m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0')) .arg(m_aquireIntoSaveParam.m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0'))
.arg(m_aquireIntoSaveParam.m_fileNameExt); .arg(m_aquireIntoSaveParam.m_fileNameExt);
QFileInfo fileInfo(m_scanFileName); QFileInfo fileInfo(m_scanFileName);
if (fileInfo.isFile()) if (fileInfo.isFile())
{ {
++m_fileNameStartIndex; ++m_aquireIntoSaveParam.m_fileNameStartIndex;
} }
else else
{ {
break; break;
} }
} }
}
else
{
HGChar uuid[256];
HGBase_GetUuid(uuid, 256);
m_scanFileName = m_aquireIntoSaveParam.m_savePath + QString(uuid) + "." + m_aquireIntoSaveParam.m_fileNameExt;
}
HGImgFmtSaveInfo saveInfo; HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = (HGUInt)m_aquireIntoSaveParam.m_jpegQuality; saveInfo.jpegQuality = (HGUInt)m_aquireIntoSaveParam.m_jpegQuality;
@ -1247,7 +1266,8 @@ void MainWindow::on_newImage(void *image)
if (HGBASE_ERR_OK == HGImgFmt_SaveImage((HGImage)image, fmtType, &saveInfo, getStdString(m_scanFileName).c_str())) if (HGBASE_ERR_OK == HGImgFmt_SaveImage((HGImage)image, fmtType, &saveInfo, getStdString(m_scanFileName).c_str()))
{ {
emit post_new_image(m_scanFileName); emit post_new_image(m_scanFileName);
++m_fileNameStartIndex; if (-1 == m_scanInsertPos)
++m_aquireIntoSaveParam.m_fileNameStartIndex;
} }
m_scanFileName.clear(); m_scanFileName.clear();
@ -1286,7 +1306,8 @@ void MainWindow::on_scanFinishEvent()
m_scanImgFmtWriter = nullptr; m_scanImgFmtWriter = nullptr;
emit post_new_image(m_scanFileName); emit post_new_image(m_scanFileName);
m_scanFileName.clear(); m_scanFileName.clear();
++m_fileNameStartIndex; if (-1 == m_scanInsertPos)
++m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoMultiPageCount = 0; m_aquireIntoMultiPageCount = 0;
} }
@ -1327,6 +1348,8 @@ void MainWindow::on_scanFinishEvent()
assert(ScanType_InsertScanInto == m_scanType); assert(ScanType_InsertScanInto == m_scanType);
assert(-1 != m_scanInsertPos); assert(-1 != m_scanInsertPos);
std::list<QString> backupFileNames;
int count = 0; int count = 0;
m_thumb->getItemCount(&count); m_thumb->getItemCount(&count);
for (int i = m_scanInsertPos; i < count; ++i) for (int i = m_scanInsertPos; i < count; ++i)
@ -1338,21 +1361,11 @@ void MainWindow::on_scanFinishEvent()
for (iter = m_curBatchFileList.begin(); iter != m_curBatchFileList.end(); ++iter) for (iter = m_curBatchFileList.begin(); iter != m_curBatchFileList.end(); ++iter)
{ {
if (*iter == fileName) if (*iter == fileName)
{
m_curBatchFileList.erase(iter);
m_curBatchTmpFileList.push_back(fileName);
break;
}
}
}
std::list<QString>::iterator iter;
for (iter = m_curBatchTmpFileList.begin(); iter != m_curBatchTmpFileList.end(); ++iter)
{ {
HGChar filePath[256]; HGChar filePath[256];
HGBase_GetFilePath((*iter).toLocal8Bit().toStdString().c_str(), filePath, 256); HGBase_GetFilePath(fileName.toLocal8Bit().toStdString().c_str(), filePath, 256);
HGChar fileSuffix[256]; HGChar fileSuffix[256];
HGBase_GetFileSuffix((*iter).toLocal8Bit().toStdString().c_str(), fileSuffix, 256); HGBase_GetFileSuffix(fileName.toLocal8Bit().toStdString().c_str(), fileSuffix, 256);
HGChar uuid[256]; HGChar uuid[256];
HGBase_GetUuid(uuid, 256); HGBase_GetUuid(uuid, 256);
@ -1361,15 +1374,35 @@ void MainWindow::on_scanFinishEvent()
QString strUuid = QString::fromLocal8Bit(uuid); QString strUuid = QString::fromLocal8Bit(uuid);
QString newFileName = strFilePath + strUuid + "." + strFileSuffix; QString newFileName = strFilePath + strUuid + "." + strFileSuffix;
QFile file(fileName);
file.rename(newFileName);
m_thumb->updateItem(fileName, newFileName, false);
m_curBatchFileList.erase(iter);
backupFileNames.push_back(fileName);
m_curBatchTmpFileList.push_back(newFileName);
break;
}
}
}
std::list<QString>::iterator iter;
for (iter = m_curBatchTmpFileList.begin(); iter != m_curBatchTmpFileList.end(); ++iter)
{
if (!backupFileNames.empty())
{
QString &newFileName = backupFileNames.front();
QFile file(*iter); QFile file(*iter);
file.rename(newFileName); file.rename(newFileName);
m_thumb->updateItem(*iter, newFileName, false); m_thumb->updateItem(*iter, newFileName, false);
*iter = newFileName; m_curBatchFileList.push_back(newFileName);
++m_aquireIntoSaveParam.m_fileNameStartIndex;
backupFileNames.erase(backupFileNames.begin());
continue;
} }
int fileNameStartIndex = 1;//m_aquireIntoSaveParam.m_fileNameStartIndex;
for (iter = m_curBatchTmpFileList.begin(); iter != m_curBatchTmpFileList.end(); ++iter)
{
HGChar filePath[256]; HGChar filePath[256];
HGBase_GetFilePath((*iter).toLocal8Bit().toStdString().c_str(), filePath, 256); HGBase_GetFilePath((*iter).toLocal8Bit().toStdString().c_str(), filePath, 256);
HGChar fileSuffix[256]; HGChar fileSuffix[256];
@ -1382,12 +1415,12 @@ void MainWindow::on_scanFinishEvent()
while (1) while (1)
{ {
newFileName = strFilePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2") newFileName = strFilePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2")
.arg(fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0')) .arg(m_aquireIntoSaveParam.m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0'))
.arg(strFileSuffix); .arg(strFileSuffix);
QFileInfo fileInfo(newFileName); QFileInfo fileInfo(newFileName);
if (fileInfo.isFile()) if (fileInfo.isFile())
{ {
++fileNameStartIndex; ++m_aquireIntoSaveParam.m_fileNameStartIndex;
} }
else else
{ {
@ -1399,7 +1432,7 @@ void MainWindow::on_scanFinishEvent()
file.rename(newFileName); file.rename(newFileName);
m_thumb->updateItem(*iter, newFileName, false); m_thumb->updateItem(*iter, newFileName, false);
m_curBatchFileList.push_back(newFileName); m_curBatchFileList.push_back(newFileName);
++fileNameStartIndex; ++m_aquireIntoSaveParam.m_fileNameStartIndex;
} }
m_curBatchTmpFileList.clear(); m_curBatchTmpFileList.clear();
@ -3338,7 +3371,7 @@ void MainWindow::on_act_acquireInto_triggered()
return; return;
} }
Dialog_AquireInto dlg(m_fileNameStartIndex, this); Dialog_AquireInto dlg(m_aquireIntoSaveParam.m_fileNameStartIndex, this);
connect(&dlg, SIGNAL(reloadAcquireIntoCfg()), this, SLOT(on_reloadAcquireIntoCfg())); connect(&dlg, SIGNAL(reloadAcquireIntoCfg()), this, SLOT(on_reloadAcquireIntoCfg()));
if (dlg.exec()) if (dlg.exec())
{ {
@ -3347,7 +3380,6 @@ void MainWindow::on_act_acquireInto_triggered()
m_scanCurIndex = -1; m_scanCurIndex = -1;
m_aquireIntoSaveParam = dlg.getSaveParam(); m_aquireIntoSaveParam = dlg.getSaveParam();
m_fileNameStartIndex = m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoInBlank = true; m_aquireIntoInBlank = true;
m_aquireIntoBatchStartIndex = 0; m_aquireIntoBatchStartIndex = 0;
m_aquireIntoPageIndex = 0; m_aquireIntoPageIndex = 0;
@ -3411,9 +3443,8 @@ void MainWindow::on_act_insertFromScanner_triggered()
break; break;
} }
Dialog_AquireInto dlg2(m_fileNameStartIndex, this); Dialog_AquireInto dlg2(-1, this);
m_aquireIntoSaveParam = dlg2.getSaveParam(); m_aquireIntoSaveParam = dlg2.getSaveParam();
m_fileNameStartIndex = m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoInBlank = true; m_aquireIntoInBlank = true;
m_aquireIntoBatchStartIndex = 0; m_aquireIntoBatchStartIndex = 0;
m_aquireIntoPageIndex = 0; m_aquireIntoPageIndex = 0;

View File

@ -317,7 +317,6 @@ private:
QString m_scanFileName; QString m_scanFileName;
HGImgFmtWriter m_scanImgFmtWriter; HGImgFmtWriter m_scanImgFmtWriter;
AquireIntoSaveParam m_aquireIntoSaveParam; AquireIntoSaveParam m_aquireIntoSaveParam;
int m_fileNameStartIndex;
bool m_aquireIntoInBlank; bool m_aquireIntoInBlank;
int m_aquireIntoBatchStartIndex; int m_aquireIntoBatchStartIndex;
int m_aquireIntoPageIndex; int m_aquireIntoPageIndex;