fix h scroll error

This commit is contained in:
Roy Qu 2024-03-30 10:28:33 +08:00
parent 3a098349bc
commit 3681b1f56f
3 changed files with 41 additions and 41 deletions

View File

@ -146,13 +146,11 @@ int Document::blockEnded(int line)
int Document::maxLineWidth() { int Document::maxLineWidth() {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (mIndexOfLongestLine < 0) { if (mIndexOfLongestLine < 0) {
qDebug()<<"!!!!";
int MaxLen = -1; int MaxLen = -1;
mIndexOfLongestLine = -1; mIndexOfLongestLine = -1;
if (mLines.count() > 0 ) { if (mLines.count() > 0 ) {
for (int i=0;i<mLines.size();i++) { for (int i=0;i<mLines.size();i++) {
int len = mLines[i]->mWidth; int len = mLines[i]->mWidth;
qDebug()<<len<<i;
if (len > MaxLen) { if (len > MaxLen) {
MaxLen = len; MaxLen = len;
mIndexOfLongestLine = i; mIndexOfLongestLine = i;
@ -161,7 +159,6 @@ int Document::maxLineWidth() {
} }
} }
if (mIndexOfLongestLine >= 0) { if (mIndexOfLongestLine >= 0) {
qDebug()<<mIndexOfLongestLine<<this->getLine(mIndexOfLongestLine);
return mLines[mIndexOfLongestLine]->width(); return mLines[mIndexOfLongestLine]->width();
} else } else
return -1; return -1;
@ -197,8 +194,8 @@ void Document::insertItem(int line, const QString &s)
PDocumentLine documentLine = std::make_shared<DocumentLine>( PDocumentLine documentLine = std::make_shared<DocumentLine>(
mUpdateDocumentLineWidthFunc); mUpdateDocumentLineWidthFunc);
documentLine->setLineText(s); documentLine->setLineText(s);
mIndexOfLongestLine = -1;
mLines.insert(line,documentLine); mLines.insert(line,documentLine);
setIndexOfLongestLine(-1);
endUpdate(); endUpdate();
} }
@ -207,7 +204,6 @@ void Document::addItem(const QString &s)
beginUpdate(); beginUpdate();
PDocumentLine line = std::make_shared<DocumentLine>(mUpdateDocumentLineWidthFunc); PDocumentLine line = std::make_shared<DocumentLine>(mUpdateDocumentLineWidthFunc);
line->setLineText(s); line->setLineText(s);
mIndexOfLongestLine = -1;
mLines.append(line); mLines.append(line);
endUpdate(); endUpdate();
} }
@ -287,13 +283,13 @@ void Document::setContents(const QStringList &text)
}); });
internalClear(); internalClear();
if (text.count() > 0) { if (text.count() > 0) {
mIndexOfLongestLine = -1;
int FirstAdded = mLines.count(); int FirstAdded = mLines.count();
foreach (const QString& s,text) { foreach (const QString& s,text) {
addItem(s); addItem(s);
} }
emit inserted(FirstAdded,text.count()); emit inserted(FirstAdded,text.count());
setIndexOfLongestLine(-1);
} }
} }
@ -340,16 +336,15 @@ void Document::addLines(const QStringList &strings)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (strings.count() > 0) { if (strings.count() > 0) {
mIndexOfLongestLine = -1;
beginUpdate(); beginUpdate();
auto action = finally([this]{ auto action = finally([this]{
endUpdate(); endUpdate();
}); });
int FirstAdded = mLines.count(); int FirstAdded = mLines.count();
for (const QString& s:strings) { for (const QString& s:strings) {
addItem(s); addItem(s);
} }
setIndexOfLongestLine(-1);
emit inserted(FirstAdded,strings.count()); emit inserted(FirstAdded,strings.count());
} }
} }
@ -389,7 +384,7 @@ void Document::deleteLines(int index, int numLines)
}); });
if (mIndexOfLongestLine>=index) { if (mIndexOfLongestLine>=index) {
if (mIndexOfLongestLine <index+numLines) { if (mIndexOfLongestLine <index+numLines) {
invalidateIndexOfLongestLine(); setIndexOfLongestLine(-1);
} else { } else {
mIndexOfLongestLine -= numLines; mIndexOfLongestLine -= numLines;
} }
@ -444,9 +439,9 @@ void Document::deleteAt(int index)
} }
beginUpdate(); beginUpdate();
if (mIndexOfLongestLine == index) if (mIndexOfLongestLine == index)
invalidateIndexOfLongestLine(); setIndexOfLongestLine(-1);
else if (mIndexOfLongestLine>index) else if (mIndexOfLongestLine>index)
invalidateIndexOfLongestLine(); setIndexOfLongestLine(-1);
mLines.removeAt(index); mLines.removeAt(index);
emit deleted(index,1); emit deleted(index,1);
endUpdate(); endUpdate();
@ -475,12 +470,15 @@ void Document::putLine(int index, const QString &s, bool notify) {
listIndexOutOfBounds(index); listIndexOutOfBounds(index);
} }
beginUpdate(); beginUpdate();
int oldWidth = -1; int oldMaxWidth = maxLineWidth();
if (mIndexOfLongestLine == index) mLines[index]->setLineText(s);
oldWidth = mLines[index]->mWidth; int newWidth = mLines[index]->width(true);
mLines[index]->setLineText( s ); if (mIndexOfLongestLine == index && oldMaxWidth>newWidth ) {
if (mIndexOfLongestLine == index && oldWidth>mLines[index]->width() ) setIndexOfLongestLine(-1);
invalidateIndexOfLongestLine(); } else {
if (newWidth > oldMaxWidth)
setIndexOfLongestLine(index);
}
if (notify) if (notify)
emit putted(index); emit putted(index);
endUpdate(); endUpdate();
@ -505,7 +503,7 @@ void Document::insertLines(int index, int numLines)
return; return;
beginUpdate(); beginUpdate();
auto action = finally([this]{ auto action = finally([this]{
invalidateIndexOfLongestLine(); setIndexOfLongestLine(-1);
endUpdate(); endUpdate();
}); });
PDocumentLine line; PDocumentLine line;
@ -636,7 +634,7 @@ void Document::loadFromFile(const QString& filename, const QByteArray& encoding,
auto action = finally([this]{ auto action = finally([this]{
if (mLines.count()>0) if (mLines.count()>0)
emit inserted(0,mLines.count()); emit inserted(0,mLines.count());
invalidateIndexOfLongestLine(); setIndexOfLongestLine(-1);
endUpdate(); endUpdate();
}); });
//test for utf8 / utf 8 bom //test for utf8 / utf 8 bom
@ -1165,7 +1163,7 @@ void Document::internalClear()
beginUpdate(); beginUpdate();
int oldCount = mLines.count(); int oldCount = mLines.count();
mLines.clear(); mLines.clear();
invalidateIndexOfLongestLine(); setIndexOfLongestLine(-1);
emit deleted(0,oldCount); emit deleted(0,oldCount);
endUpdate(); endUpdate();
} }
@ -1213,17 +1211,17 @@ void Document::emitMaxLineWidthChanged()
void Document::updateLongestLineWidth(int line, int width) void Document::updateLongestLineWidth(int line, int width)
{ {
if (mIndexOfLongestLine<0) { if (mIndexOfLongestLine<0) {
mIndexOfLongestLine = line; setIndexOfLongestLine(line);
emitMaxLineWidthChanged();
} else if (mLines[mIndexOfLongestLine]->mWidth < width) { } else if (mLines[mIndexOfLongestLine]->mWidth < width) {
mIndexOfLongestLine = line; setIndexOfLongestLine(line);
emitMaxLineWidthChanged();
} }
} }
void Document::invalidateIndexOfLongestLine() void Document::setIndexOfLongestLine(int line)
{ {
mIndexOfLongestLine = -1; mIndexOfLongestLine = line;
//mIndexOfLongestLine may not change, but it's width changed.
//so we must emit the signal here
emitMaxLineWidthChanged(); emitMaxLineWidthChanged();
} }
@ -1279,9 +1277,8 @@ void Document::invalidateAllLineWidth()
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
for (PDocumentLine& line:mLines) { for (PDocumentLine& line:mLines) {
line->invalidateWidth(); line->invalidateWidth();
qDebug()<<line->mWidth<<line->mLineText<<"invalidated";
} }
invalidateIndexOfLongestLine(); setIndexOfLongestLine(-1);
// mIndexOfLongestLine = -1; // mIndexOfLongestLine = -1;
} }
@ -1322,9 +1319,9 @@ int DocumentLine::glyphWidth(int i)
return calcSegmentInterval(mGlyphStartPositionList, mWidth, i); return calcSegmentInterval(mGlyphStartPositionList, mWidth, i);
} }
int DocumentLine::width() int DocumentLine::width(bool forceUpdate)
{ {
if(mWidth<0) if(mWidth<0 || forceUpdate)
updateWidth(); updateWidth();
return mWidth; return mWidth;
} }

