forked from qt-creator/qt-creator
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 <hjk@qt.io> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -6838,6 +6838,13 @@ void TextEditorWidget::copyLine()
|
|||||||
copy();
|
copy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextEditorWidget::copyWithHtml()
|
||||||
|
{
|
||||||
|
if (!multiTextCursor().hasSelection())
|
||||||
|
return;
|
||||||
|
QGuiApplication::clipboard()->setMimeData(createMimeDataFromSelection(true));
|
||||||
|
}
|
||||||
|
|
||||||
void TextEditorWidgetPrivate::addCursorsToLineEnds()
|
void TextEditorWidgetPrivate::addCursorsToLineEnds()
|
||||||
{
|
{
|
||||||
MultiTextCursor multiCursor = q->multiTextCursor();
|
MultiTextCursor multiCursor = q->multiTextCursor();
|
||||||
@@ -7522,6 +7529,11 @@ void TextEditorWidget::switchUtf8bom()
|
|||||||
}
|
}
|
||||||
|
|
||||||
QMimeData *TextEditorWidget::createMimeDataFromSelection() const
|
QMimeData *TextEditorWidget::createMimeDataFromSelection() const
|
||||||
|
{
|
||||||
|
return createMimeDataFromSelection(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
QMimeData *TextEditorWidget::createMimeDataFromSelection(bool withHtml) const
|
||||||
{
|
{
|
||||||
if (multiTextCursor().hasSelection()) {
|
if (multiTextCursor().hasSelection()) {
|
||||||
auto mimeData = new QMimeData;
|
auto mimeData = new QMimeData;
|
||||||
@@ -7530,7 +7542,7 @@ QMimeData *TextEditorWidget::createMimeDataFromSelection() const
|
|||||||
mimeData->setText(text);
|
mimeData->setText(text);
|
||||||
|
|
||||||
// Copy the selected text as HTML
|
// Copy the selected text as HTML
|
||||||
{
|
if (withHtml) {
|
||||||
// Create a new document from the selected text document fragment
|
// Create a new document from the selected text document fragment
|
||||||
auto tempDocument = new QTextDocument;
|
auto tempDocument = new QTextDocument;
|
||||||
QTextCursor tempCursor(tempDocument);
|
QTextCursor tempCursor(tempDocument);
|
||||||
|
|||||||
@@ -347,6 +347,7 @@ public:
|
|||||||
|
|
||||||
void cutLine();
|
void cutLine();
|
||||||
void copyLine();
|
void copyLine();
|
||||||
|
void copyWithHtml();
|
||||||
void duplicateSelection();
|
void duplicateSelection();
|
||||||
void duplicateSelectionAndComment();
|
void duplicateSelectionAndComment();
|
||||||
void deleteLine();
|
void deleteLine();
|
||||||
@@ -512,6 +513,7 @@ protected:
|
|||||||
void dragEnterEvent(QDragEnterEvent *e) override;
|
void dragEnterEvent(QDragEnterEvent *e) override;
|
||||||
|
|
||||||
QMimeData *createMimeDataFromSelection() const override;
|
QMimeData *createMimeDataFromSelection() const override;
|
||||||
|
QMimeData *createMimeDataFromSelection(bool withHtml) const;
|
||||||
bool canInsertFromMimeData(const QMimeData *source) const override;
|
bool canInsertFromMimeData(const QMimeData *source) const override;
|
||||||
void insertFromMimeData(const QMimeData *source) override;
|
void insertFromMimeData(const QMimeData *source) override;
|
||||||
void dragLeaveEvent(QDragLeaveEvent *e) override;
|
void dragLeaveEvent(QDragLeaveEvent *e) override;
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ public:
|
|||||||
QAction *m_undoAction = nullptr;
|
QAction *m_undoAction = nullptr;
|
||||||
QAction *m_redoAction = nullptr;
|
QAction *m_redoAction = nullptr;
|
||||||
QAction *m_copyAction = nullptr;
|
QAction *m_copyAction = nullptr;
|
||||||
|
QAction *m_copyHtmlAction = nullptr;
|
||||||
QAction *m_cutAction = nullptr;
|
QAction *m_cutAction = nullptr;
|
||||||
QAction *m_autoIndentAction = nullptr;
|
QAction *m_autoIndentAction = nullptr;
|
||||||
QAction *m_autoFormatAction = nullptr;
|
QAction *m_autoFormatAction = nullptr;
|
||||||
@@ -307,6 +308,10 @@ void TextEditorActionHandlerPrivate::createActions()
|
|||||||
[] (TextEditorWidget *w) { w->copyLine(); }, false, tr("Copy &Line"),
|
[] (TextEditorWidget *w) { w->copyLine(); }, false, tr("Copy &Line"),
|
||||||
QKeySequence(tr("Ctrl+Ins")),
|
QKeySequence(tr("Ctrl+Ins")),
|
||||||
G_EDIT_TEXT, advancedEditMenu);
|
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,
|
registerAction(ADD_CURSORS_TO_LINE_ENDS,
|
||||||
[] (TextEditorWidget *w) { w->addCursorsToLineEnds(); }, false, tr("Create Cursors at Selected Line Ends"),
|
[] (TextEditorWidget *w) { w->addCursorsToLineEnds(); }, false, tr("Create Cursors at Selected Line Ends"),
|
||||||
QKeySequence(tr("Alt+Shift+I")),
|
QKeySequence(tr("Alt+Shift+I")),
|
||||||
@@ -496,6 +501,8 @@ void TextEditorActionHandlerPrivate::updateCopyAction(bool hasCopyableText)
|
|||||||
&& !m_currentEditorWidget->isReadOnly());
|
&& !m_currentEditorWidget->isReadOnly());
|
||||||
if (m_copyAction)
|
if (m_copyAction)
|
||||||
m_copyAction->setEnabled(hasCopyableText);
|
m_copyAction->setEnabled(hasCopyableText);
|
||||||
|
if (m_copyHtmlAction)
|
||||||
|
m_copyHtmlAction->setEnabled(hasCopyableText);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor)
|
void TextEditorActionHandlerPrivate::updateCurrentEditor(Core::IEditor *editor)
|
||||||
|
|||||||
@@ -150,6 +150,7 @@ const char MOVE_LINE_UP[] = "TextEditor.MoveLineUp";
|
|||||||
const char MOVE_LINE_DOWN[] = "TextEditor.MoveLineDown";
|
const char MOVE_LINE_DOWN[] = "TextEditor.MoveLineDown";
|
||||||
const char COPY_LINE_UP[] = "TextEditor.CopyLineUp";
|
const char COPY_LINE_UP[] = "TextEditor.CopyLineUp";
|
||||||
const char COPY_LINE_DOWN[] = "TextEditor.CopyLineDown";
|
const char COPY_LINE_DOWN[] = "TextEditor.CopyLineDown";
|
||||||
|
const char COPY_WITH_HTML[] = "TextEditor.CopyWithHtml";
|
||||||
const char JOIN_LINES[] = "TextEditor.JoinLines";
|
const char JOIN_LINES[] = "TextEditor.JoinLines";
|
||||||
const char INSERT_LINE_ABOVE[] = "TextEditor.InsertLineAboveCurrentLine";
|
const char INSERT_LINE_ABOVE[] = "TextEditor.InsertLineAboveCurrentLine";
|
||||||
const char INSERT_LINE_BELOW[] = "TextEditor.InsertLineBelowCurrentLine";
|
const char INSERT_LINE_BELOW[] = "TextEditor.InsertLineBelowCurrentLine";
|
||||||
|
|||||||
Reference in New Issue
Block a user