fix #303087: Fixed a bug where the cursor would be on the wrong height after deleting all chars in a line

This commit is contained in:
DangWang 2020-04-22 10:39:24 +03:00
parent b2c71cd588
commit 723f6b8132
3 changed files with 33 additions and 12 deletions

View file

@ -803,6 +803,7 @@ int TextBlock::column(qreal x, TextBase* t) const
void TextBlock::insert(TextCursor* cursor, const QString& s)
{
int rcol, ridx;
removeEmptyFragment(); // since we are going to write text, we don't need an empty fragment to hold format info. if such exists, delete it
auto i = fragment(cursor->column(), &rcol, &ridx);
if (i != _fragments.end()) {
if (!(i->format == *cursor->format())) {
@ -826,15 +827,29 @@ void TextBlock::insert(TextCursor* cursor, const QString& s)
}
//---------------------------------------------------------
// insertEmptyFragment
//
// insertEmptyFragmentIfNeeded
// used to insert an empty TextFragment in TextBlocks that have none
// that way, the formatting information (most importantly the font size) of the line is preserved
//
//---------------------------------------------------------
void TextBlock::insertEmptyFragment(TextCursor* cursor)
void TextBlock::insertEmptyFragmentIfNeeded(TextCursor* cursor)
{
if (_fragments.size() == 0 || _fragments.at(0).text != "")
_fragments.insert(0, TextFragment(cursor, ""));
}
//---------------------------------------------------------
// removeEmptyFragment
//---------------------------------------------------------
void TextBlock::removeEmptyFragment()
{
if (_fragments.size() > 0 && _fragments.at(0).text == "")
_fragments.removeAt(0);
}
//---------------------------------------------------------
// fragment
// inputs:
@ -868,7 +883,7 @@ QList<TextFragment>::iterator TextBlock::fragment(int column, int* rcol, int* ri
// remove
//---------------------------------------------------------
QString TextBlock::remove(int column)
QString TextBlock::remove(int column, TextCursor* cursor)
{
int col = 0;
QString s;
@ -888,6 +903,7 @@ QString TextBlock::remove(int column)
if (i->text.isEmpty())
_fragments.erase(i);
simplify();
insertEmptyFragmentIfNeeded(cursor); // without this, cursorRect can't calculate the y position of the cursor correctly
return s;
}
++idx;
@ -897,6 +913,7 @@ QString TextBlock::remove(int column)
++rcol;
}
}
insertEmptyFragmentIfNeeded(cursor); // without this, cursorRect can't calculate the y position of the cursor correctly
return s;
// qDebug("TextBlock::remove: column %d not found", column);
}
@ -927,7 +944,7 @@ void TextBlock::simplify()
// remove
//---------------------------------------------------------
QString TextBlock::remove(int start, int n)
QString TextBlock::remove(int start, int n, TextCursor* cursor)
{
if (n == 0)
return QString();
@ -951,8 +968,10 @@ QString TextBlock::remove(int start, int n)
inc = false;
}
--n;
if (n == 0)
if (n == 0) {
insertEmptyFragmentIfNeeded(cursor); // without this, cursorRect can't calculate the y position of the cursor correctly
return s;
}
continue;
}
++idx;
@ -964,6 +983,7 @@ QString TextBlock::remove(int start, int n)
if (inc)
++i;
}
insertEmptyFragmentIfNeeded(cursor); // without this, cursorRect can't calculate the y position of the cursor correctly
return s;
}
@ -1267,7 +1287,7 @@ void TextBase::createLayout()
if (rows() <= cursor.row())
_layout.append(TextBlock());
if(_layout[cursor.row()].fragments().size() == 0)
_layout[cursor.row()].insertEmptyFragment(&cursor); // used to preserve the Font size of the line (font info is held in TextFragments, see PR #5881)
_layout[cursor.row()].insertEmptyFragmentIfNeeded(&cursor); // used to preserve the Font size of the line (font info is held in TextFragments, see PR #5881)
_layout[cursor.row()].setEol(true);
cursor.setRow(cursor.row() + 1);
cursor.setColumn(0);

View file

@ -193,9 +193,10 @@ class TextBlock {
QRectF boundingRect(int col1, int col2, const TextBase*) const;
int columns() const;
void insert(TextCursor*, const QString&);
void insertEmptyFragment(TextCursor*);
QString remove(int column);
QString remove(int start, int n);
void insertEmptyFragmentIfNeeded(TextCursor*);
void removeEmptyFragment();
QString remove(int column, TextCursor*);
QString remove(int start, int n, TextCursor*);
int column(qreal x, TextBase*) const;
TextBlock split(int column);
qreal xpos(int col, const TextBase*) const;

View file

@ -477,7 +477,7 @@ void ChangeText::removeText(EditData* ed)
int column = c.column();
for (int n = 0; n < s.size(); ++n)
l.remove(column);
l.remove(column, &c);
c.text()->triggerLayout();
if (ed)
*c.text()->cursor(*ed) = tc;
@ -679,7 +679,7 @@ void TextBase::inputTransition(EditData& ed, QInputMethodEvent* ie)
while (n--) {
if (_cursor->movePosition(QTextCursor::Left)) {
TextBlock& l = _cursor->curLine();
l.remove(_cursor->column());
l.remove(_cursor->column(), _cursor);
_cursor->text()->triggerLayout();
_cursor->text()->setTextInvalid();
}
@ -743,7 +743,7 @@ void TextBase::endHexState(EditData& ed)
int c1 = c2 - (hexState + 1);
TextBlock& t = _layout[_cursor->row()];
QString ss = t.remove(c1, hexState + 1);
QString ss = t.remove(c1, hexState + 1, _cursor);
bool ok;
int code = ss.mid(1).toInt(&ok, 16);
_cursor->setColumn(c1);