diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 844d39c..a058551 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(accessibleapps) +add_subdirectory(dumper) diff --git a/examples/dumper/CMakeLists.txt b/examples/dumper/CMakeLists.txt new file mode 100644 index 0000000..47f2803 --- /dev/null +++ b/examples/dumper/CMakeLists.txt @@ -0,0 +1,32 @@ +project(dumper) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++0x") +add_definitions(${Qt5Core_EXECUTABLE_COMPILE_FLAGS}) + +include_directories( + ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/src +) + +#To find the installed library: +#set(LibKdeAccessibilityClient_DIR /usr/local/lib/cmake/) +#find_package(LibKdeAccessibilityClient REQUIRED) + +#Instead we just use the one in the build directory since that's what we want to test. + +set(accessibleapps_SRCS + main.cpp + dumper.cpp +) + +set(accessibleapps_UIS +) + +qt5_wrap_ui(accessibleapps_UI_HDRS ${accessibleapps_UIS}) + +add_executable(dumper ${accessibleapps_SRCS} ${accessibleapps_MOC} ${accessibleapps_UI_HDRS}) + +target_link_libraries(dumper ${Qt5Widgets_LIBRARIES} ${Qt5Test_LIBRARIES} ${QACCESSIBILITYCLIENT_LIB_NAME}) + +install(TARGETS dumper DESTINATION bin) diff --git a/examples/dumper/dumper.cpp b/examples/dumper/dumper.cpp new file mode 100644 index 0000000..8293787 --- /dev/null +++ b/examples/dumper/dumper.cpp @@ -0,0 +1,71 @@ +/* + Copyright 2018 Frederik Gladhorn + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include +#include + +#include "dumper.h" + +using namespace QAccessibleClient; + +QTextStream out(stdout); + +Dumper::Dumper(QObject *parent) + : QObject(parent) +{ +} + +void Dumper::run(const QString &appname) const { + Registry r; + const auto apps = r.applications(); + for (const AccessibleObject &app : apps) { + if (appname.isEmpty() || app.name().contains(appname)) { + printChild(app); + } + } +} + +void Dumper::printChild(const AccessibleObject &object, int indent) const +{ + auto spaces = QStringLiteral(" "); + if (!object.isValid()) { + out << spaces.repeated(indent) << "INVALID CHILD" << endl; + return; + } + + auto name = object.name().isEmpty() ? QStringLiteral("[unnamed]") : object.name(); + QString info = QString("%1 [%2 - %3] '%4'").arg(name, QString::number(object.role()), object.roleName(), object.description()); + if (m_showStates) { + info += QString(" [%1]").arg(object.stateString()); + } + out << spaces.repeated(indent) << info << endl; + int childCount = object.childCount(); + for (int i = 0; i < childCount; ++i) { + AccessibleObject child = object.child(i); + // simple test if the parent is consistent + if (child.parent() != object) { + out << spaces.repeated(indent + 4) << "WARNING: inconsistent parent/child hierarchy"; + } + if (child.indexInParent() != i) { + out << spaces.repeated(indent + 4) << QString::fromLatin1("WARNING: child index inconsistent: child claims to be child %1 parent thinks it is %2").arg(child.indexInParent(), i); + } + printChild(child, indent + 1); + } +} diff --git a/examples/dumper/dumper.h b/examples/dumper/dumper.h new file mode 100644 index 0000000..2f50279 --- /dev/null +++ b/examples/dumper/dumper.h @@ -0,0 +1,40 @@ +/* + Copyright 2018 Frederik Gladhorn + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include "qaccessibilityclient/accessibleobject.h" +#include "qaccessibilityclient/registry.h" + +#ifndef DUMPER_H +#define DUMPER_H + +class Dumper : public QObject +{ + Q_OBJECT +public: + Dumper(QObject *parent = nullptr); + void run(const QString &appname) const; + void printChild(const QAccessibleClient::AccessibleObject &object, int indent = 0) const; + void showStates(bool show) { m_showStates = show; } + +private: + bool m_showStates = false; +}; + +#endif diff --git a/examples/dumper/main.cpp b/examples/dumper/main.cpp new file mode 100644 index 0000000..163ce28 --- /dev/null +++ b/examples/dumper/main.cpp @@ -0,0 +1,54 @@ +/* + Copyright 2018 Frederik Gladhorn + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include +#include +#include + +#include "dumper.h" + +int main(int argc, char** argv) +{ + QCoreApplication app(argc, argv); + app.setApplicationName(QStringLiteral("Accessibility Tree dumper")); + + QCommandLineParser p; + p.addPositionalArgument(QStringLiteral("appname"), QStringLiteral("Application name")); + QCommandLineOption states(QStringLiteral("states")); + p.addOption(states); + + if (!p.parse(app.arguments())) { + QTextStream out(stdout); + out << QStringLiteral("Could not parse command line arguments."); + out << p.helpText(); + exit(1); + } + + Dumper d; + if (p.isSet(states)) { + d.showStates(true); + } + + if (p.positionalArguments().size() == 1) { + d.run(p.positionalArguments().at(0)); + } else { + d.run(QString()); + } +}