1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "accessibilityinspector.h"
30 
31 #include "screenreader.h"
32 #include "optionswidget.h"
33 #include "accessibilityscenemanager.h"
34 
35 #include <QtDeclarative/QtDeclarative>
36 
mousePressEvent(QGraphicsSceneMouseEvent * event)37 void MouseInterceptingGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
38 {
39     emit mousePressed(event->scenePos().toPoint());
40     QGraphicsScene::mousePressEvent(event);
41 }
42 
mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)43 void MouseInterceptingGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
44 {
45     emit mouseDobleClicked();
46     QGraphicsScene::mouseDoubleClickEvent(event);
47 }
48 
49 AccessibilitySceneManager *sceneManager = 0;
50 QAccessible::UpdateHandler previousUpdateHandler = 0;
51 bool updateHandlerRecursion = false;
accessibilityUpdateHandler(QAccessibleEvent * event)52 void accessibilityUpdateHandler(QAccessibleEvent *event)
53 {
54     if (updateHandlerRecursion)
55         return;
56 
57     updateHandlerRecursion = true;
58 
59     if (sceneManager) {
60         sceneManager->handleUpdate(event);
61 
62         //qDebug() << "update";
63     }
64 
65     if (previousUpdateHandler) // call prev just to be sure.
66         previousUpdateHandler(event);
67 
68     updateHandlerRecursion = false;
69 }
70 
AccessibilityInspector(QObject * parent)71 AccessibilityInspector::AccessibilityInspector(QObject *parent) :
72     QObject(parent)
73 {
74 }
75 
~AccessibilityInspector()76 AccessibilityInspector::~AccessibilityInspector()
77 {
78     delete optionsWidget;
79     delete accessibilityScene;
80     delete accessibilityView;
81     delete accessibilityTreeScene;
82     delete accessibilityTreeView;
83     delete screenReader;
84 }
85 
inspectWindow(QWindow * window)86 void AccessibilityInspector::inspectWindow(QWindow *window)
87 {
88     qDebug() << "AccessibilityInspector::inspectWindow()" << window;
89     if (window->parent() || window->transientParent())
90         return;
91 
92     optionsWidget = new OptionsWidget();
93 
94     accessibilityScene = new MouseInterceptingGraphicsScene();
95 
96     accessibilityView = new QGraphicsView();
97     accessibilityView->setScene(accessibilityScene);
98     accessibilityView->resize(640, 480);
99     accessibilityView->scale(1.3, 1.3);
100 
101     accessibilityTreeScene = new QGraphicsScene();
102 
103     accessibilityTreeView = new QGraphicsView();
104     accessibilityTreeView->setScene(accessibilityTreeScene);
105     accessibilityTreeView->resize(640, 480);
106 
107     sceneManager = new AccessibilitySceneManager();
108     QObject::connect(optionsWidget, SIGNAL(optionsChanged()), sceneManager, SLOT(updateAccessibilitySceneItemFlags()));
109     QObject::connect(optionsWidget, SIGNAL(refreshClicked()), sceneManager, SLOT(populateAccessibilityScene()));
110     QObject::connect(optionsWidget, SIGNAL(refreshClicked()), sceneManager, SLOT(populateAccessibilityTreeScene()));
111     QObject::connect(optionsWidget, SIGNAL(scaleChanged(int)), sceneManager, SLOT(changeScale(int)));
112 
113     sceneManager->setOptionsWidget(optionsWidget);
114     sceneManager->setRootWindow(window);
115     sceneManager->setScene(accessibilityScene);
116     sceneManager->setView(accessibilityView);
117     sceneManager->setTreeScene(accessibilityTreeScene);
118     sceneManager->setTreeView(accessibilityTreeView);
119 
120     screenReader = new ScreenReader;
121     QObject::connect(accessibilityScene, SIGNAL(mousePressed(QPoint)), screenReader, SLOT(touchPoint(QPoint)));
122     QObject::connect(accessibilityScene, SIGNAL(mouseDobleClicked()), screenReader, SLOT(activate()));
123     QObject::connect(screenReader, SIGNAL(selected(QObject*)), sceneManager, SLOT(setSelected(QObject*)));
124     screenReader->setRootObject(window);
125     screenReader->setOptionsWidget(optionsWidget);
126 
127     previousUpdateHandler = QAccessible::installUpdateHandler(accessibilityUpdateHandler);
128 
129     QTimer::singleShot(100, sceneManager, SLOT(populateAccessibilityScene()));
130     QTimer::singleShot(100, sceneManager, SLOT(populateAccessibilityTreeScene()));
131 
132     QSettings settings;
133     accessibilityView->restoreGeometry(settings.value("accessiblityGeometry").toByteArray());
134     accessibilityView->setObjectName(QLatin1String("accessibilityInspectorView"));
135     accessibilityView->show();
136 
137 
138     accessibilityTreeView->restoreGeometry(settings.value("treeGeometry").toByteArray());
139     accessibilityTreeView->setObjectName(QLatin1String("accessibilityInspectorTreeView"));
140     accessibilityTreeView->show();
141     optionsWidget->restoreGeometry(settings.value("optionsGeometry").toByteArray());
142     optionsWidget->setObjectName(QLatin1String("accessibilityInspectorOptions"));
143     optionsWidget->show();
144 }
145 
saveWindowGeometry()146 void AccessibilityInspector::saveWindowGeometry()
147 {
148     QSettings settings;
149     settings.setValue("accessiblityGeometry", accessibilityView->saveGeometry());
150     settings.setValue("treeGeometry", accessibilityTreeView->saveGeometry());
151     settings.setValue("optionsGeometry", optionsWidget->saveGeometry());
152 }
153 
translateRole(QAccessible::Role role)154 QString translateRole(QAccessible::Role role)
155 {
156     return qAccessibleRoleString(role);
157 }
158 
159