Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickaccessibleattached.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#if QT_CONFIG(accessibility)
7
8#include <QtQml/qqmlinfo.h>
9
10#include "private/qquickitem_p.h"
11
13
289QMetaMethod QQuickAccessibleAttached::sigPress;
290QMetaMethod QQuickAccessibleAttached::sigToggle;
291QMetaMethod QQuickAccessibleAttached::sigIncrease;
292QMetaMethod QQuickAccessibleAttached::sigDecrease;
293QMetaMethod QQuickAccessibleAttached::sigScrollUp;
294QMetaMethod QQuickAccessibleAttached::sigScrollDown;
295QMetaMethod QQuickAccessibleAttached::sigScrollLeft;
296QMetaMethod QQuickAccessibleAttached::sigScrollRight;
297QMetaMethod QQuickAccessibleAttached::sigPreviousPage;
298QMetaMethod QQuickAccessibleAttached::sigNextPage;
299
300QQuickAccessibleAttached::QQuickAccessibleAttached(QObject *parent)
301 : QObject(parent), m_role(QAccessible::NoRole)
302{
303 Q_ASSERT(parent);
304 if (!item()) {
305 qmlWarning(parent) << "Accessible must be attached to an Item";
306 return;
307 }
308
309 // Enable accessibility for items with accessible content. This also
310 // enables accessibility for the ancestors of souch items.
311 item()->d_func()->setAccessible();
312 QAccessibleEvent ev(item(), QAccessible::ObjectCreated);
313 QAccessible::updateAccessibility(&ev);
314
315 if (const QMetaObject *pmo = parent->metaObject()) {
316 auto connectPropertyChangeSignal = [parent, pmo, this](
317 const char *propertyName, const char *signalName, int slotIndex)
318 {
319 // basically does this:
320 // if the parent has the property \a propertyName with the associated \a signalName:
321 // connect(parent, signalName, this, slotIndex)
322
323 // Note that we explicitly want to only connect to standard property/signal naming
324 // convention: "value" & "valueChanged"
325 // (e.g. avoid a compound property with e.g. a signal notifier named "updated()")
326 int idxProperty = pmo->indexOfProperty(propertyName);
327 if (idxProperty != -1) {
328 const QMetaProperty property = pmo->property(idxProperty);
329 const QMetaMethod signal = property.notifySignal();
330 if (signal.name() == signalName)
331 QMetaObject::connect(parent, signal.methodIndex(), this, slotIndex);
332 }
333 return;
334 };
335 const QMetaObject &smo = staticMetaObject;
336 static const int valueChangedIndex = smo.indexOfSlot("valueChanged()");
337 connectPropertyChangeSignal("value", "valueChanged", valueChangedIndex);
338
339 static const int cursorPositionChangedIndex = smo.indexOfSlot("cursorPositionChanged()");
340 connectPropertyChangeSignal("cursorPosition", "cursorPositionChanged",
341 cursorPositionChangedIndex);
342 }
343
344 if (!sigPress.isValid()) {
345 sigPress = QMetaMethod::fromSignal(&QQuickAccessibleAttached::pressAction);
346 sigToggle = QMetaMethod::fromSignal(&QQuickAccessibleAttached::toggleAction);
347 sigIncrease = QMetaMethod::fromSignal(&QQuickAccessibleAttached::increaseAction);
348 sigDecrease = QMetaMethod::fromSignal(&QQuickAccessibleAttached::decreaseAction);
349 sigScrollUp = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollUpAction);
350 sigScrollDown = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollDownAction);
351 sigScrollLeft = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollLeftAction);
352 sigScrollRight = QMetaMethod::fromSignal(&QQuickAccessibleAttached::scrollRightAction);
353 sigPreviousPage = QMetaMethod::fromSignal(&QQuickAccessibleAttached::previousPageAction);
354 sigNextPage= QMetaMethod::fromSignal(&QQuickAccessibleAttached::nextPageAction);
355 }
356}
357
358QQuickAccessibleAttached::~QQuickAccessibleAttached()
359{
360}
361
362void QQuickAccessibleAttached::setRole(QAccessible::Role role)
363{
364 if (role != m_role) {
365 m_role = role;
366 Q_EMIT roleChanged();
367 // There is no way to signify role changes at the moment.
368 // QAccessible::updateAccessibility(parent(), 0, QAccessible::);
369
370 switch (role) {
371 case QAccessible::CheckBox:
372 case QAccessible::RadioButton:
373 if (!m_stateExplicitlySet.focusable)
374 m_state.focusable = true;
375 if (!m_stateExplicitlySet.checkable)
376 m_state.checkable = true;
377 break;
378 case QAccessible::Button:
379 case QAccessible::MenuItem:
380 case QAccessible::PageTab:
381 case QAccessible::SpinBox:
382 case QAccessible::ComboBox:
383 case QAccessible::Terminal:
384 case QAccessible::ScrollBar:
385 if (!m_stateExplicitlySet.focusable)
386 m_state.focusable = true;
387 break;
388 case QAccessible::EditableText:
389 if (!m_stateExplicitlySet.editable)
390 m_state.editable = true;
391 if (!m_stateExplicitlySet.focusable)
392 m_state.focusable = true;
393 break;
394 case QAccessible::StaticText:
395 if (!m_stateExplicitlySet.readOnly)
396 m_state.readOnly = true;
397 if (!m_stateExplicitlySet.focusable)
398 m_state.focusable = true;
399 break;
400 default:
401 break;
402 }
403 }
404}
405
406bool QQuickAccessibleAttached::wasNameExplicitlySet() const
407{
408 return m_nameExplicitlySet;
409}
410
411// Allows types to attach an accessible name to an item as a convenience,
412// so long as the user hasn't done so themselves.
413void QQuickAccessibleAttached::setNameImplicitly(const QString &name)
414{
415 setName(name);
416 m_nameExplicitlySet = false;
417}
418
419QQuickAccessibleAttached *QQuickAccessibleAttached::qmlAttachedProperties(QObject *obj)
420{
421 return new QQuickAccessibleAttached(obj);
422}
423
424bool QQuickAccessibleAttached::ignored() const
425{
426 return item() ? !item()->d_func()->isAccessible : false;
427}
428
429void QQuickAccessibleAttached::setIgnored(bool ignored)
430{
431 if (this->ignored() != ignored && item()) {
432 item()->d_func()->isAccessible = !ignored;
433 emit ignoredChanged();
434 }
435}
436
437bool QQuickAccessibleAttached::doAction(const QString &actionName)
438{
439 QMetaMethod *sig = nullptr;
440 if (actionName == QAccessibleActionInterface::pressAction())
441 sig = &sigPress;
442 else if (actionName == QAccessibleActionInterface::toggleAction())
443 sig = &sigToggle;
444 else if (actionName == QAccessibleActionInterface::increaseAction())
445 sig = &sigIncrease;
446 else if (actionName == QAccessibleActionInterface::decreaseAction())
447 sig = &sigDecrease;
448 else if (actionName == QAccessibleActionInterface::scrollUpAction())
449 sig = &sigScrollUp;
450 else if (actionName == QAccessibleActionInterface::scrollDownAction())
451 sig = &sigScrollDown;
452 else if (actionName == QAccessibleActionInterface::scrollLeftAction())
453 sig = &sigScrollLeft;
454 else if (actionName == QAccessibleActionInterface::scrollRightAction())
455 sig = &sigScrollRight;
456 else if (actionName == QAccessibleActionInterface::previousPageAction())
457 sig = &sigPreviousPage;
458 else if (actionName == QAccessibleActionInterface::nextPageAction())
459 sig = &sigNextPage;
460 if (sig && isSignalConnected(*sig))
461 return sig->invoke(this);
462 return false;
463}
464
465void QQuickAccessibleAttached::availableActions(QStringList *actions) const
466{
467 if (isSignalConnected(sigPress))
468 actions->append(QAccessibleActionInterface::pressAction());
469 if (isSignalConnected(sigToggle))
470 actions->append(QAccessibleActionInterface::toggleAction());
471 if (isSignalConnected(sigIncrease))
472 actions->append(QAccessibleActionInterface::increaseAction());
473 if (isSignalConnected(sigDecrease))
474 actions->append(QAccessibleActionInterface::decreaseAction());
475 if (isSignalConnected(sigScrollUp))
476 actions->append(QAccessibleActionInterface::scrollUpAction());
477 if (isSignalConnected(sigScrollDown))
478 actions->append(QAccessibleActionInterface::scrollDownAction());
479 if (isSignalConnected(sigScrollLeft))
480 actions->append(QAccessibleActionInterface::scrollLeftAction());
481 if (isSignalConnected(sigScrollRight))
482 actions->append(QAccessibleActionInterface::scrollRightAction());
483 if (isSignalConnected(sigPreviousPage))
484 actions->append(QAccessibleActionInterface::previousPageAction());
485 if (isSignalConnected(sigNextPage))
486 actions->append(QAccessibleActionInterface::nextPageAction());
487}
488
489QString QQuickAccessibleAttached::stripHtml(const QString &html)
490{
491#ifndef QT_NO_TEXTHTMLPARSER
492 QTextDocument doc;
493 doc.setHtml(html);
494 return doc.toPlainText();
495#else
496 return html;
497#endif
498}
499
501
502#include "moc_qquickaccessibleattached_p.cpp"
503
504#endif
The QAccessible class provides enums and static functions related to accessibility.
\inmodule QtCore
Definition qmetaobject.h:19
static QMetaMethod fromSignal(PointerToMemberFunction signal)
bool invoke(QObject *object, Qt::ConnectionType connectionType, QGenericReturnArgument returnValue, QGenericArgument val0=QGenericArgument(nullptr), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument()) const
\obsolete [6.5] Please use the variadic overload of this function
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\reentrant \inmodule QtGui
void setHtml(const QString &html)
Replaces the entire contents of the document with the given HTML-formatted text in the html string.
QString toPlainText() const
Returns the plain text contained in the document.
employee setName("Richard Schmit")
auto signal
Combined button and popup list for selecting options.
GLuint name
GLhandleARB obj
[2]
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_EMIT
#define emit
QGraphicsItem * item
\inmodule QtCore
int indexOfSlot(const char *slot) const
Finds slot and returns its index; otherwise returns -1.
static Connection connect(const QObject *sender, int signal_index, const QObject *receiver, int method_index, int type=0, int *types=nullptr)
Definition qobject.cpp:3556