Commit 3dc48f11 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Reuse HtmlLabel for UrlLabel

Apart from reducing the amout of duplicated code, this makes the link
displayed by the UrlLabel accessible.

GnuPG-bug-id: 6034
parent dac69031
......@@ -2,7 +2,7 @@
view/urllabel.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileCopyrightText: 2021, 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
......@@ -12,73 +12,33 @@
#include "urllabel.h"
#include <QAccessible>
#include <QPalette>
#include <QUrl>
using namespace Kleo;
class UrlLabel::Private
{
UrlLabel *q;
public:
Private(UrlLabel *q) : q{q} {}
void ensureLinkColorIsValid();
void updateLabel();
QUrl url;
QString text;
QColor linkColor;
};
void UrlLabel::Private::ensureLinkColorIsValid()
UrlLabel::UrlLabel(QWidget *parent)
: HtmlLabel{parent}
{
if (!linkColor.isValid()) {
linkColor = q->palette().link().color();
}
}
void UrlLabel::Private::updateLabel()
UrlLabel::~UrlLabel() = default;
void UrlLabel::setUrl(const QUrl &url, const QString &text)
{
// we prepend a zero-width-space character to work around a bug in QLabel::focusNextPrevChild(false)
// which makes it impossible to leave the label with Shift+Tab if the text starts with a link
static const QString templateString{QLatin1String{"&#8203;<a style=\"color: %1;\" href=\"%2\">%3</a>"}};
static const QString templateString{QLatin1String{"&#8203;<a href=\"%1\">%2</a>"}};
if (url.isEmpty()) {
q->clear();
HtmlLabel::setHtml({});
return;
}
ensureLinkColorIsValid();
q->setText(templateString.arg(
linkColor.name(),
setHtml(templateString.arg(
url.url(QUrl::FullyEncoded),
text.isEmpty() ? url.toDisplayString().toHtmlEscaped() : text.toHtmlEscaped()));
}
UrlLabel::UrlLabel(QWidget *parent)
: QLabel{parent}
, d{new Private{this}}
{
setTextFormat(Qt::RichText);
setTextInteractionFlags(Qt::TextBrowserInteraction);
}
UrlLabel::~UrlLabel() = default;
void UrlLabel::setUrl(const QUrl &url, const QString &text)
{
d->url = url;
d->text = text;
d->updateLabel();
}
void UrlLabel::setLinkColor(const QColor &color)
{
d->linkColor = color;
d->updateLabel();
}
void UrlLabel::focusInEvent(QFocusEvent *event)
{
// immediately focus the URL when the label get focus
......@@ -87,13 +47,3 @@ void UrlLabel::focusInEvent(QFocusEvent *event)
focusNextPrevChild(true);
}
}
bool UrlLabel::focusNextPrevChild(bool next)
{
const bool result = QLabel::focusNextPrevChild(next);
if (hasFocus() && hasSelectedText()) {
QAccessibleTextSelectionEvent ev(this, selectionStart(), selectionStart() + selectedText().size());
QAccessible::updateAccessibility(&ev);
}
return result;
}
......@@ -2,7 +2,7 @@
view/urllabel.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileCopyrightText: 2021, 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
......@@ -10,14 +10,14 @@
#pragma once
#include <QLabel>
#include "htmllabel.h"
#include <memory>
namespace Kleo
{
class UrlLabel : public QLabel
class UrlLabel : public HtmlLabel
{
Q_OBJECT
public:
......@@ -26,18 +26,11 @@ public:
void setUrl(const QUrl &url, const QString &text = {});
void setLinkColor(const QColor &color);
protected:
void focusInEvent(QFocusEvent *event) override;
bool focusNextPrevChild(bool next) override;
private:
using QLabel::setText;
private:
class Private;
std::unique_ptr<Private> d;
using HtmlLabel::setHtml;
};
}
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