From 8fab2011ba59ea47e9fae6d8059b4c4c0c9c9203 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Tue, 30 Aug 2022 15:34:20 +0200 Subject: [PATCH] Editor: remove html from default mime data This is known to be expensive and a lot of users actually prefer plain text in the copied mime data to avoid unwanted coloring in 3rd party tools. Add a separate action (Edit > Advanced > Copy With Highlighting) that preserves the original functionality. Change-Id: I0f3c529146ab94d0cd9ce9e3b997c8501a577f03 Reviewed-by: hjk Reviewed-by: Eike Ziller --- src/plugins/texteditor/texteditor.cpp | 14 +++++++++++++- src/plugins/texteditor/texteditor.h | 2 ++ src/plugins/texteditor/texteditoractionhandler.cpp | 7 +++++++ src/plugins/texteditor/texteditorconstants.h | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index f17f6148ccd..20c13095b83 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -6838,6 +6838,13 @@ void TextEditorWidget::copyLine() copy(); } +void TextEditorWidget::copyWithHtml() +{ + if (!multiTextCursor().hasSelection()) + return; + QGuiApplication::clipboard()->setMimeData(createMimeDataFromSelection(true)); +} + void TextEditorWidgetPrivate::addCursorsToLineEnds() { MultiTextCursor multiCursor = q->multiTextCursor(); @@ -7522,6 +7529,11 @@ void TextEditorWidget::switchUtf8bom() } QMimeData *TextEditorWidget::createMimeDataFromSelection() const +{ + return createMimeDataFromSelection(false); +} + +QMimeData *TextEditorWidget::createMimeDataFromSelection(bool withHtml) const { if (multiTextCursor().hasSelection()) { auto mimeData = new QMimeData; @@ -7530,7 +7542,7 @@ QMimeData *TextEditorWidget::createMimeDataFromSelection() const mimeData->setText(text); // Copy the selected text as HTML - { + if (withHtml) { // Create a new document from the selected text document fragment auto tempDocument = new QTextDocument; QTextCursor tempCursor(tempDocument); diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 2aa8bc18846..ae7f2f82cce 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -347,6 +347,7 @@ public: void cutLine(); void copyLine(); + void copyWithHtml(); void duplicateSelection(); void duplicateSelectionAndComment(); void deleteLine(); @@ -512,6 +513,7 @@ protected: void dragEnterEvent(QDragEnterEvent *e) override; QMimeData *createMimeDataFromSelection() const override; + QMimeData *createMimeDataFromSelection(bool withHtml) const; bool canInsertFromMimeData(const QMimeData *source) const override; void insertFromMimeData(const QMimeData *source) override; void dragLeaveEvent(QDragLeaveEvent *e) override; diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp index ee6a2720717..2fe72ab27e8 100644 --- a/src/plugins/texteditor/texteditoractionhandler.cpp +++ b/src/plugins/texteditor/texteditoractionhandler.cpp @@ -104,6 +104,7 @@ public: QAction *m_undoAction = nullptr; QAction *m_redoAction = nullptr; QAction *m_copyAction = nullptr; + QAction *m_copyHtmlAction = nullptr; QAction *m_cutAction = nullptr; QAction *m_autoIndentAction = nullptr; QAction *m_autoFormatAction = nullptr; @@ -307,6 +308,10 @@ void TextEditorActionHandlerPrivate::createActions() [] (TextEditorWidget *w) { w->copyLine(); }, false, tr("Copy &Line"), QKeySequence(tr("Ctrl+Ins")), G_EDIT_TEXT, advancedEditMenu); + m_copyHtmlAction = registerAction(COPY_WITH_HTML, + [] (TextEditorWidget *w) { w->copyWithHtml(); }, true, tr("Copy With Highlighting"), + QKeySequence(), G_EDIT_TEXT, advancedEditMenu); + registerAction(ADD_CURSORS_TO_LINE_ENDS, [] (TextEditorWidget *w) { w->addCursorsToLineEnds(); }, false, tr("Create Cursors at Selected Line Ends"), QKeySequence(tr("Alt+Shift+I")), @@ -496,6 +501,8 @@ void TextEditorActionHandlerPrivate::updateCopyAction(bool hasCopyableText) && !m_currentEditorWidget->isReadOnly()); if (m_copyAction) m_copyAction->setEnabled(hasCopyableText); + if (m_copyHtmlAction) + m_copyHtmlAction->setEnabled(hasCopyableText); } void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor) diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 013005e6584..f37938ee1a8 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -150,6 +150,7 @@ const char MOVE_LINE_UP[] = "TextEditor.MoveLineUp"; const char MOVE_LINE_DOWN[] = "TextEditor.MoveLineDown"; const char COPY_LINE_UP[] = "TextEditor.CopyLineUp"; const char COPY_LINE_DOWN[] = "TextEditor.CopyLineDown"; +const char COPY_WITH_HTML[] = "TextEditor.CopyWithHtml"; const char JOIN_LINES[] = "TextEditor.JoinLines"; const char INSERT_LINE_ABOVE[] = "TextEditor.InsertLineAboveCurrentLine"; const char INSERT_LINE_BELOW[] = "TextEditor.InsertLineBelowCurrentLine";