File indexing completed on 2024-05-12 16:39:56
0001 /* This file is part of the KDE project 0002 Copyright (C) 2011-2018 Jarosław Staniek <staniek@kde.org> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KexiLinkWidget.h" 0021 0022 #include <QEvent> 0023 #include <QShortcut> 0024 0025 #include <KColorScheme> 0026 0027 class Q_DECL_HIDDEN KexiLinkWidget::Private 0028 { 0029 public: 0030 explicit Private(KexiLinkWidget* qq) : q(qq) 0031 { 0032 q->setFocusPolicy(Qt::TabFocus); // not a Strong or Click focus, otherwise we're loosing 0033 // focus from important input widgets (e.g. in a 0034 // KexiAssistantPage) 0035 q->setTextFormat(Qt::RichText); 0036 updateColors(); 0037 } 0038 0039 void updateColors() { 0040 KColorScheme scheme(q->palette().currentColorGroup()); 0041 linkColor = scheme.foreground(KColorScheme::LinkText).color(); 0042 } 0043 0044 void updateText() { 0045 QString text; 0046 text = QString("<a href=\"%1\" style=\"color:%2;\">%3</a>") 0047 .arg(link).arg(linkColor.name()).arg(linkText); 0048 if (!format.isEmpty()) { 0049 text = QString(format).replace("%L", text); 0050 } 0051 q->setText(text); 0052 } 0053 0054 KexiLinkWidget * const q; 0055 QString link; 0056 QString linkText; 0057 QString format; 0058 QColor linkColor; 0059 QShortcut *shortcut = nullptr; 0060 }; 0061 0062 KexiLinkWidget::KexiLinkWidget(QWidget* parent) 0063 : QLabel(parent), d(new Private(this)) 0064 { 0065 } 0066 0067 KexiLinkWidget::KexiLinkWidget( 0068 const QString& link, const QString& linkText, QWidget* parent) 0069 : QLabel(parent), d(new Private(this)) 0070 { 0071 d->link = link; 0072 d->linkText = linkText; 0073 d->updateText(); 0074 } 0075 0076 KexiLinkWidget::~KexiLinkWidget() 0077 { 0078 delete d; 0079 } 0080 0081 QString KexiLinkWidget::link() const 0082 { 0083 return d->link; 0084 } 0085 0086 void KexiLinkWidget::setLink(const QString& link) 0087 { 0088 d->link = link; 0089 d->updateText(); 0090 } 0091 0092 QString KexiLinkWidget::linkText() const 0093 { 0094 return d->linkText; 0095 } 0096 0097 void KexiLinkWidget::setLinkText(const QString& linkText) 0098 { 0099 d->linkText = linkText; 0100 d->updateText(); 0101 } 0102 0103 QString KexiLinkWidget::format() const 0104 { 0105 return d->format; 0106 } 0107 0108 void KexiLinkWidget::setFormat(const QString& format) 0109 { 0110 d->format = format; 0111 d->updateText(); 0112 } 0113 0114 void KexiLinkWidget::click() 0115 { 0116 emit linkActivated(d->link); 0117 } 0118 0119 QKeySequence KexiLinkWidget::shortcut() const 0120 { 0121 return d->shortcut ? d->shortcut->key() : QKeySequence(); 0122 } 0123 0124 void KexiLinkWidget::setShortcut(const QKeySequence &key) 0125 { 0126 if (!d->shortcut) { 0127 d->shortcut = new QShortcut(this); 0128 connect(d->shortcut, &QShortcut::activatedAmbiguously, this, &KexiLinkWidget::click); 0129 } 0130 d->shortcut->setKey(key); 0131 } 0132 0133 void KexiLinkWidget::changeEvent(QEvent* event) 0134 { 0135 switch (event->type()) { 0136 case QEvent::EnabledChange: 0137 case QEvent::PaletteChange: 0138 d->updateColors(); 0139 d->updateText(); 0140 break; 0141 default:; 0142 } 0143 QLabel::changeEvent(event); 0144 } 0145