Warning, file /libraries/baloo-widgets/src/tagcheckbox.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
0003     SPDX-FileCopyrightText: 2010 Sebastian Trueg <trueg@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "tagcheckbox.h"
0009 #include "tagwidget.h"
0010 
0011 #include <QHBoxLayout>
0012 #include <QLabel>
0013 #include <QMouseEvent>
0014 
0015 using namespace Baloo;
0016 
0017 TagCheckBox::TagCheckBox(const QString &tag, QWidget *parent)
0018     : QWidget(parent)
0019     , m_tag(tag)
0020 {
0021     auto layout = new QHBoxLayout(this);
0022     layout->setContentsMargins(0, 0, 0, 0);
0023 
0024     m_label = new QLabel(tag.split(QLatin1Char('/'), Qt::SkipEmptyParts).last(), this);
0025     m_label->setToolTip(tag);
0026     m_label->setMouseTracking(true);
0027     m_label->setTextFormat(Qt::PlainText);
0028     m_label->setForegroundRole(parent->foregroundRole());
0029     m_child = m_label;
0030 
0031     m_child->installEventFilter(this);
0032     m_child->setMouseTracking(true);
0033     layout->addWidget(m_child);
0034 }
0035 
0036 TagCheckBox::~TagCheckBox() = default;
0037 
0038 void TagCheckBox::leaveEvent(QEvent *event)
0039 {
0040     QWidget::leaveEvent(event);
0041     enableUrlHover(false);
0042 }
0043 
0044 bool TagCheckBox::eventFilter(QObject *watched, QEvent *event)
0045 {
0046     if (watched == m_child) {
0047         switch (event->type()) {
0048         case QEvent::MouseMove: {
0049             auto me = static_cast<QMouseEvent *>(event);
0050             enableUrlHover(tagRect().contains(me->pos()));
0051             break;
0052         }
0053 
0054         case QEvent::MouseButtonRelease: {
0055             auto me = static_cast<QMouseEvent *>(event);
0056             if (me->button() == Qt::LeftButton && tagRect().contains(me->pos())) {
0057                 Q_EMIT tagClicked(m_tag);
0058                 return true;
0059             }
0060             break;
0061         }
0062 
0063         default:
0064             // do nothing
0065             break;
0066         }
0067     }
0068 
0069     return QWidget::eventFilter(watched, event);
0070 }
0071 
0072 QRect TagCheckBox::tagRect() const
0073 {
0074     return QRect(QPoint(0, 0), m_label->size());
0075 }
0076 
0077 void TagCheckBox::enableUrlHover(bool enable)
0078 {
0079     if (m_urlHover != enable) {
0080         m_urlHover = enable;
0081         QFont f = font();
0082         if (enable)
0083             f.setUnderline(true);
0084         m_child->setFont(f);
0085         m_child->setCursor(enable ? Qt::PointingHandCursor : Qt::ArrowCursor);
0086     }
0087 }