From 1e73b7beb2f869d3c8d5f0d46128274b49e69478 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sat, 30 Mar 2024 18:34:12 +0800 Subject: [PATCH] optimize max long width calculation --- libs/qsynedit/qsynedit/document.cpp | 35 +++++++++++++---------------- libs/qsynedit/qsynedit/document.h | 1 - 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/libs/qsynedit/qsynedit/document.cpp b/libs/qsynedit/qsynedit/document.cpp index 6487abd2..c8636eb0 100644 --- a/libs/qsynedit/qsynedit/document.cpp +++ b/libs/qsynedit/qsynedit/document.cpp @@ -308,6 +308,7 @@ void Document::beginUpdate() { if (mUpdateCount == 0) { setUpdateState(true); + beginSetLinesWidth(); } mUpdateCount++; } @@ -317,6 +318,7 @@ void Document::endUpdate() mUpdateCount--; if (mUpdateCount == 0) { setUpdateState(false); + endSetLinesWidth(); } } @@ -470,14 +472,10 @@ void Document::putLine(int index, const QString &s, bool notify) { listIndexOutOfBounds(index); } beginUpdate(); - int oldMaxWidth = maxLineWidth(); mLines[index]->setLineText(s); - int newWidth = mLines[index]->width(true); - if (mIndexOfLongestLine == index && oldMaxWidth>newWidth ) { + if (mIndexOfLongestLine == index) { + // width is invalidated, so we must recalculate longest line setIndexOfLongestLine(-1); - } else { - if (newWidth > oldMaxWidth) - setIndexOfLongestLine(index); } if (notify) emit putted(index); @@ -1193,9 +1191,19 @@ void Document::setLineWidth(int line, const QString &lineText, int newWidth, con return ; if (lineText != mLines[line]->lineText()) return; + int oldWidth = mLines[line]->mWidth; mLines[line]->mWidth = newWidth; mLines[line]->mGlyphStartPositionList = glyphStartPositionList; - updateLongestLineWidth(line , newWidth); + if (mIndexOfLongestLine<0) { + setIndexOfLongestLine(line); + } else if (mIndexOfLongestLine == line) { + if (oldWidth > newWidth) + setIndexOfLongestLine(-1); + else + emitMaxLineWidthChanged(); + } else if (mLines[mIndexOfLongestLine]->mWidth < newWidth) { + setIndexOfLongestLine(line); + } Q_ASSERT(mLines[line]->mGlyphStartPositionList.length() == mLines[line]->mGlyphStartCharList.length()); } @@ -1208,17 +1216,6 @@ void Document::emitMaxLineWidthChanged() } } -void Document::updateLongestLineWidth(int line, int width) -{ - if (mIndexOfLongestLine<0) { - setIndexOfLongestLine(line); - } else if (mIndexOfLongestLine == line) { - emitMaxLineWidthChanged(); - } else if (mLines[mIndexOfLongestLine]->mWidth < width) { - setIndexOfLongestLine(line); - } -} - void Document::setIndexOfLongestLine(int line) { mIndexOfLongestLine = line; @@ -1331,8 +1328,8 @@ int DocumentLine::width(bool forceUpdate) void DocumentLine::setLineText(const QString &newLineText) { mLineText = newLineText; - mWidth=-1; mGlyphStartCharList = calcGlyphStartCharList(newLineText); + invalidateWidth(); } void DocumentLine::updateWidth() diff --git a/libs/qsynedit/qsynedit/document.h b/libs/qsynedit/qsynedit/document.h index 3a1d9a1b..96a0660f 100644 --- a/libs/qsynedit/qsynedit/document.h +++ b/libs/qsynedit/qsynedit/document.h @@ -580,7 +580,6 @@ private: void endSetLinesWidth(); void setLineWidth(int line, const QString& lineText, int newWidth, const QList glyphStartPositionList); void emitMaxLineWidthChanged(); - void updateLongestLineWidth(int line, int width); void setIndexOfLongestLine(int line); int glyphWidth(const QString& glyph, int left,