TextEditor: Remove use of deprecated additionalFormats

Replace with formats

Change-Id: I9a24c0ad138c378aed1c49a15b80b7f64edabe30
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Orgad Shaneh
2016-08-03 23:09:08 +03:00
committed by Orgad Shaneh
parent 8932e0bcb4
commit 6afcf33f7a
7 changed files with 32 additions and 32 deletions

View File

@@ -165,8 +165,8 @@ void CppEditorDocument::applyFontSettings()
// Clear all additional formats since they may have changed // Clear all additional formats since they may have changed
QTextBlock b = document()->firstBlock(); QTextBlock b = document()->firstBlock();
while (b.isValid()) { while (b.isValid()) {
QList<QTextLayout::FormatRange> noFormats; QVector<QTextLayout::FormatRange> noFormats;
highlighter->setExtraAdditionalFormats(b, noFormats); highlighter->setExtraFormats(b, noFormats);
b = b.next(); b = b.next();
} }
} }

View File

@@ -77,7 +77,7 @@ public:
// But then again, the wording of the text most likely // But then again, the wording of the text most likely
// doesn't work if you split it up, nor are our parsers // doesn't work if you split it up, nor are our parsers
// anywhere near being that good // anywhere near being that good
QList<QTextLayout::FormatRange> formats; QVector<QTextLayout::FormatRange> formats;
private: private:
void setMark(TextEditor::TextMark *mark); void setMark(TextEditor::TextMark *mark);

View File

@@ -694,7 +694,7 @@ QSize TaskDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelInd
int height = 0; int height = 0;
description.replace(QLatin1Char('\n'), QChar::LineSeparator); description.replace(QLatin1Char('\n'), QChar::LineSeparator);
QTextLayout tl(description); QTextLayout tl(description);
tl.setAdditionalFormats(index.data(TaskModel::Task_t).value<Task>().formats); tl.setFormats(index.data(TaskModel::Task_t).value<Task>().formats);
tl.beginLayout(); tl.beginLayout();
while (true) { while (true) {
QTextLine line = tl.createLine(); QTextLine line = tl.createLine();
@@ -794,7 +794,7 @@ void TaskDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
int height = 0; int height = 0;
description.replace(QLatin1Char('\n'), QChar::LineSeparator); description.replace(QLatin1Char('\n'), QChar::LineSeparator);
QTextLayout tl(description); QTextLayout tl(description);
tl.setAdditionalFormats(index.data(TaskModel::Task_t).value<Task>().formats); tl.setFormats(index.data(TaskModel::Task_t).value<Task>().formats);
tl.beginLayout(); tl.beginLayout();
while (true) { while (true) {
QTextLine line = tl.createLine(); QTextLine line = tl.createLine();

View File

@@ -86,14 +86,14 @@ void SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
// clear formats of blocks until blockNumber // clear formats of blocks until blockNumber
while (currentBlockNumber < blockNumber) { while (currentBlockNumber < blockNumber) {
QList<QTextLayout::FormatRange> noFormats; QVector<QTextLayout::FormatRange> noFormats;
highlighter->setExtraAdditionalFormats(b, noFormats); highlighter->setExtraFormats(b, noFormats);
b = b.next(); b = b.next();
++currentBlockNumber; ++currentBlockNumber;
} }
// collect all the formats for the current line // collect all the formats for the current line
QList<QTextLayout::FormatRange> formats; QVector<QTextLayout::FormatRange> formats;
formats.reserve(to - from); formats.reserve(to - from);
forever { forever {
QTextLayout::FormatRange formatRange; QTextLayout::FormatRange formatRange;
@@ -113,7 +113,7 @@ void SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
if (nextBlockNumber != blockNumber) if (nextBlockNumber != blockNumber)
break; break;
} }
highlighter->setExtraAdditionalFormats(b, formats); highlighter->setExtraFormats(b, formats);
b = b.next(); b = b.next();
++currentBlockNumber; ++currentBlockNumber;
} }
@@ -143,8 +143,8 @@ void SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
QTextBlock b = doc->findBlockByNumber(firstBlockToClear); QTextBlock b = doc->findBlockByNumber(firstBlockToClear);
while (b.isValid()) { while (b.isValid()) {
QList<QTextLayout::FormatRange> noFormats; QVector<QTextLayout::FormatRange> noFormats;
highlighter->setExtraAdditionalFormats(b, noFormats); highlighter->setExtraFormats(b, noFormats);
b = b.next(); b = b.next();
} }
} }

View File

