Baloo Widgets

tagcheckbox.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
3 SPDX-FileCopyrightText: 2010 Sebastian Trueg <trueg@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "tagcheckbox.h"
9#include "tagwidget.h"
10
11#include <QHBoxLayout>
12#include <QLabel>
13#include <QMouseEvent>
14
15using namespace Baloo;
16
17TagCheckBox::TagCheckBox(const QString &tag, QWidget *parent)
18 : QLabel(parent)
19 , m_tag(tag)
20{
21 auto lastSegment = tag.split(QLatin1Char('/'), Qt::SkipEmptyParts).last();
22 setText(lastSegment);
23
24 setToolTip(tag);
25 setTextFormat(Qt::PlainText);
26 setForegroundRole(parent->foregroundRole());
27
28 installEventFilter(this);
29}
30
31TagCheckBox::~TagCheckBox() = default;
32
33bool TagCheckBox::eventFilter(QObject *watched, QEvent *event)
34{
35 if (watched == this) {
36 switch (event->type()) {
37 case QEvent::Enter: {
38 enableUrlHover(true);
39 break;
40 }
41 case QEvent::Leave: {
42 enableUrlHover(false);
43 break;
44 }
45 case QEvent::MouseMove: {
46 auto me = static_cast<QMouseEvent *>(event);
47 enableUrlHover(rect().contains(me->pos()));
48 break;
49 }
50
52 auto me = static_cast<QMouseEvent *>(event);
53 if (me->button() == Qt::LeftButton && rect().contains(me->pos())) {
54 Q_EMIT tagClicked(m_tag);
55 return true;
56 }
57 break;
58 }
59
60 default:
61 // do nothing
62 break;
63 }
64 }
65
66 return QWidget::eventFilter(watched, event);
67}
68
69void TagCheckBox::enableUrlHover(bool enable)
70{
71 if (m_urlHover != enable) {
72 m_urlHover = enable;
73 QFont f = font();
74 f.setUnderline(enable);
75 setFont(f);
77 }
78}
79
80#include "moc_tagcheckbox.cpp"
void setUnderline(bool enable)
virtual bool event(QEvent *e) override
Q_EMITQ_EMIT
virtual bool eventFilter(QObject *watched, QEvent *event)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
PointingHandCursor
LeftButton
SkipEmptyParts
PlainText
void setCursor(const QCursor &)
QPalette::ColorRole foregroundRole() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.