mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
NotebookExplorer session
This commit is contained in:
parent
1843ca5bfd
commit
14a3270184
@ -66,6 +66,8 @@ void SessionConfig::init()
|
|||||||
m_searchOption.fromJson(sessionJobj[QStringLiteral("search_option")].toObject());
|
m_searchOption.fromJson(sessionJobj[QStringLiteral("search_option")].toObject());
|
||||||
|
|
||||||
m_viewAreaSession = readByteArray(sessionJobj, QStringLiteral("viewarea_session"));
|
m_viewAreaSession = readByteArray(sessionJobj, QStringLiteral("viewarea_session"));
|
||||||
|
|
||||||
|
m_notebookExplorerSession = readByteArray(sessionJobj, QStringLiteral("notebook_explorer_session"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SessionConfig::loadCore(const QJsonObject &p_session)
|
void SessionConfig::loadCore(const QJsonObject &p_session)
|
||||||
@ -183,6 +185,7 @@ QJsonObject SessionConfig::toJson() const
|
|||||||
obj[QStringLiteral("export_option")] = m_exportOption.toJson();
|
obj[QStringLiteral("export_option")] = m_exportOption.toJson();
|
||||||
obj[QStringLiteral("search_option")] = m_searchOption.toJson();
|
obj[QStringLiteral("search_option")] = m_searchOption.toJson();
|
||||||
writeByteArray(obj, QStringLiteral("viewarea_session"), m_viewAreaSession);
|
writeByteArray(obj, QStringLiteral("viewarea_session"), m_viewAreaSession);
|
||||||
|
writeByteArray(obj, QStringLiteral("notebook_explorer_session"), m_notebookExplorerSession);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,3 +326,15 @@ void SessionConfig::setViewAreaSession(const QByteArray &p_bytes)
|
|||||||
{
|
{
|
||||||
updateConfigWithoutCheck(m_viewAreaSession, p_bytes, this);
|
updateConfigWithoutCheck(m_viewAreaSession, p_bytes, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray SessionConfig::getNotebookExplorerSessionAndClear()
|
||||||
|
{
|
||||||
|
QByteArray bytes;
|
||||||
|
m_notebookExplorerSession.swap(bytes);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SessionConfig::setNotebookExplorerSession(const QByteArray &p_bytes)
|
||||||
|
{
|
||||||
|
updateConfigWithoutCheck(m_notebookExplorerSession, p_bytes, this);
|
||||||
|
}
|
||||||
|
@ -92,7 +92,10 @@ namespace vnotex
|
|||||||
void setSearchOption(const SearchOption &p_option);
|
void setSearchOption(const SearchOption &p_option);
|
||||||
|
|
||||||
QByteArray getViewAreaSessionAndClear();
|
QByteArray getViewAreaSessionAndClear();
|
||||||
void setViewAreaSession(const QByteArray &p_obj);
|
void setViewAreaSession(const QByteArray &p_bytes);
|
||||||
|
|
||||||
|
QByteArray getNotebookExplorerSessionAndClear();
|
||||||
|
void setNotebookExplorerSession(const QByteArray &p_bytes);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void loadCore(const QJsonObject &p_session);
|
void loadCore(const QJsonObject &p_session);
|
||||||
@ -134,6 +137,8 @@ namespace vnotex
|
|||||||
SearchOption m_searchOption;
|
SearchOption m_searchOption;
|
||||||
|
|
||||||
QByteArray m_viewAreaSession;
|
QByteArray m_viewAreaSession;
|
||||||
|
|
||||||
|
QByteArray m_notebookExplorerSession;
|
||||||
};
|
};
|
||||||
} // ns vnotex
|
} // ns vnotex
|
||||||
|
|
||||||
|
@ -40,6 +40,19 @@ NotebookExplorer::NotebookExplorer(QWidget *p_parent)
|
|||||||
: QFrame(p_parent)
|
: QFrame(p_parent)
|
||||||
{
|
{
|
||||||
setupUI();
|
setupUI();
|
||||||
|
|
||||||
|
auto mainWindow = VNoteX::getInst().getMainWindow();
|
||||||
|
connect(mainWindow, &MainWindow::mainWindowClosed,
|
||||||
|
this, [this](const QSharedPointer<Event> &p_event) {
|
||||||
|
if (p_event->m_handled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
saveSession();
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(mainWindow, &MainWindow::mainWindowStarted,
|
||||||
|
this, &NotebookExplorer::loadSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NotebookExplorer::setupUI()
|
void NotebookExplorer::setupUI()
|
||||||
@ -169,6 +182,8 @@ void NotebookExplorer::reloadNotebook(const Notebook *p_notebook)
|
|||||||
|
|
||||||
void NotebookExplorer::setCurrentNotebook(const QSharedPointer<Notebook> &p_notebook)
|
void NotebookExplorer::setCurrentNotebook(const QSharedPointer<Notebook> &p_notebook)
|
||||||
{
|
{
|
||||||
|
updateSession();
|
||||||
|
|
||||||
m_currentNotebook = p_notebook;
|
m_currentNotebook = p_notebook;
|
||||||
|
|
||||||
ID id = p_notebook ? p_notebook->getId() : static_cast<ID>(Notebook::InvalidId);
|
ID id = p_notebook ? p_notebook->getId() : static_cast<ID>(Notebook::InvalidId);
|
||||||
@ -176,6 +191,8 @@ void NotebookExplorer::setCurrentNotebook(const QSharedPointer<Notebook> &p_note
|
|||||||
|
|
||||||
m_nodeExplorer->setNotebook(p_notebook);
|
m_nodeExplorer->setNotebook(p_notebook);
|
||||||
|
|
||||||
|
recoverSession();
|
||||||
|
|
||||||
emit updateTitleBarMenuActions();
|
emit updateTitleBarMenuActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -414,3 +431,62 @@ void NotebookExplorer::setupViewMenu(QMenu *p_menu)
|
|||||||
m_nodeExplorer->setViewOrder(order);
|
m_nodeExplorer->setViewOrder(order);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NotebookExplorer::saveSession()
|
||||||
|
{
|
||||||
|
updateSession();
|
||||||
|
|
||||||
|
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
|
||||||
|
sessionConfig.setNotebookExplorerSession(m_session.serialize());
|
||||||
|
}
|
||||||
|
|
||||||
|
void NotebookExplorer::loadSession()
|
||||||
|
{
|
||||||
|
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
|
||||||
|
m_session = NotebookExplorerSession::deserialize(sessionConfig.getNotebookExplorerSessionAndClear());
|
||||||
|
|
||||||
|
m_sessionLoaded = true;
|
||||||
|
|
||||||
|
recoverSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NotebookExplorer::updateSession()
|
||||||
|
{
|
||||||
|
if (!m_sessionLoaded || !m_currentNotebook) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& nbSession = m_session.m_notebooks[m_currentNotebook->getRootFolderPath()];
|
||||||
|
nbSession.m_recovered = true;
|
||||||
|
|
||||||
|
auto node = currentExploredNode();
|
||||||
|
if (node) {
|
||||||
|
nbSession.m_currentNodePath = node->fetchPath();
|
||||||
|
} else {
|
||||||
|
nbSession.m_currentNodePath.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NotebookExplorer::recoverSession()
|
||||||
|
{
|
||||||
|
if (!m_sessionLoaded || !m_currentNotebook) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto it = m_session.m_notebooks.find(m_currentNotebook->getRootFolderPath());
|
||||||
|
if (it != m_session.m_notebooks.end()) {
|
||||||
|
qDebug() << it.value().m_recovered << it.value().m_currentNodePath;
|
||||||
|
|
||||||
|
if (it.value().m_recovered || it.value().m_currentNodePath.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
it.value().m_recovered = true;
|
||||||
|
|
||||||
|
auto node = m_currentNotebook->loadNodeByPath(it.value().m_currentNodePath);
|
||||||
|
qDebug() << "node" << node;
|
||||||
|
if (node) {
|
||||||
|
m_nodeExplorer->setCurrentNode(node.data());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,7 +4,9 @@
|
|||||||
#include <QFrame>
|
#include <QFrame>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
|
|
||||||
#include "global.h"
|
#include <core/global.h>
|
||||||
|
|
||||||
|
#include "notebookexplorersession.h"
|
||||||
|
|
||||||
class QMenu;
|
class QMenu;
|
||||||
|
|
||||||
@ -68,11 +70,23 @@ namespace vnotex
|
|||||||
|
|
||||||
void setupViewMenu(QMenu *p_menu);
|
void setupViewMenu(QMenu *p_menu);
|
||||||
|
|
||||||
|
void saveSession();
|
||||||
|
|
||||||
|
void loadSession();
|
||||||
|
|
||||||
|
void updateSession();
|
||||||
|
|
||||||
|
void recoverSession();
|
||||||
|
|
||||||
NotebookSelector *m_selector = nullptr;
|
NotebookSelector *m_selector = nullptr;
|
||||||
|
|
||||||
NotebookNodeExplorer *m_nodeExplorer = nullptr;
|
NotebookNodeExplorer *m_nodeExplorer = nullptr;
|
||||||
|
|
||||||
QSharedPointer<Notebook> m_currentNotebook;
|
QSharedPointer<Notebook> m_currentNotebook;
|
||||||
|
|
||||||
|
NotebookExplorerSession m_session;
|
||||||
|
|
||||||
|
bool m_sessionLoaded = false;
|
||||||
};
|
};
|
||||||
} // ns vnotex
|
} // ns vnotex
|
||||||
|
|
||||||
|
41
src/widgets/notebookexplorersession.cpp
Normal file
41
src/widgets/notebookexplorersession.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "notebookexplorersession.h"
|
||||||
|
|
||||||
|
using namespace vnotex;
|
||||||
|
|
||||||
|
QDataStream &::vnotex::operator<<(QDataStream &p_ds, const NotebookSession &p_session)
|
||||||
|
{
|
||||||
|
p_ds << p_session.m_currentNodePath;
|
||||||
|
return p_ds;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDataStream &::vnotex::operator>>(QDataStream &p_ds, NotebookSession &p_session)
|
||||||
|
{
|
||||||
|
p_ds >> p_session.m_currentNodePath;
|
||||||
|
return p_ds;
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray NotebookExplorerSession::serialize() const
|
||||||
|
{
|
||||||
|
QByteArray data;
|
||||||
|
QDataStream outs(&data, QIODevice::WriteOnly);
|
||||||
|
outs.setVersion(QDataStream::Qt_5_12);
|
||||||
|
|
||||||
|
outs << m_notebooks;
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
NotebookExplorerSession NotebookExplorerSession::deserialize(const QByteArray &p_data)
|
||||||
|
{
|
||||||
|
NotebookExplorerSession session;
|
||||||
|
if (p_data.isEmpty()) {
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
QDataStream ins(p_data);
|
||||||
|
ins.setVersion(QDataStream::Qt_5_12);
|
||||||
|
|
||||||
|
ins >> session.m_notebooks;
|
||||||
|
|
||||||
|
return session;
|
||||||
|
}
|
32
src/widgets/notebookexplorersession.h
Normal file
32
src/widgets/notebookexplorersession.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef NOTEBOOKEXPLORERSESSION_H
|
||||||
|
#define NOTEBOOKEXPLORERSESSION_H
|
||||||
|
|
||||||
|
#include <QDataStream>
|
||||||
|
#include <QHash>
|
||||||
|
|
||||||
|
namespace vnotex
|
||||||
|
{
|
||||||
|
struct NotebookSession
|
||||||
|
{
|
||||||
|
// Used to judge whether this session has been recovered.
|
||||||
|
bool m_recovered = false;
|
||||||
|
|
||||||
|
QString m_currentNodePath;
|
||||||
|
};
|
||||||
|
|
||||||
|
class NotebookExplorerSession
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QByteArray serialize() const;
|
||||||
|
|
||||||
|
static NotebookExplorerSession deserialize(const QByteArray &p_data);
|
||||||
|
|
||||||
|
// Notebook's path to its session.
|
||||||
|
QHash<QString, NotebookSession> m_notebooks;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern QDataStream &operator<<(QDataStream &p_ds, const NotebookSession &p_session);
|
||||||
|
extern QDataStream &operator>>(QDataStream &p_ds, NotebookSession &p_session);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // NOTEBOOKEXPLORERSESSION_H
|
@ -415,8 +415,7 @@ void ViewSplit::updateWindowList(QMenu *p_menu)
|
|||||||
for (int i = 0; i < cnt; ++i) {
|
for (int i = 0; i < cnt; ++i) {
|
||||||
auto window = getViewWindow(i);
|
auto window = getViewWindow(i);
|
||||||
|
|
||||||
auto act = new QAction(window->getIcon(),
|
auto act = new QAction(window->getName(),
|
||||||
window->getName(),
|
|
||||||
m_windowListActionGroup);
|
m_windowListActionGroup);
|
||||||
act->setToolTip(window->getTitle());
|
act->setToolTip(window->getTitle());
|
||||||
act->setData(i);
|
act->setData(i);
|
||||||
|
@ -178,13 +178,16 @@ void ViewWindow::detachFromBuffer(bool p_quiet)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const QIcon &ViewWindow::getIcon() const
|
QIcon ViewWindow::getIcon() const
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
if (m_buffer) {
|
if (m_buffer) {
|
||||||
return m_buffer->isModified() ? s_modifiedIcon : s_savedIcon;
|
return m_buffer->isModified() ? s_modifiedIcon : s_savedIcon;
|
||||||
} else {
|
} else {
|
||||||
return s_savedIcon;
|
return s_savedIcon;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
return QIcon();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ViewWindow::getName() const
|
QString ViewWindow::getName() const
|
||||||
|
@ -43,7 +43,7 @@ namespace vnotex
|
|||||||
// User request to open the buffer attached to this ViewWindow again.
|
// User request to open the buffer attached to this ViewWindow again.
|
||||||
virtual void openTwice(const QSharedPointer<FileOpenParameters> &p_paras) = 0;
|
virtual void openTwice(const QSharedPointer<FileOpenParameters> &p_paras) = 0;
|
||||||
|
|
||||||
virtual const QIcon &getIcon() const;
|
virtual QIcon getIcon() const;
|
||||||
|
|
||||||
virtual QString getName() const;
|
virtual QString getName() const;
|
||||||
|
|
||||||
|
@ -47,6 +47,7 @@ SOURCES += \
|
|||||||
$$PWD/mainwindow.cpp \
|
$$PWD/mainwindow.cpp \
|
||||||
$$PWD/markdownviewwindow.cpp \
|
$$PWD/markdownviewwindow.cpp \
|
||||||
$$PWD/navigationmodemgr.cpp \
|
$$PWD/navigationmodemgr.cpp \
|
||||||
|
$$PWD/notebookexplorersession.cpp \
|
||||||
$$PWD/outlinepopup.cpp \
|
$$PWD/outlinepopup.cpp \
|
||||||
$$PWD/outlineprovider.cpp \
|
$$PWD/outlineprovider.cpp \
|
||||||
$$PWD/outlineviewer.cpp \
|
$$PWD/outlineviewer.cpp \
|
||||||
@ -140,6 +141,7 @@ HEADERS += \
|
|||||||
$$PWD/markdownviewwindow.h \
|
$$PWD/markdownviewwindow.h \
|
||||||
$$PWD/navigationmodemgr.h \
|
$$PWD/navigationmodemgr.h \
|
||||||
$$PWD/navigationmodewrapper.h \
|
$$PWD/navigationmodewrapper.h \
|
||||||
|
$$PWD/notebookexplorersession.h \
|
||||||
$$PWD/outlinepopup.h \
|
$$PWD/outlinepopup.h \
|
||||||
$$PWD/outlineprovider.h \
|
$$PWD/outlineprovider.h \
|
||||||
$$PWD/outlineviewer.h \
|
$$PWD/outlineviewer.h \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user