Browse Source

close all files before closing app

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 years ago
parent
commit
3722e6cb71
8 changed files with 47 additions and 20 deletions
  1. 14 1
      src/veditarea.cpp
  2. 1 0
      src/veditarea.h
  3. 1 0
      src/vedittab.cpp
  4. 25 7
      src/veditwindow.cpp
  5. 1 1
      src/veditwindow.h
  6. 5 0
      src/vmainwindow.cpp
  7. 0 3
      src/voutline.cpp
  8. 0 8
      src/vtoc.h

+ 14 - 1
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);

+ 1 - 0
src/veditarea.h

@@ -22,6 +22,7 @@ class VEditArea : public QWidget
 public:
     explicit VEditArea(VNote *vnote, QWidget *parent = 0);
     bool isFileOpened(const QString &notebook, const QString &relativePath);
+    bool closeAllFiles(bool p_forced);
 
 signals:
     void curTabStatusChanged(const QString &notebook, const QString &relativePath,

+ 1 - 0
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)

+ 25 - 7
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 &notebook, 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 &notebook, 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;
+        }
+    }
+    updateTabListMenu();
+    if (ret) {
+        emit requestRemoveSplit(this);
     }
-    return true;
+    return ret;
 }
 
 int VEditWindow::openFileInTab(const QString &notebook, const QString &relativePath,

+ 1 - 1
src/veditwindow.h

@@ -29,7 +29,7 @@ public:
     void saveAndReadFile();
     void handleNotebookRenamed(const QVector<VNotebook> &notebooks, const QString &oldName,
                                const QString &newName);
-    bool closeAllFiles();
+    bool closeAllFiles(bool p_forced);
     void setRemoveSplitEnable(bool enabled);
     void requestUpdateTabStatus();
     void requestUpdateOutline();

+ 5 - 0
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);
 }

+ 0 - 3
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);

+ 0 - 8
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: