Baloo Widgets

tagcheckbox.cpp
1 /*
2  This file is part of the Baloo KDE project.
3  Copyright (C) 2013 Vishesh Handa <[email protected]>
4  Copyright (C) 2010 Sebastian Trueg <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "tagcheckbox.h"
24 #include "tagwidget.h"
25 
26 #include <QMouseEvent>
27 #include <QHBoxLayout>
28 #include <QLabel>
29 
30 using namespace Baloo;
31 
32 TagCheckBox::TagCheckBox(const QString& tag, QWidget* parent)
33  : QWidget( parent ),
34  m_label(nullptr),
35  m_tag(tag),
36  m_urlHover(false)
37 {
38  QHBoxLayout* layout = new QHBoxLayout(this);
39  layout->setContentsMargins(0, 0, 0, 0);
40 
41  m_label = new QLabel(tag.split(QLatin1Char('/'), QString::SkipEmptyParts).last(), this);
42  m_label->setToolTip(tag);
43  m_label->setMouseTracking(true);
44  m_label->setTextFormat(Qt::PlainText);
45  m_label->setForegroundRole(parent->foregroundRole());
46  m_child = m_label;
47 
48  m_child->installEventFilter( this );
49  m_child->setMouseTracking(true);
50  layout->addWidget( m_child );
51 }
52 
53 TagCheckBox::~TagCheckBox()
54 {
55 }
56 
57 void TagCheckBox::leaveEvent( QEvent* event )
58 {
59  QWidget::leaveEvent( event );
60  enableUrlHover( false );
61 }
62 
63 
64 bool TagCheckBox::eventFilter( QObject* watched, QEvent* event )
65 {
66  if( watched == m_child ) {
67  switch( event->type() ) {
68  case QEvent::MouseMove: {
69  QMouseEvent* me = static_cast<QMouseEvent*>(event);
70  enableUrlHover( tagRect().contains(me->pos()) );
71  break;
72  }
73 
75  QMouseEvent* me = static_cast<QMouseEvent*>(event);
76  if (me->button() == Qt::LeftButton && tagRect().contains(me->pos())) {
77  Q_EMIT tagClicked( m_tag );
78  return true;
79  }
80  break;
81  }
82 
83  default:
84  // do nothing
85  break;
86  }
87  }
88 
89  return QWidget::eventFilter( watched, event );
90 }
91 
92 
93 QRect TagCheckBox::tagRect() const
94 {
95  return QRect(QPoint(0, 0), m_label->size());
96 }
97 
98 
99 void TagCheckBox::enableUrlHover( bool enable )
100 {
101  if( m_urlHover != enable ) {
102  m_urlHover = enable;
103  QFont f = font();
104  if(enable)
105  f.setUnderline(true);
106  m_child->setFont(f);
107  m_child->setCursor( enable ? Qt::PointingHandCursor : Qt::ArrowCursor );
108  }
109 }
110 
virtual QLayout * layout() override
QEvent::Type type() const const
void setContentsMargins(int left, int top, int right, int bottom)
PlainText
void setUnderline(bool enable)
LeftButton
QPalette::ColorRole foregroundRole() const const
virtual bool event(QEvent *e)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
Qt::MouseButton button() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
PointingHandCursor
virtual bool eventFilter(QObject *watched, QEvent *event)
T & last()
QPoint pos() const const
void setToolTip(const QString &)
virtual void leaveEvent(QEvent *event)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:07:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.