fix: cpu load high.

This commit is contained in:
Roy Qu 2024-04-01 15:45:04 +08:00
parent c5b07ff321
commit 9a8751c7c8
3 changed files with 27 additions and 23 deletions

View File

@ -145,19 +145,6 @@ int Document::blockEnded(int line)
int Document::maxLineWidth() { int Document::maxLineWidth() {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (mIndexOfLongestLine < 0) {
int MaxLen = -1;
mIndexOfLongestLine = -1;
if (mLines.count() > 0 ) {
for (int i=0;i<mLines.size();i++) {
int len = mLines[i]->mWidth;
if (len > MaxLen) {
MaxLen = len;
mIndexOfLongestLine = i;
}
}
}
}
if (mIndexOfLongestLine >= 0) { if (mIndexOfLongestLine >= 0) {
return mLines[mIndexOfLongestLine]->width(); return mLines[mIndexOfLongestLine]->width();
} else } else
@ -1179,7 +1166,7 @@ void Document::endSetLinesWidth()
mSetLineWidthLockCount--; mSetLineWidthLockCount--;
if (mSetLineWidthLockCount == 0) { if (mSetLineWidthLockCount == 0) {
if (mMaxLineChangedInSetLinesWidth) if (mMaxLineChangedInSetLinesWidth)
emit maxLineWidthChanged(); updateMaxLineWidthAndNotify();
} }
} }
@ -1195,30 +1182,47 @@ void Document::setLineWidth(int line, const QString &lineText, int newWidth, con
mLines[line]->mGlyphStartPositionList = glyphStartPositionList; mLines[line]->mGlyphStartPositionList = glyphStartPositionList;
if (mIndexOfLongestLine<0) { if (mIndexOfLongestLine<0) {
mIndexOfLongestLine = line; mIndexOfLongestLine = line;
notifyMaxLineWidthChanged(); updateMaxLineWidthChanged();
} else if (mIndexOfLongestLine == line) { } else if (mIndexOfLongestLine == line) {
if (oldWidth > newWidth) { if (oldWidth > newWidth) {
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
notifyMaxLineWidthChanged(); updateMaxLineWidthChanged();
} else { } else if (oldWidth < newWidth) {
notifyMaxLineWidthChanged(); updateMaxLineWidthChanged();
} }
} else if (mLines[mIndexOfLongestLine]->mWidth < newWidth) { } else if (mLines[mIndexOfLongestLine]->mWidth < newWidth) {
mIndexOfLongestLine = line; mIndexOfLongestLine = line;
notifyMaxLineWidthChanged(); updateMaxLineWidthChanged();
} }
Q_ASSERT(mLines[line]->mGlyphStartPositionList.length() == mLines[line]->mGlyphStartCharList.length()); Q_ASSERT(mLines[line]->mGlyphStartPositionList.length() == mLines[line]->mGlyphStartCharList.length());
} }
void Document::notifyMaxLineWidthChanged() void Document::updateMaxLineWidthChanged()
{ {
if (mSetLineWidthLockCount>0) { if (mSetLineWidthLockCount>0) {
mMaxLineChangedInSetLinesWidth = true; mMaxLineChangedInSetLinesWidth = true;
} else { } else {
emit maxLineWidthChanged(); updateMaxLineWidthAndNotify();
} }
} }
void Document::updateMaxLineWidthAndNotify()
{
int MaxLen = -1;
mIndexOfLongestLine = -1;
if (mLines.count() > 0 ) {
for (int i=0;i<mLines.size();i++) {
int len = mLines[i]->mWidth;
if (len > MaxLen) {
MaxLen = len;
mIndexOfLongestLine = i;
}
}
}
if (mIndexOfLongestLine>=0)
emit maxLineWidthChanged();
}
QList<int> Document::calcGlyphPositionList(const QString &lineText, const QList<int> &glyphStartCharList, int left, int &right) const QList<int> Document::calcGlyphPositionList(const QString &lineText, const QList<int> &glyphStartCharList, int left, int &right) const
{ {
return calcGlyphPositionList(lineText, glyphStartCharList, return calcGlyphPositionList(lineText, glyphStartCharList,

View File

@ -579,7 +579,8 @@ private:
void beginSetLinesWidth(); void beginSetLinesWidth();
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 notifyMaxLineWidthChanged(); void updateMaxLineWidthChanged();
void updateMaxLineWidthAndNotify();
int glyphWidth(const QString& glyph, int left, int glyphWidth(const QString& glyph, int left,
const QFontMetrics &fontMetrics, const QFontMetrics &fontMetrics,

View File

@ -930,7 +930,6 @@ void QSynEdit::invalidateLines(int firstLine, int lastLine)
{ {
if (!isVisible()) if (!isVisible())
return; return;
// qDebug()<<"invalidate lines:"<<firstLine<<lastLine;
if (firstLine == -1 && lastLine == -1) { if (firstLine == -1 && lastLine == -1) {
QRect rcInval = clientRect(); QRect rcInval = clientRect();
rcInval.setLeft(rcInval.left()+mGutterWidth); rcInval.setLeft(rcInval.left()+mGutterWidth);