update current header in outline correctly

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-09 21:41:57 +08:00
parent 849fdf05bd
commit 454072c9ba
6 changed files with 63 additions and 48 deletions

View File

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

View File

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

View File

@ -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) void VEditTab::scrollToAnchor(const VAnchor &anchor)
@ -353,12 +358,12 @@ void VEditTab::scrollToAnchor(const VAnchor &anchor)
void VEditTab::updateCurHeader(const QString &anchor) void VEditTab::updateCurHeader(const QString &anchor)
{ {
if (curHeader == anchor) { if (curHeader.anchor.mid(1) == anchor) {
return; return;
} }
curHeader = anchor; curHeader = VAnchor(QDir::cleanPath(QDir(noteFile->basePath).filePath(noteFile->fileName)),
"#" + anchor, -1);
if (!anchor.isEmpty()) { if (!anchor.isEmpty()) {
emit curHeaderChanged(VAnchor(QDir::cleanPath(QDir(noteFile->basePath).filePath(noteFile->fileName)), emit curHeaderChanged(curHeader);
"#" + anchor, -1));
} }
} }

View File

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

View File

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

View File

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