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
OpenOffice.org, 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/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.
......
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