Fix QAccessibleTextWidget::characterRect for off-cursor positions

Current code retrieved the character format at position where the cursor
currently was, irrespective of the position actually passed to the
characterRect function. This is now fixed and we retrieve the character
format for the QTextFragment containing the specified offset.

This fixes display of visual bounds around text by screen reader in some
cases. E.g. on OS X, when searching text using VoiceOver, VoiceOver
first queries the visual bounds of the found text (and thus also
characterRect), and only then it moves cursor position to the found
text. If before the change of position the cursor was on some text with
different metrics (i.e. a bigger font), the visual bounds for the found
text reflected those metrics, not the metrics of the actual found text
for which the bounds were drawn. This resulted in smaller/bigger
rectangles around the found text than was actually correct.

Change-Id: If23b4b8492ec77f0f073fc5c25628b67b483724e
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@theqtcompany.com>
bb10
Boris Dušek 2015-01-04 22:50:38 +01:00
parent e19a43be90
commit 8944d2dcbb
1 changed files with 14 additions and 1 deletions

View File

@ -667,7 +667,20 @@ QRect QAccessibleTextWidget::characterRect(int offset) const
if (line.isValid()) {
qreal x = line.cursorToX(relativeOffset);
QFontMetrics fm(textCursor().charFormat().font());
QTextCharFormat format;
QTextBlock::iterator iter = block.begin();
if (iter.atEnd())
format = block.charFormat();
else {
while (!iter.atEnd() && !iter.fragment().contains(offset))
++iter;
if (iter.atEnd()) // newline should have same format as preceding character
--iter;
format = iter.fragment().charFormat();
}
QFontMetrics fm(format.font());
const QString ch = text(offset, offset + 1);
if (!ch.isEmpty()) {
int w = fm.width(ch);