Commit 181b086f authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

Added syntax highlighted code to clipboard as HTML when copying

The generated HTML is not the prettiest HTML in the universe, but it
does the job. When copying code from Qt Creator to Microsoft Office or, the syntax highlighting is now preserved. The code also
pastes as preformatted text.

Reviewed-by: mae
Task-number: QTCREATORBUG-727
parent 9b365395
......@@ -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
......@@ -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/"), 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();
// Copy the selected text as HTML
// Create a new document from the selected text document fragment
QTextDocument *tempDocument = new QTextDocument;
QTextCursor tempCursor(tempDocument);
// Apply the additional formats set by the syntax highlighter
QTextBlock start = document()->findBlock(cursor.selectionStart());
QTextBlock end = document()->findBlock(cursor.selectionEnd());
end =;
const int selectionStart = cursor.selectionStart();
const int endOfDocument = tempDocument->characterCount() - 1;
for (QTextBlock current = start; current.isValid() && current != end; current = {
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)
tempCursor.setPosition(qMax(start, 0));
tempCursor.setPosition(qMin(end, endOfDocument), QTextCursor::KeepAnchor);
// Reset the user states since they are not interesting
for (QTextBlock block = tempDocument->begin(); block.isValid(); block =
// Make sure the text appears pre-formatted
tempCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
QTextBlockFormat blockFormat = tempCursor.blockFormat();
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.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment