KIO

kurlnavigatortogglebutton.cpp
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
16namespace KDEPrivate
17{
18static constexpr int s_iconSize = KIconLoader::SizeSmallMedium;
19
20KUrlNavigatorToggleButton::KUrlNavigatorToggleButton(KUrlNavigator *parent)
21 : KUrlNavigatorButtonBase(parent)
22{
23 setCheckable(true);
24 connect(this, &QAbstractButton::toggled, this, &KUrlNavigatorToggleButton::updateToolTip);
25 connect(this, &QAbstractButton::clicked, this, &KUrlNavigatorToggleButton::updateCursor);
26
27#ifndef QT_NO_ACCESSIBILITY
28 setAccessibleName(i18n("Edit mode"));
29#endif
30
31 updateToolTip();
32}
33
34KUrlNavigatorToggleButton::~KUrlNavigatorToggleButton()
35{
36}
37
38QSize KUrlNavigatorToggleButton::sizeHint() const
39{
40 QSize size = KUrlNavigatorButtonBase::sizeHint();
41 size.setWidth(qMax(s_iconSize, iconSize().width()) + 4);
42 return size;
43}
44
45void KUrlNavigatorToggleButton::enterEvent(QEnterEvent *event)
46{
47 KUrlNavigatorButtonBase::enterEvent(event);
48 updateCursor();
49}
50
51void KUrlNavigatorToggleButton::leaveEvent(QEvent *event)
52{
53 KUrlNavigatorButtonBase::leaveEvent(event);
54 setCursor(Qt::ArrowCursor);
55}
56
57void 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);
66
67 if (m_pixmap.isNull() || m_pixmap.devicePixelRatioF() != devicePixelRatioF()) {
68 const QSize tickIconSize = QSize(s_iconSize, s_iconSize).expandedTo(iconSize());
69 m_pixmap = QIcon::fromTheme(QStringLiteral("dialog-ok")).pixmap(tickIconSize, devicePixelRatioF());
70 }
71 style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, m_pixmap);
72 } else if (isDisplayHintEnabled(EnteredHint)) {
73 painter.setPen(Qt::NoPen);
74 painter.setBrush(palette().color(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, verticalGap, caretWidth, buttonHeight - 2 * verticalGap);
80 }
81}
82
83void 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
92void KUrlNavigatorToggleButton::updateCursor()
93{
94 setCursor(isChecked() ? Qt::ArrowCursor : Qt::IBeamCursor);
95}
96
97} // namespace KDEPrivate
98
99#include "moc_kurlnavigatortogglebutton_p.cpp"
Widget that allows to navigate through the paths of an URL.
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void clicked(bool checked)
void toggled(bool checked)
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
QSize expandedTo(const QSize &otherSize) const const
void setWidth(int width)
AlignCenter
ArrowCursor
LeftToRight
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.