Implemented QAccessibleTextEdit::attributes()

Handling font properties and colors
Created test: tst_QAccessibility::textAttributes

Merge-request: 2626
Reviewed-by: Harald Fernengel <harald.fernengel@nokia.com>
(cherry picked from commit f1a6766432f66220275aa7902e4c2414a3069cd1)

Change-Id: I388bc660af20149934110d7894840eccecf81f2a
Reviewed-on: http://codereview.qt.nokia.com/3036
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@nokia.com>
bb10
Frederik Gladhorn 2011-08-16 15:19:09 +02:00 committed by Qt by Nokia
parent a2cbaf9137
commit b8cd3fe7b6
2 changed files with 161 additions and 5 deletions

View File

@ -1321,11 +1321,112 @@ void QAccessibleTextEdit::addSelection(int startOffset, int endOffset)
QString QAccessibleTextEdit::attributes(int offset, int *startOffset, int *endOffset)
{
// TODO - wait for a definition of attributes
Q_UNUSED(offset);
Q_UNUSED(startOffset);
Q_UNUSED(endOffset);
return QString();
/* The list of attributes can be found at:
http://linuxfoundation.org/collaborate/workgroups/accessibility/iaccessible2/textattributes
*/
if (offset >= characterCount()) {
*startOffset = -1;
*endOffset = -1;
return QString();
}
QMap<QString, QString> attrs;
QTextCursor cursor = textEdit()->textCursor();
//cursor.charFormat returns the format of the previous character
cursor.setPosition(offset + 1);
QTextCharFormat charFormat = cursor.charFormat();
cursor.setPosition(offset);
QTextBlockFormat blockFormat = cursor.blockFormat();
QTextCharFormat charFormatComp;
QTextBlockFormat blockFormatComp;
*startOffset = offset;
cursor.setPosition(*startOffset);
while (*startOffset > 0) {
charFormatComp = cursor.charFormat();
cursor.setPosition(*startOffset - 1);
blockFormatComp = cursor.blockFormat();
if ((charFormat == charFormatComp) && (blockFormat == blockFormatComp))
(*startOffset)--;
else
break;
}
int limit = characterCount() + 1;
*endOffset = offset + 1;
cursor.setPosition(*endOffset);
while (*endOffset < limit) {
blockFormatComp = cursor.blockFormat();
cursor.setPosition(*endOffset + 1);
charFormatComp = cursor.charFormat();
if ((charFormat == charFormatComp) && (cursor.blockFormat() == blockFormatComp))
(*endOffset)++;
else
break;
}
QString family = charFormat.fontFamily();
if (!family.isEmpty()) {
family = family.replace('\\',"\\\\");
family = family.replace(':',"\\:");
family = family.replace(',',"\\,");
family = family.replace('=',"\\=");
family = family.replace(';',"\\;");
family = family.replace('\"',"\\\"");
attrs["font-family"] = '"'+family+'"';
}
int fontSize = int(charFormat.fontPointSize());
if (fontSize)
attrs["font-size"] = QString::number(fontSize).append("pt");
//Different weight values are not handled
attrs["font-weight"] = (charFormat.fontWeight() > QFont::Normal) ? "bold" : "normal";
QFont::Style style = charFormat.font().style();
attrs["font-style"] = (style == QFont::StyleItalic) ? "italic" : ((style == QFont::StyleOblique) ? "oblique": "normal");
attrs["text-underline-style"] = charFormat.font().underline() ? "solid" : "none";
QTextCharFormat::VerticalAlignment alignment = charFormat.verticalAlignment();
attrs["text-position"] = (alignment == QTextCharFormat::AlignSubScript) ? "sub" : ((alignment == QTextCharFormat::AlignSuperScript) ? "super" : "baseline" );
QBrush background = charFormat.background();
if (background.style() == Qt::SolidPattern) {
attrs["background-color"] = QString("rgb(%1,%2,%3)").arg(background.color().red()).arg(background.color().green()).arg(background.color().blue());
}
QBrush foreground = charFormat.foreground();
if (foreground.style() == Qt::SolidPattern) {
attrs["color"] = QString("rgb(%1,%2,%3)").arg(foreground.color().red()).arg(foreground.color().green()).arg(foreground.color().blue());
}
switch (blockFormat.alignment() & (Qt::AlignLeft | Qt::AlignRight | Qt::AlignHCenter | Qt::AlignJustify)) {
case Qt::AlignLeft:
attrs["text-align"] = "left";
break;
case Qt::AlignRight:
attrs["text-align"] = "right";
break;
case Qt::AlignHCenter:
attrs["text-align"] = "center";
break;
case Qt::AlignJustify:
attrs["text-align"] = "left";
break;
}
QString result;
foreach (const QString &attributeName, attrs.keys()) {
result.append(attributeName).append(':').append(attrs[attributeName]).append(';');
}
return result;
}
int QAccessibleTextEdit::cursorPosition()

View File

@ -230,6 +230,7 @@ private slots:
void navigateHierarchy();
void navigateSlider();
void navigateCovered();
void textAttributes();
void hideShowTest();
void userActionCount();
@ -862,6 +863,60 @@ void tst_QAccessibility::accessibleName()
QTestAccessibility::clearEvents();
}
void tst_QAccessibility::textAttributes()
{
QTextEdit textEdit;
int startOffset;
int endOffset;
QString attributes;
QString text("<html><head></head><body>"
"Hello, <b>this</b> is an <i><b>example</b> text</i>."
"<span style=\"font-family: monospace\">Multiple fonts are used.</span>"
"Multiple <span style=\"font-size: 8pt\">text sizes</span> are used."
"Let's give some color to <span style=\"color:#f0f1f2; background-color:#14f01e\">Qt</span>."
"</body></html>");
textEdit.setText(text);
QAccessibleInterface *interface = QAccessible::queryAccessibleInterface(&textEdit);
QAccessibleTextInterface *textInterface=interface->textInterface();
QVERIFY(textInterface);
QCOMPARE(textInterface->characterCount(), 112);
attributes = textInterface->attributes(10, &startOffset, &endOffset);
QCOMPARE(startOffset, 7);
QCOMPARE(endOffset, 11);
attributes.prepend(';');
QVERIFY(attributes.contains(QLatin1String(";font-weight:bold;")));
attributes = textInterface->attributes(18, &startOffset, &endOffset);
QCOMPARE(startOffset, 18);
QCOMPARE(endOffset, 25);
attributes.prepend(';');
QVERIFY(attributes.contains(QLatin1String(";font-weight:bold;")));
QVERIFY(attributes.contains(QLatin1String(";font-style:italic;")));
attributes = textInterface->attributes(34, &startOffset, &endOffset);
QCOMPARE(startOffset, 31);
QCOMPARE(endOffset, 55);
attributes.prepend(';');
QVERIFY(attributes.contains(QLatin1String(";font-family:\"monospace\";")));
attributes = textInterface->attributes(65, &startOffset, &endOffset);
QCOMPARE(startOffset, 64);
QCOMPARE(endOffset, 74);
attributes.prepend(';');
QVERIFY(attributes.contains(QLatin1String(";font-size:8pt;")));
attributes = textInterface->attributes(110, &startOffset, &endOffset);
QCOMPARE(startOffset, 109);
QCOMPARE(endOffset, 111);
attributes.prepend(';');
QVERIFY(attributes.contains(QLatin1String(";background-color:rgb(20,240,30);")));
QVERIFY(attributes.contains(QLatin1String(";color:rgb(240,241,242);")));
}
void tst_QAccessibility::hideShowTest()
{
QWidget * const window = new QWidget();