Просмотр исходного кода

add read and edit action in view window

Le Tan 5 лет назад
Родитель
Сommit
d51e486a84

+ 12 - 0
src/widgets/editors/markdowneditor.cpp

@@ -839,6 +839,18 @@ void MarkdownEditor::handleContextMenuEvent(QContextMenuEvent *p_event, bool *p_
         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()) {
         QClipboard *clipboard = QApplication::clipboard();
         const QMimeData *mimeData = clipboard->mimeData();

+ 2 - 0
src/widgets/editors/markdowneditor.h

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

+ 16 - 0
src/widgets/editors/markdownviewer.cpp

@@ -12,6 +12,9 @@
 #include <utils/clipboardutils.h>
 #include <utils/fileutils.h>
 #include <utils/utils.h>
+#include <utils/widgetutils.h>
+#include <core/configmgr.h>
+#include <core/editorconfig.h>
 #include "../widgetsfactory.h"
 
 using namespace vnotex;
@@ -112,6 +115,19 @@ void MarkdownViewer::contextMenuEvent(QContextMenuEvent *p_event)
     }
 #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:
     // - the default one use the fully-encoded URL to fetch the image while
     // Windows seems to not recognize it.

+ 2 - 0
src/widgets/editors/markdownviewer.h

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

+ 2 - 2
src/widgets/mainwindow.cpp

@@ -296,10 +296,10 @@ void MainWindow::closeEvent(QCloseEvent *p_event)
 
     if(!isExit && toTray == -1){
         int ret =  MessageBoxHelper::questionYesNo(MessageBoxHelper::Question,
-                                                   tr("Close %1").arg(qApp->applicationName()),
                                                    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."),
+                                                   QString(),
                                                    this);
         if (ret == QMessageBox::Yes) {
             ConfigMgr::getInst().getSessionConfig().setMinimizeToSystemTray(true);

+ 10 - 0
src/widgets/markdownviewwindow.cpp

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

+ 41 - 19
src/widgets/viewwindow.cpp

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

+ 6 - 0
src/widgets/viewwindow.h

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