فهرست منبع

update current header in outline correctly

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 سال پیش
والد
کامیت
454072c9ba
6فایلهای تغییر یافته به همراه60 افزوده شده و 45 حذف شده
  1. 7 8
      src/veditarea.cpp
  2. 1 1
      src/veditarea.h
  3. 11 6
      src/vedittab.cpp
  4. 3 2
      src/vedittab.h
  5. 34 25
      src/veditwindow.cpp
  6. 4 3
      src/veditwindow.h

+ 7 - 8
src/veditarea.cpp

@@ -165,17 +165,16 @@ void VEditArea::setCurrentWindow(int windowIndex, bool setFocus)
     }
 
 out:
-    // Update tab status
-    noticeTabStatus();
-    emit outlineChanged(getWindow(windowIndex)->getTabOutline());
+    // Update status
+    updateWindowStatus();
 }
 
-void VEditArea::noticeTabStatus()
+void VEditArea::updateWindowStatus()
 {
-    QString notebook, relativePath;
-    bool editMode, modifiable;
-    getWindow(curWindowIndex)->getTabStatus(notebook, relativePath, editMode, modifiable);
-    emit curTabStatusChanged(notebook, relativePath, editMode, modifiable);
+    VEditWindow *win = getWindow(curWindowIndex);
+    win->requestUpdateTabStatus();
+    win->requestUpdateOutline();
+    win->requestUpdateCurHeader();
 }
 
 void VEditArea::closeFile(QJsonObject fileJson)

+ 1 - 1
src/veditarea.h

@@ -60,7 +60,7 @@ private:
     inline VEditWindow *getWindow(int windowIndex) const;
     void insertSplitWindow(int idx);
     void removeSplitWindow(VEditWindow *win);
-    void noticeTabStatus();
+    void updateWindowStatus();
 
     VNote *vnote;
     int curWindowIndex;

+ 11 - 6
src/vedittab.cpp

@@ -333,9 +333,14 @@ void VEditTab::parseTocLi(QXmlStreamReader &xml, QVector<VHeader> &headers, int
     }
 }
 
-const VToc& VEditTab::getOutline() const
+void VEditTab::requestUpdateCurHeader()
 {
-    return tableOfContent;
+    emit curHeaderChanged(curHeader);
+}
+
+void VEditTab::requestUpdateOutline()
+{
+    emit outlineChanged(tableOfContent);
 }
 
 void VEditTab::scrollToAnchor(const VAnchor &anchor)
@@ -353,12 +358,12 @@ void VEditTab::scrollToAnchor(const VAnchor &anchor)
 
 void VEditTab::updateCurHeader(const QString &anchor)
 {
-    if (curHeader == anchor) {
+    if (curHeader.anchor.mid(1) == anchor) {
         return;
     }
-    curHeader = anchor;
+    curHeader = VAnchor(QDir::cleanPath(QDir(noteFile->basePath).filePath(noteFile->fileName)),
+                        "#" + anchor, -1);
     if (!anchor.isEmpty()) {
-        emit curHeaderChanged(VAnchor(QDir::cleanPath(QDir(noteFile->basePath).filePath(noteFile->fileName)),
-                                      "#" + anchor, -1));
+        emit curHeaderChanged(curHeader);
     }
 }

+ 3 - 2
src/vedittab.h

@@ -33,7 +33,8 @@ public:
     inline bool getIsEditMode() const;
     inline bool isModified() const;
     void focusTab();
-    const VToc& getOutline() const;
+    void requestUpdateOutline();
+    void requestUpdateCurHeader();
     void scrollToAnchor(const VAnchor& anchor);
 
 signals:
@@ -65,7 +66,7 @@ private:
     VDocument document;
     MarkdownConverterType mdConverterType;
     VToc tableOfContent;
-    QString curHeader;
+    VAnchor curHeader;
 };
 
 inline bool VEditTab::getIsEditMode() const

+ 34 - 25
src/veditwindow.cpp

@@ -93,7 +93,7 @@ int VEditWindow::insertTabWithData(int index, QWidget *page,
     int idx = insertTab(index, page, label);
     QTabBar *tabs = tabBar();
     tabs->setTabData(idx, tabData);
-    noticeTabStatus(currentIndex());
+    noticeStatus(currentIndex());
     return idx;
 }
 
@@ -116,7 +116,7 @@ out:
         editFile();
     }
     focusWindow();
-    noticeTabStatus(idx);
+    noticeStatus(idx);
     return idx;
 }
 
@@ -204,7 +204,7 @@ bool VEditWindow::handleTabCloseRequest(int index)
         delete editor;
     }
     updateTabListMenu();
