| 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 | QStyleOptionButton buttonOption; |
| 94 | buttonOption.initFrom(w: this); |
| 95 | buttonOption.rect = QRect(0, 0, width(), height()); |
| 96 | buttonOption.features = QStyleOptionButton::Flat; |
| 97 | |
| 98 | // Draw button graphic |
| 99 | if (isHighlighted) { |
| 100 | style()->drawPrimitive(pe: QStyle::PE_PanelButtonCommand, opt: &buttonOption, p: painter, w: this); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | QColor KUrlNavigatorButtonBase::foregroundColor() const |
| 105 | { |
| 106 | const bool isHighlighted = isDisplayHintEnabled(hint: EnteredHint) || isDisplayHintEnabled(hint: DraggedHint) || isDisplayHintEnabled(hint: PopupActiveHint); |
| 107 | |
| 108 | QColor foregroundColor = palette().color(cg: QPalette::ColorGroup::Normal, cr: QPalette::Text); |
| 109 | |
| 110 | int alpha = m_active ? 255 : 128; |
| 111 | if (!m_active && !isHighlighted) { |
| 112 | alpha -= alpha / 4; |
| 113 | } |
| 114 | foregroundColor.setAlpha(alpha); |
| 115 | |
| 116 | return foregroundColor; |
| 117 | } |
| 118 | |
| 119 | void KUrlNavigatorButtonBase::activate() |
| 120 | { |
| 121 | setActive(true); |
| 122 | } |
| 123 | |
| 124 | } // namespace KDEPrivate |
| 125 | |
| 126 | #include "moc_kurlnavigatorbuttonbase_p.cpp" |
| 127 | |