Baloo Widgets

tagcheckbox.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Vishesh Handa <[email protected]>
3  SPDX-FileCopyrightText: 2010 Sebastian Trueg <[email protected]>
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 
15 using namespace Baloo;
16 
17 TagCheckBox::TagCheckBox(const QString &tag, QWidget *parent)
18  : QWidget(parent)
19  , m_tag(tag)
20 {
21  auto layout = new QHBoxLayout(this);
22  layout->setContentsMargins(0, 0, 0, 0);
23 
24  m_label = new QLabel(tag.split(QLatin1Char('/'), Qt::SkipEmptyParts).last(), this);
25  m_label->setToolTip(tag);
26  m_label->setMouseTracking(true);
27  m_label->setTextFormat(Qt::PlainText);
28  m_label->setForegroundRole(parent->foregroundRole());
29  m_child = m_label;
30 
31  m_child->installEventFilter(this);
32  m_child->setMouseTracking(true);
33  layout->addWidget(m_child);
34 }
35 
36 TagCheckBox::~TagCheckBox() = default;
37 
38 void TagCheckBox::leaveEvent(QEvent *event)
39 {
40  QWidget::leaveEvent(event);
41  enableUrlHover(false);
42 }
43 
44 bool TagCheckBox::eventFilter(QObject *watched, QEvent *event)
45 {
46  if (watched == m_child) {
47  switch (event->type()) {
48  case QEvent::MouseMove: {
49  auto me = static_cast<QMouseEvent *>(event);
50  enableUrlHover(tagRect().contains(me->pos()));
51  break;
52  }
53 
55  auto me = static_cast<QMouseEvent *>(event);
56  if (me->button() == Qt::LeftButton && tagRect().contains(me->pos())) {
57  Q_EMIT tagClicked(m_tag);
58  return true;
59  }
60  break;
61  }
62 
63  default:
64  // do nothing
65  break;
66  }
67  }
68 
69  return QWidget::eventFilter(watched, event);
70 }
71 
72 QRect TagCheckBox::tagRect() const
73 {
74  return QRect(QPoint(0, 0), m_label->size());
75 }
76 
77 void TagCheckBox::enableUrlHover(bool enable)
78 {
79  if (m_urlHover != enable) {
80  m_urlHover = enable;
81  QFont f = font();
82  if (enable)
83  f.setUnderline(true);
84  m_child->setFont(f);
85  m_child->setCursor(enable ? Qt::PointingHandCursor : Qt::ArrowCursor);
86  }
87 }
PlainText
virtual QLayout * layout() override
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
virtual void leaveEvent(QEvent *event)
void setUnderline(bool enable)
LeftButton
virtual bool eventFilter(QObject *watched, QEvent *event)
SkipEmptyParts
PointingHandCursor
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void addWidget(QWidget *w)
void setContentsMargins(int left, int top, int right, int bottom)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:10:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.