1 | /* $Id: QITableWidget.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QITableWidget class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QAccessibleWidget>
|
---|
30 | #include <QPainter>
|
---|
31 | #include <QResizeEvent>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "QITableWidget.h"
|
---|
35 |
|
---|
36 | /* Other VBox includes: */
|
---|
37 | #include "iprt/assert.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /** QAccessibleObject extension used as an accessibility interface for QITableWidgetItem. */
|
---|
41 | class QIAccessibilityInterfaceForQITableWidgetItem : public QAccessibleObject
|
---|
42 | {
|
---|
43 | public:
|
---|
44 |
|
---|
45 | /** Returns an accessibility interface for passed @a strClassname and @a pObject. */
|
---|
46 | static QAccessibleInterface *pFactory(const QString &strClassname, QObject *pObject)
|
---|
47 | {
|
---|
48 | /* Creating QITableWidgetItem accessibility interface: */
|
---|
49 | if (pObject && strClassname == QLatin1String("QITableWidgetItem"))
|
---|
50 | return new QIAccessibilityInterfaceForQITableWidgetItem(pObject);
|
---|
51 |
|
---|
52 | /* Null by default: */
|
---|
53 | return 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** Constructs an accessibility interface passing @a pObject to the base-class. */
|
---|
57 | QIAccessibilityInterfaceForQITableWidgetItem(QObject *pObject)
|
---|
58 | : QAccessibleObject(pObject)
|
---|
59 | {}
|
---|
60 |
|
---|
61 | /** Returns the parent. */
|
---|
62 | virtual QAccessibleInterface *parent() const RT_OVERRIDE;
|
---|
63 |
|
---|
64 | /** Returns the number of children. */
|
---|
65 | virtual int childCount() const RT_OVERRIDE { return 0; }
|
---|
66 | /** Returns the child with the passed @a iIndex. */
|
---|
67 | virtual QAccessibleInterface *child(int iIndex) const RT_OVERRIDE { Q_UNUSED(iIndex); return 0; }
|
---|
68 | /** Returns the index of the passed @a pChild. */
|
---|
69 | virtual int indexOfChild(const QAccessibleInterface *pChild) const RT_OVERRIDE { Q_UNUSED(pChild); return -1; }
|
---|
70 |
|
---|
71 | /** Returns the rect. */
|
---|
72 | virtual QRect rect() const RT_OVERRIDE;
|
---|
73 | /** Returns a text for the passed @a enmTextRole. */
|
---|
74 | virtual QString text(QAccessible::Text enmTextRole) const RT_OVERRIDE;
|
---|
75 |
|
---|
76 | /** Returns the role. */
|
---|
77 | virtual QAccessible::Role role() const RT_OVERRIDE;
|
---|
78 | /** Returns the state. */
|
---|
79 | virtual QAccessible::State state() const RT_OVERRIDE;
|
---|
80 |
|
---|
81 | private:
|
---|
82 |
|
---|
83 | /** Returns corresponding QITableWidgetItem. */
|
---|
84 | QITableWidgetItem *item() const { return qobject_cast<QITableWidgetItem*>(object()); }
|
---|
85 | };
|
---|
86 |
|
---|
87 |
|
---|
88 | /** QAccessibleWidget extension used as an accessibility interface for QITableWidget. */
|
---|
89 | class QIAccessibilityInterfaceForQITableWidget : public QAccessibleWidget
|
---|
90 | {
|
---|
91 | public:
|
---|
92 |
|
---|
93 | /** Returns an accessibility interface for passed @a strClassname and @a pObject. */
|
---|
94 | static QAccessibleInterface *pFactory(const QString &strClassname, QObject *pObject)
|
---|
95 | {
|
---|
96 | /* Creating QITableWidget accessibility interface: */
|
---|
97 | if (pObject && strClassname == QLatin1String("QITableWidget"))
|
---|
98 | return new QIAccessibilityInterfaceForQITableWidget(qobject_cast<QWidget*>(pObject));
|
---|
99 |
|
---|
100 | /* Null by default: */
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Constructs an accessibility interface passing @a pWidget to the base-class. */
|
---|
105 | QIAccessibilityInterfaceForQITableWidget(QWidget *pWidget)
|
---|
106 | : QAccessibleWidget(pWidget, QAccessible::List)
|
---|
107 | {}
|
---|
108 |
|
---|
109 | /** Returns the number of children. */
|
---|
110 | virtual int childCount() const RT_OVERRIDE;
|
---|
111 | /** Returns the child with the passed @a iIndex. */
|
---|
112 | virtual QAccessibleInterface *child(int iIndex) const RT_OVERRIDE;
|
---|
113 | /** Returns the index of the passed @a pChild. */
|
---|
114 | virtual int indexOfChild(const QAccessibleInterface *pChild) const RT_OVERRIDE;
|
---|
115 |
|
---|
116 | /** Returns a text for the passed @a enmTextRole. */
|
---|
117 | virtual QString text(QAccessible::Text enmTextRole) const RT_OVERRIDE;
|
---|
118 |
|
---|
119 | private:
|
---|
120 |
|
---|
121 | /** Returns corresponding QITableWidget. */
|
---|
122 | QITableWidget *table() const { return qobject_cast<QITableWidget*>(widget()); }
|
---|
123 | };
|
---|
124 |
|
---|
125 |
|
---|
126 | /*********************************************************************************************************************************
|
---|
127 | * Class QIAccessibilityInterfaceForQITableWidgetItem implementation. *
|
---|
128 | *********************************************************************************************************************************/
|
---|
129 |
|
---|
130 | QAccessibleInterface *QIAccessibilityInterfaceForQITableWidgetItem::parent() const
|
---|
131 | {
|
---|
132 | /* Make sure item still alive: */
|
---|
133 | AssertPtrReturn(item(), 0);
|
---|
134 |
|
---|
135 | /* Return the parent: */
|
---|
136 | return QAccessible::queryAccessibleInterface(item()->parentTable());
|
---|
137 | }
|
---|
138 |
|
---|
139 | QRect QIAccessibilityInterfaceForQITableWidgetItem::rect() const
|
---|
140 | {
|
---|
141 | /* Make sure item still alive: */
|
---|
142 | AssertPtrReturn(item(), QRect());
|
---|
143 |
|
---|
144 | /* Compose common region: */
|
---|
145 | QRegion region;
|
---|
146 |
|
---|
147 | /* Append item rectangle: */
|
---|
148 | const QRect itemRectInViewport = item()->parentTable()->visualItemRect(item());
|
---|
149 | const QSize itemSize = itemRectInViewport.size();
|
---|
150 | const QPoint itemPosInViewport = itemRectInViewport.topLeft();
|
---|
151 | const QPoint itemPosInScreen = item()->parentTable()->viewport()->mapToGlobal(itemPosInViewport);
|
---|
152 | const QRect itemRectInScreen = QRect(itemPosInScreen, itemSize);
|
---|
153 | region += itemRectInScreen;
|
---|
154 |
|
---|
155 | /* Return common region bounding rectangle: */
|
---|
156 | return region.boundingRect();
|
---|
157 | }
|
---|
158 |
|
---|
159 | QString QIAccessibilityInterfaceForQITableWidgetItem::text(QAccessible::Text enmTextRole) const
|
---|
160 | {
|
---|
161 | /* Make sure item still alive: */
|
---|
162 | AssertPtrReturn(item(), QString());
|
---|
163 |
|
---|
164 | /* Return a text for the passed enmTextRole: */
|
---|
165 | switch (enmTextRole)
|
---|
166 | {
|
---|
167 | case QAccessible::Name: return item()->text();
|
---|
168 | default: break;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* Null-string by default: */
|
---|
172 | return QString();
|
---|
173 | }
|
---|
174 |
|
---|
175 | QAccessible::Role QIAccessibilityInterfaceForQITableWidgetItem::role() const
|
---|
176 | {
|
---|
177 | return QAccessible::ListItem;
|
---|
178 | }
|
---|
179 |
|
---|
180 | QAccessible::State QIAccessibilityInterfaceForQITableWidgetItem::state() const
|
---|
181 | {
|
---|
182 | /* Make sure item still alive: */
|
---|
183 | AssertPtrReturn(item(), QAccessible::State());
|
---|
184 |
|
---|
185 | /* Compose the state: */
|
---|
186 | QAccessible::State state;
|
---|
187 | state.focusable = true;
|
---|
188 | state.selectable = true;
|
---|
189 |
|
---|
190 | /* Compose the state of current item: */
|
---|
191 | if ( item()
|
---|
192 | && item() == QITableWidgetItem::toItem(item()->tableWidget()->currentItem()))
|
---|
193 | {
|
---|
194 | state.active = true;
|
---|
195 | state.focused = true;
|
---|
196 | state.selected = true;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* Compose the state of checked item: */
|
---|
200 | if ( item()
|
---|
201 | && item()->checkState() != Qt::Unchecked)
|
---|
202 | {
|
---|
203 | state.checked = true;
|
---|
204 | if (item()->checkState() == Qt::PartiallyChecked)
|
---|
205 | state.checkStateMixed = true;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /* Return the state: */
|
---|
209 | return state;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /*********************************************************************************************************************************
|
---|
214 | * Class QIAccessibilityInterfaceForQITableWidget implementation. *
|
---|
215 | *********************************************************************************************************************************/
|
---|
216 |
|
---|
217 | int QIAccessibilityInterfaceForQITableWidget::childCount() const
|
---|
218 | {
|
---|
219 | /* Make sure table still alive: */
|
---|
220 | AssertPtrReturn(table(), 0);
|
---|
221 |
|
---|
222 | /* Return the number of children: */
|
---|
223 | return table()->rowCount() * table()->columnCount();
|
---|
224 | }
|
---|
225 |
|
---|
226 | QAccessibleInterface *QIAccessibilityInterfaceForQITableWidget::child(int iIndex) const
|
---|
227 | {
|
---|
228 | /* Make sure table still alive: */
|
---|
229 | AssertPtrReturn(table(), 0);
|
---|
230 | /* Make sure index is valid: */
|
---|
231 | AssertReturn(iIndex >= 0 && iIndex < childCount(), 0);
|
---|
232 |
|
---|
233 | /* Return the child with the passed iIndex: */
|
---|
234 | const int iRow = iIndex / table()->columnCount();
|
---|
235 | const int iColumn = iIndex % table()->columnCount();
|
---|
236 | return QAccessible::queryAccessibleInterface(table()->childItem(iRow, iColumn));
|
---|
237 | }
|
---|
238 |
|
---|
239 | int QIAccessibilityInterfaceForQITableWidget::indexOfChild(const QAccessibleInterface *pChild) const
|
---|
240 | {
|
---|
241 | /* Search for corresponding child: */
|
---|
242 | for (int iIndex = 0; iIndex < childCount(); ++iIndex)
|
---|
243 | if (child(iIndex) == pChild)
|
---|
244 | return iIndex;
|
---|
245 |
|
---|
246 | /* -1 by default: */
|
---|
247 | return -1;
|
---|
248 | }
|
---|
249 |
|
---|
250 | QString QIAccessibilityInterfaceForQITableWidget::text(QAccessible::Text /* enmTextRole */) const
|
---|
251 | {
|
---|
252 | /* Make sure table still alive: */
|
---|
253 | AssertPtrReturn(table(), QString());
|
---|
254 |
|
---|
255 | /* Gather suitable text: */
|
---|
256 | QString strText = table()->toolTip();
|
---|
257 | if (strText.isEmpty())
|
---|
258 | strText = table()->whatsThis();
|
---|
259 | return strText;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /*********************************************************************************************************************************
|
---|
264 | * Class QITableWidgetItem implementation. *
|
---|
265 | *********************************************************************************************************************************/
|
---|
266 |
|
---|
267 | /* static */
|
---|
268 | QITableWidgetItem *QITableWidgetItem::toItem(QTableWidgetItem *pItem)
|
---|
269 | {
|
---|
270 | /* Make sure alive QITableWidgetItem passed: */
|
---|
271 | if (!pItem || pItem->type() != ItemType)
|
---|
272 | return 0;
|
---|
273 |
|
---|
274 | /* Return casted QITableWidgetItem: */
|
---|
275 | return static_cast<QITableWidgetItem*>(pItem);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /* static */
|
---|
279 | const QITableWidgetItem *QITableWidgetItem::toItem(const QTableWidgetItem *pItem)
|
---|
280 | {
|
---|
281 | /* Make sure alive QITableWidgetItem passed: */
|
---|
282 | if (!pItem || pItem->type() != ItemType)
|
---|
283 | return 0;
|
---|
284 |
|
---|
285 | /* Return casted QITableWidgetItem: */
|
---|
286 | return static_cast<const QITableWidgetItem*>(pItem);
|
---|
287 | }
|
---|
288 |
|
---|
289 | QITableWidgetItem::QITableWidgetItem(const QString &strText /* = QString() */)
|
---|
290 | : QTableWidgetItem(strText, ItemType)
|
---|
291 | {
|
---|
292 | }
|
---|
293 |
|
---|
294 | QITableWidget *QITableWidgetItem::parentTable() const
|
---|
295 | {
|
---|
296 | return tableWidget() ? qobject_cast<QITableWidget*>(tableWidget()) : 0;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | /*********************************************************************************************************************************
|
---|
301 | * Class QITableWidget implementation. *
|
---|
302 | *********************************************************************************************************************************/
|
---|
303 |
|
---|
304 | QITableWidget::QITableWidget(QWidget *pParent)
|
---|
305 | : QTableWidget(pParent)
|
---|
306 | {
|
---|
307 | /* Install QITableWidget accessibility interface factory: */
|
---|
308 | QAccessible::installFactory(QIAccessibilityInterfaceForQITableWidget::pFactory);
|
---|
309 | /* Install QITableWidgetItem accessibility interface factory: */
|
---|
310 | QAccessible::installFactory(QIAccessibilityInterfaceForQITableWidgetItem::pFactory);
|
---|
311 |
|
---|
312 | // WORKAROUND:
|
---|
313 | // Ok, what do we have here..
|
---|
314 | // There is a bug in QAccessible framework which might be just treated like
|
---|
315 | // a functionality flaw. It consist in fact that if an accessibility client
|
---|
316 | // is enabled, base-class can request an accessibility interface in own
|
---|
317 | // constructor before the sub-class registers own factory, so we have to
|
---|
318 | // recreate interface after we finished with our own initialization.
|
---|
319 | QAccessibleInterface *pInterface = QAccessible::queryAccessibleInterface(this);
|
---|
320 | if (pInterface)
|
---|
321 | {
|
---|
322 | QAccessible::deleteAccessibleInterface(QAccessible::uniqueId(pInterface));
|
---|
323 | QAccessible::queryAccessibleInterface(this); // <= new one, proper..
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | QITableWidgetItem *QITableWidget::childItem(int iRow, int iColumn) const
|
---|
328 | {
|
---|
329 | return item(iRow, iColumn) ? QITableWidgetItem::toItem(item(iRow, iColumn)) : 0;
|
---|
330 | }
|
---|
331 |
|
---|
332 | QModelIndex QITableWidget::itemIndex(QTableWidgetItem *pItem)
|
---|
333 | {
|
---|
334 | return indexFromItem(pItem);
|
---|
335 | }
|
---|
336 |
|
---|
337 | void QITableWidget::paintEvent(QPaintEvent *pEvent)
|
---|
338 | {
|
---|
339 | /* Create item painter: */
|
---|
340 | QPainter painter;
|
---|
341 | painter.begin(viewport());
|
---|
342 |
|
---|
343 | /* Notify listeners about painting: */
|
---|
344 | for (int iRow = 0; iRow < rowCount(); ++iRow)
|
---|
345 | for (int iColumn = 0; iColumn < rowCount(); ++iColumn)
|
---|
346 | emit painted(item(iRow, iColumn), &painter);
|
---|
347 |
|
---|
348 | /* Close item painter: */
|
---|
349 | painter.end();
|
---|
350 |
|
---|
351 | /* Call to base-class: */
|
---|
352 | QTableWidget::paintEvent(pEvent);
|
---|
353 | }
|
---|
354 |
|
---|
355 | void QITableWidget::resizeEvent(QResizeEvent *pEvent)
|
---|
356 | {
|
---|
357 | /* Call to base-class: */
|
---|
358 | QTableWidget::resizeEvent(pEvent);
|
---|
359 |
|
---|
360 | /* Notify listeners about resizing: */
|
---|
361 | emit resized(pEvent->size(), pEvent->oldSize());
|
---|
362 | }
|
---|