View File

@ -132,7 +132,7 @@ private:
* @brief get the width (pixel) of the line text * @brief get the width (pixel) of the line text
* @return the width (in width) * @return the width (in width)
*/ */
int width(); int width(bool forceUpdate=false);
/** /**
* @brief get the state of the syntax highlighter after this line is parsed * @brief get the state of the syntax highlighter after this line is parsed
@ -581,7 +581,7 @@ private:
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 updateLongestLineWidth(int line, int width);
void invalidateIndexOfLongestLine(); void setIndexOfLongestLine(int line);
int glyphWidth(const QString& glyph, int left, int glyphWidth(const QString& glyph, int left,
const QFontMetrics &fontMetrics, const QFontMetrics &fontMetrics,

View File

@ -288,9 +288,8 @@ bool QSynEdit::canRedo() const
int QSynEdit::maxScrollWidth() const int QSynEdit::maxScrollWidth() const
{ {
int maxWidth = mDocument->maxLineWidth(); int maxWidth = mDocument->maxLineWidth();
qDebug()<<maxWidth; if (maxWidth < 0)
if (maxWidth <= 0) return -1;
return 0;
if (useCodeFolding()) if (useCodeFolding())
maxWidth += stringWidth(syntaxer()->foldString(""),maxWidth); maxWidth += stringWidth(syntaxer()->foldString(""),maxWidth);
if (mOptions.testFlag(eoScrollPastEol)) if (mOptions.testFlag(eoScrollPastEol))
@ -704,9 +703,7 @@ DisplayCoord QSynEdit::bufferToDisplayPos(const BufferCoord &p) const
// Account for tabs and charColumns // Account for tabs and charColumns
if (p.line-1 <mDocument->count()) if (p.line-1 <mDocument->count())
result.x = charToGlyphLeft(p.line,p.ch); result.x = charToGlyphLeft(p.line,p.ch);
// Account for code folding result.row = lineToRow(result.row);
if (useCodeFolding())
result.row = foldLineToRow(result.row);
return result; return result;
} }
@ -867,7 +864,10 @@ int QSynEdit::rowToLine(int aRow) const
int QSynEdit::lineToRow(int aLine) const int QSynEdit::lineToRow(int aLine) const
{ {
return bufferToDisplayPos({1, aLine}).row; if (useCodeFolding())
return foldLineToRow(aLine);
else
return aLine;
} }
int QSynEdit::foldRowToLine(int row) const int QSynEdit::foldRowToLine(int row) const
@ -3092,7 +3092,7 @@ void QSynEdit::updateHScrollbar()
mStateFlags.setFlag(StateFlag::sfHScrollbarChanged); mStateFlags.setFlag(StateFlag::sfHScrollbarChanged);
} else { } else {
mStateFlags.setFlag(StateFlag::sfHScrollbarChanged,false); mStateFlags.setFlag(StateFlag::sfHScrollbarChanged,false);
updateHScrollBarLater(); doUpdateHScrollbar();
} }
} }
@ -3100,6 +3100,8 @@ void QSynEdit::doUpdateHScrollbar()
{ {
int nMin = 0; int nMin = 0;
int nMax = maxScrollWidth(); int nMax = maxScrollWidth();
if (nMax<0)
return;
int nPage = viewWidth(); int nPage = viewWidth();
int nPos = mLeftPos; int nPos = mLeftPos;
horizontalScrollBar()->setMinimum(nMin); horizontalScrollBar()->setMinimum(nMin);
@ -5951,6 +5953,8 @@ void QSynEdit::resizeEvent(QResizeEvent *)
void QSynEdit::timerEvent(QTimerEvent *event) void QSynEdit::timerEvent(QTimerEvent *event)
{ {
if (event->timerId() == m_blinkTimerId) { if (event->timerId() == m_blinkTimerId) {
if (mPaintLock>0)
return;
m_blinkStatus = 1- m_blinkStatus; m_blinkStatus = 1- m_blinkStatus;
updateCaret(); updateCaret();
} }
@ -6801,7 +6805,6 @@ void QSynEdit::setLeftPos(int value)
if (mScrollBars == ScrollStyle::ssBoth || mScrollBars == ScrollStyle::ssHorizontal) if (mScrollBars == ScrollStyle::ssBoth || mScrollBars == ScrollStyle::ssHorizontal)
horizontalScrollBar()->setValue(value); horizontalScrollBar()->setValue(value);
else { else {
mLeftPos = value;
invalidate(); invalidate();
} }
} }