-    noticeTabStatus(currentIndex());
+    noticeStatus(currentIndex());
     // User clicks the close button. We should make this window
     // to be current window.
     emit getFocused();
@@ -216,14 +216,14 @@ void VEditWindow::readFile()
     VEditTab *editor = getTab(currentIndex());
     Q_ASSERT(editor);
     editor->readFile();
-    noticeTabStatus(currentIndex());
+    noticeStatus(currentIndex());
 }
 
 void VEditWindow::saveAndReadFile()
 {
     saveFile();
     readFile();
-    noticeTabStatus(currentIndex());
+    noticeStatus(currentIndex());
 }
 
 void VEditWindow::editFile()
@@ -231,7 +231,7 @@ void VEditWindow::editFile()
     VEditTab *editor = getTab(currentIndex());
     Q_ASSERT(editor);
     editor->editFile();
-    noticeTabStatus(currentIndex());
+    noticeStatus(currentIndex());
 }
 
 void VEditWindow::saveFile()
@@ -275,33 +275,29 @@ void VEditWindow::noticeTabStatus(int index)
                           editMode, modifiable);
 }
 
-void VEditWindow::getTabStatus(QString &notebook, QString &relativePath,
-                               bool &editMode, bool &modifiable) const
+void VEditWindow::requestUpdateTabStatus()
+{
+    noticeTabStatus(currentIndex());
+}
+
+void VEditWindow::requestUpdateOutline()
 {
     int idx = currentIndex();
     if (idx == -1) {
-        notebook = relativePath = "";
-        editMode = modifiable = false;
+        emit outlineChanged(VToc());
         return;
     }
-
-    QJsonObject tabJson = tabBar()->tabData(idx).toJsonObject();
-    Q_ASSERT(!tabJson.isEmpty());
-    notebook = tabJson["notebook"].toString();
-    relativePath = tabJson["relative_path"].toString();
-    VEditTab *editor = getTab(idx);
-    editMode = editor->getIsEditMode();
-    modifiable = tabJson["modifiable"].toBool();
+    getTab(idx)->requestUpdateOutline();
 }
 
-VToc VEditWindow::getTabOutline() const
+void VEditWindow::requestUpdateCurHeader()
 {
     int idx = currentIndex();
     if (idx == -1) {
-        return VToc();
+        emit curHeaderChanged(VAnchor());
+        return;
     }
-
-    return getTab(idx)->getOutline();
+    getTab(idx)->requestUpdateCurHeader();
 }
 
 void VEditWindow::focusWindow()
@@ -318,8 +314,7 @@ void VEditWindow::handleTabbarClicked(int index)
 {
     // The child will emit getFocused here
     focusWindow();
-    noticeTabStatus(index);
-    emit outlineChanged(getTab(index)->getOutline());
+    noticeStatus(index);
 }
 
 void VEditWindow::mousePressEvent(QMouseEvent *event)
@@ -347,7 +342,7 @@ void VEditWindow::tabListJump(QAction *action)
                             tabJson["relative_path"].toString());
     Q_ASSERT(idx >= 0);
     setCurrentIndex(idx);
-    noticeTabStatus(idx);
+    noticeStatus(idx);
 }
 
 void VEditWindow::updateTabListMenu()
@@ -412,3 +407,17 @@ void VEditWindow::scrollCurTab(const VAnchor &anchor)
         getTab(idx)->scrollToAnchor(anchor);
     }
 }
+
+void VEditWindow::noticeStatus(int index)
+{
+    noticeTabStatus(index);
+
+    if (index == -1) {
+        emit outlineChanged(VToc());
+        emit curHeaderChanged(VAnchor());
+    } else {
+        VEditTab *tab = getTab(index);
+        tab->requestUpdateOutline();
+        tab->requestUpdateCurHeader();
+    }
+}

+ 4 - 3
src/veditwindow.h

@@ -32,9 +32,9 @@ public:
                                const QString &newName);
     bool closeAllFiles();
     void setRemoveSplitEnable(bool enabled);
-    void getTabStatus(QString &notebook, QString &relativePath,
-                      bool &editMode, bool &modifiable) const;
-    VToc getTabOutline() const;
+    void requestUpdateTabStatus();
+    void requestUpdateOutline();
+    void requestUpdateCurHeader();
     // Focus to current tab's editor
     void focusWindow();
     void scrollCurTab(const VAnchor &anchor);
@@ -72,6 +72,7 @@ private:
     inline VEditTab *getTab(int tabIndex) const;
     void noticeTabStatus(int index);
     void updateTabListMenu();
+    void noticeStatus(int index);
 
     VNote *vnote;
     // Button in the right corner