KIO

kurlnavigatorbuttonbase.cpp
1/*
2 SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz@gmx.at>
3 SPDX-FileCopyrightText: 2006 Aaron J. Seigo <aseigo@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kurlnavigatorbuttonbase_p.h"
9
10#include <KLocalizedString>
11#include <KUrlNavigator>
12
13#include <QStyle>
14#include <QStyleOptionViewItem>
15
16namespace KDEPrivate
17{
18KUrlNavigatorButtonBase::KUrlNavigatorButtonBase(KUrlNavigator *parent)
19 : QPushButton(parent)
20 , m_active(true)
21 , m_displayHint(0)
22{
23 setFocusPolicy(Qt::TabFocus);
25 setMinimumHeight(parent->minimumHeight());
26 setAttribute(Qt::WA_LayoutUsesWidgetRect);
27
28 connect(this, &KUrlNavigatorButtonBase::pressed, parent, &KUrlNavigator::requestActivation);
29}
30
31KUrlNavigatorButtonBase::~KUrlNavigatorButtonBase()
32{
33}
34
35void KUrlNavigatorButtonBase::setActive(bool active)
36{
37 if (m_active != active) {
38 m_active = active;
39 update();
40 }
41}
42
43bool KUrlNavigatorButtonBase::isActive() const
44{
45 return m_active;
46}
47
48void KUrlNavigatorButtonBase::setDisplayHintEnabled(DisplayHint hint, bool enable)
49{
50 if (enable) {
51 m_displayHint = m_displayHint | hint;
52 } else {
53 m_displayHint = m_displayHint & ~hint;
54 }
55 update();
56}
57
58bool KUrlNavigatorButtonBase::isDisplayHintEnabled(DisplayHint hint) const
59{
60 return (m_displayHint & hint) > 0;
61}
62
63void KUrlNavigatorButtonBase::focusInEvent(QFocusEvent *event)
64{
65 setDisplayHintEnabled(EnteredHint, true);
67}
68
69void KUrlNavigatorButtonBase::focusOutEvent(QFocusEvent *event)
70{
71 setDisplayHintEnabled(EnteredHint, false);
73}
74
75void KUrlNavigatorButtonBase::enterEvent(QEnterEvent *event)
76{
78 setDisplayHintEnabled(EnteredHint, true);
79 update();
80}
81
82void KUrlNavigatorButtonBase::leaveEvent(QEvent *event)
83{
85 setDisplayHintEnabled(EnteredHint, false);
86 update();
87}
88
89void KUrlNavigatorButtonBase::drawHoverBackground(QPainter *painter)
90{
91 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) || isDisplayHintEnabled(DraggedHint) || isDisplayHintEnabled(PopupActiveHint);
92
93 QColor backgroundColor = isHighlighted ? palette().color(QPalette::Highlight) : Qt::transparent;
94 if (!m_active && isHighlighted) {
95 backgroundColor.setAlpha(128);
96 }
97
98 if (backgroundColor != Qt::transparent) {
99 // TODO: the backgroundColor should be applied to the style
101 option.initFrom(this);
103 option.viewItemPosition = QStyleOptionViewItem::OnlyOne;
104 style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, this);
105 }
106}
107
108QColor KUrlNavigatorButtonBase::foregroundColor() const
109{
110 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) || isDisplayHintEnabled(DraggedHint) || isDisplayHintEnabled(PopupActiveHint);
111
112 QColor foregroundColor = palette().color(foregroundRole());
113
114 int alpha = m_active ? 255 : 128;
115 if (!m_active && !isHighlighted) {
116 alpha -= alpha / 4;
117 }
118 foregroundColor.setAlpha(alpha);
119
120 return foregroundColor;
121}
122
123void KUrlNavigatorButtonBase::activate()
124{
125 setActive(true);
126}
127
128} // namespace KDEPrivate
129
130#include "moc_kurlnavigatorbuttonbase_p.cpp"
Widget that allows to navigate through the paths of an URL.
void requestActivation()
Activates the URL navigator (KUrlNavigator::isActive() will return true) and emits the signal KUrlNav...
void update(Part *part, const QByteArray &data, qint64 dataSize)
QAction * hint(const QObject *recvr, const char *slot, QObject *parent)
void setAlpha(int alpha)
virtual void focusInEvent(QFocusEvent *e) override
virtual void focusOutEvent(QFocusEvent *e) override
PE_PanelItemViewItem
void initFrom(const QWidget *widget)
TabFocus
transparent
WA_LayoutUsesWidgetRect
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual void enterEvent(QEnterEvent *event)
virtual void leaveEvent(QEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:04:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.