@@ -100,14 +100,14 @@ void SyntaxHighlighterPrivate::applyFormatChanges(int from, int charsRemoved, in
QTextLayout *layout = currentBlock.layout(); QTextLayout *layout = currentBlock.layout();
QList<QTextLayout::FormatRange> ranges = layout->additionalFormats(); QVector<QTextLayout::FormatRange> ranges = layout->formats();
bool doAdjustRange = currentBlock.contains(from); bool doAdjustRange = currentBlock.contains(from);
QList<QTextLayout::FormatRange> old_ranges; QVector<QTextLayout::FormatRange> old_ranges;
if (!ranges.isEmpty()) { if (!ranges.isEmpty()) {
QList<QTextLayout::FormatRange>::Iterator it = ranges.begin(); auto it = ranges.begin();
while (it != ranges.end()) { while (it != ranges.end()) {
if (it->format.property(QTextFormat::UserProperty).toBool()) { if (it->format.property(QTextFormat::UserProperty).toBool()) {
if (doAdjustRange) if (doAdjustRange)
@@ -126,7 +126,7 @@ void SyntaxHighlighterPrivate::applyFormatChanges(int from, int charsRemoved, in
QTextLayout::FormatRange r; QTextLayout::FormatRange r;
r.start = -1; r.start = -1;
QList<QTextLayout::FormatRange> new_ranges; QVector<QTextLayout::FormatRange> new_ranges;
int i = 0; int i = 0;
while (i < formatChanges.count()) { while (i < formatChanges.count()) {
@@ -167,7 +167,7 @@ void SyntaxHighlighterPrivate::applyFormatChanges(int from, int charsRemoved, in
if (formatsChanged) { if (formatsChanged) {
ranges.append(new_ranges); ranges.append(new_ranges);
layout->setAdditionalFormats(ranges); layout->setFormats(ranges);
doc->markContentsDirty(currentBlock.position(), currentBlock.length()); doc->markContentsDirty(currentBlock.position(), currentBlock.length());
} }
} }
@@ -238,10 +238,10 @@ void SyntaxHighlighterPrivate::reformatBlock(const QTextBlock &block, int from,
The SyntaxHighlighter class is a copied and forked version of the QSyntaxHighlighter. There are The SyntaxHighlighter class is a copied and forked version of the QSyntaxHighlighter. There are
a couple of binary incompatible changes that prevent doing this directly in Qt. a couple of binary incompatible changes that prevent doing this directly in Qt.
The main difference from the QSyntaxHighlighter is the addition of setExtraAdditionalFormats. The main difference from the QSyntaxHighlighter is the addition of setExtraFormats.
This method prevents redoing syntax highlighting when setting the additionalFormats on the This method prevents redoing syntax highlighting when setting the formats on the
layout and subsequently marking the document contents dirty. It thus prevents the redoing of the layout and subsequently marking the document contents dirty. It thus prevents the redoing of the
semantic highlighting, which sets extra additionalFormats, and so on. semantic highlighting, which sets extra formats, and so on.
Another way to implement the semantic highlighting is to use ExtraSelections on Another way to implement the semantic highlighting is to use ExtraSelections on
Q(Plain)TextEdit. The drawback of QTextEdit::setExtraSelections is that ExtraSelection uses a Q(Plain)TextEdit. The drawback of QTextEdit::setExtraSelections is that ExtraSelection uses a
@@ -255,7 +255,7 @@ void SyntaxHighlighterPrivate::reformatBlock(const QTextBlock &block, int from,
document. This means that every editor needs a highlighter, instead of every document. This document. This means that every editor needs a highlighter, instead of every document. This
could become expensive when multiple editors with the same document are opened. could become expensive when multiple editors with the same document are opened.
So, we use AdditionalFormats, because all those highlights should get removed or redone soon So, we use formats, because all those highlights should get removed or redone soon
after the change happens. after the change happens.
*/ */
@@ -316,7 +316,7 @@ void SyntaxHighlighter::setDocument(QTextDocument *doc)
QTextCursor cursor(d->doc); QTextCursor cursor(d->doc);
cursor.beginEditBlock(); cursor.beginEditBlock();
for (QTextBlock blk = d->doc->begin(); blk.isValid(); blk = blk.next()) for (QTextBlock blk = d->doc->begin(); blk.isValid(); blk = blk.next())
blk.layout()->clearAdditionalFormats(); blk.layout()->clearFormats();
cursor.endEditBlock(); cursor.endEditBlock();
} }
d->doc = doc; d->doc = doc;
@@ -635,10 +635,10 @@ static bool byStartOfRange(const QTextLayout::FormatRange &range, const QTextLay
// The formats is passed in by reference in order to prevent unnecessary copying of its items. // The formats is passed in by reference in order to prevent unnecessary copying of its items.
// After this function returns, the list is modified, and should be considered invalidated! // After this function returns, the list is modified, and should be considered invalidated!
void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block, void SyntaxHighlighter::setExtraFormats(const QTextBlock &block,
QList<QTextLayout::FormatRange> &formats) QVector<QTextLayout::FormatRange> &formats)
{ {
// qDebug() << "setAdditionalFormats() on block" << block.blockNumber(); // qDebug() << "setFormats() on block" << block.blockNumber();
// qDebug() << " is valid:" << (block.isValid() ? "Yes" : "No"); // qDebug() << " is valid:" << (block.isValid() ? "Yes" : "No");
// qDebug() << " has layout:" << (block.layout() ? "Yes" : "No"); // qDebug() << " has layout:" << (block.layout() ? "Yes" : "No");
// if (block.layout()) qDebug() << " has text:" << (block.text().isEmpty() ? "No" : "Yes"); // if (block.layout()) qDebug() << " has text:" << (block.text().isEmpty() ? "No" : "Yes");
@@ -655,9 +655,9 @@ void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
Utils::sort(formats, byStartOfRange); Utils::sort(formats, byStartOfRange);
const QList<QTextLayout::FormatRange> all = block.layout()->additionalFormats(); const QVector<QTextLayout::FormatRange> all = block.layout()->formats();
QList<QTextLayout::FormatRange> previousSemanticFormats; QVector<QTextLayout::FormatRange> previousSemanticFormats;
QList<QTextLayout::FormatRange> formatsToApply; QVector<QTextLayout::FormatRange> formatsToApply;
previousSemanticFormats.reserve(all.size()); previousSemanticFormats.reserve(all.size());
formatsToApply.reserve(all.size() + formats.size()); formatsToApply.reserve(all.size() + formats.size());
@@ -693,7 +693,7 @@ void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
bool wasInReformatBlocks = d->inReformatBlocks; bool wasInReformatBlocks = d->inReformatBlocks;
d->inReformatBlocks = true; d->inReformatBlocks = true;
block.layout()->setAdditionalFormats(formatsToApply); block.layout()->setFormats(formatsToApply);
document()->markContentsDirty(block.position(), blockLength - 1); document()->markContentsDirty(block.position(), blockLength - 1);
d->inReformatBlocks = wasInReformatBlocks; d->inReformatBlocks = wasInReformatBlocks;
} }

View File

@@ -58,7 +58,7 @@ public:
void setDocument(QTextDocument *doc); void setDocument(QTextDocument *doc);
QTextDocument *document() const; QTextDocument *document() const;
void setExtraAdditionalFormats(const QTextBlock& block, QList<QTextLayout::FormatRange> &formats); void setExtraFormats(const QTextBlock &block, QVector<QTextLayout::FormatRange> &formats);
static QList<QColor> generateColors(int n, const QColor &background); static QList<QColor> generateColors(int n, const QColor &background);

View File

@@ -867,7 +867,7 @@ void TextEditorWidgetPrivate::print(QPrinter *printer)
srcBlock = srcBlock.next(), dstBlock = dstBlock.next()) { srcBlock = srcBlock.next(), dstBlock = dstBlock.next()) {
QList<QTextLayout::FormatRange> formatList = srcBlock.layout()->additionalFormats(); QVector<QTextLayout::FormatRange> formatList = srcBlock.layout()->formats();
if (backgroundIsDark) { if (backgroundIsDark) {
// adjust syntax highlighting colors for better contrast // adjust syntax highlighting colors for better contrast
for (int i = formatList.count() - 1; i >= 0; --i) { for (int i = formatList.count() - 1; i >= 0; --i) {
@@ -885,7 +885,7 @@ void TextEditorWidgetPrivate::print(QPrinter *printer)
} }
} }
dstBlock.layout()->setAdditionalFormats(formatList); dstBlock.layout()->setFormats(formatList);
} }
QAbstractTextDocumentLayout *layout = doc->documentLayout(); QAbstractTextDocumentLayout *layout = doc->documentLayout();
@@ -6775,7 +6775,7 @@ QMimeData *TextEditorWidget::createMimeDataFromSelection() const
for (QTextBlock current = start; current.isValid() && current != end; current = current.next()) { for (QTextBlock current = start; current.isValid() && current != end; current = current.next()) {
if (selectionVisible(current.blockNumber())) { if (selectionVisible(current.blockNumber())) {
const QTextLayout *layout = current.layout(); const QTextLayout *layout = current.layout();
foreach (const QTextLayout::FormatRange &range, layout->additionalFormats()) { foreach (const QTextLayout::FormatRange &range, layout->formats()) {
const int startPosition = current.position() + range.start - selectionStart - removedCount; const int startPosition = current.position() + range.start - selectionStart - removedCount;
const int endPosition = startPosition + range.length; const int endPosition = startPosition + range.length;
if (endPosition <= 0 || startPosition >= endOfDocument - removedCount) if (endPosition <= 0 || startPosition >= endOfDocument - removedCount)