diff options
author | Oswald Buddenhagen <oswald.buddenhagen@digia.com> | 2014-09-10 11:41:29 +0200 |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@digia.com> | 2014-09-10 11:42:50 +0200 |
commit | d572ab1bb446e880fcb8d27294ba8149550f1659 (patch) | |
tree | d29c449b551e47569c6d9f146ba9e86810c05353 /src/gui/accessible | |
parent | 211cef46f6d9d8738c09f906f9c0c3080b445dc8 (diff) | |
parent | 71df09b6cca7cd7a673bf39f49d0dda28b78a860 (diff) |
Merge remote-tracking branch 'origin/5.4' into dev
Change-Id: Id4997327cc01bd4bb397a463bdffbd15e80398ef
Diffstat (limited to 'src/gui/accessible')
-rw-r--r-- | src/gui/accessible/qaccessible.cpp | 96 |
1 files changed, 92 insertions, 4 deletions
diff --git a/src/gui/accessible/qaccessible.cpp b/src/gui/accessible/qaccessible.cpp index f9fbc92d20..b3d7843506 100644 --- a/src/gui/accessible/qaccessible.cpp +++ b/src/gui/accessible/qaccessible.cpp @@ -1897,16 +1897,65 @@ QDebug operator<<(QDebug d, const QAccessibleEvent &ev) */ /*! + \internal + Helper for finding line breaks in textBeforeOffset/textAtOffset/textAfterOffset. + \a beforeAtAfter is the line we look for. -1 for before, 0 for at and 1 for after. +*/ +static QString textLineBoundary(int beforeAtAfter, const QString &text, int offset, int *startOffset, int *endOffset) +{ + Q_ASSERT(beforeAtAfter >= -1 && beforeAtAfter <= 1); + Q_ASSERT(*startOffset == -1 && *endOffset == -1); + int length = text.length(); + Q_ASSERT(offset >= 0 && offset <= length); + + // move offset into the right range (if asking for line before or after + if (beforeAtAfter == 1) { + offset = text.indexOf(QChar::LineFeed, qMin(offset, length - 1)); + if (offset < 0) + return QString(); // after the last line comes nothing + ++offset; // move after the newline + } else if (beforeAtAfter == -1) { + offset = text.lastIndexOf(QChar::LineFeed, qMax(offset - 1, 0)); + if (offset < 0) + return QString(); // before first line comes nothing + } + + if (offset > 0) + *startOffset = text.lastIndexOf(QChar::LineFeed, offset - 1); + ++*startOffset; // move to the char after the newline (0 if lastIndexOf returned -1) + + *endOffset = text.indexOf(QChar::LineFeed, qMin(offset, length - 1)) + 1; // include newline char + if (*endOffset <= 0 || *endOffset > length) + *endOffset = length; // if the text doesn't end with a newline it ends at length + + return text.mid(*startOffset, *endOffset - *startOffset); +} + +/*! Returns the text item of type \a boundaryType that is close to offset \a offset and sets \a startOffset and \a endOffset values to the start and end positions of that item; returns an empty string if there is no such an item. Sets \a startOffset and \a endOffset values to -1 on error. + + This default implementation is provided for small text edits. A word processor or + text editor should provide their own efficient implementations. This function makes no + distinction between paragraphs and lines. + + \note this function can not take the cursor position into account. By convention + an \a offset of -2 means that this function should use the cursor position as offset. + Thus an offset of -2 must be converted to the cursor position before calling this + function. + An offset of -1 is used for the text length and custom implementations of this function + have to return the result as if the length was passed in as offset. */ QString QAccessibleTextInterface::textBeforeOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const { const QString txt = text(0, characterCount()); + if (offset == -1) + offset = txt.length(); + *startOffset = *endOffset = -1; if (txt.isEmpty() || offset <= 0 || offset > txt.length()) return QString(); @@ -1922,7 +1971,11 @@ QString QAccessibleTextInterface::textBeforeOffset(int offset, QAccessible::Text case QAccessible::SentenceBoundary: type = QTextBoundaryFinder::Sentence; break; - default: + case QAccessible::LineBoundary: + case QAccessible::ParagraphBoundary: + // Lines can not use QTextBoundaryFinder since Line there means any potential line-break. + return textLineBoundary(-1, txt, offset, startOffset, endOffset); + case QAccessible::NoBoundary: // return empty, this function currently only supports single lines, so there can be no line before return QString(); } @@ -1954,12 +2007,26 @@ QString QAccessibleTextInterface::textBeforeOffset(int offset, QAccessible::Text and sets \a startOffset and \a endOffset values to the start and end positions of that item; returns an empty string if there is no such an item. Sets \a startOffset and \a endOffset values to -1 on error. + + This default implementation is provided for small text edits. A word processor or + text editor should provide their own efficient implementations. This function makes no + distinction between paragraphs and lines. + + \note this function can not take the cursor position into account. By convention + an \a offset of -2 means that this function should use the cursor position as offset. + Thus an offset of -2 must be converted to the cursor position before calling this + function. + An offset of -1 is used for the text length and custom implementations of this function + have to return the result as if the length was passed in as offset. */ QString QAccessibleTextInterface::textAfterOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const { const QString txt = text(0, characterCount()); + if (offset == -1) + offset = txt.length(); + *startOffset = *endOffset = -1; if (txt.isEmpty() || offset < 0 || offset >= txt.length()) return QString(); @@ -1975,7 +2042,11 @@ QString QAccessibleTextInterface::textAfterOffset(int offset, QAccessible::TextB case QAccessible::SentenceBoundary: type = QTextBoundaryFinder::Sentence; break; - default: + case QAccessible::LineBoundary: + case QAccessible::ParagraphBoundary: + // Lines can not use QTextBoundaryFinder since Line there means any potential line-break. + return textLineBoundary(1, txt, offset, startOffset, endOffset); + case QAccessible::NoBoundary: // return empty, this function currently only supports single lines, so there can be no line after return QString(); } @@ -2018,12 +2089,26 @@ QString QAccessibleTextInterface::textAfterOffset(int offset, QAccessible::TextB and sets \a startOffset and \a endOffset values to the start and end positions of that item; returns an empty string if there is no such an item. Sets \a startOffset and \a endOffset values to -1 on error. + + This default implementation is provided for small text edits. A word processor or + text editor should provide their own efficient implementations. This function makes no + distinction between paragraphs and lines. + + \note this function can not take the cursor position into account. By convention + an \a offset of -2 means that this function should use the cursor position as offset. + Thus an offset of -2 must be converted to the cursor position before calling this + function. + An offset of -1 is used for the text length and custom implementations of this function + have to return the result as if the length was passed in as offset. */ QString QAccessibleTextInterface::textAtOffset(int offset, QAccessible::TextBoundaryType boundaryType, int *startOffset, int *endOffset) const { const QString txt = text(0, characterCount()); + if (offset == -1) + offset = txt.length(); + *startOffset = *endOffset = -1; if (txt.isEmpty() || offset < 0 || offset > txt.length()) return QString(); @@ -2042,8 +2127,11 @@ QString QAccessibleTextInterface::textAtOffset(int offset, QAccessible::TextBoun case QAccessible::SentenceBoundary: type = QTextBoundaryFinder::Sentence; break; - default: - // return the whole line + case QAccessible::LineBoundary: + case QAccessible::ParagraphBoundary: + // Lines can not use QTextBoundaryFinder since Line there means any potential line-break. + return textLineBoundary(0, txt, offset, startOffset, endOffset); + case QAccessible::NoBoundary: *startOffset = 0; *endOffset = txt.length(); return txt; |