add read and edit action in view window

This commit is contained in:
Le Tan 2020-12-20 11:25:53 +08:00
parent 3cc882829a
commit d51e486a84
8 changed files with 91 additions and 21 deletions

View File

@ -839,6 +839,18 @@ void MarkdownEditor::handleContextMenuEvent(QContextMenuEvent *p_event, bool *p_
pasteAct = WidgetUtils::findActionByObjectName(actions, "edit-paste"); pasteAct = WidgetUtils::findActionByObjectName(actions, "edit-paste");
} }
if (!m_textEdit->hasSelection()) {
auto readAct = new QAction(tr("&Read"), menu);
WidgetUtils::addActionShortcutText(readAct,
ConfigMgr::getInst().getEditorConfig().getShortcut(EditorConfig::Shortcut::EditRead));
connect(readAct, &QAction::triggered,
this, &MarkdownEditor::readRequested);
menu->insertAction(firstAct, readAct);
if (firstAct) {
menu->insertSeparator(firstAct);
}
}
if (pasteAct && pasteAct->isEnabled()) { if (pasteAct && pasteAct->isEnabled()) {
QClipboard *clipboard = QApplication::clipboard(); QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData(); const QMimeData *mimeData = clipboard->mimeData();

View File

@ -94,6 +94,8 @@ namespace vnotex
void htmlToMarkdownRequested(quint64 p_id, TimeStamp p_timeStamp, const QString &p_html); void htmlToMarkdownRequested(quint64 p_id, TimeStamp p_timeStamp, const QString &p_html);
void readRequested();
private slots: private slots:
void handleCanInsertFromMimeData(const QMimeData *p_source, bool *p_handled, bool *p_allowed); void handleCanInsertFromMimeData(const QMimeData *p_source, bool *p_handled, bool *p_allowed);

View File

@ -12,6 +12,9 @@
#include <utils/clipboardutils.h> #include <utils/clipboardutils.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/utils.h> #include <utils/utils.h>
#include <utils/widgetutils.h>
#include <core/configmgr.h>
#include <core/editorconfig.h>
#include "../widgetsfactory.h" #include "../widgetsfactory.h"
using namespace vnotex; using namespace vnotex;
@ -112,6 +115,19 @@ void MarkdownViewer::contextMenuEvent(QContextMenuEvent *p_event)
} }
#endif #endif
if (!hasSelection()) {
auto firstAct = actions.isEmpty() ? nullptr : actions[0];
auto editAct = new QAction(tr("&Edit"), menu.data());
WidgetUtils::addActionShortcutText(editAct,
ConfigMgr::getInst().getEditorConfig().getShortcut(EditorConfig::Shortcut::EditRead));
connect(editAct, &QAction::triggered,
this, &MarkdownViewer::editRequested);
menu->insertAction(firstAct, editAct);
if (firstAct) {
menu->insertSeparator(firstAct);
}
}
// We need to replace the "Copy Image" action: // We need to replace the "Copy Image" action:
// - the default one use the fully-encoded URL to fetch the image while // - the default one use the fully-encoded URL to fetch the image while
// Windows seems to not recognize it. // Windows seems to not recognize it.

View File

@ -27,6 +27,8 @@ namespace vnotex
signals: signals:
void zoomFactorChanged(qreal p_factor); void zoomFactorChanged(qreal p_factor);
void editRequested();
protected: protected:
void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE; void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE;

View File

