File indexing completed on 2024-05-12 05:46:54
0001 // Copyright (C) 2000 Peter Putzer <putzer@kde.org> 0002 0003 // This library is free software; you can redistribute it and/or 0004 // modify it under the terms of the GNU Lesser General Public 0005 // License as published by the Free Software Foundation; either 0006 // version 2.1 of the License, or (at your option) any later version. 0007 0008 // This library is distributed in the hope that it will be useful, 0009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 // Lesser General Public License for more details. 0012 0013 // You should have received a copy of the GNU Lesser General Public 0014 // License along with this library; if not, write to the Free Software 0015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0016 // 02110-1301 USA 0017 0018 #include "kurllabel.h" 0019 0020 #include <QTimer> 0021 #include <QApplication> 0022 #include <QMouseEvent> 0023 #include <QPalette> 0024 0025 class Q_DECL_HIDDEN KUrlLabel::Private 0026 { 0027 public: 0028 Private(const QString &_url, KUrlLabel *_parent) 0029 : parent(_parent), 0030 url(_url), 0031 tipText(url), 0032 linkColor(_parent->palette().color(QPalette::Active, QPalette::Link)), 0033 highlightedLinkColor(_parent->palette().color(QPalette::Active, QPalette::BrightText)), 0034 cursor(nullptr), 0035 textUnderlined(true), 0036 realUnderlined(true), 0037 useTips(false), 0038 useCursor(false), 0039 glowEnabled(true), 0040 floatEnabled(false), 0041 timer(new QTimer(parent)) 0042 { 0043 connect(timer, &QTimer::timeout, parent, [this]() { updateColor(); }); 0044 } 0045 0046 ~Private() 0047 { 0048 } 0049 0050 void updateColor() 0051 { 0052 timer->stop(); 0053 0054 if (!(glowEnabled || floatEnabled) || !parent->rect().contains(parent->mapFromGlobal(QCursor::pos()))) { 0055 setLinkColor(linkColor); 0056 } 0057 } 0058 0059 void setLinkColor(const QColor &color) 0060 { 0061 QPalette palette = parent->palette(); 0062 palette.setColor(QPalette::WindowText, color); 0063 parent->setPalette(palette); 0064 0065 parent->update(); 0066 } 0067 0068 KUrlLabel *parent; 0069 0070 QString url; 0071 QString tipText; 0072 QColor linkColor; 0073 QColor highlightedLinkColor; 0074 QCursor *cursor; 0075 bool textUnderlined : 1; 0076 bool realUnderlined : 1; 0077 bool useTips : 1; 0078 bool useCursor : 1; 0079 bool glowEnabled : 1; 0080 bool floatEnabled : 1; 0081 QPixmap alternatePixmap; 0082 QPixmap realPixmap; 0083 QTimer *timer; 0084 }; 0085 0086 KUrlLabel::KUrlLabel(const QString &url, const QString &text, QWidget *parent) 0087 : QLabel(!text.isNull() ? text : url, parent), 0088 d(new Private(url, this)) 0089 { 0090 setFont(font()); 0091 setCursor(QCursor(Qt::PointingHandCursor)); 0092 d->setLinkColor(d->linkColor); 0093 } 0094 0095 KUrlLabel::KUrlLabel(QWidget *parent) 0096 : QLabel(parent), 0097 d(new Private(QString(), this)) 0098 { 0099 setFont(font()); 0100 setCursor(QCursor(Qt::PointingHandCursor)); 0101 d->setLinkColor(d->linkColor); 0102 } 0103 0104 KUrlLabel::~KUrlLabel() 0105 { 0106 delete d; 0107 } 0108 0109 void KUrlLabel::mouseReleaseEvent(QMouseEvent *event) 0110 { 0111 QLabel::mouseReleaseEvent(event); 0112 0113 d->setLinkColor(d->highlightedLinkColor); 0114 d->timer->start(300); 0115 0116 switch (event->button()) { 0117 case Qt::LeftButton: 0118 emit leftClickedUrl(); 0119 emit leftClickedUrl(d->url); 0120 break; 0121 0122 case Qt::MidButton: 0123 emit middleClickedUrl(); 0124 emit middleClickedUrl(d->url); 0125 break; 0126 0127 case Qt::RightButton: 0128 emit rightClickedUrl(); 0129 emit rightClickedUrl(d->url); 0130 break; 0131 0132 default: 0133 break; 0134 } 0135 } 0136 0137 void KUrlLabel::setFont(const QFont &font) 0138 { 0139 QFont newFont = font; 0140 newFont.setUnderline(d->textUnderlined); 0141 0142 QLabel::setFont(newFont); 0143 } 0144 0145 void KUrlLabel::setUnderline(bool on) 0146 { 0147 d->textUnderlined = on; 0148 0149 setFont(font()); 0150 } 0151 0152 void KUrlLabel::setUrl(const QString &url) 0153 { 0154 if (d->tipText == d->url) { // update the tip as well 0155 d->tipText = url; 0156 setUseTips(d->useTips); 0157 } 0158 0159 d->url = url; 0160 } 0161 0162 QString KUrlLabel::url() const 0163 { 0164 return d->url; 0165 } 0166 0167 void KUrlLabel::setUseCursor(bool on, QCursor *cursor) 0168 { 0169 d->useCursor = on; 0170 d->cursor = cursor; 0171 0172 if (on) { 0173 if (cursor) { 0174 setCursor(*cursor); 0175 } else { 0176 setCursor(QCursor(Qt::PointingHandCursor)); 0177 } 0178 } else { 0179 unsetCursor(); 0180 } 0181 } 0182 0183 bool KUrlLabel::useCursor() const 0184 { 0185 return d->useCursor; 0186 } 0187 0188 void KUrlLabel::setUseTips(bool on) 0189 { 0190 d->useTips = on; 0191 0192 if (on) { 0193 setToolTip(d->tipText); 0194 } else { 0195 setToolTip(QString()); 0196 } 0197 } 0198 0199 void KUrlLabel::setTipText(const QString &tipText) 0200 { 0201 d->tipText = tipText; 0202 0203 setUseTips(d->useTips); 0204 } 0205 0206 bool KUrlLabel::useTips() const 0207 { 0208 return d->useTips; 0209 } 0210 0211 QString KUrlLabel::tipText() const 0212 { 0213 return d->tipText; 0214 } 0215 0216 void KUrlLabel::setHighlightedColor(const QColor &color) 0217 { 0218 d->linkColor = color; 0219 0220 if (!d->timer->isActive()) { 0221 d->setLinkColor(color); 0222 } 0223 } 0224 0225 void KUrlLabel::setHighlightedColor(const QString &color) 0226 { 0227 setHighlightedColor(QColor(color)); 0228 } 0229 0230 void KUrlLabel::setSelectedColor(const QColor &color) 0231 { 0232 d->highlightedLinkColor = color; 0233 0234 if (d->timer->isActive()) { 0235 d->setLinkColor(color); 0236 } 0237 } 0238 0239 void KUrlLabel::setSelectedColor(const QString &color) 0240 { 0241 setSelectedColor(QColor(color)); 0242 } 0243 0244 void KUrlLabel::setGlowEnabled(bool glowEnabled) 0245 { 0246 d->glowEnabled = glowEnabled; 0247 } 0248 0249 void KUrlLabel::setFloatEnabled(bool floatEnabled) 0250 { 0251 d->floatEnabled = floatEnabled; 0252 } 0253 0254 bool KUrlLabel::isGlowEnabled() const 0255 { 0256 return d->glowEnabled; 0257 } 0258 0259 bool KUrlLabel::isFloatEnabled() const 0260 { 0261 return d->floatEnabled; 0262 } 0263 0264 void KUrlLabel::setAlternatePixmap(const QPixmap &pixmap) 0265 { 0266 d->alternatePixmap = pixmap; 0267 } 0268 0269 const QPixmap *KUrlLabel::alternatePixmap() const 0270 { 0271 return &d->alternatePixmap; 0272 } 0273 0274 void KUrlLabel::enterEvent(QEvent *event) 0275 { 0276 QLabel::enterEvent(event); 0277 0278 if (!d->alternatePixmap.isNull() && pixmap()) { 0279 d->realPixmap = *pixmap(); 0280 setPixmap(d->alternatePixmap); 0281 } 0282 0283 if (d->glowEnabled || d->floatEnabled) { 0284 d->timer->stop(); 0285 0286 d->setLinkColor(d->highlightedLinkColor); 0287 0288 d->realUnderlined = d->textUnderlined; 0289 0290 if (d->floatEnabled) { 0291 setUnderline(true); 0292 } 0293 } 0294 0295 emit enteredUrl(); 0296 emit enteredUrl(d->url); 0297 } 0298 0299 void KUrlLabel::leaveEvent(QEvent *event) 0300 { 0301 QLabel::leaveEvent(event); 0302 0303 if (!d->alternatePixmap.isNull() && pixmap()) { 0304 setPixmap(d->realPixmap); 0305 } 0306 0307 if ((d->glowEnabled || d->floatEnabled) && !d->timer->isActive()) { 0308 d->setLinkColor(d->linkColor); 0309 } 0310 0311 setUnderline(d->realUnderlined); 0312 0313 emit leftUrl(); 0314 emit leftUrl(d->url); 0315 } 0316 0317 bool KUrlLabel::event(QEvent *event) 0318 { 0319 if (event->type() == QEvent::PaletteChange) { 0320 /** 0321 * Use parentWidget() unless you are a toplevel widget, then try qAapp 0322 */ 0323 QPalette palette = parentWidget() ? parentWidget()->palette() : qApp->palette(); 0324 0325 palette.setBrush(QPalette::Base, palette.brush(QPalette::Normal, QPalette::Window)); 0326 palette.setColor(QPalette::WindowText, this->palette().color(QPalette::Active, QPalette::WindowText)); 0327 setPalette(palette); 0328 0329 d->linkColor = palette.color(QPalette::Active, QPalette::Link); 0330 d->updateColor(); 0331 0332 return true; 0333 } else { 0334 return QLabel::event(event); 0335 } 0336 } 0337 0338 #include "moc_kurllabel.cpp"