mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
restore current edit tab and cursor position info at startup
This commit is contained in:
parent
6fe82d3db0
commit
a4ab2ae671
@ -813,6 +813,7 @@ bool VEditArea::handleKeyNavigation(int p_key, bool &p_succeed)
|
|||||||
|
|
||||||
int VEditArea::openFiles(const QVector<VFileSessionInfo> &p_files)
|
int VEditArea::openFiles(const QVector<VFileSessionInfo> &p_files)
|
||||||
{
|
{
|
||||||
|
VFile *curFile = NULL;
|
||||||
int nrOpened = 0;
|
int nrOpened = 0;
|
||||||
for (auto const & info : p_files) {
|
for (auto const & info : p_files) {
|
||||||
QString filePath = VUtils::validFilePathToOpen(info.m_file);
|
QString filePath = VUtils::validFilePathToOpen(info.m_file);
|
||||||
@ -828,6 +829,10 @@ int VEditArea::openFiles(const QVector<VFileSessionInfo> &p_files)
|
|||||||
VEditTab *tab = openFile(file, info.m_mode, true);
|
VEditTab *tab = openFile(file, info.m_mode, true);
|
||||||
++nrOpened;
|
++nrOpened;
|
||||||
|
|
||||||
|
if (info.m_active) {
|
||||||
|
curFile = file;
|
||||||
|
}
|
||||||
|
|
||||||
VEditTabInfo tabInfo;
|
VEditTabInfo tabInfo;
|
||||||
tabInfo.m_editTab = tab;
|
tabInfo.m_editTab = tab;
|
||||||
info.toEditTabInfo(&tabInfo);
|
info.toEditTabInfo(&tabInfo);
|
||||||
@ -835,6 +840,10 @@ int VEditArea::openFiles(const QVector<VFileSessionInfo> &p_files)
|
|||||||
tab->tryRestoreFromTabInfo(tabInfo);
|
tab->tryRestoreFromTabInfo(tabInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (curFile) {
|
||||||
|
openFile(curFile, OpenFileMode::Read, false);
|
||||||
|
}
|
||||||
|
|
||||||
return nrOpened;
|
return nrOpened;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1055,3 +1055,17 @@ VVim *VEditor::getVim() const
|
|||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool VEditor::setCursorPosition(int p_blockNumber, int p_posInBlock)
|
||||||
|
{
|
||||||
|
QTextDocument *doc = documentW();
|
||||||
|
QTextBlock block = doc->findBlockByNumber(p_blockNumber);
|
||||||
|
if (!block.isValid() || block.length() <= p_posInBlock) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextCursor cursor = textCursorW();
|
||||||
|
cursor.setPosition(block.position() + p_posInBlock);
|
||||||
|
setTextCursorW(cursor);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -152,6 +152,8 @@ public:
|
|||||||
|
|
||||||
virtual VWordCountInfo fetchWordCountInfo() const = 0;
|
virtual VWordCountInfo fetchWordCountInfo() const = 0;
|
||||||
|
|
||||||
|
virtual bool setCursorPosition(int p_blockNumber, int p_posInBlock);
|
||||||
|
|
||||||
// Wrapper functions for QPlainTextEdit/QTextEdit.
|
// Wrapper functions for QPlainTextEdit/QTextEdit.
|
||||||
// Ends with W to distinguish it from the original interfaces.
|
// Ends with W to distinguish it from the original interfaces.
|
||||||
public:
|
public:
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
VFileSessionInfo::VFileSessionInfo()
|
VFileSessionInfo::VFileSessionInfo()
|
||||||
: m_mode(OpenFileMode::Read),
|
: m_mode(OpenFileMode::Read),
|
||||||
|
m_active(false),
|
||||||
m_headerIndex(-1),
|
m_headerIndex(-1),
|
||||||
m_cursorBlockNumber(-1),
|
m_cursorBlockNumber(-1),
|
||||||
m_cursorPositionInBlock(-1)
|
m_cursorPositionInBlock(-1)
|
||||||
@ -19,6 +20,7 @@ VFileSessionInfo::VFileSessionInfo(const QString &p_file,
|
|||||||
OpenFileMode p_mode)
|
OpenFileMode p_mode)
|
||||||
: m_file(p_file),
|
: m_file(p_file),
|
||||||
m_mode(p_mode),
|
m_mode(p_mode),
|
||||||
|
m_active(false),
|
||||||
m_headerIndex(-1),
|
m_headerIndex(-1),
|
||||||
m_cursorBlockNumber(-1),
|
m_cursorBlockNumber(-1),
|
||||||
m_cursorPositionInBlock(-1)
|
m_cursorPositionInBlock(-1)
|
||||||
@ -57,6 +59,7 @@ VFileSessionInfo VFileSessionInfo::fromSettings(const QSettings *p_settings)
|
|||||||
info.m_mode = OpenFileMode::Read;
|
info.m_mode = OpenFileMode::Read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info.m_active = p_settings->value(FileSessionConfig::c_active).toBool();
|
||||||
info.m_headerIndex = p_settings->value(FileSessionConfig::c_headerIndex).toInt();
|
info.m_headerIndex = p_settings->value(FileSessionConfig::c_headerIndex).toInt();
|
||||||
info.m_cursorBlockNumber = p_settings->value(FileSessionConfig::c_cursorBlockNumber).toInt();
|
info.m_cursorBlockNumber = p_settings->value(FileSessionConfig::c_cursorBlockNumber).toInt();
|
||||||
info.m_cursorPositionInBlock = p_settings->value(FileSessionConfig::c_cursorPositionInBlock).toInt();
|
info.m_cursorPositionInBlock = p_settings->value(FileSessionConfig::c_cursorPositionInBlock).toInt();
|
||||||
@ -68,6 +71,7 @@ void VFileSessionInfo::toSettings(QSettings *p_settings) const
|
|||||||
{
|
{
|
||||||
p_settings->setValue(FileSessionConfig::c_file, m_file);
|
p_settings->setValue(FileSessionConfig::c_file, m_file);
|
||||||
p_settings->setValue(FileSessionConfig::c_mode, (int)m_mode);
|
p_settings->setValue(FileSessionConfig::c_mode, (int)m_mode);
|
||||||
|
p_settings->setValue(FileSessionConfig::c_active, m_active);
|
||||||
p_settings->setValue(FileSessionConfig::c_headerIndex, m_headerIndex);
|
p_settings->setValue(FileSessionConfig::c_headerIndex, m_headerIndex);
|
||||||
p_settings->setValue(FileSessionConfig::c_cursorBlockNumber, m_cursorBlockNumber);
|
p_settings->setValue(FileSessionConfig::c_cursorBlockNumber, m_cursorBlockNumber);
|
||||||
p_settings->setValue(FileSessionConfig::c_cursorPositionInBlock, m_cursorPositionInBlock);
|
p_settings->setValue(FileSessionConfig::c_cursorPositionInBlock, m_cursorPositionInBlock);
|
||||||
|
@ -11,6 +11,9 @@ namespace FileSessionConfig
|
|||||||
static const QString c_file = "file";
|
static const QString c_file = "file";
|
||||||
static const QString c_mode = "mode";
|
static const QString c_mode = "mode";
|
||||||
|
|
||||||
|
// Whether it is current file.
|
||||||
|
static const QString c_active = "active";
|
||||||
|
|
||||||
// Index in outline of the anchor.
|
// Index in outline of the anchor.
|
||||||
static const QString c_headerIndex = "header_index";
|
static const QString c_headerIndex = "header_index";
|
||||||
|
|
||||||
@ -38,12 +41,20 @@ public:
|
|||||||
|
|
||||||
void toSettings(QSettings *p_settings) const;
|
void toSettings(QSettings *p_settings) const;
|
||||||
|
|
||||||
|
void setActive(bool p_active)
|
||||||
|
{
|
||||||
|
m_active = p_active;
|
||||||
|
}
|
||||||
|
|
||||||
// Absolute path of the file.
|
// Absolute path of the file.
|
||||||
QString m_file;
|
QString m_file;
|
||||||
|
|
||||||
// Mode of this file in this session.
|
// Mode of this file in this session.
|
||||||
OpenFileMode m_mode;
|
OpenFileMode m_mode;
|
||||||
|
|
||||||
|
// Whether this file is current file.
|
||||||
|
bool m_active;
|
||||||
|
|
||||||
// Index in outline of the header.
|
// Index in outline of the header.
|
||||||
int m_headerIndex;
|
int m_headerIndex;
|
||||||
|
|
||||||
|
@ -2229,6 +2229,10 @@ void VMainWindow::closeEvent(QCloseEvent *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
VFileSessionInfo info = VFileSessionInfo::fromEditTabInfo(&tab);
|
VFileSessionInfo info = VFileSessionInfo::fromEditTabInfo(&tab);
|
||||||
|
if (tab.m_editTab == m_curTab) {
|
||||||
|
info.setActive(true);
|
||||||
|
}
|
||||||
|
|
||||||
fileInfos.push_back(info);
|
fileInfos.push_back(info);
|
||||||
|
|
||||||
qDebug() << "file session:" << info.m_file << (info.m_mode == OpenFileMode::Edit);
|
qDebug() << "file session:" << info.m_file << (info.m_mode == OpenFileMode::Edit);
|
||||||
|
@ -874,9 +874,21 @@ bool VMdTab::restoreFromTabInfo(const VEditTabInfo &p_info)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ret = false;
|
||||||
|
// Restore cursor position.
|
||||||
|
if (m_isEditMode
|
||||||
|
&& m_editor
|
||||||
|
&& p_info.m_cursorBlockNumber > -1
|
||||||
|
&& p_info.m_cursorPositionInBlock > -1) {
|
||||||
|
ret = m_editor->setCursorPosition(p_info.m_cursorBlockNumber, p_info.m_cursorPositionInBlock);
|
||||||
|
}
|
||||||
|
|
||||||
// Restore header.
|
// Restore header.
|
||||||
VHeaderPointer header(m_file, p_info.m_headerIndex);
|
if (!ret) {
|
||||||
bool ret = scrollToHeaderInternal(header);
|
VHeaderPointer header(m_file, p_info.m_headerIndex);
|
||||||
|
ret = scrollToHeaderInternal(header);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user