From bf70c23110b51b15457bbca36cc9879a6f25fa4d Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 4 Feb 2018 16:47:23 +0800 Subject: [PATCH] combine EditNote and SaveExitNote --- src/resources/docs/shortcuts_en.md | 10 +-- src/resources/docs/shortcuts_zh.md | 10 +-- src/resources/vnote.ini | 6 +- src/vmainwindow.cpp | 123 ++++++++++++++++++----------- src/vmainwindow.h | 14 +++- 5 files changed, 93 insertions(+), 70 deletions(-) diff --git a/src/resources/docs/shortcuts_en.md b/src/resources/docs/shortcuts_en.md index 1bf40410..f6f37a11 100644 --- a/src/resources/docs/shortcuts_en.md +++ b/src/resources/docs/shortcuts_en.md @@ -19,10 +19,10 @@ Scroll in all directions. Recover last closed file. - `Ctrl+Alt+L` Open Flash Page. +- `Ctrl+T` +Edit current note or save changes and exit edit mode. ### Read Mode -- `Ctrl+W` -Edit current note. - `H`/`J`/`K`/`L` Navigation, corresponding to Left/Down/Up/Right arrow keys. - `Ctrl+U` @@ -54,8 +54,6 @@ Recover the page zoom factor to 100%. ### Edit Mode - `Ctrl+S` Save current changes. -- `Ctrl+T` -Save current changes and exit edit mode. - `Ctrl + +/-` Zoom in/out the page. - `Ctrl+Wheel` @@ -121,10 +119,6 @@ CaptainMode=Ctrl+E NewNote=Ctrl+Alt+N ; Save current note SaveNote=Ctrl+S -; Save changes and enter read mode -SaveAndRead=Ctrl+T -; Edit current note -EditNote=Ctrl+W ; Close current note CloseNote= ; Open file/replace dialog diff --git a/src/resources/docs/shortcuts_zh.md b/src/resources/docs/shortcuts_zh.md index ef5a2cf8..8feaf108 100644 --- a/src/resources/docs/shortcuts_zh.md +++ b/src/resources/docs/shortcuts_zh.md @@ -19,10 +19,10 @@ 恢复上一个关闭的文件。 - `Ctrl+Alt+L` 打开灵犀页。 +- `Ctrl+T` +编辑当前笔记或保存更改并退出编辑模式。 ### 阅读模式 -- `Ctrl+W` -编辑当前笔记。 - `H`/`J`/`K`/`L` 导航,对应于左/下/上/右方向键。 - `Ctrl+U` @@ -54,8 +54,6 @@ ### 编辑模式 - `Ctrl+S` 保存当前更改。 -- `Ctrl+T` -保存当前更改并退出编辑模式。 - `Ctrl + +/-` 放大/缩小页面。 - `Ctrl+Wheel` @@ -122,10 +120,6 @@ CaptainMode=Ctrl+E NewNote=Ctrl+Alt+N ; Save current note SaveNote=Ctrl+S -; Save changes and enter read mode -SaveAndRead=Ctrl+T -; Edit current note -EditNote=Ctrl+W ; Close current note CloseNote= ; Open file/replace dialog diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 95c0a434..c654d8cb 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -246,10 +246,8 @@ NewNote=Ctrl+Alt+N NewSubfolder=Ctrl+Alt+S ; Save current note SaveNote=Ctrl+S -; Save changes and enter read mode -SaveAndRead=Ctrl+T -; Edit current note -EditNote=Ctrl+W +; Edit or read note +EditReadNote=Ctrl+T ; Close current note CloseNote= ; Open file/replace dialog diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 9fed4318..19b7985f 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -700,47 +700,23 @@ void VMainWindow::initFileToolBar(QSize p_iconSize) connect(deleteNoteAct, &QAction::triggered, this, &VMainWindow::deleteCurNote); - editNoteAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/edit_note.svg"), - tr("Edit"), this); - editNoteAct->setStatusTip(tr("Edit current note")); - keySeq = g_config->getShortcutKeySequence("EditNote"); - seq = QKeySequence(keySeq); - if (!seq.isEmpty()) { - editNoteAct->setText(tr("Edit\t%1").arg(VUtils::getShortcutText(keySeq))); - editNoteAct->setShortcut(seq); + m_editReadAct = new QAction(this); + connect(m_editReadAct, &QAction::triggered, + this, &VMainWindow::toggleEditReadMode); + + m_discardExitAct = new QAction(VIconUtils::menuIcon(":/resources/icons/discard_exit.svg"), + tr("Discard Changes And Read"), + this); + keySeq = VUtils::getCaptainShortcutSequenceText("DiscardAndRead"); + if (!keySeq.isEmpty()) { + m_discardExitAct->setText(tr("Discard Changes And Read\t%1").arg(keySeq)); } - connect(editNoteAct, &QAction::triggered, - editArea, &VEditArea::editFile); - - discardExitAct = new QAction(VIconUtils::menuIcon(":/resources/icons/discard_exit.svg"), - tr("Discard Changes And Read"), this); - QString keyText = VUtils::getCaptainShortcutSequenceText("DiscardAndRead"); - if (!keyText.isEmpty()) { - discardExitAct->setText(tr("Discard Changes And Read\t%1").arg(keyText)); - } - - discardExitAct->setStatusTip(tr("Discard changes and exit edit mode")); - connect(discardExitAct, &QAction::triggered, + m_discardExitAct->setStatusTip(tr("Discard changes and exit edit mode")); + connect(m_discardExitAct, &QAction::triggered, editArea, &VEditArea::readFile); - QMenu *exitEditMenu = new QMenu(this); - exitEditMenu->setToolTipsVisible(true); - exitEditMenu->addAction(discardExitAct); - - saveExitAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/save_exit.svg"), - tr("Save Changes And Read"), this); - saveExitAct->setStatusTip(tr("Save changes and exit edit mode")); - saveExitAct->setMenu(exitEditMenu); - keySeq = g_config->getShortcutKeySequence("SaveAndRead"); - seq = QKeySequence(keySeq); - if (!seq.isEmpty()) { - saveExitAct->setText(tr("Save Changes And Read\t%1").arg(VUtils::getShortcutText(keySeq))); - saveExitAct->setShortcut(seq); - } - - connect(saveExitAct, &QAction::triggered, - editArea, &VEditArea::saveAndReadFile); + updateEditReadAct(NULL); saveNoteAct = new QAction(VIconUtils::toolButtonIcon(":/resources/icons/save_note.svg"), tr("Save"), this); @@ -759,9 +735,8 @@ void VMainWindow::initFileToolBar(QSize p_iconSize) newNoteAct->setEnabled(false); noteInfoAct->setEnabled(false); deleteNoteAct->setEnabled(false); - editNoteAct->setEnabled(false); - saveExitAct->setVisible(false); - discardExitAct->setVisible(false); + m_editReadAct->setEnabled(false); + m_discardExitAct->setEnabled(false); saveNoteAct->setEnabled(false); fileToolBar->addWidget(m_avatarBtn); @@ -769,8 +744,8 @@ void VMainWindow::initFileToolBar(QSize p_iconSize) fileToolBar->addAction(newNoteAct); fileToolBar->addAction(deleteNoteAct); fileToolBar->addAction(noteInfoAct); - fileToolBar->addAction(editNoteAct); - fileToolBar->addAction(saveExitAct); + fileToolBar->addAction(m_editReadAct); + fileToolBar->addAction(m_discardExitAct); fileToolBar->addAction(saveNoteAct); } @@ -1855,10 +1830,8 @@ void VMainWindow::updateActionsStateFromTab(const VEditTab *p_tab) m_printAct->setEnabled(file && file->getDocType() == DocType::Markdown); m_exportAsPDFAct->setEnabled(file && file->getDocType() == DocType::Markdown); - discardExitAct->setVisible(file && editMode); - saveExitAct->setVisible(file && editMode); - editNoteAct->setEnabled(file && !editMode); - editNoteAct->setVisible(!saveExitAct->isVisible()); + updateEditReadAct(p_tab); + saveNoteAct->setEnabled(file && editMode && file->isModifiable()); deleteNoteAct->setEnabled(file && file->getType() == FileType::Note); noteInfoAct->setEnabled(file && !systemFile); @@ -2679,7 +2652,7 @@ bool VMainWindow::discardAndReadByCaptain(void *p_target, void *p_data) Q_UNUSED(p_data); VMainWindow *obj = static_cast(p_target); if (obj->m_curTab) { - obj->discardExitAct->trigger(); + obj->m_discardExitAct->trigger(); obj->m_curTab->setFocus(); return false; @@ -2986,3 +2959,59 @@ void VMainWindow::initVimCmd() m_vimCmd->hide(); } + +void VMainWindow::toggleEditReadMode() +{ + if (!m_curTab) { + return; + } + + if (m_curTab->isEditMode()) { + // Save changes and read. + editArea->saveAndReadFile(); + } else { + // Edit. + editArea->editFile(); + } +} + +void VMainWindow::updateEditReadAct(const VEditTab *p_tab) +{ + static QIcon editIcon = VIconUtils::toolButtonIcon(":/resources/icons/edit_note.svg"); + static QString editText; + static QIcon readIcon = VIconUtils::toolButtonIcon(":/resources/icons/save_exit.svg"); + static QString readText; + + if (editText.isEmpty()) { + QString keySeq = g_config->getShortcutKeySequence("EditReadNote"); + QKeySequence seq(keySeq); + if (!seq.isEmpty()) { + QString shortcutText = VUtils::getShortcutText(keySeq); + editText = tr("Edit\t%1").arg(shortcutText); + readText = tr("Save Changes And Read\t%1").arg(shortcutText); + + m_editReadAct->setShortcut(seq); + } else { + editText = tr("Edit"); + readText = tr("Save Changes And Read"); + } + } + + if (!p_tab || !p_tab->isEditMode()) { + // Edit. + m_editReadAct->setIcon(editIcon); + m_editReadAct->setText(editText); + m_editReadAct->setStatusTip(tr("Edit current note")); + + m_discardExitAct->setEnabled(false); + } else { + // Read. + m_editReadAct->setIcon(readIcon); + m_editReadAct->setText(readText); + m_editReadAct->setStatusTip(tr("Save changes and exit edit mode")); + + m_discardExitAct->setEnabled(true); + } + + m_editReadAct->setEnabled(p_tab); +} diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 8a34affa..edfc021d 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -168,6 +168,8 @@ private slots: void customShortcut(); + void toggleEditReadMode(); + protected: void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; @@ -257,6 +259,8 @@ private: void initThemeMenu(QMenu *p_emnu); + void updateEditReadAct(const VEditTab *p_tab); + // Captain mode functions. // Popup the attachment list if it is enabled. @@ -329,11 +333,15 @@ private: QAction *newRootDirAct; QAction *newNoteAct; QAction *noteInfoAct; + QAction *deleteNoteAct; - QAction *editNoteAct; + + // Toggle read and edit note. + QAction *m_editReadAct; + QAction *saveNoteAct; - QAction *saveExitAct; - QAction *discardExitAct; + + QAction *m_discardExitAct; QAction *expandViewAct;