Commit 9ffe17d3 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Implement the action interface for AccessibleLink

GnuPG-bug-id: 6034
parent 0ddc9744
......@@ -113,6 +113,33 @@ QAccessible::State AccessibleLink::state() const
return s;
}
void *AccessibleLink::interface_cast(QAccessible::InterfaceType t)
{
if (t == QAccessible::ActionInterface) {
return static_cast<QAccessibleActionInterface*>(this);
}
return nullptr;
}
QStringList AccessibleLink::actionNames() const
{
return {pressAction()};
}
void AccessibleLink::doAction(const QString &actionName)
{
if (actionName == pressAction()) {
if (auto ap = anchorProvider()) {
ap->activateAnchor(mIndex);
}
}
}
QStringList AccessibleLink::keyBindingsForAction(const QString &) const
{
return {};
}
int AccessibleLink::index() const
{
return mIndex;
......
......@@ -19,7 +19,7 @@ namespace Kleo
{
class AnchorProvider;
class AccessibleLink: public QAccessibleInterface
class AccessibleLink: public QAccessibleInterface, public QAccessibleActionInterface
{
public:
AccessibleLink(QWidget *label, int index);
......@@ -42,6 +42,13 @@ public:
QAccessible::Role role() const override;
QAccessible::State state() const override;
void *interface_cast(QAccessible::InterfaceType t) override;
// QAccessibleActionInterface
QStringList actionNames() const override;
void doAction(const QString &actionName) override;
QStringList keyBindingsForAction(const QString &actionName) const override;
int index() const;
private:
......
......@@ -23,6 +23,7 @@ public:
virtual int numberOfAnchors() const = 0;
virtual QString anchorText(int index) const = 0;
virtual QString anchorHref(int index) const = 0;
virtual void activateAnchor(int index) = 0;
virtual int selectedAnchor() const = 0;
};
......
......@@ -14,6 +14,7 @@
#include "utils/accessibility.h"
#include <QAccessible>
#include <QDesktopServices>
#include <QTextBlock>
#include <QTextCursor>
#include <QTextDocument>
......@@ -188,6 +189,32 @@ QString HtmlLabel::anchorHref(int index) const
return {};
}
void HtmlLabel::activateAnchor(int index)
{
// based on QWidgetTextControlPrivate::activateLinkUnderCursor
if (index < 0 || index >= numberOfAnchors()) {
return;
}
const auto &anchor = d->anchors()[index];
if (anchor.href.isEmpty()) {
return;
}
if (hasFocus()) {
// move cursor just before the anchor and clear the selection
setSelection(anchor.start, 0);
// focus the anchor
focusNextPrevChild(true);
} else {
// clear the selection moving the cursor just after the anchor
setSelection(anchor.end, 0);
}
if (openExternalLinks()) {
QDesktopServices::openUrl(QUrl{anchor.href});
} else {
Q_EMIT linkActivated(anchor.href);
}
}
int HtmlLabel::selectedAnchor() const
{
return d->anchorIndex(selectionStart());
......
......@@ -35,6 +35,7 @@ public:
int numberOfAnchors() const override;
QString anchorText(int index) const override;
QString anchorHref(int index) const override;
void activateAnchor(int index) override;
int selectedAnchor() const override;
protected:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment