diff --git a/src/view/urllabel.cpp b/src/view/urllabel.cpp index 877dd4819..91bdc9d00 100644 --- a/src/view/urllabel.cpp +++ b/src/view/urllabel.cpp @@ -1,57 +1,98 @@ /* view/urllabel.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "urllabel.h" #include +#include using namespace Kleo; -UrlLabel::UrlLabel(QWidget *parent) - : QLabel{parent} +class UrlLabel::Private { - setTextFormat(Qt::RichText); - setTextInteractionFlags(Qt::TextBrowserInteraction); -} + UrlLabel *q; +public: + Private(UrlLabel *q) : q{q} {} -void UrlLabel::setUrl(const QUrl &url, const QString &text) + void ensureLinkColorIsValid(); + void updateLabel(); + + QUrl url; + QString text; + QColor linkColor; +}; + +void UrlLabel::Private::ensureLinkColorIsValid() +{ + if (!linkColor.isValid()) { + linkColor = q->palette().link().color(); + } +} +void UrlLabel::Private::updateLabel() { // 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{"​%2"}}; + static const QString templateString{QLatin1String{"​%3"}}; if (url.isEmpty()) { - clear(); + q->clear(); return; } - setText(templateString.arg(url.url(QUrl::FullyEncoded), text.isEmpty() ? url.toDisplayString().toHtmlEscaped() : text.toHtmlEscaped())); + ensureLinkColorIsValid(); + q->setText(templateString.arg( + linkColor.name(), + 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); +} + +Kleo::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 QLabel::focusInEvent(event); if (!hasSelectedText()) { 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; } diff --git a/src/view/urllabel.h b/src/view/urllabel.h index 3ffeb4124..2b0f188d8 100644 --- a/src/view/urllabel.h +++ b/src/view/urllabel.h @@ -1,34 +1,43 @@ /* view/urllabel.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include +#include + namespace Kleo { class UrlLabel : public QLabel { Q_OBJECT public: explicit UrlLabel(QWidget *parent = nullptr); + ~UrlLabel() override; 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 d; }; }