forked from qt-creator/qt-creator
Omit separator lines when copying selected text
In addition rename int start -> startPosition and int end -> endPosition to avoid a name clash with QTextBlock start inside BaseTextEditorWidget::createMimeDataFromSelection(). Change-Id: I7f54e4046913b5d5d9ddd3c07fd2747b4ca6f3fb Reviewed-by: hjk <hjk121@nokiamail.com> Reviewed-by: David Schulz <david.schulz@digia.com>
This commit is contained in:
@@ -97,6 +97,7 @@ protected:
|
|||||||
virtual int lineNumberDigits() const;
|
virtual int lineNumberDigits() const;
|
||||||
virtual bool selectionVisible(int blockNumber) const;
|
virtual bool selectionVisible(int blockNumber) const;
|
||||||
virtual bool replacementVisible(int blockNumber) const;
|
virtual bool replacementVisible(int blockNumber) const;
|
||||||
|
QString plainTextFromSelection(const QTextCursor &cursor) const;
|
||||||
virtual void paintEvent(QPaintEvent *e);
|
virtual void paintEvent(QPaintEvent *e);
|
||||||
virtual void scrollContentsBy(int dx, int dy);
|
virtual void scrollContentsBy(int dx, int dy);
|
||||||
|
|
||||||
@@ -137,6 +138,41 @@ bool DiffViewEditorWidget::replacementVisible(int blockNumber) const
|
|||||||
return m_skippedLines.value(blockNumber);
|
return m_skippedLines.value(blockNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DiffViewEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const
|
||||||
|
{
|
||||||
|
const int startPosition = cursor.selectionStart();
|
||||||
|
const int endPosition = cursor.selectionEnd();
|
||||||
|
if (startPosition == endPosition)
|
||||||
|
return QString(); // no selection
|
||||||
|
|
||||||
|
QTextBlock startBlock = document()->findBlock(startPosition);
|
||||||
|
QTextBlock endBlock = document()->findBlock(endPosition);
|
||||||
|
QTextBlock block = startBlock;
|
||||||
|
QString text;
|
||||||
|
bool textInserted = false;
|
||||||
|
while (block.isValid() && block.blockNumber() <= endBlock.blockNumber()) {
|
||||||
|
if (selectionVisible(block.blockNumber())) {
|
||||||
|
if (block == startBlock) {
|
||||||
|
if (block == endBlock)
|
||||||
|
text = cursor.selectedText(); // just one line text
|
||||||
|
else
|
||||||
|
text = block.text().mid(startPosition - block.position());
|
||||||
|
} else {
|
||||||
|
if (textInserted)
|
||||||
|
text += QLatin1Char('\n');
|
||||||
|
if (block == endBlock)
|
||||||
|
text += block.text().left(endPosition - block.position());
|
||||||
|
else
|
||||||
|
text += block.text();
|
||||||
|
}
|
||||||
|
textInserted = true;
|
||||||
|
}
|
||||||
|
block = block.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
return convertToPlainText(text);
|
||||||
|
}
|
||||||
|
|
||||||
void DiffViewEditorWidget::setLineNumber(int blockNumber, const QString &lineNumber)
|
void DiffViewEditorWidget::setLineNumber(int blockNumber, const QString &lineNumber)
|
||||||
{
|
{
|
||||||
m_lineNumbers.insert(blockNumber, lineNumber);
|
m_lineNumbers.insert(blockNumber, lineNumber);
|
||||||
|
@@ -170,10 +170,8 @@ Core::IEditor *BaseTextEditorWidget::openEditorAt(const QString &fileName, int l
|
|||||||
return editor;
|
return editor;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString BaseTextEditorWidget::plainTextFromSelection() const
|
QString BaseTextEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const
|
||||||
{
|
{
|
||||||
QTextCursor cursor = textCursor();
|
|
||||||
|
|
||||||
// Copy the selected text as plain text
|
// Copy the selected text as plain text
|
||||||
QString text = cursor.selectedText();
|
QString text = cursor.selectedText();
|
||||||
return convertToPlainText(text);
|
return convertToPlainText(text);
|
||||||
@@ -2863,29 +2861,36 @@ QString BaseTextEditorWidgetPrivate::copyBlockSelection()
|
|||||||
const TabSettings &ts = q->tabSettings();
|
const TabSettings &ts = q->tabSettings();
|
||||||
QTextBlock block = m_blockSelection.firstBlock.block();
|
QTextBlock block = m_blockSelection.firstBlock.block();
|
||||||
QTextBlock lastBlock = m_blockSelection.lastBlock.block();
|
QTextBlock lastBlock = m_blockSelection.lastBlock.block();
|
||||||
|
bool textInserted = false;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
QString text = block.text();
|
if (q->selectionVisible(block.blockNumber())) {
|
||||||
int startOffset = 0;
|
if (textInserted)
|
||||||
int startPos = ts.positionAtColumn(text, m_blockSelection.firstVisualColumn, &startOffset);
|
selection += QLatin1Char('\n');
|
||||||
int endOffset = 0;
|
textInserted = true;
|
||||||
int endPos = ts.positionAtColumn(text, m_blockSelection.lastVisualColumn, &endOffset);
|
|
||||||
|
|
||||||
if (startPos == endPos) {
|
QString text = block.text();
|
||||||
selection += QString(endOffset - startOffset, QLatin1Char(' '));
|
int startOffset = 0;
|
||||||
} else {
|
int startPos = ts.positionAtColumn(text, m_blockSelection.firstVisualColumn, &startOffset);
|
||||||
if (startOffset < 0)
|
int endOffset = 0;
|
||||||
selection += QString(-startOffset, QLatin1Char(' '));
|
int endPos = ts.positionAtColumn(text, m_blockSelection.lastVisualColumn, &endOffset);
|
||||||
if (endOffset < 0)
|
|
||||||
--endPos;
|
if (startPos == endPos) {
|
||||||
selection += text.mid(startPos, endPos - startPos);
|
selection += QString(endOffset - startOffset, QLatin1Char(' '));
|
||||||
if (endOffset < 0)
|
} else {
|
||||||
selection += QString(ts.m_tabSize + endOffset, QLatin1Char(' '));
|
if (startOffset < 0)
|
||||||
else if (endOffset > 0)
|
selection += QString(-startOffset, QLatin1Char(' '));
|
||||||
selection += QString(endOffset, QLatin1Char(' '));
|
if (endOffset < 0)
|
||||||
|
--endPos;
|
||||||
|
selection += text.mid(startPos, endPos - startPos);
|
||||||
|
if (endOffset < 0)
|
||||||
|
selection += QString(ts.m_tabSize + endOffset, QLatin1Char(' '));
|
||||||
|
else if (endOffset > 0)
|
||||||
|
selection += QString(endOffset, QLatin1Char(' '));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (block == lastBlock)
|
if (block == lastBlock)
|
||||||
break;
|
break;
|
||||||
selection += QLatin1Char('\n');
|
|
||||||
block = block.next();
|
block = block.next();
|
||||||
}
|
}
|
||||||
return selection;
|
return selection;
|
||||||
@@ -5977,7 +5982,7 @@ QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const
|
|||||||
QTextCursor cursor = textCursor();
|
QTextCursor cursor = textCursor();
|
||||||
QMimeData *mimeData = new QMimeData;
|
QMimeData *mimeData = new QMimeData;
|
||||||
|
|
||||||
QString text = plainTextFromSelection();
|
QString text = plainTextFromSelection(cursor);
|
||||||
mimeData->setText(text);
|
mimeData->setText(text);
|
||||||
|
|
||||||
// Copy the selected text as HTML
|
// Copy the selected text as HTML
|
||||||
@@ -5989,21 +5994,33 @@ QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const
|
|||||||
|
|
||||||
// Apply the additional formats set by the syntax highlighter
|
// Apply the additional formats set by the syntax highlighter
|
||||||
QTextBlock start = document()->findBlock(cursor.selectionStart());
|
QTextBlock start = document()->findBlock(cursor.selectionStart());
|
||||||
QTextBlock end = document()->findBlock(cursor.selectionEnd());
|
QTextBlock last = document()->findBlock(cursor.selectionEnd());
|
||||||
end = end.next();
|
QTextBlock end = last.next();
|
||||||
|
|
||||||
const int selectionStart = cursor.selectionStart();
|
const int selectionStart = cursor.selectionStart();
|
||||||
const int endOfDocument = tempDocument->characterCount() - 1;
|
const int endOfDocument = tempDocument->characterCount() - 1;
|
||||||
|
int removedCount = 0;
|
||||||
for (QTextBlock current = start; current.isValid() && current != end; current = current.next()) {
|
for (QTextBlock current = start; current.isValid() && current != end; current = current.next()) {
|
||||||
const QTextLayout *layout = current.layout();
|
if (selectionVisible(current.blockNumber())) {
|
||||||
foreach (const QTextLayout::FormatRange &range, layout->additionalFormats()) {
|
const QTextLayout *layout = current.layout();
|
||||||
const int start = current.position() + range.start - selectionStart;
|
foreach (const QTextLayout::FormatRange &range, layout->additionalFormats()) {
|
||||||
const int end = start + range.length;
|
const int startPosition = current.position() + range.start - selectionStart - removedCount;
|
||||||
if (end <= 0 || start >= endOfDocument)
|
const int endPosition = startPosition + range.length;
|
||||||
continue;
|
if (endPosition <= 0 || startPosition >= endOfDocument)
|
||||||
tempCursor.setPosition(qMax(start, 0));
|
continue;
|
||||||
tempCursor.setPosition(qMin(end, endOfDocument), QTextCursor::KeepAnchor);
|
tempCursor.setPosition(qMax(startPosition, 0));
|
||||||
tempCursor.setCharFormat(range.format);
|
tempCursor.setPosition(qMin(endPosition, endOfDocument), QTextCursor::KeepAnchor);
|
||||||
|
tempCursor.setCharFormat(range.format);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const int startPosition = current.position() - start.position() - removedCount;
|
||||||
|
int endPosition = startPosition + current.text().count();
|
||||||
|
if (current != last)
|
||||||
|
endPosition++;
|
||||||
|
removedCount += endPosition - startPosition;
|
||||||
|
tempCursor.setPosition(startPosition);
|
||||||
|
tempCursor.setPosition(endPosition, QTextCursor::KeepAnchor);
|
||||||
|
tempCursor.deleteChar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6041,7 +6058,7 @@ QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const
|
|||||||
selend.movePosition(QTextCursor::StartOfBlock);
|
selend.movePosition(QTextCursor::StartOfBlock);
|
||||||
cursor.setPosition(selstart.position());
|
cursor.setPosition(selstart.position());
|
||||||
cursor.setPosition(selend.position(), QTextCursor::KeepAnchor);
|
cursor.setPosition(selend.position(), QTextCursor::KeepAnchor);
|
||||||
text = cursor.selectedText();
|
text = plainTextFromSelection(cursor);
|
||||||
mimeData->setData(QLatin1String(kTextBlockMimeType), text.toUtf8());
|
mimeData->setData(QLatin1String(kTextBlockMimeType), text.toUtf8());
|
||||||
}
|
}
|
||||||
return mimeData;
|
return mimeData;
|
||||||
|
@@ -354,7 +354,7 @@ protected:
|
|||||||
bool canInsertFromMimeData(const QMimeData *source) const;
|
bool canInsertFromMimeData(const QMimeData *source) const;
|
||||||
void insertFromMimeData(const QMimeData *source);
|
void insertFromMimeData(const QMimeData *source);
|
||||||
|
|
||||||
virtual QString plainTextFromSelection() const;
|
virtual QString plainTextFromSelection(const QTextCursor &cursor) const;
|
||||||
static QString convertToPlainText(const QString &txt);
|
static QString convertToPlainText(const QString &txt);
|
||||||
|
|
||||||
virtual QString lineNumber(int blockNumber) const;
|
virtual QString lineNumber(int blockNumber) const;
|
||||||
|
Reference in New Issue
Block a user