mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 05:49:53 +08:00
VOutline: change current anchor when current item changed
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
46c8025215
commit
fe4fb25105
@ -346,6 +346,10 @@ void VEditTab::requestUpdateOutline()
|
||||
|
||||
void VEditTab::scrollToAnchor(const VAnchor &anchor)
|
||||
{
|
||||
if (anchor == curHeader) {
|
||||
return;
|
||||
}
|
||||
curHeader = anchor;
|
||||
if (isEditMode) {
|
||||
if (anchor.lineNumber > -1) {
|
||||
textEditor->scrollToLine(anchor.lineNumber);
|
||||
@ -355,7 +359,6 @@ void VEditTab::scrollToAnchor(const VAnchor &anchor)
|
||||
document.scrollToAnchor(anchor.anchor.mid(1));
|
||||
}
|
||||
}
|
||||
curHeader = anchor;
|
||||
}
|
||||
|
||||
void VEditTab::updateCurHeader(const QString &anchor)
|
||||
|
@ -28,7 +28,7 @@ void VFileList::setupUI()
|
||||
|
||||
connect(fileList, &QListWidget::customContextMenuRequested,
|
||||
this, &VFileList::contextMenuRequested);
|
||||
connect(fileList, &QListWidget::itemActivated,
|
||||
connect(fileList, &QListWidget::itemClicked,
|
||||
this, &VFileList::handleItemClicked);
|
||||
|
||||
setLayout(mainLayout);
|
||||
@ -443,3 +443,14 @@ bool VFileList::copyFile(VDirectory *p_destDir, const QString &p_destName, VFile
|
||||
}
|
||||
return destFile != NULL;
|
||||
}
|
||||
|
||||
void VFileList::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
if (event->key() == Qt::Key_Return) {
|
||||
QListWidgetItem *item = fileList->currentItem();
|
||||
if (item) {
|
||||
handleItemClicked(item);
|
||||
}
|
||||
}
|
||||
QWidget::keyPressEvent(event);
|
||||
}
|
||||
|
@ -47,6 +47,9 @@ public slots:
|
||||
void setDirectory(VDirectory *p_directory);
|
||||
void newFile();
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
private:
|
||||
void setupUI();
|
||||
void updateFileList();
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <QVector>
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
#include <QKeyEvent>
|
||||
#include "voutline.h"
|
||||
#include "vtoc.h"
|
||||
|
||||
@ -12,16 +13,17 @@ VOutline::VOutline(QWidget *parent)
|
||||
setHeaderHidden(true);
|
||||
setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
|
||||
connect(this, &VOutline::itemClicked,
|
||||
this, &VOutline::handleItemClicked);
|
||||
connect(this, &VOutline::currentItemChanged,
|
||||
this, &VOutline::handleCurItemChanged);
|
||||
}
|
||||
|
||||
void VOutline::updateOutline(const VToc &toc)
|
||||
{
|
||||
// Clear current header
|
||||
curHeader = VAnchor();
|
||||
outline = toc;
|
||||
updateTreeFromOutline(outline);
|
||||
expandTree();
|
||||
updateCurHeader(curHeader);
|
||||
}
|
||||
|
||||
void VOutline::updateTreeFromOutline(const VToc &toc)
|
||||
@ -70,25 +72,34 @@ void VOutline::expandTree()
|
||||
return;
|
||||
}
|
||||
expandAll();
|
||||
setItemSelected(topLevelItem(0), true);
|
||||
}
|
||||
|
||||
|
||||
void VOutline::handleItemClicked(QTreeWidgetItem *item, int column)
|
||||
void VOutline::handleCurItemChanged(QTreeWidgetItem *p_curItem, QTreeWidgetItem *p_preItem)
|
||||
{
|
||||
Q_ASSERT(item && column == 0);
|
||||
QJsonObject itemJson = item->data(0, Qt::UserRole).toJsonObject();
|
||||
if (!p_curItem) {
|
||||
return;
|
||||
}
|
||||
QJsonObject itemJson = p_curItem->data(0, Qt::UserRole).toJsonObject();
|
||||
QString anchor = itemJson["anchor"].toString();
|
||||
int lineNumber = itemJson["line_number"].toInt();
|
||||
qDebug() << "click anchor" << anchor << lineNumber;
|
||||
curHeader.filePath = outline.filePath;
|
||||
curHeader.anchor = anchor;
|
||||
curHeader.lineNumber = lineNumber;
|
||||
VAnchor tmp;
|
||||
tmp.filePath = outline.filePath;
|
||||
tmp.anchor = anchor;
|
||||
tmp.lineNumber = lineNumber;
|
||||
if (tmp == curHeader) {
|
||||
return;
|
||||
}
|
||||
curHeader = tmp;
|
||||
qDebug() << "current header changed" << tmp.anchor << tmp.lineNumber;
|
||||
emit outlineItemActivated(curHeader);
|
||||
}
|
||||
|
||||
void VOutline::updateCurHeader(const VAnchor &anchor)
|
||||
{
|
||||
qDebug() << "update current header" << anchor.anchor << anchor.lineNumber;
|
||||
if (anchor == curHeader) {
|
||||
return;
|
||||
}
|
||||
curHeader = anchor;
|
||||
if (outline.type == VHeaderType::Anchor) {
|
||||
selectAnchor(anchor.anchor);
|
||||
@ -100,11 +111,6 @@ void VOutline::updateCurHeader(const VAnchor &anchor)
|
||||
|
||||
void VOutline::selectAnchor(const QString &anchor)
|
||||
{
|
||||
QList<QTreeWidgetItem *> selected = selectedItems();
|
||||
foreach (QTreeWidgetItem *item, selected) {
|
||||
setItemSelected(item, false);
|
||||
}
|
||||
|
||||
int nrTop = topLevelItemCount();
|
||||
for (int i = 0; i < nrTop; ++i) {
|
||||
if (selectAnchorOne(topLevelItem(i), anchor)) {
|
||||
@ -121,8 +127,7 @@ bool VOutline::selectAnchorOne(QTreeWidgetItem *item, const QString &anchor)
|
||||
QJsonObject itemJson = item->data(0, Qt::UserRole).toJsonObject();
|
||||
QString itemAnchor = itemJson["anchor"].toString();
|
||||
if (itemAnchor == anchor) {
|
||||
// Select this item
|
||||
setItemSelected(item, true);
|
||||
setCurrentItem(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -137,11 +142,6 @@ bool VOutline::selectAnchorOne(QTreeWidgetItem *item, const QString &anchor)
|
||||
|
||||
void VOutline::selectLineNumber(int lineNumber)
|
||||
{
|
||||
QList<QTreeWidgetItem *> selected = selectedItems();
|
||||
foreach (QTreeWidgetItem *item, selected) {
|
||||
setItemSelected(item, false);
|
||||
}
|
||||
|
||||
int nrTop = topLevelItemCount();
|
||||
for (int i = 0; i < nrTop; ++i) {
|
||||
if (selectLineNumberOne(topLevelItem(i), lineNumber)) {
|
||||
@ -159,7 +159,7 @@ bool VOutline::selectLineNumberOne(QTreeWidgetItem *item, int lineNumber)
|
||||
int itemLineNum = itemJson["line_number"].toInt();
|
||||
if (itemLineNum == lineNumber) {
|
||||
// Select this item
|
||||
setItemSelected(item, true);
|
||||
setCurrentItem(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -171,3 +171,14 @@ bool VOutline::selectLineNumberOne(QTreeWidgetItem *item, int lineNumber)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void VOutline::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
if (event->key() == Qt::Key_Return) {
|
||||
QTreeWidgetItem *item = currentItem();
|
||||
if (item) {
|
||||
item->setExpanded(!item->isExpanded());
|
||||
}
|
||||
}
|
||||
QTreeWidget::keyPressEvent(event);
|
||||
}
|
||||
|
@ -17,8 +17,11 @@ public slots:
|
||||
void updateOutline(const VToc &toc);
|
||||
void updateCurHeader(const VAnchor &anchor);
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
private slots:
|
||||
void handleItemClicked(QTreeWidgetItem *item, int column);
|
||||
void handleCurItemChanged(QTreeWidgetItem *p_curItem, QTreeWidgetItem *p_preItem);
|
||||
|
||||
private:
|
||||
void updateTreeFromOutline(const VToc &toc);
|
||||
|
@ -29,6 +29,12 @@ struct VAnchor
|
||||
QString filePath;
|
||||
QString anchor;
|
||||
int lineNumber;
|
||||
|
||||
bool operator==(const VAnchor &p_anchor) const {
|
||||
return (p_anchor.filePath == filePath
|
||||
&& p_anchor.anchor == anchor
|
||||
&& p_anchor.lineNumber == lineNumber);
|
||||
}
|
||||
};
|
||||
|
||||
class VToc
|
||||
|
Loading…
x
Reference in New Issue
Block a user