Refactoring. Another case for QScopeGuard.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-07-04 17:32:58 +03:00
parent 2ac4267dd3
commit 16975ec6b9
2 changed files with 10 additions and 13 deletions

View file

@ -48,6 +48,12 @@
#include "version.h" #include "version.h"
#include "mapplication.h" // Should be last because of definning qApp #include "mapplication.h" // Should be last because of definning qApp
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
#include "../vmisc/backport/qscopeguard.h"
#else
#include <QScopeGuard>
#endif
#include <QFileDialog> #include <QFileDialog>
#include <QFileInfo> #include <QFileInfo>
#include <QMessageBox> #include <QMessageBox>
@ -822,17 +828,16 @@ bool TMainWindow::FileSaveAs()
QString fileName = QFileDialog::getSaveFileName(this, tr("Save as"), dir + QChar('/') + fName, filters); QString fileName = QFileDialog::getSaveFileName(this, tr("Save as"), dir + QChar('/') + fName, filters);
auto RemoveTempDir = [usedNotExistedDir, dir]() auto RemoveTempDir = qScopeGuard([usedNotExistedDir, dir]()
{ {
if (usedNotExistedDir) if (usedNotExistedDir)
{ {
QDir(dir).rmpath(QChar('.')); QDir(dir).rmpath(QChar('.'));
} }
}; });
if (fileName.isEmpty()) if (fileName.isEmpty())
{ {
RemoveTempDir();
return false; return false;
} }
@ -850,7 +855,6 @@ bool TMainWindow::FileSaveAs()
{ {
qCCritical(tMainWindow, "%s", qCCritical(tMainWindow, "%s",
qUtf8Printable(tr("Failed to lock. This file already opened in another window."))); qUtf8Printable(tr("Failed to lock. This file already opened in another window.")));
RemoveTempDir();
return false; return false;
} }
} }
@ -876,7 +880,6 @@ bool TMainWindow::FileSaveAs()
// Restore previous state // Restore previous state
m->SetReadOnly(readOnly); m->SetReadOnly(readOnly);
mIsReadOnly = readOnly; mIsReadOnly = readOnly;
RemoveTempDir();
return false; return false;
} }
@ -892,10 +895,8 @@ bool TMainWindow::FileSaveAs()
{ {
qCCritical(tMainWindow, "%s", qUtf8Printable(tr("Failed to lock. This file already opened in another window. " qCCritical(tMainWindow, "%s", qUtf8Printable(tr("Failed to lock. This file already opened in another window. "
"Expect collissions when run 2 copies of the program."))); "Expect collissions when run 2 copies of the program.")));
RemoveTempDir();
return false; return false;
} }
RemoveTempDir();
return true; return true;
} }

View file

@ -2809,18 +2809,17 @@ bool MainWindow::on_actionSaveAs_triggered()
#endif #endif
); );
auto RemoveTempDir = [usedNotExistedDir, dir]() auto RemoveTempDir = qScopeGuard([usedNotExistedDir, dir]()
{ {
if (usedNotExistedDir) if (usedNotExistedDir)
{ {
QDir directory(dir); QDir directory(dir);
directory.rmpath(QChar('.')); directory.rmpath(QChar('.'));
} }
}; });
if (fileName.isEmpty()) if (fileName.isEmpty())
{ {
RemoveTempDir();
return false; return false;
} }
@ -2839,7 +2838,6 @@ bool MainWindow::on_actionSaveAs_triggered()
{ {
qCCritical(vMainWindow, "%s", qCCritical(vMainWindow, "%s",
qUtf8Printable(tr("Failed to lock. This file already opened in another window."))); qUtf8Printable(tr("Failed to lock. This file already opened in another window.")));
RemoveTempDir();
return false; return false;
} }
} }
@ -2865,7 +2863,6 @@ bool MainWindow::on_actionSaveAs_triggered()
doc->SetReadOnly(readOnly); doc->SetReadOnly(readOnly);
doc->SetModified(wasModified); doc->SetModified(wasModified);
RemoveTempDir();
return result; return result;
} }
else if (not oldFilePath.isEmpty()) else if (not oldFilePath.isEmpty())
@ -2899,7 +2896,6 @@ bool MainWindow::on_actionSaveAs_triggered()
"collissions when run 2 copies of the program."))); "collissions when run 2 copies of the program.")));
} }
RemoveTempDir();
return result; return result;
} }