From 2ae8aa38b3b89d55b1d8d133981e810110c2d656 Mon Sep 17 00:00:00 2001
From: Michael Weghorn <m.weghorn@posteo.de>
Date: Sat, 4 Nov 2023 22:19:46 +0100
Subject: [PATCH] DEMO: Adjust widgetgallery example to trigger a11y focus
 issue

Adjust the example:

* add a QTableView
* select an item in each of the 3 item views (QTableView, QTreeView,
  QListView) when toggling the "Simple Input Widgets" checkbox

When toggling the checkbox, the issue can be observed with Orca:
Instead of announcing the new (un)checked state of the textbox,
Orca announces one of the newly selected items as the new focus
item when the fix is not in place.

Change-Id: I5cad8e06f8292f6649a666a89333b964630d1652
---
 examples/widgets/gallery/widgetgallery.cpp | 26 ++++++++++++++++++++--
 examples/widgets/gallery/widgetgallery.h   | 11 ++++++++-
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/examples/widgets/gallery/widgetgallery.cpp b/examples/widgets/gallery/widgetgallery.cpp
index 2de46419d9..d40a6a7dab 100644
--- a/examples/widgets/gallery/widgetgallery.cpp
+++ b/examples/widgets/gallery/widgetgallery.cpp
@@ -30,6 +30,7 @@
 #include <QTextBrowser>
 #include <QTreeView>
 #include <QTableWidget>
+#include <QTableView>
 #include <QTextEdit>
 #include <QToolBox>
 #include <QToolButton>
@@ -155,6 +156,10 @@ WidgetGallery::WidgetGallery(QWidget *parent)
     connect(disableWidgetsCheckBox, &QCheckBox::toggled,
             simpleInputWidgetsGroupBox, &QWidget::setDisabled);
 
+    // DEMO: select an item in all of the item views when toggling the "Simple Input Widgets" checkbox
+    connect(simpleInputWidgetsGroupBox, &QGroupBox::toggled,
+            this, &WidgetGallery::selectItem);
+
     auto topLayout = new QHBoxLayout;
     topLayout->addWidget(styleLabel);
     topLayout->addWidget(styleComboBox);
@@ -308,11 +313,15 @@ QTabWidget *WidgetGallery::createItemViewTabWidget()
     auto result = createWidget<QTabWidget>("bottomLeftTabWidget");
     result->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
 
-    auto treeView = createWidget<QTreeView>("treeView");
+    treeView = createWidget<QTreeView>("treeView");
     auto fileSystemModel = new QFileSystemModel(treeView);
     fileSystemModel->setRootPath(QDir::rootPath());
     treeView->setModel(fileSystemModel);
 
+    tableView = createWidget<QTableView>("tableView");
+    auto tableModel = new QStandardItemModel(10, 10);
+    tableView->setModel(tableModel);
+
     auto tableWidget = createWidget<QTableWidget>("tableWidget");
     tableWidget->setRowCount(10);
     tableWidget->setColumnCount(10);
@@ -323,7 +332,7 @@ QTabWidget *WidgetGallery::createItemViewTabWidget()
     listModel->appendRow(new QStandardItem(QIcon(QLatin1String(":/qt-project.org/styles/commonstyle/images/computer-32.png")),
                                            tr("Computer")));
 
-    auto listView = createWidget<QListView>("listView");
+    listView = createWidget<QListView>("listView");
     listView->setModel(listModel);
 
     auto iconModeListView = createWidget<QListView>("iconModeListView");
@@ -331,6 +340,7 @@ QTabWidget *WidgetGallery::createItemViewTabWidget()
     iconModeListView->setModel(listModel);
 
     result->addTab(embedIntoHBoxLayout(treeView), tr("&Tree View"));
+    result->addTab(embedIntoHBoxLayout(tableView), tr("Table &View"));
     result->addTab(embedIntoHBoxLayout(tableWidget), tr("T&able"));
     result->addTab(embedIntoHBoxLayout(listView), tr("&List"));
     result->addTab(embedIntoHBoxLayout(iconModeListView), tr("&Icon Mode List"));
@@ -414,6 +424,18 @@ void WidgetGallery::updateSystemInfo()
     systemInfoTextBrowser->setHtml(systemInfo);
 }
 
+void WidgetGallery::selectItem()
+{
+    const QModelIndex listIndex = listView->model()->index(0, 0);
+    listView->setCurrentIndex(listIndex);
+
+    const QModelIndex treeIndex = treeView->model()->index(0, 0);
+    treeView->setCurrentIndex(treeIndex);
+
+    const QModelIndex tableIndex = tableView->model()->index(2, 2);
+    tableView->setCurrentIndex(tableIndex);
+}
+
 void WidgetGallery::helpOnCurrentWidget()
 {
     // Skip over internal widgets
diff --git a/examples/widgets/gallery/widgetgallery.h b/examples/widgets/gallery/widgetgallery.h
index c96437a466..2d8a40a4d4 100644
--- a/examples/widgets/gallery/widgetgallery.h
+++ b/examples/widgets/gallery/widgetgallery.h
@@ -8,9 +8,12 @@
 
 QT_BEGIN_NAMESPACE
 class QGroupBox;
+class QListView;
 class QProgressBar;
 class QTabWidget;
+class QTableView;
 class QTextBrowser;
+class QTreeView;
 class QToolBox;
 QT_END_NAMESPACE
 
@@ -28,16 +31,22 @@ private slots:
     void advanceProgressBar();
     void helpOnCurrentWidget();
     void updateSystemInfo();
+    void selectItem();
 
 private:
     static QGroupBox *createButtonsGroupBox();
-    static QTabWidget *createItemViewTabWidget();
+    QTabWidget *createItemViewTabWidget();
     static QGroupBox *createSimpleInputWidgetsGroupBox();
     QToolBox *createTextToolBox();
     QProgressBar *createProgressBar();
 
     QProgressBar *progressBar;
     QTextBrowser *systemInfoTextBrowser;
+
+    QListView *listView;
+    QTableView *tableView;
+    QTreeView *treeView;
+
 };
 
 #endif // WIDGETGALLERY_H
-- 
2.42.0

