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 bool selectionVisible(int blockNumber) const;
|
||||
virtual bool replacementVisible(int blockNumber) const;
|
||||
QString plainTextFromSelection(const QTextCursor &cursor) const;
|
||||
virtual void paintEvent(QPaintEvent *e);
|
||||
virtual void scrollContentsBy(int dx, int dy);
|
||||
|
||||
@@ -137,6 +138,41 @@ bool DiffViewEditorWidget::replacementVisible(int blockNumber) const
|
||||
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)
|
||||
{
|
||||
m_lineNumbers.insert(blockNumber, lineNumber);
|
||||
|
@@ -170,10 +170,8 @@ Core::IEditor *BaseTextEditorWidget::openEditorAt(const QString &fileName, int l
|
||||
return editor;
|
||||
}
|
||||
|
||||
QString BaseTextEditorWidget::plainTextFromSelection() const
|
||||
QString BaseTextEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const
|
||||
{
|
||||
QTextCursor cursor = textCursor();
|
||||
|
||||
// Copy the selected text as plain text
|
||||
QString text = cursor.selectedText();
|
||||
return convertToPlainText(text);
|
||||
@@ -2863,7 +2861,13 @@ QString BaseTextEditorWidgetPrivate::copyBlockSelection()
|
||||
const TabSettings &ts = q->tabSettings();
|
||||
QTextBlock block = m_blockSelection.firstBlock.block();
|
||||
QTextBlock lastBlock = m_blockSelection.lastBlock.block();
|
||||
bool textInserted = false;
|
||||
for (;;) {
|
||||
if (q->selectionVisible(block.blockNumber())) {
|
||||
if (textInserted)
|
||||
selection += QLatin1Char('\n');
|
||||
textInserted = true;
|
||||
|
||||
QString text = block.text();
|
||||
int startOffset = 0;
|
||||
int startPos = ts.positionAtColumn(text, m_blockSelection.firstVisualColumn, &startOffset);
|
||||
@@ -2883,9 +2887,10 @@ QString BaseTextEditorWidgetPrivate::copyBlockSelection()
|
||||
else if (endOffset > 0)
|
||||
selection += QString(endOffset, QLatin1Char(' '));
|
||||
}
|
||||
}
|
||||
if (block == lastBlock)
|
||||
break;
|
||||
selection += QLatin1Char('\n');
|
||||
|
||||
block = block.next();
|
||||
}
|
||||
return selection;
|
||||
@@ -5977,7 +5982,7 @@ QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const
|
||||
QTextCursor cursor = textCursor();
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
|
||||
QString text = plainTextFromSelection();
|
||||
QString text = plainTextFromSelection(cursor);
|
||||
mimeData->setText(text);
|
||||
|
||||
// Copy the selected text as HTML
|
||||
@@ -5989,22 +5994,34 @@ QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const
|
||||
|
||||
// Apply the additional formats set by the syntax highlighter
|
||||
QTextBlock start = document()->findBlock(cursor.selectionStart());
|
||||
QTextBlock end = document()->findBlock(cursor.selectionEnd());
|
||||
end = end.next();
|
||||
QTextBlock last = document()->findBlock(cursor.selectionEnd());
|
||||
QTextBlock end = last.next();
|
||||
|
||||
const int selectionStart = cursor.selectionStart();
|
||||
const int endOfDocument = tempDocument->characterCount() - 1;
|
||||
int removedCount = 0;
|
||||
for (QTextBlock current = start; current.isValid() && current != end; current = current.next()) {
|
||||
if (selectionVisible(current.blockNumber())) {
|
||||
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)
|
||||
const int startPosition = current.position() + range.start - selectionStart - removedCount;
|
||||
const int endPosition = startPosition + range.length;
|
||||
if (endPosition <= 0 || startPosition >= endOfDocument)
|
||||
continue;
|
||||
tempCursor.setPosition(qMax(start, 0));
|
||||
tempCursor.setPosition(qMin(end, endOfDocument), QTextCursor::KeepAnchor);
|
||||
tempCursor.setPosition(qMax(startPosition, 0));
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// Reset the user states since they are not interesting
|
||||
@@ -6041,7 +6058,7 @@ QMimeData *BaseTextEditorWidget::createMimeDataFromSelection() const
|
||||
selend.movePosition(QTextCursor::StartOfBlock);
|
||||
cursor.setPosition(selstart.position());
|
||||
cursor.setPosition(selend.position(), QTextCursor::KeepAnchor);
|
||||
text = cursor.selectedText();
|
||||
text = plainTextFromSelection(cursor);
|
||||
mimeData->setData(QLatin1String(kTextBlockMimeType), text.toUtf8());
|
||||
}
|
||||
return mimeData;
|
||||
|
@@ -354,7 +354,7 @@ protected:
|
||||
bool canInsertFromMimeData(const QMimeData *source) const;
|
||||
void insertFromMimeData(const QMimeData *source);
|
||||
|
||||
virtual QString plainTextFromSelection() const;
|
||||
virtual QString plainTextFromSelection(const QTextCursor &cursor) const;
|
||||
static QString convertToPlainText(const QString &txt);
|
||||
|
||||
virtual QString lineNumber(int blockNumber) const;
|
||||
|
Reference in New Issue
Block a user