5#include <AppKit/AppKit.h>
13#include <QtCore/qlogging.h>
14#include <QtGui/private/qaccessiblecache_p.h>
15#include <QtGui/private/qaccessiblebridgeutils_p.h>
16#include <QtGui/qaccessible.h>
20Q_STATIC_LOGGING_CATEGORY(lcAccessibilityTable,
"qt.accessibility.table")
22using namespace Qt::StringLiterals;
24#if QT_CONFIG(accessibility)
27
28
29
30
31
32
33
34
35
36
37
38
39static void convertLineOffset(QAccessibleTextInterface *text,
int *line,
int *offset, NSUInteger *start = 0, NSUInteger *end = 0)
41 Q_ASSERT(*line == -1 || *offset == -1);
42 Q_ASSERT(*line != -1 || *offset != -1);
43 Q_ASSERT(*offset <= text->characterCount());
46 int curStart = 0, curEnd = 0;
50 text->textAtOffset(curStart, QAccessible::LineBoundary, &curStart, &curEnd);
52 if (curStart == -1 || curEnd == -1) {
66 text->textAtOffset(curEnd, QAccessible::LineBoundary, &nextStart, &nextEnd);
67 if (nextEnd == curEnd)
70 }
while ((*line == -1 || curLine < *line) && (*offset == -1 || (curEnd <= *offset)) && curEnd <= text->characterCount());
72 curEnd = qMin(curEnd, text->characterCount());
79 Q_ASSERT(curStart >= 0);
80 Q_ASSERT(curEnd >= 0);
87@implementation QMacAccessibilityElement {
93 NSMutableArray<QMacAccessibilityElement *> *rows;
94 NSMutableArray<QMacAccessibilityElement *> *columns;
102 NSString *synthesizedRole;
105- (instancetype)initWithId:(QAccessible::Id)anId
107 return [self initWithId:anId role:nil];
110- (instancetype)initWithId:(QAccessible::Id)anId role:(NSAccessibilityRole)role
112 Q_ASSERT((
int)anId < 0);
120 synthesizedRole = role;
124 if (!synthesizedRole) {
125 if (QAccessibleInterface *iface = QAccessible::accessibleInterface(axid)) {
126 if (iface->tableInterface()) {
127 [self updateTableModel];
128 }
else if (
const auto *cell = iface->tableCellInterface()) {
131 m_rowIndex = cell->rowIndex();
132 m_columnIndex = cell->columnIndex();
133 QAccessibleInterface *table = cell->table();
135 QAccessibleTableInterface *tableInterface = table->tableInterface();
137 if (tableInterface && (m_rowIndex >= 0) && (m_columnIndex >= 0)) {
138 auto *tableElement = [QMacAccessibilityElement elementWithInterface:table];
139 Q_ASSERT(tableElement);
140 if (!tableElement->rows
141 ||
int(tableElement->rows.count) <= m_rowIndex
142 ||
int(tableElement->rows.count) != tableInterface->rowCount()) {
143 qCWarning(lcAccessibilityTable)
144 <<
"Cell requested for row" << m_rowIndex <<
"is out of"
145 <<
"bounds for table with" << (tableElement->rows ?
146 tableElement->rows.count : tableInterface->rowCount())
147 <<
"rows! Resizing table model.";
148 [tableElement updateTableModel];
151 Q_ASSERT(tableElement->rows);
152 Q_ASSERT(
int(tableElement->rows.count) > m_rowIndex);
154 auto *rowElement = tableElement->rows[m_rowIndex];
155 if (!rowElement->columns ||
int(rowElement->columns.count) != tableInterface->columnCount()) {
156 if (rowElement->columns) {
157 qCWarning(lcAccessibilityTable)
158 <<
"Table representation column count is out of sync:"
159 << rowElement->columns.count <<
"!=" << tableInterface->columnCount();
160 [rowElement->columns autorelease];
161 rowElement->columns = nil;
163 rowElement->columns = [rowElement populateTableRow:tableInterface->columnCount()];
164 [rowElement->columns retain];
167 qCDebug(lcAccessibilityTable) <<
"Creating cell representation for"
168 << m_rowIndex << m_columnIndex
170 << tableElement->rows.count <<
"rows and"
171 << rowElement->columns.count <<
"columns";
173 rowElement->columns[m_columnIndex] = self;
184
185
186
187
188
189
190
191
192+ (instancetype)elementWithId:(QAccessible::Id)anId
198 QAccessibleCache *cache = QAccessibleCache::instance();
200 QMacAccessibilityElement *element = cache->elementForId(anId);
202 Q_ASSERT(QAccessible::accessibleInterface(anId));
203 element = [[self alloc] initWithId:anId];
204 if (cache->insertElement(anId, element))
210+ (instancetype)elementWithInterface:(QAccessibleInterface *)iface
216 const QAccessible::Id anId = QAccessible::uniqueId(iface);
217 return [self elementWithId:anId];
228 [columns autorelease];
231 synthesizedRole = nil;
233 NSAccessibilityPostNotification(self, NSAccessibilityUIElementDestroyedNotification);
240 NSAccessibilityPostNotification(self, NSAccessibilityUIElementDestroyedNotification);
248- (BOOL)isEqual:(id)object {
249 if ([object isKindOfClass:[QMacAccessibilityElement
class]]) {
250 QMacAccessibilityElement *other = object;
253 return other == self;
255 return other->axid == axid && other->synthesizedRole == synthesizedRole
257 || (other->m_rowIndex == m_rowIndex && other->m_columnIndex == m_columnIndex));
267- (BOOL)isManagedByParent {
268 return synthesizedRole != nil;
271- (NSMutableArray *)populateTableArray:(NSAccessibilityRole)role count:(
int)count
273 if (self.qtInterface) {
274 auto *array = [NSMutableArray<QMacAccessibilityElement *> arrayWithCapacity:count];
276 for (
int n = 0; n < count; ++n) {
278 QMacAccessibilityElement *element =
279 [[QMacAccessibilityElement alloc] initWithId:axid role:role];
281 if (role == NSAccessibilityRowRole)
282 element->m_rowIndex = n;
283 else if (role == NSAccessibilityColumnRole)
284 element->m_columnIndex = n;
285 [array addObject:element];
288 qWarning(
"QCocoaAccessibility: invalid child");
296- (NSMutableArray *)populateTableRow:(
int)count
298 Q_ASSERT(synthesizedRole == NSAccessibilityRowRole);
299 qCDebug(lcAccessibilityTable) <<
"Populating table row" << m_rowIndex
300 <<
"with" << count <<
"placeholder cells";
309 auto *array = [NSMutableArray<QMacAccessibilityElement *> arrayWithCapacity:count];
312 for (
int n = 0; n < count; ++n) {
314 QMacAccessibilityElement *cell =
315 [[QMacAccessibilityElement alloc] initWithId:axid role:NSAccessibilityCellRole];
317 cell->m_rowIndex = m_rowIndex;
318 cell->m_columnIndex = n;
319 [array addObject:cell];
326- (
void)updateTableModel
328 if (QAccessibleInterface *iface = self.qtInterface) {
329 if (QAccessibleTableInterface *table = iface->tableInterface()) {
330 Q_ASSERT(!self.isManagedByParent);
331 qCDebug(lcAccessibilityTable) <<
"Updating table representation with"
332 << table->rowCount() << table->columnCount();
337 rows = [self populateTableArray:NSAccessibilityRowRole count:table->rowCount()];
340 [columns autorelease];
343 columns = [self populateTableArray:NSAccessibilityColumnRole count:table->columnCount()];
349- (QAccessibleInterface *)qtInterface
351 QAccessibleInterface *iface = QAccessible::accessibleInterface(axid);
352 if (!iface || !iface->isValid())
358 if (synthesizedRole == NSAccessibilityCellRole) {
360 QAccessibleTableInterface *table = iface->tableInterface();
362 QAccessibleInterface *cell = table->cellAt(m_rowIndex, m_columnIndex);
365 Q_ASSERT(cell->isValid());
369 axid = QAccessible::uniqueId(cell);
370 synthesizedRole = nil;
375 QAccessibleCache *cache = QAccessibleCache::instance();
376 Q_ASSERT(!cache->elementForId(axid) || !cache->elementForId(axid)->synthesizedRole);
377 cache->insertElement(axid, self);
386- (BOOL)isAccessibilityFocused
389 id focusedElement = NSApp.accessibilityApplicationFocusedUIElement;
390 return [focusedElement isEqual:self];
395+ (id) lineNumberForIndex: (
int)index forText:(
const QString &)text
397 auto textBefore = QStringView(text).left(index);
398 qsizetype newlines = textBefore.count(u'\n');
402- (BOOL) accessibilityNotifiesWhenDestroyed {
406- (NSString *) accessibilityRole {
409 return synthesizedRole;
410 if (QAccessibleInterface *iface = self.qtInterface)
411 return QCocoaAccessible::macRole(iface);
412 return NSAccessibilityUnknownRole;
415- (NSString *) accessibilitySubRole {
416 if (QAccessibleInterface *iface = self.qtInterface)
417 return QCocoaAccessible::macSubrole(iface);
418 return NSAccessibilityUnknownRole;
421- (NSString *) accessibilityRoleDescription {
422 if (self.qtInterface)
423 return NSAccessibilityRoleDescription(self.accessibilityRole, self.accessibilitySubRole);
424 return NSAccessibilityUnknownRole;
427- (NSArray *) accessibilityChildren {
429 if (synthesizedRole == NSAccessibilityCellRole)
432 QAccessibleInterface *iface = self.qtInterface;
435 if (QAccessibleTableInterface *table = iface->tableInterface()) {
437 if (!synthesizedRole) {
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470 NSArray *rs = [self accessibilityRows];
471 NSArray *cs = [self accessibilityColumns];
472 const int rCount =
int([rs count]);
473 const int cCount =
int([cs count]);
474 int childCount = rCount + cCount;
475 NSMutableArray<QMacAccessibilityElement *> *tableChildren =
476 [NSMutableArray<QMacAccessibilityElement *> arrayWithCapacity:childCount];
477 for (
int i = 0; i < rCount; ++i) {
478 [tableChildren addObject:[rs objectAtIndex:i]];
480 for (
int i = 0; i < cCount; ++i) {
481 [tableChildren addObject:[cs objectAtIndex:i]];
483 return NSAccessibilityUnignoredChildren(tableChildren);
484 }
else if (synthesizedRole == NSAccessibilityColumnRole) {
486 }
else if (synthesizedRole == NSAccessibilityRowRole) {
489 Q_ASSERT(m_rowIndex >= 0);
490 Q_ASSERT(rows == nil);
491 const unsigned int numColumns = table->columnCount();
492 if (!columns || columns.count != numColumns) {
494 [columns autorelease];
497 columns = [self populateTableRow:numColumns];
500 return NSAccessibilityUnignoredChildren(columns);
503 return QCocoaAccessible::unignoredChildren(iface);
506- (NSArray *) accessibilitySelectedChildren {
507 QAccessibleInterface *iface = QAccessible::accessibleInterface(axid);
508 if (!iface || !iface->isValid())
511 QAccessibleSelectionInterface *selection = iface->selectionInterface();
515 const QList<QAccessibleInterface *> selectedList = selection->selectedItems();
516 const qsizetype numSelected = selectedList.size();
517 NSMutableArray<QMacAccessibilityElement *> *selectedChildren =
518 [NSMutableArray<QMacAccessibilityElement *> arrayWithCapacity:numSelected];
519 for (QAccessibleInterface *selectedChild : selectedList) {
520 if (selectedChild && selectedChild->isValid()) {
521 QAccessible::Id id = QAccessible::uniqueId(selectedChild);
522 QMacAccessibilityElement *element = [QMacAccessibilityElement elementWithId:id];
524 [selectedChildren addObject:element];
527 return NSAccessibilityUnignoredChildren(selectedChildren);
530- (id) accessibilityWindow {
532 NSAccessibilityElement *parent = self.accessibilityParent;
533 if (parent && parent.accessibilityRole == NSAccessibilityWindowRole)
535 return [parent accessibilityWindow];
538- (id) accessibilityTopLevelUIElementAttribute {
540 return [self.accessibilityParent accessibilityTopLevelUIElementAttribute];
543- (NSString *) accessibilityTitle {
544 if (QAccessibleInterface *iface = self.qtInterface) {
545 if (iface->role() == QAccessible::StaticText)
547 if (self.isManagedByParent)
549 return iface->text(QAccessible::Name).toNSString();
554- (id) accessibilityTitleUIElement {
555 QAccessibleInterface *iface = self.qtInterface;
559 const auto labelRelations = iface->relations(QAccessible::Label);
560 if (labelRelations.empty())
563 QAccessibleInterface *label = labelRelations.first().first;
567 QMacAccessibilityElement *accessibleElement = [QMacAccessibilityElement elementWithInterface:label];
568 if (!accessibleElement)
571 return NSAccessibilityUnignoredAncestor(accessibleElement);
574- (NSString*) accessibilityIdentifier {
575 if (QAccessibleInterface *iface = self.qtInterface)
576 return QAccessibleBridgeUtils::accessibleId(iface).toNSString();
580- (BOOL) isAccessibilityEnabled {
581 if (QAccessibleInterface *iface = self.qtInterface)
582 return !iface->state().disabled;
586- (id)accessibilityParent {
587 if (synthesizedRole == NSAccessibilityCellRole) {
588 QAccessibleInterface *tableInterface = QAccessible::accessibleInterface(axid);
589 if (!tableInterface || !tableInterface->isValid())
591 QMacAccessibilityElement *tableElement =
592 [QMacAccessibilityElement elementWithId:axid];
593 if (!tableElement->rows || m_rowIndex < 0
594 || m_rowIndex >=
int(tableElement->rows.count))
596 QMacAccessibilityElement *rowElement = tableElement->rows[m_rowIndex];
600 QAccessibleInterface *iface = self.qtInterface;
604 if (self.isManagedByParent) {
606 return NSAccessibilityUnignoredAncestor([QMacAccessibilityElement elementWithId:axid]);
609 if (QAccessibleInterface *parent = iface->parent()) {
610 if (parent->tableInterface()) {
611 QMacAccessibilityElement *tableElement =
612 [QMacAccessibilityElement elementWithInterface:parent];
617 if (QAccessibleTableCellInterface *cell = iface->tableCellInterface())
618 rowIndex = cell->rowIndex();
619 else if (m_rowIndex >= 0 && m_columnIndex >= 0)
620 rowIndex = m_rowIndex;
621 if (!tableElement->rows || rowIndex < 0 || rowIndex >=
int(tableElement->rows.count))
623 QMacAccessibilityElement *rowElement = tableElement->rows[rowIndex];
624 return NSAccessibilityUnignoredAncestor(rowElement);
629 if (!QCocoaAccessible::isRepresentedByAppKit(parent))
630 return NSAccessibilityUnignoredAncestor([QMacAccessibilityElement elementWithInterface: parent]);
633 if (QWindow *window = iface->window()) {
634 QPlatformWindow *platformWindow = window->handle();
635 if (platformWindow) {
636 QCocoaWindow *win =
static_cast<QCocoaWindow*>(platformWindow);
637 return NSAccessibilityUnignoredAncestor(qnsview_cast(win->view()));
643- (NSRect)accessibilityFrame {
644 QAccessibleInterface *iface = self.qtInterface;
649 if (self.isManagedByParent) {
650 if (QAccessibleTableInterface *table = iface->tableInterface()) {
657 const bool isRow = synthesizedRole == NSAccessibilityRowRole;
659 int &row = isRow ? cellPos.ry() : cellPos.rx();
660 int &col = isRow ? cellPos.rx() : cellPos.ry();
662 NSUInteger trackIndex = self.accessibilityIndex;
663 if (trackIndex != NSNotFound) {
664 row =
int(trackIndex);
665 if (QAccessibleInterface *firstCell = table->cellAt(cellPos.y(), cellPos.x())) {
666 rect = firstCell->rect();
667 col = isRow ? table->columnCount() : table->rowCount();
670 if (QAccessibleInterface *lastCell =
671 table->cellAt(cellPos.y(), cellPos.x()))
672 rect = rect.united(lastCell->rect());
678 rect = iface->rect();
681 return QCocoaScreen::mapToNative(rect);
684- (NSString*)accessibilityLabel {
685 if (QAccessibleInterface *iface = self.qtInterface)
686 return iface->text(QAccessible::Description).toNSString();
687 qWarning() <<
"Called accessibilityLabel on invalid object: " << axid;
691- (
void)setAccessibilityLabel:(NSString*)label{
692 if (QAccessibleInterface *iface = self.qtInterface)
693 iface->setText(QAccessible::Description, QString::fromNSString(label));
696- (NSAccessibilityOrientation)accessibilityOrientation {
697 QAccessibleInterface *iface = self.qtInterface;
699 return NSAccessibilityOrientationUnknown;
701 NSAccessibilityOrientation nsOrientation = NSAccessibilityOrientationUnknown;
702 if (QAccessibleAttributesInterface *attributesIface = iface->attributesInterface()) {
703 const QVariant orientationVariant =
704 attributesIface->attributeValue(QAccessible::Attribute::Orientation);
705 if (orientationVariant.isValid()) {
706 Q_ASSERT(orientationVariant.canConvert<Qt::Orientation>());
707 const Qt::Orientation orientation = orientationVariant.value<Qt::Orientation>();
708 nsOrientation = orientation == Qt::Horizontal ? NSAccessibilityOrientationHorizontal
709 : NSAccessibilityOrientationVertical;
712 return nsOrientation;
715- (id) accessibilityValue {
716 if (QAccessibleInterface *iface = self.qtInterface) {
719 if (QCocoaAccessible::hasValueAttribute(iface))
720 return QCocoaAccessible::getValueAttribute(iface);
726- (id) accessibilityMinValue {
727 if (QAccessibleInterface *iface = self.qtInterface) {
728 if (iface->valueInterface()) {
729 return QCocoaAccessible::valueAsNSObject(iface->valueInterface()->minimumValue());
736- (id) accessibilityMaxValue {
737 if (QAccessibleInterface *iface = self.qtInterface) {
738 if (iface->valueInterface()) {
739 return QCocoaAccessible::valueAsNSObject(iface->valueInterface()->maximumValue());
745- (NSInteger) accessibilityNumberOfCharacters {
746 if (QAccessibleInterface *iface = self.qtInterface) {
747 if (QAccessibleTextInterface *text = iface->textInterface())
748 return text->characterCount();
753- (NSString *) accessibilitySelectedText {
754 if (QAccessibleInterface *iface = self.qtInterface) {
755 if (QAccessibleTextInterface *text = iface->textInterface()) {
758 text->selection(0, &start, &end);
759 return text->text(start, end).toNSString();
765- (NSRange) accessibilitySelectedTextRange {
766 QAccessibleInterface *iface = self.qtInterface;
769 if (QAccessibleTextInterface *text = iface->textInterface()) {
772 if (text->selectionCount() > 0) {
773 text->selection(0, &start, &end);
775 start = text->cursorPosition();
778 return NSMakeRange(quint32(start), quint32(end - start));
780 return NSMakeRange(0, 0);
783- (NSInteger)accessibilityLineForIndex:(NSInteger)index {
784 QAccessibleInterface *iface = self.qtInterface;
787 if (QAccessibleTextInterface *text = iface->textInterface()) {
788 QString textToPos = text->text(0, index);
789 return textToPos.count(
'\n');
794- (NSRange)accessibilityVisibleCharacterRange {
795 QAccessibleInterface *iface = self.qtInterface;
799 if (QAccessibleTextInterface *text = iface->textInterface())
800 return NSMakeRange(0,
static_cast<uint>(text->characterCount()));
801 return NSMakeRange(0,
static_cast<uint>(iface->text(QAccessible::Name).length()));
804- (NSInteger) accessibilityInsertionPointLineNumber {
805 QAccessibleInterface *iface = self.qtInterface;
808 if (QAccessibleTextInterface *text = iface->textInterface()) {
809 int position = text->cursorPosition();
810 return [self accessibilityLineForIndex:position];
815- (NSArray *)accessibilityAttributeNames {
816 NSMutableArray *attributes = [[NSMutableArray
new] autorelease];
817 QAccessibleInterface *iface = self.qtInterface;
819#if QT_APPLE_SDK_EQUAL_OR_ABOVE(MACOS(26
))
820 if (@available(macOS 26, *)) {
821 [attributes addObject:NSAccessibilityLanguageAttribute];
823 if (iface && iface->role() == QAccessible::Heading) {
824 QAccessibleAttributesInterface *attributesIface = iface->attributesInterface();
826 && attributesIface->attributeKeys().contains(QAccessible::Attribute::Level))
827 [attributes addObject:NSAccessibilityHeadingLevelAttribute];
832 if (iface && iface->state().expandable)
833 [attributes addObject:NSAccessibilityExpandedAttribute];
838- (id)accessibilityAttributeValue:(NSString *)attribute {
839 QAccessibleInterface *iface = self.qtInterface;
841 qWarning() <<
"Called attribute on invalid object: " << axid;
845#if QT_APPLE_SDK_EQUAL_OR_ABOVE(MACOS(26
))
846 if (@available(macOS 26, *)) {
847 if ([attribute isEqualToString:NSAccessibilityLanguageAttribute]) {
848 QAccessibleAttributesInterface *attributesIface = iface->attributesInterface();
850 || !attributesIface->attributeKeys().contains(QAccessible::Attribute::Locale))
853 const auto &localeVariant =
854 attributesIface->attributeValue(QAccessible::Attribute::Locale);
855 return localeVariant.toLocale().bcp47Name().toNSString();
858 if ([attribute isEqualToString:NSAccessibilityHeadingLevelAttribute]) {
859 if (iface->role() != QAccessible::Heading)
862 auto attributesIface = iface->attributesInterface();
863 if (!attributesIface)
866 const QVariant levelVariant =
867 attributesIface->attributeValue(QAccessible::Attribute::Level);
868 if (!levelVariant.isValid() || !levelVariant.canConvert<
int>())
871 const int level = levelVariant.toInt();
880 if ([attribute isEqualToString:NSAccessibilityExpandedAttribute])
881 return iface->state().expanded ? @YES : @NO;
886- (NSArray *)accessibilityParameterizedAttributeNames {
888 QAccessibleInterface *iface = self.qtInterface;
890 qWarning() <<
"Called attribute on invalid object: " << axid;
894 if (iface->textInterface()) {
896 NSAccessibilityStringForRangeParameterizedAttribute,
897 NSAccessibilityLineForIndexParameterizedAttribute,
898 NSAccessibilityRangeForLineParameterizedAttribute,
899 NSAccessibilityRangeForPositionParameterizedAttribute,
901 NSAccessibilityBoundsForRangeParameterizedAttribute,
903 NSAccessibilityStyleRangeForIndexParameterizedAttribute,
904 NSAccessibilityAttributedStringForRangeParameterizedAttribute
911- (id)accessibilityAttributeValue:(NSString *)attribute forParameter:(id)parameter {
912 QAccessibleInterface *iface = self.qtInterface;
914 qWarning() <<
"Called attribute on invalid object: " << axid;
918 if (!iface->textInterface())
921 if ([attribute isEqualToString: NSAccessibilityStringForRangeParameterizedAttribute]) {
922 NSRange range = [parameter rangeValue];
923 QString text = iface->textInterface()->text(range.location, range.location + range.length);
924 return text.toNSString();
926 if ([attribute isEqualToString: NSAccessibilityLineForIndexParameterizedAttribute]) {
927 int index = [parameter intValue];
928 if (index < 0 || index > iface->textInterface()->characterCount())
931 if (iface->state().multiLine) {
933 convertLineOffset(iface->textInterface(), &line, &index);
937 if ([attribute isEqualToString: NSAccessibilityRangeForLineParameterizedAttribute]) {
938 int line = [parameter intValue];
942 NSUInteger startOffset = 0;
943 NSUInteger endOffset = 0;
944 convertLineOffset(iface->textInterface(), &line, &lineOffset, &startOffset, &endOffset);
945 return [NSValue valueWithRange:NSMakeRange(startOffset, endOffset - startOffset)];
947 if ([attribute isEqualToString: NSAccessibilityBoundsForRangeParameterizedAttribute]) {
948 NSRange range = [parameter rangeValue];
949 QRect firstRect = iface->textInterface()->characterRect(range.location);
951 if (range.length > 0) {
952 NSUInteger position = range.location + range.length - 1;
953 if (position > range.location && iface->textInterface()->text(position, position + 1) ==
"\n"_L1)
955 QRect lastRect = iface->textInterface()->characterRect(position);
956 rect = firstRect.united(lastRect);
961 return [NSValue valueWithRect:QCocoaScreen::mapToNative(rect)];
963 if ([attribute isEqualToString: NSAccessibilityAttributedStringForRangeParameterizedAttribute]) {
964 NSRange range = [parameter rangeValue];
965 QString text = iface->textInterface()->text(range.location, range.location + range.length);
966 return [[NSAttributedString alloc] initWithString:text.toNSString()];
967 }
else if ([attribute isEqualToString: NSAccessibilityRangeForPositionParameterizedAttribute]) {
968 QPoint point = QCocoaScreen::mapFromNative([parameter pointValue]).toPoint();
969 int offset = iface->textInterface()->offsetAtPoint(point);
970 return [NSValue valueWithRange:NSMakeRange(
static_cast<NSUInteger>(offset), 1)];
971 }
else if ([attribute isEqualToString: NSAccessibilityStyleRangeForIndexParameterizedAttribute]) {
974 iface->textInterface()->attributes([parameter intValue], &start, &end);
975 return [NSValue valueWithRange:NSMakeRange(
static_cast<NSUInteger>(start),
static_cast<NSUInteger>(end - start))];
980- (BOOL)accessibilityIsAttributeSettable:(NSString *)attribute {
981 QAccessibleInterface *iface = self.qtInterface;
985 if ([attribute isEqualToString:NSAccessibilityFocusedAttribute]) {
986 return iface->state().focusable ? YES : NO;
987 }
else if ([attribute isEqualToString:NSAccessibilityValueAttribute]) {
988 if (iface->textInterface() && iface->state().editable)
990 if (iface->valueInterface())
993 }
else if ([attribute isEqualToString:NSAccessibilitySelectedTextRangeAttribute]) {
994 return iface->textInterface() ? YES : NO;
999- (
void)accessibilitySetValue:(id)value forAttribute:(NSString *)attribute {
1000 QAccessibleInterface *iface = self.qtInterface;
1003 if ([attribute isEqualToString:NSAccessibilityFocusedAttribute]) {
1004 if (QAccessibleActionInterface *action = iface->actionInterface())
1005 action->doAction(QAccessibleActionInterface::setFocusAction());
1006 }
else if ([attribute isEqualToString:NSAccessibilityValueAttribute]) {
1007 if (iface->textInterface()) {
1008 QString text = QString::fromNSString((NSString *)value);
1009 iface->setText(QAccessible::Value, text);
1010 }
else if (QAccessibleValueInterface *valueIface = iface->valueInterface()) {
1011 double val = [value doubleValue];
1012 valueIface->setCurrentValue(val);
1014 }
else if ([attribute isEqualToString:NSAccessibilitySelectedTextRangeAttribute]) {
1015 if (QAccessibleTextInterface *text = iface->textInterface()) {
1016 NSRange range = [value rangeValue];
1017 if (range.length > 0)
1018 text->setSelection(0, range.location, range.location + range.length);
1020 text->setCursorPosition(range.location);
1027- (NSArray *)accessibilityActionNames {
1028 NSMutableArray *nsActions = [[NSMutableArray
new] autorelease];
1029 QAccessibleInterface *iface = self.qtInterface;
1033 const QStringList &supportedActionNames = QAccessibleBridgeUtils::effectiveActionNames(iface);
1034 for (
const QString &qtAction : supportedActionNames) {
1035 NSString *nsAction = QCocoaAccessible::getTranslatedAction(qtAction);
1038 if (nsAction && ![nsActions containsObject:nsAction])
1039 [nsActions addObject : nsAction];
1045- (NSString *)accessibilityActionDescription:(NSString *)action {
1046 QAccessibleInterface *iface = self.qtInterface;
1049 QString qtAction = QCocoaAccessible::translateAction(action, iface);
1050 QString description;
1052 if (qtAction.isEmpty()) {
1053 if (QAccessibleActionInterface *actionInterface = iface->actionInterface()) {
1054 qtAction = QString::fromNSString((NSString *)action);
1055 description = actionInterface->localizedActionDescription(qtAction);
1058 description = qAccessibleLocalizedActionDescription(qtAction);
1060 return description.toNSString();
1063- (
void)accessibilityPerformAction:(NSString *)action {
1064 if (QAccessibleInterface *iface = self.qtInterface) {
1065 const QString qtAction = QCocoaAccessible::translateAction(action, iface);
1066 QAccessibleBridgeUtils::performEffectiveAction(iface, qtAction);
1072- (BOOL)accessibilityIsIgnored {
1076 if (self.isManagedByParent)
1079 if (QAccessibleInterface *iface = self.qtInterface)
1080 return QCocoaAccessible::shouldBeIgnored(iface);
1084- (id)accessibilityHitTest:(NSPoint)point
1086 QAccessibleInterface *iface = self.qtInterface;
1088 return NSAccessibilityUnignoredAncestor(self);
1090 QPointF screenPoint = QCocoaScreen::mapFromNative(point);
1091 QWindow *window = QAccessibleBridgeUtils::windowFor(iface);
1095 QAccessibleInterface *childInterface = iface;
1096 while (QAccessibleInterface *child = childInterface->childAt(screenPoint.x(), screenPoint.y())) {
1097 if (!child->isValid())
1100 QWindow *childWindow = QAccessibleBridgeUtils::windowFor(child, childInterface, window);
1101 if (childWindow != window) {
1102 if (
auto *view = QCocoaAccessible::accessibleViewFor(childWindow))
1103 return [view accessibilityHitTest:point];
1106 childInterface = child;
1110 if (childInterface == iface)
1111 return NSAccessibilityUnignoredAncestor(self);
1114 QMacAccessibilityElement *accessibleElement = [QMacAccessibilityElement elementWithInterface:childInterface];
1115 if (accessibleElement)
1116 return NSAccessibilityUnignoredAncestor(accessibleElement);
1117 return NSAccessibilityUnignoredAncestor(self);
1120- (id)accessibilityFocusedUIElement {
1121 QAccessibleInterface *iface = self.qtInterface;
1123 qWarning(
"FocusedUIElement for INVALID");
1127 QAccessibleInterface *focusInterface = iface->focusChild();
1128 if (focusInterface && focusInterface->isValid()) {
1135 QWindow *window = QAccessibleBridgeUtils::windowFor(iface);
1136 QWindow *focusWindow = QAccessibleBridgeUtils::windowFor(focusInterface, iface, window);
1137 if (focusWindow != window) {
1138 if (
auto *view = QCocoaAccessible::accessibleViewFor(focusWindow))
1139 return [view accessibilityFocusedUIElement];
1142 QMacAccessibilityElement *accessibleElement = [QMacAccessibilityElement elementWithInterface:focusInterface];
1143 return NSAccessibilityUnignoredAncestor(accessibleElement);
1146 return NSAccessibilityUnignoredAncestor(self);
1149- (NSString *) accessibilityHelp {
1150 if (QAccessibleInterface *iface = self.qtInterface) {
1151 const QString helpText = iface->text(QAccessible::Help);
1152 if (!helpText.isEmpty())
1153 return helpText.toNSString();
1159
1160
1161- (NSInteger) accessibilityIndex {
1162 NSInteger index = 0;
1163 if (synthesizedRole == NSAccessibilityCellRole)
1164 return m_columnIndex;
1165 if (QAccessibleInterface *iface = self.qtInterface) {
1166 if (self.isManagedByParent) {
1169 if (iface->tableInterface()) {
1170 if (m_rowIndex >= 0)
1171 index = NSInteger(m_rowIndex);
1172 else if (m_columnIndex >= 0)
1173 index = NSInteger(m_columnIndex);
1180- (NSArray *) accessibilityRows {
1181 if (!synthesizedRole && rows) {
1182 QAccessibleInterface *iface = self.qtInterface;
1183 QAccessibleTableInterface *tableInterface = iface ? iface->tableInterface() :
nullptr;
1184 if (tableInterface) {
1185 const unsigned int rowCount = tableInterface->rowCount();
1186 if (rows.count != rowCount) {
1187 qCDebug(lcAccessibilityTable) <<
"Updating table rows with" << rowCount <<
"rows";
1192 rows = [self populateTableArray:NSAccessibilityRowRole
1196 return NSAccessibilityUnignoredChildren(rows);
1202- (NSArray *) accessibilityColumns {
1204 if (!synthesizedRole && columns) {
1205 QAccessibleInterface *iface = self.qtInterface;
1206 if (iface && iface->tableInterface())
1207 return NSAccessibilityUnignoredChildren(columns);
1214- (NSArray *) accessibilityTabs {
1215 QAccessibleInterface *iface = self.qtInterface;
1216 if (iface && iface->role() == QAccessible::PageTabList) {
1217 return QCocoaAccessible::unignoredChildren(iface, [](QAccessibleInterface *child){
1218 return QCocoaAccessible::defaultUnignored(child)
1219 && child->role() == QAccessible::PageTab;