File indexing completed on 2024-06-16 04:56:17
0001 /* 0002 view/urllabel.cpp 0003 0004 This file is part of Kleopatra, the KDE keymanager 0005 SPDX-FileCopyrightText: 2021, 2022 g10 Code GmbH 0006 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include <config-kleopatra.h> 0012 0013 #include "urllabel.h" 0014 0015 #include <QUrl> 0016 0017 using namespace Kleo; 0018 0019 UrlLabel::UrlLabel(QWidget *parent) 0020 : HtmlLabel{parent} 0021 { 0022 } 0023 0024 UrlLabel::~UrlLabel() = default; 0025 0026 void UrlLabel::setUrl(const QUrl &url, const QString &text) 0027 { 0028 // we prepend a zero-width-space character to work around a bug in QLabel::focusNextPrevChild(false) 0029 // which makes it impossible to leave the label with Shift+Tab if the text starts with a link 0030 static const QString templateString{QLatin1StringView{"​<a href=\"%1\">%2</a>"}}; 0031 0032 if (url.isEmpty()) { 0033 HtmlLabel::setHtml({}); 0034 return; 0035 } 0036 0037 setHtml(templateString.arg( // 0038 url.url(QUrl::FullyEncoded), // 0039 text.isEmpty() ? url.toDisplayString().toHtmlEscaped() : text.toHtmlEscaped())); 0040 } 0041 0042 void UrlLabel::focusInEvent(QFocusEvent *event) 0043 { 0044 // immediately focus the URL when the label get focus 0045 QLabel::focusInEvent(event); 0046 if (!hasSelectedText()) { 0047 QMetaObject::invokeMethod( 0048 this, 0049 [this]() { 0050 focusNextPrevChild(true); 0051 }, 0052 Qt::QueuedConnection); 0053 } 0054 } 0055 0056 #include "moc_urllabel.cpp"