diff --git a/src/plugins/platforms/cocoa/qcocoaaccessibility.h b/src/plugins/platforms/cocoa/qcocoaaccessibility.h index 36795c50ed..4953be0e18 100644 --- a/src/plugins/platforms/cocoa/qcocoaaccessibility.h +++ b/src/plugins/platforms/cocoa/qcocoaaccessibility.h @@ -45,6 +45,8 @@ #include +namespace QCocoaAccessible { + /* Qt Cocoa Accessibility Overview @@ -64,5 +66,10 @@ */ NSString *macRole(QAccessible::Role); +NSString *getTranslatedAction(const QString &qtAction); +NSMutableArray *createTranslatedActionsList(const QStringList &qtActions); +QString translateAction(NSString *nsAction); + +} #endif diff --git a/src/plugins/platforms/cocoa/qcocoaaccessibility.mm b/src/plugins/platforms/cocoa/qcocoaaccessibility.mm index 4b58ef2faa..26ff9531a5 100644 --- a/src/plugins/platforms/cocoa/qcocoaaccessibility.mm +++ b/src/plugins/platforms/cocoa/qcocoaaccessibility.mm @@ -40,6 +40,8 @@ ****************************************************************************/ #include "qcocoaaccessibility.h" +namespace QCocoaAccessible { + typedef QMap QMacAccessibiltyRoleMap; Q_GLOBAL_STATIC(QMacAccessibiltyRoleMap, qMacAccessibiltyRoleMap); @@ -111,3 +113,60 @@ NSString *macRole(QAccessible::Role qtRole) return NSAccessibilityUnknownRole; } +/* + Translates a predefined QAccessibleActionInterface action to a Mac action constant. + Returns 0 if the Qt Action has no mac equivalent. Ownership of the NSString is + not transferred. +*/ +NSString *getTranslatedAction(const QString &qtAction) +{ + if (qtAction == QAccessibleActionInterface::pressAction()) + return NSAccessibilityPressAction; + else if (qtAction == QAccessibleActionInterface::increaseAction()) + return NSAccessibilityIncrementAction; + else if (qtAction == QAccessibleActionInterface::decreaseAction()) + return NSAccessibilityDecrementAction; + else if (qtAction == QAccessibleActionInterface::showMenuAction()) + return NSAccessibilityShowMenuAction; + else if (qtAction == QAccessibleActionInterface::setFocusAction()) // Not 100% sure on this one + return NSAccessibilityRaiseAction; + + // Not translated: + // + // Qt: + // static const QString &checkAction(); + // static const QString &uncheckAction(); + // + // Cocoa: + // NSAccessibilityConfirmAction; + // NSAccessibilityPickAction; + // NSAccessibilityCancelAction; + // NSAccessibilityDeleteAction; + + return 0; +} + + +/* + Translates between a Mac action constant and a QAccessibleActionInterface action + Returns an empty QString if there is no Qt predefined equivalent. +*/ +QString translateAction(NSString *nsAction) +{ + if ([nsAction compare: NSAccessibilityPressAction] == NSOrderedSame) + return QAccessibleActionInterface::pressAction(); + else if ([nsAction compare: NSAccessibilityIncrementAction] == NSOrderedSame) + return QAccessibleActionInterface::increaseAction(); + else if ([nsAction compare: NSAccessibilityDecrementAction] == NSOrderedSame) + return QAccessibleActionInterface::decreaseAction(); + else if ([nsAction compare: NSAccessibilityShowMenuAction] == NSOrderedSame) + return QAccessibleActionInterface::showMenuAction(); + else if ([nsAction compare: NSAccessibilityRaiseAction] == NSOrderedSame) + return QAccessibleActionInterface::setFocusAction(); + + // See getTranslatedAction for not matched translations. + + return QString(); +} + +} // namespace QCocoaAccessible diff --git a/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm b/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm index 11cc89f04f..1441a02d1e 100644 --- a/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm +++ b/src/plugins/platforms/cocoa/qcocoaaccessibilityelement.mm @@ -60,7 +60,7 @@ static QAccessibleInterface *acast(void *ptr) if (self) { index = aIndex; accessibleInterface = anQAccessibleInterface; - role = macRole(acast(accessibleInterface)->role()); + role = QCocoaAccessible::macRole(acast(accessibleInterface)->role()); parent = aParent; } @@ -177,17 +177,40 @@ static QAccessibleInterface *acast(void *ptr) // actions - (NSArray *)accessibilityActionNames { - return [NSArray arrayWithObject:NSAccessibilityPressAction]; + NSMutableArray * nsActions = [NSMutableArray new]; + + QAccessibleActionInterface *actionInterface = acast(accessibleInterface)->actionInterface(); + if (actionInterface) { + QStringList supportedActionNames = actionInterface->actionNames(); + + foreach (const QString &qtAction, supportedActionNames) { + NSString *nsAction = QCocoaAccessible::getTranslatedAction(qtAction); + if (nsAction) + [nsActions addObject : nsAction]; + } + } + + return nsActions; } - (NSString *)accessibilityActionDescription:(NSString *)action { + QAccessibleActionInterface *actionInterface = acast(accessibleInterface)->actionInterface(); + if (actionInterface) { + QString qtAction = QCocoaAccessible::translateAction(action); + QString description = actionInterface->localizedActionDescription(qtAction); + if (!description.isEmpty()) + return qt_mac_QStringToNSString(description); + } + return NSAccessibilityActionDescription(action); } - (void)accessibilityPerformAction:(NSString *)action { - Q_UNUSED(action); - if (acast(accessibleInterface)->actionInterface()) - acast(accessibleInterface)->actionInterface()->doAction(0); + QAccessibleActionInterface *actionInterface = acast(accessibleInterface)->actionInterface(); + if (actionInterface) { + QString qtAction = QCocoaAccessible::translateAction(action); + actionInterface->doAction(QAccessibleActionInterface::pressAction()); + } } // misc diff --git a/src/plugins/platforms/cocoa/qnsviewaccessibility.mm b/src/plugins/platforms/cocoa/qnsviewaccessibility.mm index 2e59931dd5..8cb241ab8e 100644 --- a/src/plugins/platforms/cocoa/qnsviewaccessibility.mm +++ b/src/plugins/platforms/cocoa/qnsviewaccessibility.mm @@ -60,7 +60,7 @@ - (id)accessibilityAttributeValue:(NSString *)attribute { if ([attribute isEqualToString:NSAccessibilityRoleAttribute]) { if (m_accessibleRoot) - return macRole(m_accessibleRoot->role()); + return QCocoaAccessible::macRole(m_accessibleRoot->role()); return NSAccessibilityUnknownRole; } else if ([attribute isEqualToString:NSAccessibilityRoleDescriptionAttribute]) { return NSAccessibilityRoleDescriptionForUIElement(self);