1 /*
2  *  Copyright 2012 Frederik Gladhorn <gladhorn@kde.org>
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Lesser General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2.1 of the License, or (at your option) version 3, or any
8  *  later version accepted by the membership of KDE e.V. (or its
9  *  successor approved by the membership of KDE e.V.), which shall
10  *  act as a proxy defined in Section 6 of version 3 of the license.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef ACCESSIBLETREE_H
22 #define ACCESSIBLETREE_H
23 
24 #include <qabstractitemmodel.h>
25 
26 #include <qaccessibilityclient/registry.h>
27 
28 class AccessibleTree;
29 
30 class AccessibleWrapper
31 {
32 public:
AccessibleWrapper(const QAccessibleClient::AccessibleObject & object,AccessibleWrapper * parent)33     AccessibleWrapper(const QAccessibleClient::AccessibleObject &object, AccessibleWrapper *parent)
34     : acc(object), m_parent(parent)
35     {}
36 
37     QAccessibleClient::AccessibleObject acc;
38 
~AccessibleWrapper()39     ~AccessibleWrapper() {
40         qDeleteAll(m_children);
41     }
42 
43     int childCount();
44     AccessibleWrapper *child(int index);
45     AccessibleWrapper *parent();
46 
47 private:
48     friend class AccessibleTree;
49 
50     AccessibleWrapper *m_parent;
51     QList<AccessibleWrapper*> m_children;
52 };
53 
54 class AccessibleTree :public QAbstractItemModel
55 {
56     Q_OBJECT
57 public:
58     explicit AccessibleTree(QObject* parent = nullptr);
59     ~AccessibleTree() override;
60 
61     void setRegistry(QAccessibleClient::Registry *registry);
62 
63     QModelIndex indexForAccessible(const QAccessibleClient::AccessibleObject &object);
64     bool addAccessible(const QAccessibleClient::AccessibleObject &object);
65     bool removeAccessible(const QAccessibleClient::AccessibleObject &object);
66     bool removeAccessible(const QModelIndex &index);
67     void updateAccessible(const QAccessibleClient::AccessibleObject &object);
68 
apps()69     QList<AccessibleWrapper*> apps() const { return m_apps; }
70 
71     int columnCount(const QModelIndex& parent = QModelIndex()) const override;
72     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
73     QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
74     QModelIndex parent(const QModelIndex& child) const override;
75     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
76     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
77 
78 public Q_SLOTS:
79     void resetModel();
80     /*!
81         \brief Updates all applications in the tree.
82 
83         Removes and adds top level applications. This is less invasive then modelReset.
84     */
85     void updateTopLevelApps();
86 
87 Q_SIGNALS:
88     void navigationError(const QModelIndex &) const;
89 
90 private:
91     QAccessibleClient::Registry *m_registry;
92     QList<AccessibleWrapper*> m_apps;
93 };
94 
95 #endif // ACCESSIBLETREE_H
96