diff options
author | Hannu Koivisto <hannu.koivisto@vincit.fi> | 2010-12-02 17:14:22 +0200 |
---|---|---|
committer | Pekka Vuorela <pekka.ta.vuorela@nokia.com> | 2010-12-07 14:35:20 +0200 |
commit | 2dc845e6a0c9b9f0cec32e038b0b24037fd3344a (patch) | |
tree | 3d2f2d6d9091682e5329c7cc3cb5aa21cba2f116 | |
parent | b744b69017711080ed7de39e616f89c8987cce74 (diff) |
Changes: MTextEdit: optimize styled pre-edit insertion
RevBy: Pekka Vuorela
-rwxr-xr-x | src/corelib/widgets/mtextedit.cpp | 81 |
1 files changed, 31 insertions, 50 deletions
diff --git a/src/corelib/widgets/mtextedit.cpp b/src/corelib/widgets/mtextedit.cpp index 113293b1..d4fc1925 100755 --- a/src/corelib/widgets/mtextedit.cpp +++ b/src/corelib/widgets/mtextedit.cpp @@ -590,7 +590,7 @@ bool MTextEditPrivate::doTextInsert(const QString &text, bool usePreeditStyling) int textPosition = 0; int filteredTextLength = filteredText.length(); int snippetLength = -1; - int listIndex = -1; + int listIndex = 0; int count = 0; do { @@ -755,7 +755,7 @@ void MTextEditPrivate::setPreeditText(const QString &text, // If this becomes problematic, we should move this formatting to paintContext of the view. layout->setAdditionalFormats(preeditStyles); - int listIndex = -1; + int listIndex = 0; int count = 0; insertTextWithPreeditStyling(text, listIndex, count); clearUnusedPreeditStyling(listIndex, count); @@ -1094,63 +1094,44 @@ void MTextEditPrivate::storePreeditTextStyling(int start, int end) */ void MTextEditPrivate::insertTextWithPreeditStyling(const QString &text, int ¤tListIndex, int ¤tCount) { - QTextCursor *textCursor = cursor(); - const int textLength = text.length(); - int listIndex = currentListIndex; - int count = currentCount; - int preeditStyleSize = preeditStyling.size(); - int currentIndex = textLength; + if (text.isEmpty()) { + return; + } - QTextCharFormat format = textCursor->charFormat(); + QTextCursor &textCursor(*cursor()); - styleData newStyle; - newStyle.charFormat = format; - newStyle.count = 1; + if (preeditStyling.size() == 0) { + Q_ASSERT(currentListIndex == 0); + Q_ASSERT(currentCount == 0); - if (preeditStyleSize == 0) { - preeditStyling.push_back(newStyle); - preeditStyleSize++; + styleData defaultStyle; + defaultStyle.charFormat = textCursor.charFormat(); + defaultStyle.count = text.length(); + preeditStyling.push_back(defaultStyle); } - for (int i = 0; i < textLength; ++i) { - if (preeditStyling.isEmpty() != true) { - if (count == 0) { - listIndex++; - if (listIndex == preeditStyleSize) { - QTextCharFormat charFormat = preeditStyling.at(preeditStyleSize - 1).charFormat; - textCursor->setCharFormat(charFormat); - currentIndex = i; - listIndex = preeditStyleSize - 1; - break; - } - - count = preeditStyling.at(listIndex).count; - } - - if (count == 0) { - styleData &lastStyle = preeditStyling[listIndex]; - lastStyle.count++; - count = 1; - } + const int textLength(text.length()); + const int numStyles(preeditStyling.size()); + int textIndex(0); - QTextCharFormat charFormat = preeditStyling.at(listIndex).charFormat; - textCursor->setCharFormat(charFormat); - count--; - } + Q_ASSERT((currentListIndex < numStyles) && (currentListIndex >= 0)); + Q_ASSERT(currentCount <= preeditStyling[currentListIndex].count); - textCursor->insertText(text.at(i)); - } + --currentListIndex; - for (int i = currentIndex; i < textLength; ++i) { - if (preeditStyling.isEmpty() != true) { - styleData &newStyle = preeditStyling[listIndex]; - newStyle.count++; + do { + ++currentListIndex; + const int remainingTextLength(textLength - textIndex); + if ((currentListIndex == (numStyles - 1)) + && (preeditStyling[currentListIndex].count < (currentCount + remainingTextLength))) { + preeditStyling[currentListIndex].count = remainingTextLength + currentCount; } - textCursor->insertText(text.at(i)); - } - - currentListIndex = listIndex; - currentCount = count; + const int insertLength(qMin(preeditStyling[currentListIndex].count - currentCount, + remainingTextLength)); + currentCount = (currentCount + insertLength) % preeditStyling[currentListIndex].count; + textCursor.insertText(text.mid(textIndex, insertLength), preeditStyling[currentListIndex].charFormat); + textIndex += insertLength; + } while (currentListIndex < (numStyles - 1) && (textIndex < textLength)); } |