Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qaccessiblecolorwell.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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// Qt-Security score:significant reason:default
4
5#include "private/qaccessiblecolorwell_p.h"
6
7#include <QtCore/qcoreapplication.h>
8
10
11#if QT_CONFIG(colordialog)
12
13#include "private/qcolorwell_p.h"
14
15QT_BEGIN_NAMESPACE
16class QAccessibleColorWellItem : public QAccessibleInterface
17{
18 QAccessibleColorWell *m_parent;
19
20 Q_DECLARE_TR_FUNCTIONS(QAccessibleColorWellItem)
21public:
22 QAccessibleColorWellItem(QAccessibleColorWell *parent);
23
24 virtual bool isValid() const override;
25 virtual QObject *object() const override;
26
27 virtual QAccessibleInterface *parent() const override;
28 virtual QAccessibleInterface *child(int index) const override;
29 virtual QAccessibleInterface *childAt(int x, int y) const override;
30 virtual int childCount() const override;
31 virtual int indexOfChild(const QAccessibleInterface *) const override;
32
33 virtual QString text(QAccessible::Text t) const override;
34 virtual void setText(QAccessible::Text t, const QString &text) override;
35 virtual QRect rect() const override;
36 virtual QAccessible::Role role() const override;
37 virtual QAccessible::State state() const override;
38};
39
40QAccessibleColorWellItem::QAccessibleColorWellItem(QAccessibleColorWell *parent)
41 : m_parent(parent)
42{
43}
44
45bool QAccessibleColorWellItem::isValid() const
46{
47 return m_parent->isValid();
48}
49
50QObject *QAccessibleColorWellItem::object() const
51{
52 return nullptr;
53}
54
55QAccessibleInterface *QAccessibleColorWellItem::parent() const
56{
57 return m_parent;
58}
59
60QAccessibleInterface *QAccessibleColorWellItem::child(int) const
61{
62 return nullptr;
63}
64
65QAccessibleInterface *QAccessibleColorWellItem::childAt(int, int) const
66{
67 return nullptr;
68}
69
70int QAccessibleColorWellItem::childCount() const
71{
72 return 0;
73}
74
75int QAccessibleColorWellItem::indexOfChild(const QAccessibleInterface *) const
76{
77 return -1;
78}
79
80QString QAccessibleColorWellItem::text(QAccessible::Text t) const
81{
82
83 if (t == QAccessible::Name) {
84 QRgb color = m_parent->colorWell()->rgbValues()[m_parent->indexOfChild(this)];
85 //: Color specified via its 3 RGB components (red, green, blue)
86 return tr("RGB %1, %2, %3")
87 .arg(QString::number(qRed(color)), QString::number(qGreen(color)),
88 QString::number(qBlue(color)));
89 }
90
91 return QString();
92}
93
94void QAccessibleColorWellItem::setText(QAccessible::Text, const QString &) { }
95
96QRect QAccessibleColorWellItem::rect() const
97{
98 const QColorWell *well = m_parent->colorWell();
99 const int childIndex = m_parent->indexOfChild(this);
100 // QColorWell layout is column-based, i.e. color indices 0 to QColorWell::numRows() - 1
101 // are in first column, see QColorWell::paintCellContents
102 const int col = childIndex / well->numRows();
103 const int row = childIndex % well->numRows();
104
105 return m_parent->colorWell()->cellGeometry(row, col).translated(m_parent->rect().topLeft());
106}
107
108QAccessible::Role QAccessibleColorWellItem::role() const
109{
110 return QAccessible::ListItem;
111}
112
113QAccessible::State QAccessibleColorWellItem::state() const
114{
115 QAccessible::State state;
116
117 state.invisible = m_parent->state().invisible;
118
119 state.focusable = true;
120 const int childIndex = m_parent->indexOfChild(this);
121 Q_ASSERT(childIndex >= 0);
122 QColorWell *well = m_parent->colorWell();
123 if (well->hasFocus() && well->index(well->currentRow(), well->currentColumn()) == childIndex)
124 state.focused = true;
125
126 state.selectable = true;
127 if (well->index(well->selectedRow(), well->selectedColumn() == childIndex))
128 state.selected = true;
129
130 return state;
131}
132
133QAccessibleColorWell::QAccessibleColorWell(QWidget *widget)
134 : QAccessibleWidgetV2(widget, QAccessible::List)
135{
136 m_childIds.resize(childCount());
137}
138
139QAccessibleColorWell::~QAccessibleColorWell()
140{
141 for (const std::optional<QAccessible::Id> &childId : m_childIds) {
142 if (childId.has_value())
143 QAccessible::deleteAccessibleInterface(childId.value());
144 }
145}
146
147QAccessibleInterface *QAccessibleColorWell::child(int index) const
148{
149 if (index < 0 || static_cast<size_t>(index) >= m_childIds.size())
150 return nullptr;
151
152 std::optional<QAccessible::Id> childId = m_childIds.at(index);
153 if (childId.has_value())
154 return QAccessible::accessibleInterface(childId.value());
155
156 QAccessibleInterface *child =
157 new QAccessibleColorWellItem(const_cast<QAccessibleColorWell *>(this));
158 const QAccessible::Id id = QAccessible::registerAccessibleInterface(child);
159 m_childIds[index] = id;
160 return child;
161}
162
163int QAccessibleColorWell::indexOfChild(const QAccessibleInterface *child) const
164{
165 auto it = std::find_if(m_childIds.cbegin(), m_childIds.cend(),
166 [child](const std::optional<QAccessible::Id> &id) {
167 return id.has_value()
168 && QAccessible::accessibleInterface(id.value()) == child;
169 });
170 if (it != m_childIds.cend())
171 return std::distance(m_childIds.cbegin(), it);
172
173 return -1;
174}
175
176int QAccessibleColorWell::childCount() const
177{
178 const QColorWell *well = colorWell();
179 return well->numCols() * well->numRows();
180}
181
182QColorWell *QAccessibleColorWell::colorWell() const
183{
184 return qobject_cast<QColorWell *>(object());
185}
186
187QT_END_NAMESPACE
188
189#endif // QT_CONFIG(colordialog)
QT_REQUIRE_CONFIG(accessibility)