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: Ie2a4dfc714504b7923cdaf8ff875c438aeccddee
Reviewed-by: Jan Arve Sæther <jan-arve.saether@theqtcompany.com>
This commit is contained in:
Boris Dušek 2015-03-17 23:03:23 +01:00
parent 310b5e73f0
commit 93bb16384a

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);