optimize max long width calculation

This commit is contained in:
Roy Qu 2024-03-30 18:34:12 +08:00
parent 97f4e93df3
commit 1e73b7beb2
2 changed files with 16 additions and 20 deletions

View File

@ -308,6 +308,7 @@ void Document::beginUpdate()
{ {
if (mUpdateCount == 0) { if (mUpdateCount == 0) {
setUpdateState(true); setUpdateState(true);
beginSetLinesWidth();
} }
mUpdateCount++; mUpdateCount++;
} }
@ -317,6 +318,7 @@ void Document::endUpdate()
mUpdateCount--; mUpdateCount--;
if (mUpdateCount == 0) { if (mUpdateCount == 0) {
setUpdateState(false); setUpdateState(false);
endSetLinesWidth();
} }
} }
@ -470,14 +472,10 @@ void Document::putLine(int index, const QString &s, bool notify) {
listIndexOutOfBounds(index); listIndexOutOfBounds(index);
} }
beginUpdate(); beginUpdate();
int oldMaxWidth = maxLineWidth();
mLines[index]->setLineText(s); mLines[index]->setLineText(s);
int newWidth = mLines[index]->width(true); if (mIndexOfLongestLine == index) {
if (mIndexOfLongestLine == index && oldMaxWidth>newWidth ) { // width is invalidated, so we must recalculate longest line
setIndexOfLongestLine(-1); setIndexOfLongestLine(-1);
} else {
if (newWidth > oldMaxWidth)
setIndexOfLongestLine(index);
} }
if (notify) if (notify)
emit putted(index); emit putted(index);
@ -1193,9 +1191,19 @@ void Document::setLineWidth(int line, const QString &lineText, int newWidth, con
return ; return ;
if (lineText != mLines[line]->lineText()) if (lineText != mLines[line]->lineText())
return; return;
int oldWidth = mLines[line]->mWidth;
mLines[line]->mWidth = newWidth; mLines[line]->mWidth = newWidth;
mLines[line]->mGlyphStartPositionList = glyphStartPositionList; 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()); 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) void Document::setIndexOfLongestLine(int line)
{ {
mIndexOfLongestLine = line; mIndexOfLongestLine = line;
@ -1331,8 +1328,8 @@ int DocumentLine::width(bool forceUpdate)
void DocumentLine::setLineText(const QString &newLineText) void DocumentLine::setLineText(const QString &newLineText)
{ {
mLineText = newLineText; mLineText = newLineText;
mWidth=-1;
mGlyphStartCharList = calcGlyphStartCharList(newLineText); mGlyphStartCharList = calcGlyphStartCharList(newLineText);
invalidateWidth();
} }
void DocumentLine::updateWidth() void DocumentLine::updateWidth()

View File

@ -580,7 +580,6 @@ private:
void endSetLinesWidth(); void endSetLinesWidth();
void setLineWidth(int line, const QString& lineText, int newWidth, const QList<int> glyphStartPositionList); void setLineWidth(int line, const QString& lineText, int newWidth, const QList<int> glyphStartPositionList);
void emitMaxLineWidthChanged(); void emitMaxLineWidthChanged();
void updateLongestLineWidth(int line, int width);
void setIndexOfLongestLine(int line); void setIndexOfLongestLine(int line);
int glyphWidth(const QString& glyph, int left, int glyphWidth(const QString& glyph, int left,