From 3722e6cb7168e376e44d16808e2da8f7e44466fb Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 21 Nov 2016 22:07:01 +0800 Subject: [PATCH] close all files before closing app Signed-off-by: Le Tan --- src/veditarea.cpp | 15 ++++++++++++++- src/veditarea.h | 1 + src/vedittab.cpp | 1 + src/veditwindow.cpp | 32 +++++++++++++++++++++++++------- src/veditwindow.h | 2 +- src/vmainwindow.cpp | 5 +++++ src/voutline.cpp | 3 --- src/vtoc.h | 8 -------- 8 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/veditarea.cpp b/src/veditarea.cpp index efa3b2df..ae6bede8 100644 --- a/src/veditarea.cpp +++ b/src/veditarea.cpp @@ -196,6 +196,19 @@ bool VEditArea::closeFile(QJsonObject fileJson) return ret; } +bool VEditArea::closeAllFiles(bool p_forced) +{ + int nrWin = splitter->count(); + for (int i = 0; i < nrWin; ++i) { + VEditWindow *win = getWindow(i); + setCurrentWindow(i, false); + if (!win->closeAllFiles(p_forced)) { + return false; + } + } + return true; +} + void VEditArea::editFile() { VEditWindow *win = getWindow(curWindowIndex); @@ -267,7 +280,7 @@ void VEditArea::handleSplitWindowRequest(VEditWindow *curWindow) void VEditArea::handleRemoveSplitRequest(VEditWindow *curWindow) { - if (!curWindow) { + if (!curWindow || splitter->count() <= 1) { return; } int idx = splitter->indexOf(curWindow); diff --git a/src/veditarea.h b/src/veditarea.h index 73db00a5..d0b25e4b 100644 --- a/src/veditarea.h +++ b/src/veditarea.h @@ -22,6 +22,7 @@ class VEditArea : public QWidget public: explicit VEditArea(VNote *vnote, QWidget *parent = 0); bool isFileOpened(const QString ¬ebook, const QString &relativePath); + bool closeAllFiles(bool p_forced); signals: void curTabStatusChanged(const QString ¬ebook, const QString &relativePath, diff --git a/src/vedittab.cpp b/src/vedittab.cpp index 2b92d41a..ae6bbab2 100644 --- a/src/vedittab.cpp +++ b/src/vedittab.cpp @@ -359,6 +359,7 @@ void VEditTab::scrollToAnchor(const VAnchor &anchor) document.scrollToAnchor(anchor.anchor.mid(1)); } } + curHeader = anchor; } void VEditTab::updateCurHeader(const QString &anchor) diff --git a/src/veditwindow.cpp b/src/veditwindow.cpp index a4b9c82c..124170f9 100644 --- a/src/veditwindow.cpp +++ b/src/veditwindow.cpp @@ -71,7 +71,7 @@ void VEditWindow::removeSplit() { // Close all the files one by one // If user do not want to close a file, just stop removing - if (closeAllFiles()) { + if (closeAllFiles(false)) { Q_ASSERT(count() == 0); emit requestRemoveSplit(this); } @@ -124,18 +124,21 @@ out: return idx; } +// Return true if we closed the file bool VEditWindow::closeFile(const QString ¬ebook, const QString &relativePath, bool isForced) { // Find if it has been opened already int idx = findTabByFile(notebook, relativePath); if (idx == -1) { - return true; + return false; } VEditTab *editor = getTab(idx); Q_ASSERT(editor); bool ok = true; if (!isForced) { + setCurrentIndex(idx); + noticeStatus(idx); ok = editor->requestClose(); } if (ok) { @@ -146,16 +149,31 @@ bool VEditWindow::closeFile(const QString ¬ebook, const QString &relativePath return ok; } -bool VEditWindow::closeAllFiles() +bool VEditWindow::closeAllFiles(bool p_forced) { int nrTab = count(); + bool ret = true; for (int i = 0; i < nrTab; ++i) { - // Always close the first tab - if (!handleTabCloseRequest(0)) { - return false; + VEditTab *editor = getTab(0); + bool ok = true; + if (!p_forced) { + setCurrentIndex(0); + noticeStatus(0); + ok = editor->requestClose(); + } + if (ok) { + removeTab(0); + delete editor; + } else { + ret = false; + break; } } - return true; + updateTabListMenu(); + if (ret) { + emit requestRemoveSplit(this); + } + return ret; } int VEditWindow::openFileInTab(const QString ¬ebook, const QString &relativePath, diff --git a/src/veditwindow.h b/src/veditwindow.h index 63042556..bfcce0dd 100644 --- a/src/veditwindow.h +++ b/src/veditwindow.h @@ -29,7 +29,7 @@ public: void saveAndReadFile(); void handleNotebookRenamed(const QVector ¬ebooks, const QString &oldName, const QString &newName); - bool closeAllFiles(); + bool closeAllFiles(bool p_forced); void setRemoveSplitEnable(bool enabled); void requestUpdateTabStatus(); void requestUpdateOutline(); diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index e68935ca..391ca7be 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -800,6 +800,11 @@ void VMainWindow::deleteCurNote() void VMainWindow::closeEvent(QCloseEvent *event) { + if (!editArea->closeAllFiles(false)) { + // Fail to close all the opened files, cancel closing app + event->ignore(); + return; + } saveStateAndGeometry(); QMainWindow::closeEvent(event); } diff --git a/src/voutline.cpp b/src/voutline.cpp index e4437355..3fa98a08 100644 --- a/src/voutline.cpp +++ b/src/voutline.cpp @@ -89,9 +89,6 @@ void VOutline::handleItemClicked(QTreeWidgetItem *item, int column) void VOutline::updateCurHeader(const VAnchor &anchor) { - if (anchor == curHeader) { - return; - } curHeader = anchor; if (outline.type == VHeaderType::Anchor) { selectAnchor(anchor.anchor); diff --git a/src/vtoc.h b/src/vtoc.h index a902a512..287c68cd 100644 --- a/src/vtoc.h +++ b/src/vtoc.h @@ -26,19 +26,11 @@ struct VAnchor VAnchor() : lineNumber(-1) {} VAnchor(const QString filePath, const QString &anchor, int lineNumber) : filePath(filePath), anchor(anchor), lineNumber(lineNumber) {} - inline bool operator ==(const VAnchor &p_anchor) const; QString filePath; QString anchor; int lineNumber; }; -inline bool VAnchor::operator ==(const VAnchor &p_anchor) const -{ - return p_anchor.filePath == filePath && - p_anchor.anchor == anchor && - p_anchor.lineNumber == lineNumber; -} - class VToc { public: