From 43fcdd502ab542b59ed5de8c6b054c5083eb72f7 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Tue, 9 Oct 2018 21:04:33 +0800 Subject: [PATCH] Editor: support Ctrl+Shift+V to paste as plain text --- src/resources/vnote.ini | 2 ++ src/veditor.cpp | 24 ++++++++++++++++++++++++ src/veditor.h | 3 +++ src/vmdeditor.cpp | 8 ++++++++ 4 files changed, 37 insertions(+) diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 1ad03c71..c0b4c8b0 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -429,6 +429,8 @@ OpenViaDefaultProgram=F12 FullScreen=F11 ; Universal Entry UniversalEntry=Ctrl+G +; Paste as plain text +PastePlainText=Ctrl+Shift+V [captain_mode_shortcuts] ; Define shortcuts in Captain mode here. diff --git a/src/veditor.cpp b/src/veditor.cpp index 3e99d2c2..963ce9bd 100644 --- a/src/veditor.cpp +++ b/src/veditor.cpp @@ -105,6 +105,15 @@ void VEditor::init() m_config.init(QFontMetrics(m_editor->font()), false); updateEditConfig(); + + // Init shortcuts. + QKeySequence keySeq(g_config->getShortcutKeySequence("PastePlainText")); + QShortcut *pastePlainTextShortcut = new QShortcut(keySeq, m_editor); + pastePlainTextShortcut->setContext(Qt::WidgetShortcut); + QObject::connect(pastePlainTextShortcut, &QShortcut::activated, + m_object, [this]() { + pastePlainText(); + }); } void VEditor::labelTimerTimeout() @@ -1681,3 +1690,18 @@ void VEditor::nextMatch(bool p_forward) m_findInfo.m_end); } } + +void VEditor::pastePlainText() +{ + if (!m_file || !m_file->isModifiable()) { + return; + } + + QClipboard *clipboard = QApplication::clipboard(); + QString text(clipboard->text()); + if (text.isEmpty()) { + return; + } + + m_editOps->insertText(text); +} diff --git a/src/veditor.h b/src/veditor.h index 42b0cda2..569f6d0e 100644 --- a/src/veditor.h +++ b/src/veditor.h @@ -270,6 +270,9 @@ protected: void addTempFile(const QString &p_file); + // Paste plain text. + void pastePlainText(); + QWidget *m_editor; VEditorObject *m_object; diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index a7325239..a5215163 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -367,6 +367,14 @@ void VMdEditor::contextMenuEvent(QContextMenuEvent *p_event) if (mimeData->hasHtml()) { initPasteAfterParseMenu(pasteAct, menu.data()); } + + QAction *pptAct = new QAction(tr("Paste As Plain Text"), menu.data()); + VUtils::fixTextWithShortcut(pptAct, "PastePlainText"); + connect(pptAct, &QAction::triggered, + this, [this]() { + pastePlainText(); + }); + insertActionAfter(pasteAct, pptAct, menu.data()); } if (!textCursor().hasSelection()) {