1 | /* |
2 | SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "kurlnavigatortogglebutton_p.h" |
8 | |
9 | #include <KIconLoader> |
10 | #include <KLocalizedString> |
11 | |
12 | #include <QPaintEvent> |
13 | #include <QPainter> |
14 | #include <QStyle> |
15 | |
16 | namespace KDEPrivate |
17 | { |
18 | static constexpr int s_iconSize = KIconLoader::SizeSmallMedium; |
19 | |
20 | KUrlNavigatorToggleButton::KUrlNavigatorToggleButton(KUrlNavigator *parent) |
21 | : KUrlNavigatorButtonBase(parent) |
22 | { |
23 | setCheckable(true); |
24 | connect(sender: this, signal: &QAbstractButton::toggled, context: this, slot: &KUrlNavigatorToggleButton::updateToolTip); |
25 | connect(sender: this, signal: &QAbstractButton::clicked, context: this, slot: &KUrlNavigatorToggleButton::updateCursor); |
26 | |
27 | #ifndef QT_NO_ACCESSIBILITY |
28 | setAccessibleName(i18n("Edit mode" )); |
29 | #endif |
30 | |
31 | updateToolTip(); |
32 | } |
33 | |
34 | KUrlNavigatorToggleButton::~KUrlNavigatorToggleButton() |
35 | { |
36 | } |
37 | |
38 | QSize KUrlNavigatorToggleButton::sizeHint() const |
39 | { |
40 | QSize size = KUrlNavigatorButtonBase::sizeHint(); |
41 | size.setWidth(qMax(a: s_iconSize, b: iconSize().width()) + 4); |
42 | return size; |
43 | } |
44 | |
45 | void KUrlNavigatorToggleButton::enterEvent(QEnterEvent *event) |
46 | { |
47 | KUrlNavigatorButtonBase::enterEvent(event); |
48 | updateCursor(); |
49 | } |
50 | |
51 | void KUrlNavigatorToggleButton::leaveEvent(QEvent *event) |
52 | { |
53 | KUrlNavigatorButtonBase::leaveEvent(event); |
54 | setCursor(Qt::ArrowCursor); |
55 | } |
56 | |
57 | void KUrlNavigatorToggleButton::paintEvent(QPaintEvent *event) |
58 | { |
59 | QPainter painter(this); |
60 | painter.setClipRect(event->rect()); |
61 | |
62 | const int buttonWidth = width(); |
63 | const int buttonHeight = height(); |
64 | if (isChecked()) { |
65 | drawHoverBackground(painter: &painter); |
66 | |
67 | if (m_pixmap.isNull() || m_pixmap.devicePixelRatioF() != devicePixelRatioF()) { |
68 | const QSize tickIconSize = QSize(s_iconSize, s_iconSize).expandedTo(otherSize: iconSize()); |
69 | m_pixmap = QIcon::fromTheme(QStringLiteral("dialog-ok" )).pixmap(size: tickIconSize, devicePixelRatio: devicePixelRatioF()); |
70 | } |
71 | style()->drawItemPixmap(painter: &painter, rect: rect(), alignment: Qt::AlignCenter, pixmap: m_pixmap); |
72 | } else if (isDisplayHintEnabled(hint: EnteredHint)) { |
73 | painter.setPen(Qt::NoPen); |
74 | painter.setBrush(palette().color(cr: foregroundRole())); |
75 | |
76 | const int verticalGap = 4; |
77 | const int caretWidth = 2; |
78 | const int x = (layoutDirection() == Qt::LeftToRight) ? 0 : buttonWidth - caretWidth; |
79 | painter.drawRect(x, y: verticalGap, w: caretWidth, h: buttonHeight - 2 * verticalGap); |
80 | } |
81 | } |
82 | |
83 | void KUrlNavigatorToggleButton::updateToolTip() |
84 | { |
85 | if (isChecked()) { |
86 | setToolTip(i18n("Click for Location Navigation" )); |
87 | } else { |
88 | setToolTip(i18n("Click to Edit Location" )); |
89 | } |
90 | } |
91 | |
92 | void KUrlNavigatorToggleButton::updateCursor() |
93 | { |
94 | setCursor(isChecked() ? Qt::ArrowCursor : Qt::IBeamCursor); |
95 | } |
96 | |
97 | } // namespace KDEPrivate |
98 | |
99 | #include "moc_kurlnavigatortogglebutton_p.cpp" |
100 | |