mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-21 23:19:29 +02:00
Qt: speed improvement for QTextEdit text insertions when it contains < or > chars. part of issue #22715
git-svn-id: trunk@38367 -
This commit is contained in:
parent
87ed4c7d57
commit
a2cf7dbb61
@ -412,6 +412,20 @@ end;
|
||||
procedure TQtMemoStrings.Insert(Index: integer; const S: string);
|
||||
var
|
||||
W: WideString;
|
||||
QtCursor: QTextCursorH;
|
||||
|
||||
|
||||
function WorkaroundNeeded: Boolean;
|
||||
var
|
||||
HaveLt: Boolean;
|
||||
HaveGt: Boolean;
|
||||
S1: String;
|
||||
begin
|
||||
HaveLt := System.Pos('<', S) > 0;
|
||||
HaveGt := System.Pos('>', S) > 0;
|
||||
Result := HaveLt or HaveGt;
|
||||
end;
|
||||
|
||||
begin
|
||||
if FTextChanged then InternalUpdate;
|
||||
if Index < 0 then Index := 0;
|
||||
@ -423,18 +437,16 @@ begin
|
||||
if Index <= FStringList.Count then
|
||||
begin
|
||||
FStringList.Insert(Index, S);
|
||||
// to fix #22715 we must use slower insertLine(). Rewriting
|
||||
// handle from QTextEdit to QPlainTextEdit should fix speed.
|
||||
// Currently we are missing Text alignment in QPlainTextEdit class,
|
||||
// so that's why it's not rewritten yet.
|
||||
if (Index < FStringList.Count - 1) and
|
||||
(TQtTextEdit(FOwner.Handle).getBlockCount - Index <= 1) then
|
||||
if (TQtTextEdit(FOwner.Handle).getBlockCount - Index <= 1) then
|
||||
begin
|
||||
if (System.Pos('<', S) > 0) or (System.Pos('>',S) > 0) then
|
||||
// workaround for qt richtext parser bug. issues #17170 and #22715
|
||||
if WorkaroundNeeded then
|
||||
begin
|
||||
// workaround for qt richtext parser bug
|
||||
W := GetUTF8String(S);
|
||||
TQtTextEdit(FOwner.Handle).insertLine(Index, W);
|
||||
if (Index >= FStringList.Count - 1) then
|
||||
TQtTextEdit(FOwner.Handle).appendLine(W)
|
||||
else
|
||||
TQtTextEdit(FOwner.Handle).insertLine(Index, W);
|
||||
end else
|
||||
begin
|
||||
// append is much faster in case when we add strings
|
||||
|
@ -818,6 +818,7 @@ type
|
||||
function getSelectionStart: Integer;
|
||||
function getSelectionEnd: Integer;
|
||||
function getSelectionLength: Integer;
|
||||
procedure appendLine(AText: WideString);
|
||||
procedure insertLine(const AIndex: integer; AText: WideString);
|
||||
function isUndoAvailable: Boolean;
|
||||
procedure removeLine(const AIndex: integer);
|
||||
@ -8129,6 +8130,29 @@ begin
|
||||
{$note implement TQtTextEdit.setMaxLength}
|
||||
end;
|
||||
|
||||
procedure TQtTextEdit.appendLine(AText: WideString);
|
||||
var
|
||||
QtCursor: QTextCursorH;
|
||||
WrapMode: QTextEditLineWrapMode;
|
||||
begin
|
||||
WrapMode := QTextEdit_lineWrapMode(QTextEditH(Widget));
|
||||
{we must remove wrapping to get correct line !}
|
||||
setLineWrapMode(QTextEditNoWrap);
|
||||
QtCursor := QTextCursor_create();
|
||||
try
|
||||
QTextEdit_textCursor(QTextEditH(Widget), QtCursor);
|
||||
QTextCursor_beginEditBlock(QtCursor);
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorEnd,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
QTextCursor_insertBlock(QtCursor);
|
||||
QTextCursor_endEditBlock(QtCursor);
|
||||
QTextEdit_insertPlainText(QTextEditH(Widget), @AText);
|
||||
finally
|
||||
QTextCursor_destroy(QtCursor);
|
||||
setLineWrapMode(WrapMode);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TQtTextEdit.insertLine(const AIndex: integer; AText: WideString);
|
||||
var
|
||||
QtCursor: QTextCursorH;
|
||||
@ -8141,15 +8165,33 @@ begin
|
||||
try
|
||||
QTextEdit_textCursor(QTextEditH(Widget), QtCursor);
|
||||
QTextCursor_beginEditBlock(QtCursor);
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorStart,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorStartOfLine,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
// QTextCursor slowness
|
||||
// https://bugreports.qt-project.org/browse/QTBUG-3554
|
||||
// differentiate append vs. insert issue #22715
|
||||
if AIndex >= FList.Count - 1 then
|
||||
begin
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorEnd,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
QTextCursor_insertBlock(QtCursor);
|
||||
end else
|
||||
begin
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorStart,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorStartOfLine,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
end;
|
||||
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorDown,
|
||||
QTextCursorMoveAnchor, AIndex);
|
||||
QTextCursor_insertBlock(QtCursor);
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorUp,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
// QTextCursor slowness
|
||||
// https://bugreports.qt-project.org/browse/QTBUG-3554
|
||||
// differentiate append vs. insert issue #22715
|
||||
if AIndex < FList.Count - 1 then
|
||||
begin
|
||||
QTextCursor_insertBlock(QtCursor);
|
||||
QTextCursor_movePosition(QtCursor, QTextCursorUp,
|
||||
QTextCursorMoveAnchor, 1);
|
||||
end;
|
||||
QTextCursor_insertText(QtCursor, @AText);
|
||||
QTextCursor_endEditBlock(QtCursor);
|
||||
finally
|
||||
|
Loading…
Reference in New Issue
Block a user