From 7c12b6484a4ec32bf6da24a6b6c02b3033ed505f Mon Sep 17 00:00:00 2001 From: ut001910 Date: Thu, 15 May 2025 15:01:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E4=BF=9D=E5=AD=98=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复关闭窗口保存提示 Bug: https://pms.uniontech.com/bug-view-315439.html https://pms.uniontech.com/bug-view-315425.html Log: 修复关闭窗口保存提示,与v20保持一致 --- src/startmanager.cpp | 3 ++ src/widgets/window.cpp | 65 +++++++++++++++++++++++++++++++++++++++++- src/widgets/window.h | 2 ++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/startmanager.cpp b/src/startmanager.cpp index d190aff42..afac4f9c6 100644 --- a/src/startmanager.cpp +++ b/src/startmanager.cpp @@ -147,6 +147,9 @@ void StartManager::autoBackupFile() QFileInfo fileInfo; m_qlistTemFile.clear(); listBackupInfo = Settings::instance()->settings->option("advance.editor.browsing_history_temfile")->value().toStringList(); + if (m_windows.isEmpty()) { + return; + } //记录所有的文件信息 for (int var = 0; var < m_windows.count(); ++var) { diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index c6a6d2233..b0d6c33fa 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -2593,6 +2593,65 @@ bool Window::closeAllFiles() return true; } +bool Window::saveAllFiles() +{ + qInfo() << "begin saveAllFiles()"; + QMap wrappers = m_wrappers; + for (int i = wrappers.count() - 1; i > 0; i--) { + const QString &filePath = m_tabbar->fileAt(i); + // 避免异常情况重入时当前已无标签页的情况 + if (filePath.isEmpty()) { + return false; + } + EditWrapper *wrapper = m_wrappers.value(filePath); + if (!wrapper) { + return false; + } + bool isDraftFile = wrapper->isDraftFile(); + bool isModified = wrapper->isModified(); + bool bIsBackupFile = false; + + if (wrapper->isTemFile()) { + bIsBackupFile = true; + } + + if (isModified) { + DDialog *dialog = createDialog(tr("Do you want to save this file?"), ""); + int res = dialog->exec(); + + //取消或关闭弹窗不做任务操作 + if (res == 0 || res == -1) { + return false; + } + + //不保存 + if (res == 1) { + continue; + } + + //保存 + if (res == 2) { + m_tabbar->setCurrentIndex(i); + QFileInfo fileInfo(filePath); + QString newFilePath; + if (isDraftFile) { + wrapper->saveDraftFile(newFilePath); + } else if(bIsBackupFile) { + if (!wrapper->saveFile()) { + saveAsFile(); + } + } else { + if (!wrapper->saveFile()) { + saveAsFile(); + } + } + } + } + } + qInfo() << "end saveAllFiles()"; + return true; +} + /** * @brief addTemFileTab 恢复备份文件标签页 * @param qstrPath 打开文件路径 @@ -3394,7 +3453,11 @@ void Window::closeEvent(QCloseEvent *e) QString filePath = itr.value()->textEditor()->getFilePath(); Utils::recordCloseFile(filePath); } - + if (!saveAllFiles()) { + backupFile(); + e->ignore(); + return; + } backupFile(); } } diff --git a/src/widgets/window.h b/src/widgets/window.h index 3de136ad0..3bfd09baa 100644 --- a/src/widgets/window.h +++ b/src/widgets/window.h @@ -118,6 +118,8 @@ class Window : public DMainWindow void backupFile(); // 关闭当前窗口所有文件 bool closeAllFiles(); + // 保存窗口所有文件 + bool saveAllFiles(); // 恢复备份文件标签页 void addTemFileTab(const QString &qstrPath, const QString &qstrName, const QString &qstrTruePath, const QString &lastModifiedTime, bool bIsTemFile = false);