diff --git a/dist/changes-2.0.0 b/dist/changes-2.0.0 index e49cf5c20250fea1460e8930bea174bfcb33d7a8..2bcc710c06835b70626e95031cb17880cbf5362e 100644 --- a/dist/changes-2.0.0 +++ b/dist/changes-2.0.0 @@ -30,6 +30,8 @@ Editing * Open with "System Editor" * Fixed missing semicolon after } when creating unnamed enumeration * Fixed auto indent for C style coments + * Copying text from the editor now supports HTML mime type, preserving + the syntax highlighting. C++ Support * Recognize *.cu as C files diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 5f23328526c6b45aa217e7767368884ebc156e4e..7bcca16b8485867d843743cc9997d253564c8acd 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -69,6 +69,7 @@ #include <QtGui/QStyle> #include <QtGui/QSyntaxHighlighter> #include <QtGui/QTextCursor> +#include <QtGui/QTextDocumentFragment> #include <QtGui/QTextBlock> #include <QtGui/QTextLayout> #include <QtGui/QToolBar> @@ -5327,13 +5328,57 @@ QMimeData *BaseTextEditor::createMimeDataFromSelection() const mimeData->setData(QLatin1String("application/vnd.nokia.qtcreator.vblocktext"), text.toUtf8()); mimeData->setText(text); // for exchangeability return mimeData; - } else if (textCursor().hasSelection()){ + } else if (textCursor().hasSelection()) { QTextCursor cursor = textCursor(); QMimeData *mimeData = new QMimeData; + + // Copy the selected text as plain text QString text = cursor.selectedText(); convertToPlainText(text); mimeData->setText(text); + // Copy the selected text as HTML + { + // Create a new document from the selected text document fragment + QTextDocument *tempDocument = new QTextDocument; + QTextCursor tempCursor(tempDocument); + tempCursor.insertFragment(cursor.selection()); + + // Apply the additional formats set by the syntax highlighter + QTextBlock start = document()->findBlock(cursor.selectionStart()); + QTextBlock end = document()->findBlock(cursor.selectionEnd()); + end = end.next(); + + const int selectionStart = cursor.selectionStart(); + const int endOfDocument = tempDocument->characterCount() - 1; + for (QTextBlock current = start; current.isValid() && current != end; current = current.next()) { + const QTextLayout *layout = current.layout(); + foreach (const QTextLayout::FormatRange &range, layout->additionalFormats()) { + const int start = current.position() + range.start - selectionStart; + const int end = start + range.length; + if (end <= 0 || start >= endOfDocument) + continue; + tempCursor.setPosition(qMax(start, 0)); + tempCursor.setPosition(qMin(end, endOfDocument), QTextCursor::KeepAnchor); + tempCursor.setCharFormat(range.format); + } + } + + // Reset the user states since they are not interesting + for (QTextBlock block = tempDocument->begin(); block.isValid(); block = block.next()) + block.setUserState(-1); + + // Make sure the text appears pre-formatted + tempCursor.setPosition(0); + tempCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); + QTextBlockFormat blockFormat = tempCursor.blockFormat(); + blockFormat.setNonBreakableLines(true); + tempCursor.setBlockFormat(blockFormat); + + mimeData->setHtml(tempCursor.selection().toHtml()); + delete tempDocument; + } + /* Try to figure out whether we are copying an entire block, and store the complete block including indentation in the qtcreator.blocktext mimetype.