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 | |
16 | namespace KDEPrivate |
17 | { |
18 | KUrlNavigatorButtonBase::KUrlNavigatorButtonBase(KUrlNavigator *parent) |
19 | : QPushButton(parent) |
20 | , m_active(true) |
21 | , m_displayHint(0) |
22 | { |
23 | setFocusPolicy(Qt::TabFocus); |
24 | setSizePolicy(hor: QSizePolicy::Maximum, ver: QSizePolicy::Fixed); |
25 | setMinimumHeight(parent->minimumHeight()); |
26 | setAttribute(Qt::WA_LayoutUsesWidgetRect); |
27 | |
28 | connect(sender: this, signal: &KUrlNavigatorButtonBase::pressed, context: parent, slot: &KUrlNavigator::requestActivation); |
29 | } |
30 | |
31 | KUrlNavigatorButtonBase::~KUrlNavigatorButtonBase() |
32 | { |
33 | } |
34 | |
35 | void KUrlNavigatorButtonBase::setActive(bool active) |
36 | { |
37 | if (m_active != active) { |
38 | m_active = active; |
39 | update(); |
40 | } |
41 | } |
42 | |
43 | bool KUrlNavigatorButtonBase::isActive() const |
44 | { |
45 | return m_active; |
46 | } |
47 | |
48 | void 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 | |
58 | bool KUrlNavigatorButtonBase::isDisplayHintEnabled(DisplayHint hint) const |
59 | { |
60 | return (m_displayHint & hint) > 0; |
61 | } |
62 | |
63 | void KUrlNavigatorButtonBase::focusInEvent(QFocusEvent *event) |
64 | { |
65 | setDisplayHintEnabled(hint: EnteredHint, enable: true); |
66 | QPushButton::focusInEvent(event); |
67 | } |
68 | |
69 | void KUrlNavigatorButtonBase::focusOutEvent(QFocusEvent *event) |
70 | { |
71 | setDisplayHintEnabled(hint: EnteredHint, enable: false); |
72 | QPushButton::focusOutEvent(event); |
73 | } |
74 | |
75 | void KUrlNavigatorButtonBase::enterEvent(QEnterEvent *event) |
76 | { |
77 | QPushButton::enterEvent(event); |
78 | setDisplayHintEnabled(hint: EnteredHint, enable: true); |
79 | update(); |
80 | } |
81 | |
82 | void KUrlNavigatorButtonBase::leaveEvent(QEvent *event) |
83 | { |
84 | QPushButton::leaveEvent(event); |
85 | setDisplayHintEnabled(hint: EnteredHint, enable: false); |
86 | update(); |
87 | } |
88 | |
89 | void KUrlNavigatorButtonBase::drawHoverBackground(QPainter *painter) |
90 | { |
91 | const bool isHighlighted = isDisplayHintEnabled(hint: EnteredHint) || isDisplayHintEnabled(hint: DraggedHint) || isDisplayHintEnabled(hint: PopupActiveHint); |
92 | |
93 | QColor backgroundColor = isHighlighted ? palette().color(cr: 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 |
100 | QStyleOptionViewItem option; |
101 | option.initFrom(w: this); |
102 | option.state = QStyle::State_Enabled | QStyle::State_MouseOver; |
103 | option.viewItemPosition = QStyleOptionViewItem::OnlyOne; |
104 | style()->drawPrimitive(pe: QStyle::PE_PanelItemViewItem, opt: &option, p: painter, w: this); |
105 | } |
106 | } |
107 | |
108 | QColor KUrlNavigatorButtonBase::foregroundColor() const |
109 | { |
110 | const bool isHighlighted = isDisplayHintEnabled(hint: EnteredHint) || isDisplayHintEnabled(hint: DraggedHint) || isDisplayHintEnabled(hint: PopupActiveHint); |
111 | |
112 | QColor foregroundColor = palette().color(cr: 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 | |
123 | void KUrlNavigatorButtonBase::activate() |
124 | { |
125 | setActive(true); |
126 | } |
127 | |
128 | } // namespace KDEPrivate |
129 | |
130 | #include "moc_kurlnavigatorbuttonbase_p.cpp" |
131 | |