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