Changeset View
Changeset View
Standalone View
Standalone View
src/tagcheckbox.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | This file is part of the Nepomuk KDE project. | 2 | This file is part of the Baloo project. | ||
3 | Copyright (C) 2018 Michael Heidelbach <ottwolt@gmail.com> | ||||
3 | Copyright (C) 2013 Vishesh Handa <me@vhanda.in> | 4 | Copyright (C) 2013 Vishesh Handa <me@vhanda.in> | ||
4 | Copyright (C) 2010 Sebastian Trueg <trueg@kde.org> | 5 | Copyright (C) 2010 Sebastian Trueg <trueg@kde.org> | ||
5 | 6 | | |||
6 | This library is free software; you can redistribute it and/or | 7 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Lesser General Public | 8 | modify it under the terms of the GNU Lesser General Public | ||
8 | License as published by the Free Software Foundation; either | 9 | 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 | 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 | later version accepted by the membership of KDE e.V. (or its | ||
Show All 11 Lines | |||||
22 | 23 | | |||
23 | #include "tagcheckbox.h" | 24 | #include "tagcheckbox.h" | ||
24 | #include "tagwidget.h" | 25 | #include "tagwidget.h" | ||
25 | #include "tagwidget_p.h" | 26 | #include "tagwidget_p.h" | ||
26 | 27 | | |||
27 | #include <QtGui/QMouseEvent> | 28 | #include <QtGui/QMouseEvent> | ||
28 | #include <QtWidgets/QHBoxLayout> | 29 | #include <QtWidgets/QHBoxLayout> | ||
29 | #include <QtWidgets/QLabel> | 30 | #include <QtWidgets/QLabel> | ||
31 | #include <QUrlQuery> | ||||
30 | 32 | | |||
31 | using namespace Baloo; | 33 | using namespace Baloo; | ||
32 | 34 | | |||
33 | TagCheckBox::TagCheckBox(const QString& tag, QWidget* parent) | 35 | TagCheckBox::TagCheckBox(const QString& content, | ||
34 | : QWidget( parent ), | 36 | const QString& baseUrl, | ||
35 | m_label(0), | 37 | const TagWidget::DisplayMode mode, | ||
36 | m_tag(tag), | 38 | QWidget* parent) | ||
37 | m_urlHover(false) | 39 | : QWidget(parent) | ||
40 | , m_content(content) | ||||
41 | , m_baseUrl(baseUrl) | ||||
42 | , m_mode(mode) | ||||
43 | , m_label(0) | ||||
44 | , m_urlHover(false) | ||||
38 | { | 45 | { | ||
39 | QHBoxLayout* layout = new QHBoxLayout(this); | 46 | QHBoxLayout* layout = new QHBoxLayout(this); | ||
40 | layout->setMargin(0); | 47 | layout->setMargin(0); | ||
41 | 48 | const QString displayString = (m_mode & TagWidget::DisplayMode::Tags) == TagWidget::DisplayMode::Tags | |||
42 | m_label = new QLabel(tag.split("/", QString::SkipEmptyParts).last(), this); | 49 | ? m_content.split("/", QString::SkipEmptyParts).last() | ||
43 | m_label->setToolTip(tag); | 50 | : m_content; | ||
44 | m_label->setMouseTracking(true); | 51 | m_label = new QLabel(displayString, this); | ||
52 | if (displayString != m_content) { | ||||
53 | m_label->setToolTip(m_content); | ||||
54 | } | ||||
45 | m_label->setTextFormat(Qt::PlainText); | 55 | m_label->setTextFormat(Qt::PlainText); | ||
46 | m_label->setForegroundRole(parent->foregroundRole()); | 56 | m_label->setForegroundRole(parent->foregroundRole()); | ||
47 | m_child = m_label; | | |||
48 | 57 | | |||
49 | m_child->installEventFilter( this ); | 58 | if ((m_mode & TagWidget::DisplayMode::NoLinks) != TagWidget::DisplayMode::NoLinks) { | ||
50 | m_child->setMouseTracking(true); | 59 | m_label->setMouseTracking(true); | ||
51 | layout->addWidget( m_child ); | 60 | m_label->installEventFilter(this); | ||
61 | } | ||||
62 | layout->addWidget(m_label); | ||||
52 | } | 63 | } | ||
53 | 64 | | |||
54 | TagCheckBox::~TagCheckBox() | 65 | TagCheckBox::~TagCheckBox() | ||
55 | { | 66 | { | ||
56 | } | 67 | } | ||
57 | 68 | | |||
58 | void TagCheckBox::leaveEvent( QEvent* event ) | 69 | void TagCheckBox::leaveEvent( QEvent* event ) | ||
59 | { | 70 | { | ||
60 | QWidget::leaveEvent( event ); | 71 | QWidget::leaveEvent( event ); | ||
61 | enableUrlHover( false ); | 72 | enableUrlHover( false ); | ||
62 | } | 73 | } | ||
63 | 74 | | |||
64 | 75 | | |||
65 | bool TagCheckBox::eventFilter( QObject* watched, QEvent* event ) | 76 | bool TagCheckBox::eventFilter( QObject* watched, QEvent* event ) | ||
66 | { | 77 | { | ||
67 | if( watched == m_child ) { | 78 | if (watched == m_label) { | ||
68 | switch( event->type() ) { | 79 | switch( event->type() ) { | ||
69 | case QEvent::MouseMove: { | 80 | case QEvent::MouseMove: { | ||
70 | QMouseEvent* me = static_cast<QMouseEvent*>(event); | 81 | QMouseEvent* me = static_cast<QMouseEvent*>(event); | ||
71 | enableUrlHover( tagRect().contains(me->pos()) ); | 82 | enableUrlHover( tagRect().contains(me->pos()) ); | ||
72 | break; | 83 | break; | ||
73 | } | 84 | } | ||
74 | 85 | | |||
75 | case QEvent::MouseButtonRelease: { | 86 | case QEvent::MouseButtonRelease: { | ||
76 | QMouseEvent* me = static_cast<QMouseEvent*>(event); | 87 | QMouseEvent* me = static_cast<QMouseEvent*>(event); | ||
77 | if (me->button() == Qt::LeftButton && tagRect().contains(me->pos())) { | 88 | if (me->button() == Qt::LeftButton && tagRect().contains(me->pos())) { | ||
78 | emit tagClicked( m_tag ); | 89 | // TODO: Improve url composition | ||
90 | auto url = QUrl::fromUserInput(m_baseUrl + m_content); | ||||
91 | auto scheme = url.scheme(); | ||||
92 | if (url.scheme() == QLatin1String("tags")) { | ||||
93 | // Assert correct case | ||||
94 | url.setHost(QString()); | ||||
95 | url.setPath(m_content); | ||||
96 | } else { | ||||
97 | QString query = url.query().replace(" ", "+"); | ||||
98 | url.setQuery(query); | ||||
99 | } | ||||
100 | emit tagClicked(url); | ||||
79 | return true; | 101 | return true; | ||
80 | } | 102 | } | ||
81 | break; | 103 | break; | ||
82 | } | 104 | } | ||
83 | 105 | | |||
84 | default: | 106 | default: | ||
85 | // do nothing | 107 | // do nothing | ||
86 | break; | 108 | break; | ||
87 | } | 109 | } | ||
88 | } | 110 | } | ||
89 | 111 | | |||
90 | return QWidget::eventFilter( watched, event ); | 112 | return QWidget::eventFilter( watched, event ); | ||
91 | } | 113 | } | ||
92 | 114 | | |||
93 | 115 | | |||
94 | QRect TagCheckBox::tagRect() const | 116 | QRect TagCheckBox::tagRect() const | ||
95 | { | 117 | { | ||
96 | return QRect(QPoint(0, 0), m_label->size()); | 118 | return QRect(QPoint(0, 0), m_label->size()); | ||
97 | } | 119 | } | ||
98 | 120 | | |||
99 | 121 | | |||
100 | void TagCheckBox::enableUrlHover( bool enable ) | 122 | void TagCheckBox::enableUrlHover( bool enable ) | ||
101 | { | 123 | { | ||
102 | if( m_urlHover != enable ) { | 124 | if (m_mode & TagWidget::DisplayMode::NoLinks || m_urlHover == enable ) { | ||
125 | return; | ||||
126 | } | ||||
127 | | ||||
103 | m_urlHover = enable; | 128 | m_urlHover = enable; | ||
104 | QFont f = font(); | 129 | QFont f = font(); | ||
105 | if(enable) | 130 | if(enable) { | ||
106 | f.setUnderline(true); | 131 | f.setUnderline(true); | ||
107 | m_child->setFont(f); | | |||
108 | m_child->setCursor( enable ? Qt::PointingHandCursor : Qt::ArrowCursor ); | | |||
109 | } | 132 | } | ||
133 | m_label->setFont(f); | ||||
134 | m_label->setCursor(enable ? Qt::PointingHandCursor : Qt::ArrowCursor); | ||||
110 | } | 135 | } | ||
111 | 136 | |