@ -296,10 +296,10 @@ void MainWindow::closeEvent(QCloseEvent *p_event)
if(!isExit && toTray == -1){ if(!isExit && toTray == -1){
int ret = MessageBoxHelper::questionYesNo(MessageBoxHelper::Question, int ret = MessageBoxHelper::questionYesNo(MessageBoxHelper::Question,
tr("Close %1").arg(qApp->applicationName()),
tr("Do you want to minimize %1 to system tray " tr("Do you want to minimize %1 to system tray "
"instead of quitting when closing %1?").arg(qApp->applicationName()), "instead of quitting when closed?").arg(qApp->applicationName()),
tr("You could change the option in Settings later."), tr("You could change the option in Settings later."),
QString(),
this); this);
if (ret == QMessageBox::Yes) { if (ret == QMessageBox::Yes) {
ConfigMgr::getInst().getSessionConfig().setMinimizeToSystemTray(true); ConfigMgr::getInst().getSessionConfig().setMinimizeToSystemTray(true);

View File

@ -342,6 +342,11 @@ void MarkdownViewWindow::setupTextEditor()
m_outlineProvider->setCurrentHeadingIndex(m_editor->getCurrentHeadingIndex()); m_outlineProvider->setCurrentHeadingIndex(m_editor->getCurrentHeadingIndex());
} }
}); });
connect(m_editor, &MarkdownEditor::readRequested,
this, [this]() {
read(true);
});
} }
QStackedWidget *MarkdownViewWindow::getMainStatusWidget() const QStackedWidget *MarkdownViewWindow::getMainStatusWidget() const
@ -419,6 +424,11 @@ void MarkdownViewWindow::setupViewer()
showMessage(p_url); showMessage(p_url);
}); });
connect(m_viewer, &MarkdownViewer::editRequested,
this, [this]() {
edit();
});
// Connect outline pipeline. // Connect outline pipeline.
connect(adapter, &MarkdownViewerAdapter::headingsChanged, connect(adapter, &MarkdownViewerAdapter::headingsChanged,
this, [this]() { this, [this]() {

View File

@ -283,36 +283,26 @@ QAction *ViewWindow::addAction(QToolBar *p_toolBar, ViewWindowToolBarHelper::Act
case ViewWindowToolBarHelper::EditReadDiscard: case ViewWindowToolBarHelper::EditReadDiscard:
{ {
// A combined button with Edit/Read/Discard. // A combined button with Edit/Read/Discard.
Q_ASSERT(!m_editReadDiscardAct);
act = ViewWindowToolBarHelper::addAction(p_toolBar, p_action); act = ViewWindowToolBarHelper::addAction(p_toolBar, p_action);
auto erdAct = dynamic_cast<EditReadDiscardAction *>(act); m_editReadDiscardAct = dynamic_cast<EditReadDiscardAction *>(act);
Q_ASSERT(erdAct);
connect(this, &ViewWindow::modeChanged, connect(this, &ViewWindow::modeChanged,
this, [this, erdAct]() { this, [this]() {
updateEditReadDiscardActionState(erdAct); updateEditReadDiscardActionState(m_editReadDiscardAct);
}); });
connect(erdAct, QOverload<EditReadDiscardAction::Action>::of(&EditReadDiscardAction::triggered), connect(m_editReadDiscardAct, QOverload<EditReadDiscardAction::Action>::of(&EditReadDiscardAction::triggered),
this, [this, erdAct](EditReadDiscardAction::Action p_act) { this, [this](EditReadDiscardAction::Action p_act) {
int ret = checkFileMissingOrChangedOutside();
if (Normal != ret && SavedOrReloaded != ret) {
// Recover the icon of the action.
updateEditReadDiscardActionState(erdAct);
return;
}
switch (p_act) { switch (p_act) {
case EditReadDiscardAction::Action::Edit: case EditReadDiscardAction::Action::Edit:
setMode(Mode::Edit); edit();
break; break;
case EditReadDiscardAction::Action::Read: case EditReadDiscardAction::Action::Read:
if (save(false)) { read(true);
setMode(Mode::Read);
}
break; break;
case EditReadDiscardAction::Action::Discard: case EditReadDiscardAction::Action::Discard:
discardChangesAndRead(); read(false);
break; break;
} }
setFocus();
}); });
break; break;
} }
@ -1027,3 +1017,35 @@ void ViewWindow::showMessage(const QString p_msg)
VNoteX::getInst().showStatusMessageShort(p_msg); VNoteX::getInst().showStatusMessageShort(p_msg);
} }
} }
void ViewWindow::edit()
{
int ret = checkFileMissingOrChangedOutside();
if (Normal != ret && SavedOrReloaded != ret) {
// Recover the icon of the action.
updateEditReadDiscardActionState(m_editReadDiscardAct);
return;
}
setMode(Mode::Edit);
setFocus();
}
void ViewWindow::read(bool p_save)
{
int ret = checkFileMissingOrChangedOutside();
if (Normal != ret && SavedOrReloaded != ret) {
// Recover the icon of the action.
updateEditReadDiscardActionState(m_editReadDiscardAct);
return;
}
if (p_save) {
if (save(false)) {
setMode(Mode::Read);
}
} else {
discardChangesAndRead();
}
setFocus();
}

View File

@ -210,6 +210,10 @@ namespace vnotex
void showReplaceResult(const QString &p_text, int p_totalReplaces); void showReplaceResult(const QString &p_text, int p_totalReplaces);
void edit();
void read(bool p_save);
static ViewWindow::Mode modeFromOpenParameters(const FileOpenParameters &p_paras); static ViewWindow::Mode modeFromOpenParameters(const FileOpenParameters &p_paras);
// The revision of the buffer of the last sync content. // The revision of the buffer of the last sync content.
@ -310,6 +314,8 @@ namespace vnotex
QSharedPointer<StatusWidget> m_statusWidget; QSharedPointer<StatusWidget> m_statusWidget;
EditReadDiscardAction *m_editReadDiscardAct = nullptr;
static QIcon s_savedIcon; static QIcon s_savedIcon;
static QIcon s_modifiedIcon; static QIcon s_modifiedIcon;
}; };