File indexing completed on 2024-07-21 03:41:23

0001 /*
0002     SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kurlnavigatortogglebutton_p.h"
0008 
0009 #include <KIconLoader>
0010 #include <KLocalizedString>
0011 
0012 #include <QPaintEvent>
0013 #include <QPainter>
0014 #include <QStyle>
0015 
0016 namespace KDEPrivate
0017 {
0018 static constexpr int s_iconSize = KIconLoader::SizeSmallMedium;
0019 
0020 KUrlNavigatorToggleButton::KUrlNavigatorToggleButton(KUrlNavigator *parent)
0021     : KUrlNavigatorButtonBase(parent)
0022 {
0023     setCheckable(true);
0024     connect(this, &QAbstractButton::toggled, this, &KUrlNavigatorToggleButton::updateToolTip);
0025     connect(this, &QAbstractButton::clicked, this, &KUrlNavigatorToggleButton::updateCursor);
0026 
0027 #ifndef QT_NO_ACCESSIBILITY
0028     setAccessibleName(i18n("Edit mode"));
0029 #endif
0030 
0031     updateToolTip();
0032 }
0033 
0034 KUrlNavigatorToggleButton::~KUrlNavigatorToggleButton()
0035 {
0036 }
0037 
0038 QSize KUrlNavigatorToggleButton::sizeHint() const
0039 {
0040     QSize size = KUrlNavigatorButtonBase::sizeHint();
0041     size.setWidth(qMax(s_iconSize, iconSize().width()) + 4);
0042     return size;
0043 }
0044 
0045 void KUrlNavigatorToggleButton::enterEvent(QEnterEvent *event)
0046 {
0047     KUrlNavigatorButtonBase::enterEvent(event);
0048     updateCursor();
0049 }
0050 
0051 void KUrlNavigatorToggleButton::leaveEvent(QEvent *event)
0052 {
0053     KUrlNavigatorButtonBase::leaveEvent(event);
0054     setCursor(Qt::ArrowCursor);
0055 }
0056 
0057 void KUrlNavigatorToggleButton::paintEvent(QPaintEvent *event)
0058 {
0059     QPainter painter(this);
0060     painter.setClipRect(event->rect());
0061 
0062     const int buttonWidth = width();
0063     const int buttonHeight = height();
0064     if (isChecked()) {
0065         drawHoverBackground(&painter);
0066 
0067         if (m_pixmap.isNull() || m_pixmap.devicePixelRatioF() != devicePixelRatioF()) {
0068             const QSize tickIconSize = QSize(s_iconSize, s_iconSize).expandedTo(iconSize());
0069             m_pixmap = QIcon::fromTheme(QStringLiteral("dialog-ok")).pixmap(tickIconSize, devicePixelRatioF());
0070         }
0071         style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, m_pixmap);
0072     } else if (isDisplayHintEnabled(EnteredHint)) {
0073         painter.setPen(Qt::NoPen);
0074         painter.setBrush(palette().color(foregroundRole()));
0075 
0076         const int verticalGap = 4;
0077         const int caretWidth = 2;
0078         const int x = (layoutDirection() == Qt::LeftToRight) ? 0 : buttonWidth - caretWidth;
0079         painter.drawRect(x, verticalGap, caretWidth, buttonHeight - 2 * verticalGap);
0080     }
0081 }
0082 
0083 void KUrlNavigatorToggleButton::updateToolTip()
0084 {
0085     if (isChecked()) {
0086         setToolTip(i18n("Click for Location Navigation"));
0087     } else {
0088         setToolTip(i18n("Click to Edit Location"));
0089     }
0090 }
0091 
0092 void KUrlNavigatorToggleButton::updateCursor()
0093 {
0094     setCursor(isChecked() ? Qt::ArrowCursor : Qt::IBeamCursor);
0095 }
0096 
0097 } // namespace KDEPrivate
0098 
0099 #include "moc_kurlnavigatortogglebutton_p.cpp"