File indexing completed on 2024-05-12 04:58:09
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "navigationbartoolbutton.h" 0019 #include "abstractbuttoninterface.h" 0020 0021 #include <QLabel> 0022 #include <QMouseEvent> 0023 #include <QApplication> 0024 0025 NavigationBarToolButton::NavigationBarToolButton(AbstractButtonInterface *button, QWidget *parent) 0026 : ToolButton(parent) 0027 , m_button(button) 0028 { 0029 setAutoRaise(true); 0030 setToolbarButtonLook(true); 0031 setFocusPolicy(Qt::NoFocus); 0032 0033 m_badgeLabel = new QLabel(this); 0034 m_badgeLabel->setObjectName(QSL("navigation-toolbutton-badge")); 0035 QFont f = m_badgeLabel->font(); 0036 f.setPixelSize(m_badgeLabel->height() / 2.5); 0037 m_badgeLabel->setFont(f); 0038 m_badgeLabel->hide(); 0039 0040 setToolTip(button->toolTip()); 0041 updateIcon(); 0042 updateBadge(); 0043 0044 connect(button, &AbstractButtonInterface::iconChanged, this, &NavigationBarToolButton::updateIcon); 0045 connect(button, &AbstractButtonInterface::activeChanged, this, &NavigationBarToolButton::updateIcon); 0046 connect(button, &AbstractButtonInterface::toolTipChanged, this, &NavigationBarToolButton::setToolTip); 0047 connect(button, &AbstractButtonInterface::badgeTextChanged, this, &NavigationBarToolButton::updateBadge); 0048 connect(button, &AbstractButtonInterface::visibleChanged, this, &NavigationBarToolButton::visibilityChangeRequested); 0049 } 0050 0051 void NavigationBarToolButton::updateVisibility() 0052 { 0053 setVisible(m_button->isVisible()); 0054 } 0055 0056 void NavigationBarToolButton::clicked() 0057 { 0058 auto *c = new AbstractButtonInterface::ClickController; 0059 c->visualParent = this; 0060 c->popupPosition = [=](const QSize &size) { 0061 QPoint pos = mapToGlobal(rect().bottomRight()); 0062 if (QApplication::isRightToLeft()) { 0063 pos.setX(pos.x() - rect().width()); 0064 } else { 0065 pos.setX(pos.x() - size.width()); 0066 } 0067 c->popupOpened = true; 0068 return pos; 0069 }; 0070 c->popupClosed = [=]() { 0071 setDown(false); 0072 delete c; 0073 }; 0074 Q_EMIT m_button->clicked(c); 0075 if (c->popupOpened) { 0076 setDown(true); 0077 } else { 0078 c->popupClosed(); 0079 } 0080 } 0081 0082 void NavigationBarToolButton::updateIcon() 0083 { 0084 const QIcon::Mode mode = m_button->isActive() ? QIcon::Normal : QIcon::Disabled; 0085 const QImage img = m_button->icon().pixmap(iconSize(), mode).toImage(); 0086 setIcon(QPixmap::fromImage(img, Qt::MonoOnly)); 0087 } 0088 0089 void NavigationBarToolButton::updateBadge() 0090 { 0091 if (m_button->badgeText().isEmpty()) { 0092 m_badgeLabel->hide(); 0093 } else { 0094 m_badgeLabel->setText(m_button->badgeText()); 0095 m_badgeLabel->resize(m_badgeLabel->sizeHint()); 0096 m_badgeLabel->move(width() - m_badgeLabel->width(), 0); 0097 m_badgeLabel->show(); 0098 } 0099 } 0100 0101 void NavigationBarToolButton::mouseReleaseEvent(QMouseEvent *e) 0102 { 0103 // Prevent flickering due to mouse release event restoring Down state 0104 0105 bool popupOpened = false; 0106 0107 if (e->button() == Qt::LeftButton && rect().contains(e->pos())) { 0108 clicked(); 0109 popupOpened = isDown(); 0110 } 0111 0112 if (popupOpened) { 0113 setUpdatesEnabled(false); 0114 } 0115 0116 ToolButton::mouseReleaseEvent(e); 0117 0118 if (popupOpened) { 0119 setDown(true); 0120 setUpdatesEnabled(true); 0121 } 0122 }